summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadTypes/AnyIPAddress.swift
blob: 0bfb9fa2d88fcef215c059c4d9711d16ce47f9f5 (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
//
//  AnyIPAddress.swift
//  MullvadTypes
//
//  Created by pronebird on 05/10/2021.
//  Copyright © 2021 Mullvad VPN AB. All rights reserved.
//

import Foundation
import Network

/// Container type that holds either `IPv4Address` or `IPv6Address`.
public enum AnyIPAddress: IPAddress, Codable, Equatable, CustomDebugStringConvertible {
    case ipv4(IPv4Address)
    case ipv6(IPv6Address)

    private enum CodingKeys: String, CodingKey {
        case ipv4, ipv6
    }

    private var innerAddress: IPAddress {
        switch self {
        case let .ipv4(ipv4Address):
            return ipv4Address
        case let .ipv6(ipv6Address):
            return ipv6Address
        }
    }

    public var rawValue: Data {
        innerAddress.rawValue
    }

    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        if container.contains(.ipv4) {
            self = .ipv4(try container.decode(IPv4Address.self, forKey: .ipv4))
        } else if container.contains(.ipv6) {
            self = .ipv6(try container.decode(IPv6Address.self, forKey: .ipv6))
        } else {
            throw DecodingError.dataCorruptedError(
                forKey: .ipv4,
                in: container,
                debugDescription: "Invalid AnyIPAddress representation"
            )
        }
    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)

        switch self {
        case let .ipv4(ipv4Address):
            try container.encode(ipv4Address, forKey: .ipv4)
        case let .ipv6(ipv6Address):
            try container.encode(ipv6Address, forKey: .ipv6)
        }
    }

    public init?(_ rawValue: Data, _ interface: NWInterface?) {
        if let ipv4Address = IPv4Address(rawValue, interface) {
            self = .ipv4(ipv4Address)
        } else if let ipv6Address = IPv6Address(rawValue, interface) {
            self = .ipv6(ipv6Address)
        } else {
            return nil
        }
    }

    public init?(_ string: String) {
        // Arbitrary integers should not be allowed by us and need to be handled separately
        // since Apple allows them.
        guard Int(string) == nil else { return nil }

        if let ipv4Address = IPv4Address(string) {
            self = .ipv4(ipv4Address)
        } else if let ipv6Address = IPv6Address(string) {
            self = .ipv6(ipv6Address)
        } else {
            return nil
        }
    }

    public var interface: NWInterface? {
        innerAddress.interface
    }

    public var isLoopback: Bool {
        innerAddress.isLoopback
    }

    public var isLinkLocal: Bool {
        innerAddress.isLinkLocal
    }

    public var isMulticast: Bool {
        innerAddress.isMulticast
    }

    public var debugDescription: String {
        switch self {
        case let .ipv4(ipv4Address):
            return "\(ipv4Address)"
        case let .ipv6(ipv6Address):
            return "\(ipv6Address)"
        }
    }
}