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
18 changes: 18 additions & 0 deletions FlyingSocks/Sources/Socket+Android.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ extension Socket {
return addr
}

static func makeAbstractNamespaceUnix(name: String) -> Android.sockaddr_un {
var addr: sockaddr_un = Android.sockaddr_un()
addr.sun_family = sa_family_t(AF_UNIX)

_ = withUnsafeMutableBytes(of: &addr.sun_path) { raw in
raw.initializeMemory(as: CChar.self, repeating: 0)
}

let nameBytes = Array(name.utf8.prefix(107))
Copy link
Owner

@swhitty swhitty Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should add a constant for this max as its actually used in a couple of other places (macOS: 104, linux: 108)

extension Socket {
   static let sunPathMax = MemoryLayout.size(ofValue: sockaddr_un().sun_path)
}

nameBytes.withUnsafeBytes { src in
withUnsafeMutableBytes(of: &addr.sun_path) { dst in
dst[1 ..< 1 + src.count].copyBytes(from: src)
}
}

return addr
}

static func socket(_ domain: Int32, _ type: Int32, _ protocol: Int32) -> FileDescriptorType {
Android.socket(domain, type, `protocol`)
}
Expand Down
18 changes: 18 additions & 0 deletions FlyingSocks/Sources/Socket+Glibc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ extension Socket {
return addr
}

static func makeAbstractNamespaceUnix(name: String) -> Glibc.sockaddr_un {
var addr: sockaddr_un = Glibc.sockaddr_un()
addr.sun_family = sa_family_t(AF_UNIX)

_ = withUnsafeMutableBytes(of: &addr.sun_path) { raw in
raw.initializeMemory(as: CChar.self, repeating: 0)
}

let nameBytes = Array(name.utf8.prefix(107))
nameBytes.withUnsafeBytes { src in
withUnsafeMutableBytes(of: &addr.sun_path) { dst in
dst[1 ..< 1 + src.count].copyBytes(from: src)
}
}

return addr
}

static func socket(_ domain: Int32, _ type: Int32, _ protocol: Int32) -> FileDescriptorType {
Glibc.socket(domain, type, `protocol`)
}
Expand Down
18 changes: 18 additions & 0 deletions FlyingSocks/Sources/Socket+Musl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ extension Socket {
return addr
}

static func makeAbstractNamespaceUnix(name: String) -> Musl.sockaddr_un {
var addr: sockaddr_un = Musl.sockaddr_un()
addr.sun_family = sa_family_t(AF_UNIX)

_ = withUnsafeMutableBytes(of: &addr.sun_path) { raw in
raw.initializeMemory(as: CChar.self, repeating: 0)
}

let nameBytes = Array(name.utf8.prefix(107))
nameBytes.withUnsafeBytes { src in
withUnsafeMutableBytes(of: &addr.sun_path) { dst in
dst[1 ..< 1 + src.count].copyBytes(from: src)
}
}

return addr
}

static func socket(_ domain: Int32, _ type: Int32, _ protocol: Int32) -> FileDescriptorType {
Musl.socket(domain, type, `protocol`)
}
Expand Down
18 changes: 18 additions & 0 deletions FlyingSocks/Sources/Socket+WinSock2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ extension Socket {
return addr
}

static func makeAbstractNamespaceUnix(name: String) -> WinSDK.sockaddr_un {
var addr: sockaddr_un = WinSDK.sockaddr_un()
addr.sun_family = ADDRESS_FAMILY(AF_UNIX)

_ = withUnsafeMutableBytes(of: &addr.sun_path) { raw in
raw.initializeMemory(as: CChar.self, repeating: 0)
}

let nameBytes = Array(name.utf8.prefix(107))
nameBytes.withUnsafeBytes { src in
withUnsafeMutableBytes(of: &addr.sun_path) { dst in
dst[1 ..< 1 + src.count].copyBytes(from: src)
}
}

return addr
}

static func fcntl(_ fd: FileDescriptorType, _ cmd: Int32) -> Int32 {
return -1
}
Expand Down
7 changes: 6 additions & 1 deletion FlyingSocks/Sources/SocketAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,15 @@ public extension SocketAddress where Self == sockaddr_in6 {
}

public extension SocketAddress where Self == sockaddr_un {

static func unix(path: String) -> Self {
Socket.makeAddressUnix(path: path)
}

#if canImport(Glibc) || canImport(Musl) || canImport(Android) || canImport(WinSDK)
static func unix(abstractNamespace: String) -> Self {
Socket.makeAbstractNamespaceUnix(name: abstractNamespace)
}
#endif
}

#if compiler(>=6.0)
Expand Down
27 changes: 27 additions & 0 deletions FlyingSocks/Tests/SocketAddressTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,23 @@ struct SocketAddressTests {
)
}

#if canImport(Glibc) || canImport(Musl) || canImport(Android) || canImport(WinSDK)
@Test
func unixAbstractNamespace_IsCorrectlyDecodedFromStorage() throws {
let storage = sockaddr_un
.unix(abstractNamespace: "mygreatnamespace")
.makeStorage()

var unix = try sockaddr_un.make(from: storage)
let path = withUnsafePointer(to: &unix.sun_path.1) {
return String(cString: $0)
}
#expect(
path == "mygreatnamespace"
)
}
#endif

@Test
func unix_ThrowsInvalidAddress_WhenFamilyIncorrect() {
let storage = sockaddr_in6
Expand Down Expand Up @@ -183,6 +200,16 @@ struct SocketAddressTests {
)
}

#if canImport(Glibc) || canImport(Musl) || canImport(Android) || canImport(WinSDK)
@Test
func unixAbstractNamespace_CheckSize() throws {
let sun = sockaddr_un.unix(abstractNamespace: "some_great_namespace")
#expect(
sun.size == socklen_t(MemoryLayout<sockaddr_un>.size)
)
}
#endif

@Test
func unknown_CheckSize() throws {
var sa = sockaddr()
Expand Down