@@ -79,13 +79,13 @@ private func getaddrinfo_numeric(_ string: String, family: Int32 = 0) -> NWEndpo
79
79
var result : NWEndpoint . Host ? = nil
80
80
81
81
if let sa = addrinfo. pointee. ai_addr {
82
- if ( sa. pointee. sa_family == AF_INET) {
82
+ if sa. pointee. sa_family == AF_INET {
83
83
sa. withMemoryRebound ( to: sockaddr_in. self, capacity: 1 , { ( sin) -> Void in
84
84
result = NWEndpoint . Host. ipv4 ( IPv4Address ( sin. pointee. sin_addr, interface) )
85
85
} )
86
- } else if ( sa. pointee. sa_family == AF_INET6) {
86
+ } else if sa. pointee. sa_family == AF_INET6 {
87
87
sa. withMemoryRebound ( to: sockaddr_in6. self, capacity: 1 , { ( sin6) -> Void in
88
- if ( sin6. pointee. sin6_scope_id != 0 ) {
88
+ if sin6. pointee. sin6_scope_id != 0 {
89
89
interface = NWInterface ( Int ( sin6. pointee. sin6_scope_id) )
90
90
}
91
91
let ipv6 = IPv6Address ( sin6. pointee. sin6_addr, interface) ;
@@ -107,14 +107,14 @@ private func getnameinfo_numeric(address: UnsafeRawPointer) -> String {
107
107
var result : String ? = nil
108
108
let maxLen = socklen_t ( 100 )
109
109
let storage = UnsafeMutablePointer< Int8> . allocate( capacity: Int ( maxLen) )
110
- if ( getnameinfo ( sa, socklen_t ( sa. pointee. sa_len) , storage, maxLen, nil , 0 , NI_NUMERICHOST) == 0 ) {
110
+ if getnameinfo ( sa, socklen_t ( sa. pointee. sa_len) , storage, maxLen, nil , 0 , NI_NUMERICHOST) == 0 {
111
111
result = String ( cString: storage)
112
112
}
113
113
storage. deallocate ( )
114
114
return result ?? " ? "
115
115
}
116
116
117
- /// An IP address
117
+ /// An IP address protocol
118
118
@available ( macOS 10 . 14 , iOS 12 . 0 , watchOS 5 . 0 , tvOS 12 . 0 , * )
119
119
public protocol IPAddress {
120
120
@@ -212,7 +212,7 @@ public struct IPv4Address: IPAddress, Hashable, CustomDebugStringConvertible {
212
212
/// - Parameter interface: An optional network interface to scope the address to. Defaults to nil.
213
213
/// - Returns: An IPv4Address or nil if the Data parameter did not contain an IPv4 address.
214
214
public init ? ( _ rawValue: Data , _ interface: NWInterface ? = nil ) {
215
- if ( rawValue. count != MemoryLayout< in_addr> . size) {
215
+ if rawValue. count != MemoryLayout< in_addr> . size {
216
216
return nil
217
217
}
218
218
let v4 = rawValue. withUnsafeBytes { ( ptr: UnsafePointer < in_addr > ) -> in_addr in
@@ -244,7 +244,7 @@ public struct IPv4Address: IPAddress, Hashable, CustomDebugStringConvertible {
244
244
/// The interface the address is scoped to, if any.
245
245
public let interface : NWInterface ?
246
246
247
- // Hashable
247
+ /// Hashable
248
248
public static func == ( lhs: IPv4Address , rhs: IPv4Address ) -> Bool {
249
249
return lhs. address. s_addr == rhs. address. s_addr && lhs. interface == rhs. interface
250
250
}
@@ -254,7 +254,7 @@ public struct IPv4Address: IPAddress, Hashable, CustomDebugStringConvertible {
254
254
hasher. combine ( self . interface)
255
255
}
256
256
257
- // CustomDebugStringConvertible
257
+ /// CustomDebugStringConvertible returning a debug description string of the IPv4 address and interface or just the address.
258
258
public var debugDescription : String {
259
259
var sin = sockaddr_in ( self . address, 0 )
260
260
let addressString = getnameinfo_numeric ( address: & sin)
@@ -378,7 +378,7 @@ public struct IPv6Address: IPAddress, Hashable, CustomDebugStringConvertible {
378
378
/// - Parameter interface: An optional interface the address is scoped to. Defaults to nil.
379
379
/// - Returns: nil unless the raw data contained an IPv6 address
380
380
public init ? ( _ rawValue: Data , _ interface: NWInterface ? = nil ) {
381
- if ( rawValue. count != MemoryLayout< in6_addr> . size) {
381
+ if rawValue. count != MemoryLayout< in6_addr> . size {
382
382
return nil
383
383
}
384
384
let v6 = rawValue. withUnsafeBytes { ( ptr: UnsafePointer < in6_addr > ) -> in6_addr in
@@ -434,7 +434,7 @@ public struct IPv6Address: IPAddress, Hashable, CustomDebugStringConvertible {
434
434
hasher. combine ( self . interface)
435
435
}
436
436
437
- // CustomDebugStringConvertible
437
+ /// CustomDebugStringConvertible returning a debug description string of the IPv6 address and interface or just the address.
438
438
public var debugDescription : String {
439
439
var sin6 = sockaddr_in6 ( self . address, 0 )
440
440
let addressString = getnameinfo_numeric ( address: & sin6)
@@ -524,7 +524,7 @@ public enum NWEndpoint: Hashable, CustomDebugStringConvertible {
524
524
return interface
525
525
}
526
526
}
527
-
527
+
528
528
public var debugDescription : String {
529
529
switch self {
530
530
case . ipv4( let ip4) :
@@ -571,7 +571,7 @@ public enum NWEndpoint: Hashable, CustomDebugStringConvertible {
571
571
var hints = addrinfo ( ai_flags: AI_DEFAULT, ai_family: AF_INET6, ai_socktype: SOCK_STREAM, ai_protocol: 0 ,
572
572
ai_addrlen: 0 , ai_canonname: nil , ai_addr: nil , ai_next: nil )
573
573
var resolved : UnsafeMutablePointer < addrinfo > ? = nil
574
- if ( getaddrinfo ( nil , service, & hints, & resolved) != 0 ) {
574
+ if getaddrinfo ( nil , service, & hints, & resolved) != 0 {
575
575
return nil
576
576
}
577
577
@@ -630,31 +630,32 @@ public enum NWEndpoint: Hashable, CustomDebugStringConvertible {
630
630
if let nwinterface = nw_endpoint_copy_interface ( nw) {
631
631
interface = NWInterface ( nwinterface)
632
632
}
633
- if ( nw_endpoint_get_type ( nw) == Network . nw_endpoint_type_host)
634
- {
633
+ if nw_endpoint_get_type ( nw) == Network . nw_endpoint_type_host{
634
+
635
635
let host = NWEndpoint . Host. name ( String ( cString: nw_endpoint_get_hostname ( nw) ) , interface)
636
636
self = . hostPort( host: host, port: NWEndpoint . Port ( nw_endpoint_get_port ( nw) ) )
637
- } else if ( nw_endpoint_get_type ( nw) == Network . nw_endpoint_type_address) {
637
+ } else if nw_endpoint_get_type ( nw) == Network . nw_endpoint_type_address {
638
+
638
639
let port = NWEndpoint . Port ( nw_endpoint_get_port ( nw) )
639
640
let address = nw_endpoint_get_address ( nw)
640
- if ( address. pointee. sa_family == AF_INET && address. pointee. sa_len == MemoryLayout< sockaddr_in> . size) {
641
+ if address. pointee. sa_family == AF_INET && address. pointee. sa_len == MemoryLayout< sockaddr_in> . size {
641
642
let host = address. withMemoryRebound ( to: sockaddr_in. self, capacity: 1 ) {
642
643
( sin: UnsafePointer < sockaddr_in > ) -> NWEndpoint . Host in
643
644
return NWEndpoint . Host. ipv4 ( IPv4Address ( sin. pointee. sin_addr, interface) )
644
645
}
645
646
self = . hostPort( host: host, port: port)
646
- } else if ( address. pointee. sa_family == AF_INET6 &&
647
- address. pointee. sa_len == MemoryLayout< sockaddr_in6> . size) {
647
+ } else if address. pointee. sa_family == AF_INET6 &&
648
+ address. pointee. sa_len == MemoryLayout< sockaddr_in6> . size {
648
649
let host = address. withMemoryRebound ( to: sockaddr_in6. self, capacity: 1 ) {
649
650
( sin6) -> NWEndpoint . Host in
650
- if ( interface == nil && sin6. pointee. sin6_scope_id != 0 ) {
651
+ if interface == nil && sin6. pointee. sin6_scope_id != 0 {
651
652
interface = NWInterface ( Int ( sin6. pointee. sin6_scope_id) )
652
653
}
653
654
return NWEndpoint . Host. ipv6 ( IPv6Address ( sin6. pointee. sin6_addr,
654
655
interface) )
655
656
}
656
657
self = . hostPort( host: host, port: port)
657
- } else if ( address. pointee. sa_family == AF_UNIX) {
658
+ } else if address. pointee. sa_family == AF_UNIX {
658
659
// sockaddr_un is very difficult to deal with in swift. Fortunately, nw_endpoint_copy_address_string
659
660
// already does exactly what we need.
660
661
let path = nw_endpoint_copy_address_string ( nw)
@@ -663,7 +664,7 @@ public enum NWEndpoint: Hashable, CustomDebugStringConvertible {
663
664
} else {
664
665
return nil
665
666
}
666
- } else if ( nw_endpoint_get_type ( nw) == Network . nw_endpoint_type_bonjour_service) {
667
+ } else if nw_endpoint_get_type ( nw) == Network . nw_endpoint_type_bonjour_service {
667
668
self = . service( name: String ( cString: nw_endpoint_get_bonjour_service_name ( nw) ) ,
668
669
type: String ( cString: nw_endpoint_get_bonjour_service_type ( nw) ) ,
669
670
domain: String ( cString: nw_endpoint_get_bonjour_service_domain ( nw) ) ,
0 commit comments