-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathPinger.swift
308 lines (252 loc) · 9.42 KB
/
Pinger.swift
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
//
// Pinger.swift
// PacketTunnelCore
//
// Created by pronebird on 21/02/2022.
// Copyright © 2022 Mullvad VPN AB. All rights reserved.
//
import Foundation
import Network
/// ICMP client.
public final class Pinger: PingerProtocol {
// Socket read buffer size.
private static let bufferSize = 65535
// Sender identifier passed along with ICMP packet.
private let identifier: UInt16
private var sequenceNumber: UInt16 = 0
private var socket: CFSocket?
private var readBuffer = [UInt8](repeating: 0, count: bufferSize)
private let stateLock = NSRecursiveLock()
private let replyQueue: DispatchQueue
public var onReply: ((PingerReply) -> Void)? {
get {
stateLock.withLock {
return _onReply
}
}
set {
stateLock.withLock {
_onReply = newValue
}
}
}
private var _onReply: ((PingerReply) -> Void)?
deinit {
closeSocket()
}
public init(identifier: UInt16 = 757, replyQueue: DispatchQueue) {
self.identifier = identifier
self.replyQueue = replyQueue
}
/// Open socket and optionally bind it to the given interface.
/// Automatically closes the previously opened socket when called multiple times in a row.
public func openSocket(bindTo interfaceName: String?) throws {
stateLock.lock()
defer { stateLock.unlock() }
closeSocket()
var context = CFSocketContext()
context.info = Unmanaged.passUnretained(self).toOpaque()
guard let newSocket = CFSocketCreate(
kCFAllocatorDefault,
AF_INET,
SOCK_DGRAM,
IPPROTO_ICMP,
CFSocketCallBackType.readCallBack.rawValue,
{ socket, callbackType, _, _, info in
guard let socket, let info, callbackType == .readCallBack else {
return
}
let pinger = Unmanaged<Pinger>.fromOpaque(info).takeUnretainedValue()
pinger.readSocket(socket)
},
&context
) else {
throw Error.createSocket
}
let flags = CFSocketGetSocketFlags(newSocket)
if (flags & kCFSocketCloseOnInvalidate) == 0 {
CFSocketSetSocketFlags(newSocket, flags | kCFSocketCloseOnInvalidate)
}
if let interfaceName {
try bindSocket(newSocket, to: interfaceName)
}
guard let runLoop = CFSocketCreateRunLoopSource(kCFAllocatorDefault, newSocket, 0) else {
throw Error.createRunLoop
}
CFRunLoopAddSource(CFRunLoopGetMain(), runLoop, .commonModes)
socket = newSocket
}
public func closeSocket() {
stateLock.lock()
defer { stateLock.unlock() }
if let socket {
CFSocketInvalidate(socket)
self.socket = nil
}
}
/// Send ping packet to the given address.
/// Returns `PingerSendResult` on success, otherwise throws a `Pinger.Error`.
public func send(to address: IPv4Address) throws -> PingerSendResult {
stateLock.lock()
defer { stateLock.unlock() }
guard let socket else {
throw Error.closedSocket
}
var sa = sockaddr_in()
sa.sin_len = UInt8(MemoryLayout.size(ofValue: sa))
sa.sin_family = sa_family_t(AF_INET)
sa.sin_addr = address.rawValue.withUnsafeBytes { buffer in
buffer.bindMemory(to: in_addr.self).baseAddress!.pointee
}
let sequenceNumber = nextSequenceNumber()
let packetData = ICMP.createICMPPacket(
identifier: identifier,
sequenceNumber: sequenceNumber
)
let bytesSent = packetData.withUnsafeBytes { dataBuffer -> Int in
withUnsafeBytes(of: &sa) { bufferPointer in
let sockaddrPointer = bufferPointer.bindMemory(to: sockaddr.self).baseAddress!
return sendto(
CFSocketGetNative(socket),
dataBuffer.baseAddress!,
dataBuffer.count,
0,
sockaddrPointer,
socklen_t(MemoryLayout<sockaddr_in>.size)
)
}
}
guard bytesSent >= 0 else {
throw Error.sendPacket(errno)
}
return PingerSendResult(sequenceNumber: sequenceNumber, bytesSent: UInt(bytesSent))
}
private func nextSequenceNumber() -> UInt16 {
stateLock.lock()
let (nextValue, _) = sequenceNumber.addingReportingOverflow(1)
sequenceNumber = nextValue
stateLock.unlock()
return nextValue
}
private func readSocket(_ socket: CFSocket) {
var address = sockaddr()
var addressLength = socklen_t(MemoryLayout.size(ofValue: address))
let bytesRead = recvfrom(
CFSocketGetNative(socket),
&readBuffer,
Self.bufferSize,
0,
&address,
&addressLength
)
do {
guard bytesRead > 0 else { throw Error.receivePacket(errno) }
let icmpHeader = try ICMP.parseICMPResponse(buffer: &readBuffer, length: bytesRead)
guard icmpHeader.identifier == identifier else {
throw Error.clientIdentifierMismatch
}
guard icmpHeader.type == ICMP_ECHOREPLY else {
throw Error.invalidICMPType(icmpHeader.type)
}
guard let sender = Self.makeIPAddress(from: address) else { throw Error.parseIPAddress }
replyQueue.async {
self.onReply?(.success(sender, icmpHeader.sequenceNumber))
}
} catch Pinger.Error.clientIdentifierMismatch {
// Ignore responses from other senders.
} catch {
replyQueue.async {
self.onReply?(.parseError(error))
}
}
}
private func bindSocket(_ socket: CFSocket, to interfaceName: String) throws {
var index = if_nametoindex(interfaceName)
guard index > 0 else {
throw Error.mapInterfaceNameToIndex(errno)
}
let result = setsockopt(
CFSocketGetNative(socket),
IPPROTO_IP,
IP_BOUND_IF,
&index,
socklen_t(MemoryLayout.size(ofValue: index))
)
if result == -1 {
throw Error.bindSocket(errno)
}
}
private static func makeIPAddress(from sa: sockaddr) -> IPAddress? {
if sa.sa_family == AF_INET {
return withUnsafeBytes(of: sa) { buffer -> IPAddress? in
buffer.bindMemory(to: sockaddr_in.self).baseAddress.flatMap { boundPointer in
var saddr = boundPointer.pointee
let data = Data(bytes: &saddr.sin_addr, count: MemoryLayout<in_addr>.size)
return IPv4Address(data, nil)
}
}
}
if sa.sa_family == AF_INET6 {
return withUnsafeBytes(of: sa) { buffer in
buffer.bindMemory(to: sockaddr_in6.self).baseAddress
.flatMap { boundPointer in
var saddr6 = boundPointer.pointee
let data = Data(
bytes: &saddr6.sin6_addr,
count: MemoryLayout<in6_addr>.size
)
return IPv6Address(data)
}
}
}
return nil
}
}
extension Pinger {
public enum Error: LocalizedError {
/// Failure to create a socket.
case createSocket
/// Failure to map interface name to index.
case mapInterfaceNameToIndex(Int32)
/// Failure to bind socket to interface.
case bindSocket(Int32)
/// Failure to create a runloop for socket.
case createRunLoop
/// Failure to send a packet due to socket being closed.
case closedSocket
/// Failure to send packet. Contains the `errno`.
case sendPacket(Int32)
/// Failure to receive packet. Contains the `errno`.
case receivePacket(Int32)
/// Unexpected ICMP reply type
case invalidICMPType(UInt8)
/// Response identifier does not match the sender identifier.
case clientIdentifierMismatch
/// Failure to parse IP address.
case parseIPAddress
public var errorDescription: String? {
switch self {
case .createSocket:
return "Failure to create socket."
case .mapInterfaceNameToIndex:
return "Failure to map interface name to index."
case .bindSocket:
return "Failure to bind socket to interface."
case .createRunLoop:
return "Failure to create run loop for socket."
case .closedSocket:
return "Socket is closed."
case let .sendPacket(code):
return "Failure to send packet (errno: \(code))."
case let .receivePacket(code):
return "Failure to receive packet (errno: \(code))."
case let .invalidICMPType(type):
return "Unexpected ICMP reply type: \(type)"
case .clientIdentifierMismatch:
return "Response identifier does not match the sender identifier."
case .parseIPAddress:
return "Failed to parse IP address."
}
}
}
}