summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/IPEndpoint.swift
blob: 31f2089a077817cf771d9391fc4c2758194f1171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
//  IPEndpoint.swift
//  MullvadVPN
//
//  Created by pronebird on 06/12/2019.
//  Copyright © 2019 Mullvad VPN AB. All rights reserved.
//

import Foundation
import struct Network.IPv4Address
import struct Network.IPv6Address
import protocol Network.IPAddress

struct IPv4Endpoint: Hashable, Equatable, Codable, CustomStringConvertible {
    let ip: IPv4Address
    let port: UInt16

    init(ip: IPv4Address, port: UInt16) {
        self.ip = ip
        self.port = port
    }

    init?<S>(string: S) where S: StringProtocol {
        let components = string.split(separator: ":", maxSplits: 2, omittingEmptySubsequences: false)

        if components.count == 2, let parsedIP = IPv4Address(String(components[0])), let parsedPort = UInt16(components[1]) {
            ip = parsedIP
            port = parsedPort
        } else {
            return nil
        }
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        let string = try container.decode(String.self)

        if let parsedAddress = IPv4Endpoint(string: string) {
            self = parsedAddress
        } else {
            throw DecodingError.dataCorruptedError(in: container, debugDescription: "Cannot parse the IPv4 endpoint")
        }
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()

        try container.encode("\(self)")
    }

    var description: String {
        return "\(ip):\(port)"
    }

    static func == (lhs: IPv4Endpoint, rhs: IPv4Endpoint) -> Bool {
        return lhs.ip.rawValue == rhs.ip.rawValue && lhs.port == rhs.port
    }
}

struct IPv6Endpoint: Hashable, Equatable, Codable, CustomStringConvertible {
    let ip: IPv6Address
    let port: UInt16

    init(ip: IPv6Address, port: UInt16) {
        self.ip = ip
        self.port = port
    }

    init?<S>(string: S) where S: StringProtocol {
        guard let lastColon = string.lastIndex(of: ":"), lastColon != string.endIndex else {
            return nil
        }

        let portIndex = string.index(after: lastColon)
        let addressString = string[..<lastColon]
        let portString = string[portIndex...]

        guard addressString.first == "[" && addressString.last == "]" else {
            return nil
        }

        let ipv6AddressString = String(addressString.dropFirst().dropLast())

        if let parsedIP = IPv6Address(ipv6AddressString), let parsedPort = UInt16(portString) {
            ip = parsedIP
            port = parsedPort
        } else {
            return nil
        }
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        let string = try container.decode(String.self)

        if let parsedAddress = IPv6Endpoint(string: string) {
            self = parsedAddress
        } else {
            throw DecodingError.dataCorruptedError(in: container, debugDescription: "Cannot parse the IPv6 endpoint")
        }
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()

        try container.encode("\(self)")
    }

    var description: String {
        return "[\(ip)]:\(port)"
    }

    static func == (lhs: IPv6Endpoint, rhs: IPv6Endpoint) -> Bool {
        return lhs.ip.rawValue == rhs.ip.rawValue && lhs.port == rhs.port
    }
}

enum AnyIPEndpoint: Hashable, Equatable, Codable, CustomStringConvertible {
    case ipv4(IPv4Endpoint)
    case ipv6(IPv6Endpoint)

    var ip: IPAddress {
        switch self {
        case .ipv4(let ipv4Endpoint):
            return ipv4Endpoint.ip
        case .ipv6(let ipv6Endpoint):
            return ipv6Endpoint.ip
        }
    }

    var port: UInt16 {
        switch self {
        case .ipv4(let ipv4Endpoint):
            return ipv4Endpoint.port
        case .ipv6(let ipv6Endpoint):
            return ipv6Endpoint.port
        }
    }

    init?<S>(string: S) where S: StringProtocol {
        if let ipv4Endpoint = IPv4Endpoint(string: string) {
            self = .ipv4(ipv4Endpoint)
        } else if let ipv6Endpoint = IPv6Endpoint(string: string) {
            self = .ipv6(ipv6Endpoint)
        } else {
            return nil
        }
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        let string = try container.decode(String.self)

        if let ipv4Endpoint = IPv4Endpoint(string: string) {
            self = .ipv4(ipv4Endpoint)
        } else if let ipv6Endpoint = IPv6Endpoint(string: string) {
            self = .ipv6(ipv6Endpoint)
        } else {
            throw DecodingError.dataCorruptedError(in: container, debugDescription: "Cannot parse the endpoint")
        }
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()

        try container.encode("\(self)")
    }

    var description: String {
        switch self {
        case .ipv4(let ipv4Endpoint):
            return "\(ipv4Endpoint)"
        case .ipv6(let ipv6Endpoint):
            return "\(ipv6Endpoint)"
        }
    }

    static func == (lhs: AnyIPEndpoint, rhs: AnyIPEndpoint) -> Bool {
        switch (lhs, rhs) {
        case (.ipv4(let lhsEndpoint), .ipv4(let rhsEndpoint)):
            return lhsEndpoint == rhsEndpoint

        case (.ipv6(let lhsEndpoint), .ipv6(let rhsEndpoint)):
            return lhsEndpoint == rhsEndpoint

        default:
            return false
        }
    }
}