Skip to content

Commit

Permalink
use AnySocketAddress in socketAddress extension
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoward committed May 31, 2024
1 parent d5ceaff commit 3c7cfda
Showing 1 changed file with 1 addition and 28 deletions.
29 changes: 1 addition & 28 deletions Sources/IORingFoundation/FoundationExtras.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,7 @@ import SystemPackage
public extension Data {
var socketAddress: any SocketAddress {
get throws {
try withUnsafeBytes { data -> (any SocketAddress) in
var family = sa_family_t(AF_UNSPEC)

try data.withMemoryRebound(to: sockaddr.self) {
let sa = $0.baseAddress!.pointee
family = sa.sa_family
guard sa.size <= self.count else { // ignores trailing bytes
throw Errno.addressFamilyNotSupported
}
}

switch Int32(family) {
case AF_INET:
var sin = sockaddr_in()
memcpy(&sin, data.baseAddress!, Int(sin.size))
return sin
case AF_INET6:
var sin6 = sockaddr_in6()
memcpy(&sin6, data.baseAddress!, Int(sin6.size))
return sin6
case AF_LOCAL:
var sun = sockaddr_un()
memcpy(&sun, data.baseAddress!, Int(sun.size))
return sun
default:
throw Errno.addressFamilyNotSupported
}
}
try AnySocketAddress(bytes: Array(self))
}
}
}
Expand Down

0 comments on commit 3c7cfda

Please sign in to comment.