Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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