forked from PerfectlySoft/Perfect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetNamedPipe.swift
More file actions
425 lines (368 loc) · 12.7 KB
/
Copy pathNetNamedPipe.swift
File metadata and controls
425 lines (368 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//
// NetNamedPipe.swift
// PerfectLib
//
// Created by Kyle Jessup on 7/5/15.
// Copyright (C) 2015 PerfectlySoft, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version, as supplemented by the
// Perfect Additional Terms.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License, as supplemented by the
// Perfect Additional Terms, for more details.
//
// You should have received a copy of the GNU Affero General Public License
// and the Perfect Additional Terms that immediately follow the terms and
// conditions of the GNU Affero General Public License along with this
// program. If not, see <http://www.perfect.org/AGPL_3_0_With_Perfect_Additional_Terms.txt>.
//
#if os(Linux)
import SwiftGlibc
let AF_UNIX: Int32 = 1
let SOL_SOCKET: Int32 = 1
let SCM_RIGHTS: Int32 = 0x01
#else
import Darwin
#endif
/// This sub-class of NetTCP handles networking over an AF_UNIX named pipe connection.
public class NetNamedPipe : NetTCP {
/// Initialize the object using an existing file descriptor.
public convenience init(fd: Int32) {
self.init()
self.fd.fd = fd
self.fd.family = AF_UNIX
self.fd.switchToNBIO()
}
/// Override socket initialization to handle the UNIX socket type.
public override func initSocket() {
#if os(Linux)
fd.fd = socket(AF_UNIX, Int32(SOCK_STREAM.rawValue), 0)
#else
fd.fd = socket(AF_UNIX, SOCK_STREAM, 0)
#endif
fd.family = AF_UNIX
fd.switchToNBIO()
}
public override func sockName() -> (String, UInt16) {
var addr = UnsafeMutablePointer<sockaddr_un>.alloc(1)
let len = UnsafeMutablePointer<socklen_t>.alloc(1)
defer {
addr.dealloc(1)
len.dealloc(1)
}
len.memory = socklen_t(sizeof(sockaddr_in))
getsockname(fd.fd, UnsafeMutablePointer<sockaddr>(addr), len)
var nameBuf = [CChar]()
let mirror = Mirror(reflecting: addr.memory.sun_path)
let childGen = mirror.children.generate()
for _ in 0..<1024 {
let (_, elem) = childGen.next()!
if (elem as! Int8) == 0 {
break
}
nameBuf.append(elem as! Int8)
}
nameBuf.append(0)
let s = String.fromCString(nameBuf) ?? ""
let p = UInt16(0)
return (s, p)
}
public override func peerName() -> (String, UInt16) {
var addr = UnsafeMutablePointer<sockaddr_un>.alloc(1)
let len = UnsafeMutablePointer<socklen_t>.alloc(1)
defer {
addr.dealloc(1)
len.dealloc(1)
}
len.memory = socklen_t(sizeof(sockaddr_in))
getpeername(fd.fd, UnsafeMutablePointer<sockaddr>(addr), len)
var nameBuf = [CChar]()
let mirror = Mirror(reflecting: addr.memory.sun_path)
let childGen = mirror.children.generate()
for _ in 0..<1024 {
let (_, elem) = childGen.next()!
if (elem as! Int8) == 0 {
break
}
nameBuf.append(elem as! Int8)
}
nameBuf.append(0)
let s = String.fromCString(nameBuf) ?? ""
let p = UInt16(0)
return (s, p)
}
/// Bind the socket to the address path
/// - parameter address: The path on the file system at which to create and bind the socket
/// - throws: `PerfectError.NetworkError`
public func bind(address: String) throws {
initSocket()
let utf8 = address.utf8
#if os(Linux) // BSDs have a size identifier in front, Linux does not
let addrLen = sizeof(sockaddr_un)
#else
let addrLen = sizeof(UInt8) + sizeof(sa_family_t) + utf8.count + 1
#endif
let addrPtr = UnsafeMutablePointer<UInt8>.alloc(addrLen)
defer { addrPtr.destroy() }
var memLoc = 0
#if os(Linux) // BSDs use one byte for sa_family_t, Linux uses two
let afUnixShort = UInt16(AF_UNIX)
addrPtr[memLoc] = UInt8(afUnixShort & 0xFF)
memLoc += 1
addrPtr[memLoc] = UInt8((afUnixShort >> 8) & 0xFF)
memLoc += 1
#else
addrPtr[memLoc] = UInt8(addrLen)
memLoc += 1
addrPtr[memLoc] = UInt8(AF_UNIX)
memLoc += 1
#endif
for char in utf8 {
addrPtr[memLoc] = char
memLoc += 1
}
addrPtr[memLoc] = 0
#if os(Linux)
let bRes = SwiftGlibc.bind(fd.fd, UnsafePointer<sockaddr>(addrPtr), socklen_t(addrLen))
#else
let bRes = Darwin.bind(fd.fd, UnsafePointer<sockaddr>(addrPtr), socklen_t(addrLen))
#endif
if bRes == -1 {
throw PerfectError.NetworkError(errno, String.fromCString(strerror(errno))!)
}
}
/// Connect to the indicated server socket
/// - parameter address: The server socket file.
/// - parameter timeoutSeconds: The number of seconds to wait for the connection to complete. A timeout of negative one indicates that there is no timeout.
/// - parameter callBack: The closure which will be called when the connection completes. If the connection completes successfully then the current NetNamedPipe instance will be passed to the callback, otherwise, a nil object will be passed.
/// - returns: `PerfectError.NetworkError`
public func connect(address: String, timeoutSeconds: Double, callBack: (NetNamedPipe?) -> ()) throws {
initSocket()
let utf8 = address.utf8
let addrLen = sizeof(UInt8) + sizeof(sa_family_t) + utf8.count + 1
let addrPtr = UnsafeMutablePointer<UInt8>.alloc(addrLen)
defer { addrPtr.destroy() }
var memLoc = 0
addrPtr[memLoc] = UInt8(addrLen)
memLoc += 1
addrPtr[memLoc] = UInt8(AF_UNIX)
memLoc += 1
for char in utf8 {
addrPtr[memLoc] = char
memLoc += 1
}
addrPtr[memLoc] = 0
#if os(Linux)
let cRes = SwiftGlibc.connect(fd.fd, UnsafePointer<sockaddr>(addrPtr), socklen_t(addrLen))
#else
let cRes = Darwin.connect(fd.fd, UnsafePointer<sockaddr>(addrPtr), socklen_t(addrLen))
#endif
if cRes != -1 {
callBack(self)
} else {
guard errno == EINPROGRESS else {
try ThrowNetworkError()
}
let event: LibEvent = LibEvent(base: LibEvent.eventBase, fd: fd.fd, what: EV_WRITE, userData: nil) {
(fd:Int32, w:Int16, ud:AnyObject?) -> () in
if (Int32(w) & EV_TIMEOUT) != 0 {
callBack(nil)
} else {
callBack(self)
}
}
event.add(timeoutSeconds)
}
}
/// Send the existing opened file descriptor over the connection to the recipient
/// - parameter fd: The file descriptor to send
/// - parameter callBack: The callback to call when the send completes. The parameter passed will be `true` if the send completed without error.
/// - throws: `PerfectError.NetworkError`
public func sendFd(fd: Int32, callBack: (Bool) -> ()) throws {
let length = sizeof(cmsghdr) + sizeof(Int32)
#if os(Linux)
let msghdr = UnsafeMutablePointer<SwiftGlibc.msghdr>.alloc(1)
#else
let msghdr = UnsafeMutablePointer<Darwin.msghdr>.alloc(1)
#endif
let nothingPtr = UnsafeMutablePointer<iovec>.alloc(1)
let nothing = UnsafeMutablePointer<CChar>.alloc(1)
let buffer = UnsafeMutablePointer<CChar>.alloc(length)
defer {
msghdr.destroy()
msghdr.dealloc(1)
buffer.destroy()
buffer.dealloc(length)
nothingPtr.destroy()
nothingPtr.dealloc(1)
nothing.destroy()
nothing.dealloc(1)
}
let cmsg = UnsafeMutablePointer<cmsghdr>(buffer)
#if os(Linux)
cmsg.memory.cmsg_len = Int(socklen_t(length))
#else
cmsg.memory.cmsg_len = socklen_t(length)
#endif
cmsg.memory.cmsg_level = SOL_SOCKET
cmsg.memory.cmsg_type = SCM_RIGHTS
let asInts = UnsafeMutablePointer<Int32>(cmsg.advancedBy(1))
asInts.memory = fd
nothing.memory = 33
nothingPtr.memory.iov_base = UnsafeMutablePointer<Void>(nothing)
nothingPtr.memory.iov_len = 1
msghdr.memory.msg_name = UnsafeMutablePointer<Void>(())
msghdr.memory.msg_namelen = 0
msghdr.memory.msg_flags = 0
msghdr.memory.msg_iov = nothingPtr
msghdr.memory.msg_iovlen = 1
msghdr.memory.msg_control = UnsafeMutablePointer<Void>(buffer)
#if os(Linux)
msghdr.memory.msg_controllen = Int(socklen_t(length))
#else
msghdr.memory.msg_controllen = socklen_t(length)
#endif
let res = sendmsg(Int32(self.fd.fd), msghdr, 0)
if res > 0 {
callBack(true)
} else if res == -1 && errno == EAGAIN {
let event: LibEvent = LibEvent(base: LibEvent.eventBase, fd: self.fd.fd, what: EV_WRITE, userData: nil) {
[weak self] (fd:Int32, w:Int16, ud:AnyObject?) -> () in
if (Int32(w) & EV_TIMEOUT) != 0 {
callBack(false)
} else {
do {
try self?.sendFd(fd, callBack: callBack)
} catch {
callBack(false)
}
}
}
event.add()
} else {
try ThrowNetworkError()
}
}
/// Receive an existing opened file descriptor from the sender
/// - parameter callBack: The callback to call when the receive completes. The parameter passed will be the received file descriptor or invalidSocket.
/// - throws: `PerfectError.NetworkError`
public func receiveFd(callBack: (Int32) -> ()) throws {
let length = sizeof(cmsghdr) + sizeof(Int32)
var msghdrr = msghdr()
let nothingPtr = UnsafeMutablePointer<iovec>.alloc(1)
let nothing = UnsafeMutablePointer<CChar>.alloc(1)
let buffer = UnsafeMutablePointer<CChar>.alloc(length)
defer {
buffer.destroy()
buffer.dealloc(length)
nothingPtr.destroy()
nothingPtr.dealloc(1)
nothing.destroy()
nothing.dealloc(1)
}
nothing.memory = 33
nothingPtr.memory.iov_base = UnsafeMutablePointer<Void>(nothing)
nothingPtr.memory.iov_len = 1
msghdrr.msg_iov = UnsafeMutablePointer<iovec>(nothingPtr)
msghdrr.msg_iovlen = 1
msghdrr.msg_control = UnsafeMutablePointer<Void>(buffer)
#if os(Linux)
msghdrr.msg_controllen = Int(socklen_t(length))
#else
msghdrr.msg_controllen = socklen_t(length)
#endif
let cmsg = UnsafeMutablePointer<cmsghdr>(buffer)
#if os(Linux)
cmsg.memory.cmsg_len = Int(socklen_t(length))
#else
cmsg.memory.cmsg_len = socklen_t(length)
#endif
cmsg.memory.cmsg_level = SOL_SOCKET
cmsg.memory.cmsg_type = SCM_RIGHTS
let asInts = UnsafeMutablePointer<Int32>(cmsg.advancedBy(1))
asInts.memory = -1
let res = recvmsg(Int32(self.fd.fd), &msghdrr, 0)
if res > 0 {
let receivedInt = asInts.memory
callBack(receivedInt)
} else if res == -1 && errno == EAGAIN {
let event: LibEvent = LibEvent(base: LibEvent.eventBase, fd: self.fd.fd, what: EV_READ, userData: nil) {
[weak self] (fd:Int32, w:Int16, ud:AnyObject?) -> () in
if (Int32(w) & EV_TIMEOUT) != 0 {
callBack(invalidSocket)
} else {
do {
try self?.receiveFd(callBack)
} catch {
callBack(invalidSocket)
}
}
}
event.add()
} else {
try ThrowNetworkError()
}
}
/// Send the existing & opened `File`'s descriptor over the connection to the recipient
/// - parameter file: The `File` whose descriptor to send
/// - parameter callBack: The callback to call when the send completes. The parameter passed will be `true` if the send completed without error.
/// - throws: `PerfectError.NetworkError`
public func sendFile(file: File, callBack: (Bool) -> ()) throws {
try self.sendFd(Int32(file.fd), callBack: callBack)
}
/// Send the existing & opened `NetTCP`'s descriptor over the connection to the recipient
/// - parameter file: The `NetTCP` whose descriptor to send
/// - parameter callBack: The callback to call when the send completes. The parameter passed will be `true` if the send completed without error.
/// - throws: `PerfectError.NetworkError`
public func sendFile(file: NetTCP, callBack: (Bool) -> ()) throws {
try self.sendFd(file.fd.fd, callBack: callBack)
}
/// Receive an existing opened `File` descriptor from the sender
/// - parameter callBack: The callback to call when the receive completes. The parameter passed will be the received `File` object or nil.
/// - throws: `PerfectError.NetworkError`
public func receiveFile(callBack: (File?) -> ()) throws {
try self.receiveFd {
(fd: Int32) -> () in
if fd == invalidSocket {
callBack(nil)
} else {
callBack(File(fd: fd, path: ""))
}
}
}
/// Receive an existing opened `NetTCP` descriptor from the sender
/// - parameter callBack: The callback to call when the receive completes. The parameter passed will be the received `NetTCP` object or nil.
/// - throws: `PerfectError.NetworkError`
public func receiveNetTCP(callBack: (NetTCP?) -> ()) throws {
try self.receiveFd {
(fd: Int32) -> () in
if fd == invalidSocket {
callBack(nil)
} else {
callBack(NetTCP(fd: fd))
}
}
}
/// Receive an existing opened `NetNamedPipe` descriptor from the sender
/// - parameter callBack: The callback to call when the receive completes. The parameter passed will be the received `NetNamedPipe` object or nil.
/// - throws: `PerfectError.NetworkError`
public func receiveNetNamedPipe(callBack: (NetNamedPipe?) -> ()) throws {
try self.receiveFd {
(fd: Int32) -> () in
if fd == invalidSocket {
callBack(nil)
} else {
callBack(NetNamedPipe(fd: fd))
}
}
}
override func makeFromFd(fd: Int32) -> NetTCP {
return NetNamedPipe(fd: fd)
}
}