@@ -19,18 +19,6 @@ extension SocketAddress {
1919 self . rawValue = rawValue
2020 self . rawValue. sin_family = Family . ipv4. rawValue
2121 }
22-
23- public init ? ( _ address: SocketAddress ) {
24- guard address. family == . ipv4 else { return nil }
25- let value : CInterop . SockAddrIn ? = address. withUnsafeBytes { buffer in
26- guard buffer. count >= MemoryLayout< CInterop . SockAddrIn> . size else {
27- return nil
28- }
29- return buffer. baseAddress!. load ( as: CInterop . SockAddrIn. self)
30- }
31- guard let value = value else { return nil }
32- self . rawValue = value
33- }
3422 }
3523}
3624
@@ -43,21 +31,36 @@ extension SocketAddress {
4331 SocketAddress ( buffer)
4432 }
4533 }
34+
35+ /// If `self` holds an IPv4 address, extract it, otherwise return `nil`.
36+ @_alwaysEmitIntoClient
37+ public var ipv4 : IPv4 ? {
38+ guard family == . ipv4 else { return nil }
39+ let value : CInterop . SockAddrIn ? = self . withUnsafeBytes { buffer in
40+ guard buffer. count >= MemoryLayout< CInterop . SockAddrIn> . size else {
41+ return nil
42+ }
43+ return buffer. baseAddress!. load ( as: CInterop . SockAddrIn. self)
44+ }
45+ guard let value = value else { return nil }
46+ return IPv4 ( rawValue: value)
47+ }
4648}
4749
4850// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
4951extension SocketAddress . IPv4 {
5052 /// Create a socket address from a given Internet address and port number.
5153 @_alwaysEmitIntoClient
52- public init ( address: Address , port: Port ) {
54+ public init ( address: Address , port: SocketAddress . Port ) {
5355 rawValue = CInterop . SockAddrIn ( )
5456 rawValue. sin_family = CInterop . SAFamily ( SocketDescriptor . Domain. ipv4. rawValue) ;
5557 rawValue. sin_port = port. rawValue. _networkOrder
5658 rawValue. sin_addr = CInterop . InAddr ( s_addr: address. rawValue. _networkOrder)
5759 }
5860
61+ /// TODO: doc
5962 @_alwaysEmitIntoClient
60- public init ? ( address: String , port: Port ) {
63+ public init ? ( address: String , port: SocketAddress . Port ) {
6164 guard let address = Address ( address) else { return nil }
6265 self . init ( address: address, port: port)
6366 }
@@ -84,44 +87,6 @@ extension SocketAddress.IPv4: CustomStringConvertible {
8487 }
8588}
8689
87- // @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
88- extension SocketAddress . IPv4 {
89- @frozen
90- /// The port number on which the socket is listening.
91- public struct Port : RawRepresentable , ExpressibleByIntegerLiteral , Hashable {
92- /// The port number, in host byte order.
93- public var rawValue : CInterop . InPort
94-
95- @_alwaysEmitIntoClient
96- public init ( _ value: CInterop . InPort ) {
97- self . rawValue = value
98- }
99-
100- @_alwaysEmitIntoClient
101- public init ( rawValue: CInterop . InPort ) {
102- self . init ( rawValue)
103- }
104-
105- @_alwaysEmitIntoClient
106- public init ( integerLiteral value: CInterop . InPort ) {
107- self . init ( value)
108- }
109- }
110-
111- @_alwaysEmitIntoClient
112- public var port : Port {
113- get { Port ( CInterop . InPort ( _networkOrder: rawValue. sin_port) ) }
114- set { rawValue. sin_port = newValue. rawValue. _networkOrder }
115- }
116- }
117-
118- // @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
119- extension SocketAddress . IPv4 . Port : CustomStringConvertible {
120- public var description : String {
121- rawValue. description
122- }
123- }
124-
12590// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
12691extension SocketAddress . IPv4 {
12792 @frozen
@@ -134,13 +99,9 @@ extension SocketAddress.IPv4 {
13499 public init ( rawValue: CInterop . InAddrT ) {
135100 self . rawValue = rawValue
136101 }
137-
138- @_alwaysEmitIntoClient
139- public init ( _ value: CInterop . InAddrT ) {
140- self . rawValue = value
141- }
142102 }
143103
104+ /// The 32-bit IPv4 address.
144105 @_alwaysEmitIntoClient
145106 public var address : Address {
146107 get {
@@ -151,38 +112,40 @@ extension SocketAddress.IPv4 {
151112 rawValue. sin_addr. s_addr = newValue. rawValue. _networkOrder
152113 }
153114 }
115+
116+ @_alwaysEmitIntoClient
117+ public var port : SocketAddress . Port {
118+ get { SocketAddress . Port ( CInterop . InPort ( _networkOrder: rawValue. sin_port) ) }
119+ set { rawValue. sin_port = newValue. rawValue. _networkOrder }
120+ }
154121}
155122
156123extension SocketAddress . IPv4 . Address {
157124 /// The IPv4 address 0.0.0.0.
158125 ///
159126 /// This corresponds to the C constant `INADDR_ANY`.
160127 @_alwaysEmitIntoClient
161- public static var any : Self { Self ( _INADDR_ANY) }
128+ public static var any : Self { Self ( rawValue : _INADDR_ANY) }
162129
163130 /// The IPv4 loopback address 127.0.0.1.
164131 ///
165- /// This corresponds to the C constant `INADDR_ANY `.
132+ /// This corresponds to the C constant `INADDR_LOOPBACK `.
166133 @_alwaysEmitIntoClient
167- public static var loopback : Self { Self ( _INADDR_LOOPBACK) }
134+ public static var loopback : Self { Self ( rawValue : _INADDR_LOOPBACK) }
168135
169136 /// The IPv4 broadcast address 255.255.255.255.
170137 ///
171138 /// This corresponds to the C constant `INADDR_BROADCAST`.
172139 @_alwaysEmitIntoClient
173- public static var broadcast : Self { Self ( _INADDR_BROADCAST) }
140+ public static var broadcast : Self { Self ( rawValue : _INADDR_BROADCAST) }
174141}
175142
176-
177-
178143// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
179144extension SocketAddress . IPv4 . Address : CustomStringConvertible {
180- @_alwaysEmitIntoClient
181145 public var description : String {
182146 _inet_ntop ( )
183147 }
184148
185- @usableFromInline
186149 internal func _inet_ntop( ) -> String {
187150 let addr = CInterop . InAddr ( s_addr: rawValue. _networkOrder)
188151 return withUnsafeBytes ( of: addr) { src in
@@ -211,13 +174,11 @@ extension SocketAddress.IPv4.Address: CustomStringConvertible {
211174
212175// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
213176extension SocketAddress . IPv4 . Address : LosslessStringConvertible {
214- @_alwaysEmitIntoClient
215177 public init ? ( _ description: String ) {
216178 guard let value = Self . _inet_pton ( description) else { return nil }
217179 self = value
218180 }
219181
220- @usableFromInline
221182 internal static func _inet_pton( _ string: String ) -> Self ? {
222183 string. withCString { ptr in
223184 var addr = CInterop . InAddr ( )
0 commit comments