This commit is contained in:
Max Goedjen 2022-06-18 20:19:11 -07:00
parent 84dd9403c3
commit 64b2cc6f29
No known key found for this signature in database
GPG Key ID: E58C21DD77B9B8E8

View File

@ -45,20 +45,14 @@ public class SocketController {
var addr = sockaddr_un() var addr = sockaddr_un()
addr.sun_family = sa_family_t(AF_UNIX) addr.sun_family = sa_family_t(AF_UNIX)
var len: Int = 0 let len = MemoryLayout.size(ofValue: addr.sun_path) - 1
withUnsafeMutablePointer(to: &addr.sun_path.0) { pointer in withUnsafeMutablePointer(to: &addr.sun_path.0) { pointer in
path.withCString { cstring in // The buffer is pre-zeroed, so manual termination is unnecessary.
len = strlen(cstring) precondition(memccpy(pointer, path, 0, len) != nil)
strncpy(pointer, cstring, len)
}
}
addr.sun_len = UInt8(len+2)
var data: Data!
withUnsafePointer(to: &addr) { pointer in
data = Data(bytes: pointer, count: MemoryLayout<sockaddr_un>.size)
} }
addr.sun_len = UInt8(len)
let data = withUnsafeBytes(of: &addr, Data.init(_:))
return SocketPort(protocolFamily: AF_UNIX, socketType: SOCK_STREAM, protocol: 0, address: data)! return SocketPort(protocolFamily: AF_UNIX, socketType: SOCK_STREAM, protocol: 0, address: data)!
} }