Skip to content

Commit

Permalink
Merge pull request #133 from swhitty/datagram-tests
Browse files Browse the repository at this point in the history
convert setPktInfo() to SocketOption
  • Loading branch information
swhitty authored Nov 21, 2024
2 parents 8a5f9c7 + a194c23 commit 43e8be7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
23 changes: 10 additions & 13 deletions FlyingSocks/Sources/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,14 @@ public struct Socket: Sendable, Hashable {

// enable return of ip_pktinfo/ipv6_pktinfo on recvmsg()
private func setPktInfo(domain: Int32) throws {
var enable = Int32(1)
let level: Int32
let name: Int32

switch domain {
case AF_INET:
level = Socket.ipproto_ip
name = Self.ip_pktinfo
try setValue(true, for: .packetInfoIP)
case AF_INET6:
level = Socket.ipproto_ipv6
name = Self.ipv6_recvpktinfo
try setValue(true, for: .packetInfoIPv6)
default:
return
}

let result = Socket.setsockopt(file.rawValue, level, name, &enable, socklen_t(MemoryLayout<Int32>.size))
guard result >= 0 else {
throw SocketError.makeFailed("SetPktInfoOption")
}
}

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

static var packetInfoIP: Self {
BoolSocketOption(level: Socket.ipproto_ip, name: Socket.ip_pktinfo)
}

static var packetInfoIPv6: Self {
BoolSocketOption(level: Socket.ipproto_ipv6, name: Socket.ipv6_recvpktinfo)
}

#if canImport(Darwin)
// Prevents SIG_TRAP when app is paused / running in background.
static var noSIGPIPE: Self {
Expand Down
18 changes: 18 additions & 0 deletions FlyingSocks/Tests/SocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,24 @@ struct SocketTests {
try Socket.inet_ntop(AF_INET6, &addr.sin6_addr, buffer, maxLength)
}
}

@Test
func makes_datagram_ip4() throws {
let socket = try Socket(domain: Int32(sa_family_t(AF_INET)), type: .datagram)

#expect(
try socket.getValue(for: .packetInfoIP) == true
)
}

@Test
func makes_datagram_ip6() throws {
let socket = try Socket(domain: Int32(sa_family_t(AF_INET6)), type: .datagram)

#expect(
try socket.getValue(for: .packetInfoIPv6) == true
)
}
}

extension Socket.Flags {
Expand Down
14 changes: 14 additions & 0 deletions FlyingSocks/XCTests/SocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,20 @@ final class SocketTests: XCTestCase {
let buffer = UnsafeMutablePointer<CChar>.allocate(capacity: Int(maxLength))
XCTAssertThrowsError(try Socket.inet_ntop(AF_INET6, &addr.sin6_addr, buffer, maxLength))
}

func testMakes_datagram_ip4() throws {
let socket = try Socket(domain: Int32(sa_family_t(AF_INET)), type: .datagram)
XCTAssertTrue(
try socket.getValue(for: .packetInfoIP)
)
}

func testMakes_datagram_ip6() throws {
let socket = try Socket(domain: Int32(sa_family_t(AF_INET6)), type: .datagram)
XCTAssertTrue(
try socket.getValue(for: .packetInfoIPv6)
)
}
}

extension Socket.Flags {
Expand Down

0 comments on commit 43e8be7

Please sign in to comment.