-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathIPv6SocketAddress.swift
More file actions
66 lines (52 loc) · 1.94 KB
/
IPv6SocketAddress.swift
File metadata and controls
66 lines (52 loc) · 1.94 KB
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
//
// IPv6SocketAddress.swift
//
//
// Created by Alsey Coleman Miller on 4/26/22.
//
import SystemPackage
/// IPv6 Socket Address
public struct IPv6SocketAddress: SocketAddress, Equatable, Hashable {
public typealias ProtocolID = IPv6Protocol
public var address: IPv6Address
public var port: UInt16
@_alwaysEmitIntoClient
public init(address: IPv6Address,
port: UInt16) {
self.address = address
self.port = port
}
internal init(_ cValue: CInterop.IPv6SocketAddress) {
self.init(
address: IPv6Address(cValue.sin6_addr),
port: cValue.sin6_port.networkOrder
)
}
public func withUnsafePointer<Result, Error>(
_ body: (UnsafePointer<CInterop.SocketAddress>, CInterop.SocketLength) throws(Error) -> Result
) rethrows -> Result where Error: Swift.Error {
var socketAddress = CInterop.IPv6SocketAddress()
socketAddress.sin6_family = numericCast(Self.family.rawValue)
socketAddress.sin6_port = port.networkOrder
socketAddress.sin6_addr = address.bytes
return try socketAddress.withUnsafePointer(body)
}
public static func withUnsafePointer(
_ pointer: UnsafeMutablePointer<CInterop.SocketAddress>
) -> Self {
return pointer.withMemoryRebound(to: CInterop.IPv6SocketAddress.self, capacity: 1) { pointer in
return Self.init(pointer.pointee)
}
}
public static func withUnsafePointer(
_ body: (UnsafeMutablePointer<CInterop.SocketAddress>, CInterop.SocketLength) throws -> ()
) rethrows -> Self {
var socketAddress = CInterop.IPv6SocketAddress()
try socketAddress.withUnsafeMutablePointer(body)
return Self.init(socketAddress)
}
}
extension CInterop.IPv6SocketAddress: CSocketAddress {
@_alwaysEmitIntoClient
static var family: SocketAddressFamily { .ipv6 }
}