Skip to content

Commit 3e28584

Browse files
committed
Added NetlinkSocketAddress
1 parent e38f5b9 commit 3e28584

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// SocketAddress.swift
3+
//
4+
//
5+
// Created by Alsey Coleman Miller on 4/22/22.
6+
//
7+
8+
import Foundation
9+
import SystemPackage
10+
11+
@frozen
12+
public struct NetlinkSocketAddress: Equatable, Hashable, SocketAddress {
13+
14+
public typealias ProtocolID = NetlinkSocketProtocol
15+
16+
public var processID: ProcessID
17+
18+
public var group: CInt
19+
20+
public init(
21+
processID: ProcessID = .current,
22+
group: CInt = 0
23+
) {
24+
self.processID = processID
25+
self.group = group
26+
}
27+
28+
public func withUnsafePointer<Result>(
29+
_ body: (UnsafePointer<CInterop.SocketAddress>, UInt32) throws -> Result
30+
) rethrows -> Result {
31+
let address = CInterop.NetlinkSocketAddress(
32+
processID: processID,
33+
group: group
34+
)
35+
return try address.withUnsafePointer(body)
36+
}
37+
38+
public static func withUnsafePointer(
39+
_ body: (UnsafeMutablePointer<CInterop.SocketAddress>, UInt32) throws -> ()
40+
) rethrows -> Self {
41+
var value = CInterop.NetlinkSocketAddress()
42+
try value.withUnsafeMutablePointer(body)
43+
return Self.init(
44+
processID: .init(rawValue: numericCast(value.nl_pid)),
45+
group: .init(bitPattern: value.nl_groups)
46+
)
47+
}
48+
}

0 commit comments

Comments
 (0)