From b2d628aeb4dc632dbacd08a430baf7da15c86bc1 Mon Sep 17 00:00:00 2001 From: Simon Whitty Date: Sat, 23 Nov 2024 12:54:12 +1100 Subject: [PATCH] remove AnySocketAddress --- FlyingSocks/Sources/AsyncSocket.swift | 14 ++------- FlyingSocks/Sources/Socket.swift | 6 ++-- FlyingSocks/Sources/SocketAddress.swift | 12 -------- FlyingSocks/Tests/SocketAddressTests.swift | 35 ---------------------- 4 files changed, 6 insertions(+), 61 deletions(-) diff --git a/FlyingSocks/Sources/AsyncSocket.swift b/FlyingSocks/Sources/AsyncSocket.swift index 76e5f55f..7209c1ca 100644 --- a/FlyingSocks/Sources/AsyncSocket.swift +++ b/FlyingSocks/Sources/AsyncSocket.swift @@ -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) @@ -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 diff --git a/FlyingSocks/Sources/Socket.swift b/FlyingSocks/Sources/Socket.swift index 58dbd473..7b6669d0 100644 --- a/FlyingSocks/Sources/Socket.swift +++ b/FlyingSocks/Sources/Socket.swift @@ -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( @@ -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() @@ -685,7 +685,7 @@ fileprivate extension Socket { static func withPacketInfoControl( family: sa_family_t, interfaceIndex: UInt32?, - address: (some SocketAddress)?, + address: (any SocketAddress)?, _ body: (UnsafePointer?, ControlMessageHeaderLengthType) -> T ) -> T { switch Int32(family) { diff --git a/FlyingSocks/Sources/SocketAddress.swift b/FlyingSocks/Sources/SocketAddress.swift index 91da616e..f7efd946 100644 --- a/FlyingSocks/Sources/SocketAddress.swift +++ b/FlyingSocks/Sources/SocketAddress.swift @@ -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(_ body: (_ sa: UnsafePointer) throws -> T) rethrows -> T { try withUnsafePointer(to: self) { diff --git a/FlyingSocks/Tests/SocketAddressTests.swift b/FlyingSocks/Tests/SocketAddressTests.swift index 39bbbfbd..14ce1e85 100644 --- a/FlyingSocks/Tests/SocketAddressTests.swift +++ b/FlyingSocks/Tests/SocketAddressTests.swift @@ -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