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
1 changed files with 5 additions and 11 deletions

View File

@ -45,20 +45,14 @@ public class SocketController {
var addr = sockaddr_un()
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
path.withCString { cstring in
len = strlen(cstring)
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)
// The buffer is pre-zeroed, so manual termination is unnecessary.
precondition(memccpy(pointer, path, 0, len) != nil)
}
addr.sun_len = UInt8(len)
let data = withUnsafeBytes(of: &addr, Data.init(_:))
return SocketPort(protocolFamily: AF_UNIX, socketType: SOCK_STREAM, protocol: 0, address: data)!
}