Skip to content

Commit

Permalink
remove AnySocketAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Nov 23, 2024
1 parent a2f79a4 commit b2d628a
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 61 deletions.
14 changes: 3 additions & 11 deletions FlyingSocks/Sources/AsyncSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public struct AsyncSocket: Sendable {
message: [UInt8],
to peerAddress: some SocketAddress,
interfaceIndex: UInt32? = nil,
from localAddress: (some SocketAddress)? = nil
from localAddress: (any SocketAddress)? = nil
) async throws {
let sent = try await pool.loopUntilReady(for: .write, on: socket) {
try socket.send(message: message, to: peerAddress, interfaceIndex: interfaceIndex, from: localAddress)
Expand All @@ -251,19 +251,11 @@ public struct AsyncSocket: Sendable {
}

public func send(message: Message) async throws {
let localAddress: AnySocketAddress?

if let unwrappedLocalAddress = message.localAddress {
localAddress = AnySocketAddress(unwrappedLocalAddress)
} else {
localAddress = nil
}

try await send(
message: message.bytes,
to: AnySocketAddress(message.peerAddress),
to: message.peerAddress,
interfaceIndex: message.interfaceIndex,
from: localAddress
from: message.localAddress
)
}
#endif
Expand Down
6 changes: 3 additions & 3 deletions FlyingSocks/Sources/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public struct Socket: Sendable, Hashable {
message: [UInt8],
to peerAddress: some SocketAddress,
interfaceIndex: UInt32? = nil,
from localAddress: (some SocketAddress)? = nil
from localAddress: (any SocketAddress)? = nil
) throws -> Int {
try message.withUnsafeBytes { buffer in
try send(
Expand All @@ -417,7 +417,7 @@ public struct Socket: Sendable, Hashable {
flags: Int32,
to peerAddress: some SocketAddress,
interfaceIndex: UInt32? = nil,
from localAddress: (some SocketAddress)? = nil
from localAddress: (any SocketAddress)? = nil
) throws -> Int {
var iov = iovec()
var msg = msghdr()
Expand Down Expand Up @@ -685,7 +685,7 @@ fileprivate extension Socket {
static func withPacketInfoControl<T>(
family: sa_family_t,
interfaceIndex: UInt32?,
address: (some SocketAddress)?,
address: (any SocketAddress)?,
_ body: (UnsafePointer<cmsghdr>?, ControlMessageHeaderLengthType) -> T
) -> T {
switch Int32(family) {
Expand Down
12 changes: 0 additions & 12 deletions FlyingSocks/Sources/SocketAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,6 @@ extension Socket {
}
}

public struct AnySocketAddress: Sendable, SocketAddress {
public static var family: sa_family_t {
sa_family_t(AF_UNSPEC)
}

private var storage: sockaddr_storage

public init(_ sa: any SocketAddress) {
storage = sa.makeStorage()
}
}

public extension SocketAddress {
func withSockAddr<T>(_ body: (_ sa: UnsafePointer<sockaddr>) throws -> T) rethrows -> T {
try withUnsafePointer(to: self) {
Expand Down
35 changes: 0 additions & 35 deletions FlyingSocks/Tests/SocketAddressTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -332,41 +332,6 @@ struct SocketAddressTests {
}
}
}

@Test
func testTypeErasedSockAddress() throws {
var addrIn6 = sockaddr_in6()
addrIn6.sin6_family = sa_family_t(AF_INET6)
addrIn6.sin6_port = UInt16(9090).bigEndian
addrIn6.sin6_addr = try Socket.makeInAddr(fromIP6: "fe80::1")

let storage = AnySocketAddress(addrIn6)

#expect(storage.family == sa_family_t(AF_INET6))
#expect(storage.family == addrIn6.sin6_family)

withUnsafeBytes(of: addrIn6) { addrIn6Ptr in
let storagePtr = addrIn6Ptr.bindMemory(to: sockaddr_storage.self)
#expect(storagePtr.baseAddress!.pointee.ss_family == sa_family_t(AF_INET6))
let sockaddrIn6Ptr = addrIn6Ptr.bindMemory(to: sockaddr_in6.self)
#expect(sockaddrIn6Ptr.baseAddress!.pointee.sin6_port == addrIn6.sin6_port)
let addrArray = withUnsafeBytes(of: sockaddrIn6Ptr.baseAddress!.pointee.sin6_addr) {
Array($0.bindMemory(to: UInt8.self))
}
let expectedArray = withUnsafeBytes(of: addrIn6.sin6_addr) {
Array($0.bindMemory(to: UInt8.self))
}
#expect(addrArray == expectedArray)
}

storage.withSockAddr { sa in
#expect(sa.pointee.sa_family == sa_family_t(AF_INET6))
}

addrIn6.withSockAddr { sa in
#expect(sa.pointee.sa_family == sa_family_t(AF_INET6))
}
}
}

// this is a bit ugly but necessary to get unknown_CheckSize() to function
Expand Down

0 comments on commit b2d628a

Please sign in to comment.