|
| 1 | +/* |
| 2 | + This source file is part of the Swift System open source project |
| 3 | + |
| 4 | + Copyright (c) 2021 Apple Inc. and the Swift System project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See https://swift.org/LICENSE.txt for license information |
| 8 | +*/ |
| 9 | + |
| 10 | +extension SocketAddress { |
| 11 | + @frozen |
| 12 | + public struct IPv4: RawRepresentable { |
| 13 | + public var rawValue: CInterop.SockAddrIn |
| 14 | + |
| 15 | + public init(rawValue: CInterop.SockAddrIn) { |
| 16 | + self.rawValue = rawValue |
| 17 | + self.rawValue.sin_family = CInterop.SAFamily(Family.ipv4.rawValue) |
| 18 | + } |
| 19 | + |
| 20 | + public init?(_ address: SocketAddress) { |
| 21 | + guard address.family == .ipv4 else { return nil } |
| 22 | + let value: CInterop.SockAddrIn? = address.withUnsafeBytes { buffer in |
| 23 | + guard buffer.count >= MemoryLayout<CInterop.SockAddrIn>.size else { |
| 24 | + return nil |
| 25 | + } |
| 26 | + return buffer.baseAddress!.load(as: CInterop.SockAddrIn.self) |
| 27 | + } |
| 28 | + guard let value = value else { return nil } |
| 29 | + self.rawValue = value |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +extension SocketAddress { |
| 35 | + public init(_ ipv4: IPv4) { |
| 36 | + self = Swift.withUnsafeBytes(of: ipv4.rawValue) { buffer in |
| 37 | + SocketAddress(buffer) |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +extension SocketAddress.IPv4 { |
| 43 | + public init(address: Address, port: Port) { |
| 44 | + rawValue = CInterop.SockAddrIn() |
| 45 | + rawValue.sin_family = CInterop.SAFamily(SocketDescriptor.Domain.ipv4.rawValue); |
| 46 | + rawValue.sin_port = port.rawValue._networkOrder |
| 47 | + rawValue.sin_addr = CInterop.InAddr(s_addr: address.rawValue._networkOrder) |
| 48 | + } |
| 49 | + |
| 50 | + public init?(address: String, port: Port) { |
| 51 | + guard let address = Address(address) else { return nil } |
| 52 | + self.init(address: address, port: port) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +extension SocketAddress.IPv4: Hashable { |
| 57 | + public static func ==(left: Self, right: Self) -> Bool { |
| 58 | + left.address == right.address && left.port == right.port |
| 59 | + } |
| 60 | + |
| 61 | + public func hash(into hasher: inout Hasher) { |
| 62 | + hasher.combine(address) |
| 63 | + hasher.combine(port) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +extension SocketAddress.IPv4: CustomStringConvertible { |
| 68 | + public var description: String { |
| 69 | + "\(address):\(port)" |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +extension SocketAddress.IPv4 { |
| 74 | + @frozen |
| 75 | + public struct Port: RawRepresentable, ExpressibleByIntegerLiteral, Hashable { |
| 76 | + /// The port number, in host byte order. |
| 77 | + public var rawValue: CInterop.InPort |
| 78 | + |
| 79 | + public init(_ value: CInterop.InPort) { |
| 80 | + self.rawValue = value |
| 81 | + } |
| 82 | + |
| 83 | + public init(rawValue: CInterop.InPort) { |
| 84 | + self.init(rawValue) |
| 85 | + } |
| 86 | + |
| 87 | + public init(integerLiteral value: CInterop.InPort) { |
| 88 | + self.init(value) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public var port: Port { |
| 93 | + get { Port(CInterop.InPort(_networkOrder: rawValue.sin_port)) } |
| 94 | + set { rawValue.sin_port = newValue.rawValue._networkOrder } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +extension SocketAddress.IPv4.Port: CustomStringConvertible { |
| 99 | + public var description: String { |
| 100 | + rawValue.description |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +extension SocketAddress.IPv4 { |
| 105 | + @frozen |
| 106 | + public struct Address: RawRepresentable, Hashable { |
| 107 | + /// The raw internet address value, in host byte order. |
| 108 | + public var rawValue: CInterop.InAddrT |
| 109 | + |
| 110 | + public init(rawValue: CInterop.InAddrT) { |
| 111 | + self.rawValue = rawValue |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public var address: Address { |
| 116 | + get { |
| 117 | + let value = CInterop.InAddrT(_networkOrder: rawValue.sin_addr.s_addr) |
| 118 | + return Address(rawValue: value) |
| 119 | + } |
| 120 | + set { |
| 121 | + rawValue.sin_addr.s_addr = newValue.rawValue._networkOrder |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +extension SocketAddress.IPv4.Address: CustomStringConvertible { |
| 127 | + public var description: String { |
| 128 | + _inet_ntop() |
| 129 | + } |
| 130 | + |
| 131 | + internal func _inet_ntop() -> String { |
| 132 | + let addr = CInterop.InAddr(s_addr: rawValue._networkOrder) |
| 133 | + return withUnsafeBytes(of: addr) { src in |
| 134 | + String(_unsafeUninitializedCapacity: Int(_INET_ADDRSTRLEN)) { dst in |
| 135 | + dst.baseAddress!.withMemoryRebound( |
| 136 | + to: CChar.self, |
| 137 | + capacity: Int(_INET_ADDRSTRLEN) |
| 138 | + ) { dst in |
| 139 | + let res = system_inet_ntop( |
| 140 | + _PF_INET, |
| 141 | + src.baseAddress!, |
| 142 | + dst, |
| 143 | + CInterop.SockLen(_INET_ADDRSTRLEN)) |
| 144 | + if res == -1 { |
| 145 | + let errno = Errno.current |
| 146 | + fatalError("Failed to convert IPv4 address to string: \(errno)") |
| 147 | + } |
| 148 | + let length = system_strlen(dst) |
| 149 | + assert(length <= _INET_ADDRSTRLEN) |
| 150 | + return length |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +extension SocketAddress.IPv4.Address: LosslessStringConvertible { |
| 158 | + public init?(_ description: String) { |
| 159 | + guard let value = Self._inet_pton(description) else { return nil } |
| 160 | + self = value |
| 161 | + } |
| 162 | + |
| 163 | + internal static func _inet_pton(_ string: String) -> Self? { |
| 164 | + string.withCString { ptr in |
| 165 | + var addr = CInterop.InAddr() |
| 166 | + let res = system_inet_pton(_PF_INET, ptr, &addr) |
| 167 | + guard res == 1 else { return nil } |
| 168 | + return Self(rawValue: CInterop.InAddrT(_networkOrder: addr.s_addr)) |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments