Skip to content

Commit 43e8be7

Browse files
authored
Merge pull request #133 from swhitty/datagram-tests
convert setPktInfo() to SocketOption
2 parents 8a5f9c7 + a194c23 commit 43e8be7

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

FlyingSocks/Sources/Socket.swift

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,25 +123,14 @@ public struct Socket: Sendable, Hashable {
123123

124124
// enable return of ip_pktinfo/ipv6_pktinfo on recvmsg()
125125
private func setPktInfo(domain: Int32) throws {
126-
var enable = Int32(1)
127-
let level: Int32
128-
let name: Int32
129-
130126
switch domain {
131127
case AF_INET:
132-
level = Socket.ipproto_ip
133-
name = Self.ip_pktinfo
128+
try setValue(true, for: .packetInfoIP)
134129
case AF_INET6:
135-
level = Socket.ipproto_ipv6
136-
name = Self.ipv6_recvpktinfo
130+
try setValue(true, for: .packetInfoIPv6)
137131
default:
138132
return
139133
}
140-
141-
let result = Socket.setsockopt(file.rawValue, level, name, &enable, socklen_t(MemoryLayout<Int32>.size))
142-
guard result >= 0 else {
143-
throw SocketError.makeFailed("SetPktInfoOption")
144-
}
145134
}
146135

147136
public func setValue<O: SocketOption>(_ value: O.Value, for option: O) throws {
@@ -573,6 +562,14 @@ public extension SocketOption where Self == BoolSocketOption {
573562
BoolSocketOption(name: SO_REUSEADDR)
574563
}
575564

565+
static var packetInfoIP: Self {
566+
BoolSocketOption(level: Socket.ipproto_ip, name: Socket.ip_pktinfo)
567+
}
568+
569+
static var packetInfoIPv6: Self {
570+
BoolSocketOption(level: Socket.ipproto_ipv6, name: Socket.ipv6_recvpktinfo)
571+
}
572+
576573
#if canImport(Darwin)
577574
// Prevents SIG_TRAP when app is paused / running in background.
578575
static var noSIGPIPE: Self {

FlyingSocks/Tests/SocketTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,24 @@ struct SocketTests {
321321
try Socket.inet_ntop(AF_INET6, &addr.sin6_addr, buffer, maxLength)
322322
}
323323
}
324+
325+
@Test
326+
func makes_datagram_ip4() throws {
327+
let socket = try Socket(domain: Int32(sa_family_t(AF_INET)), type: .datagram)
328+
329+
#expect(
330+
try socket.getValue(for: .packetInfoIP) == true
331+
)
332+
}
333+
334+
@Test
335+
func makes_datagram_ip6() throws {
336+
let socket = try Socket(domain: Int32(sa_family_t(AF_INET6)), type: .datagram)
337+
338+
#expect(
339+
try socket.getValue(for: .packetInfoIPv6) == true
340+
)
341+
}
324342
}
325343

326344
extension Socket.Flags {

FlyingSocks/XCTests/SocketTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,20 @@ final class SocketTests: XCTestCase {
258258
let buffer = UnsafeMutablePointer<CChar>.allocate(capacity: Int(maxLength))
259259
XCTAssertThrowsError(try Socket.inet_ntop(AF_INET6, &addr.sin6_addr, buffer, maxLength))
260260
}
261+
262+
func testMakes_datagram_ip4() throws {
263+
let socket = try Socket(domain: Int32(sa_family_t(AF_INET)), type: .datagram)
264+
XCTAssertTrue(
265+
try socket.getValue(for: .packetInfoIP)
266+
)
267+
}
268+
269+
func testMakes_datagram_ip6() throws {
270+
let socket = try Socket(domain: Int32(sa_family_t(AF_INET6)), type: .datagram)
271+
XCTAssertTrue(
272+
try socket.getValue(for: .packetInfoIPv6)
273+
)
274+
}
261275
}
262276

263277
extension Socket.Flags {

0 commit comments

Comments
 (0)