Skip to content

Commit 71b6717

Browse files
committed
Ignore parse errors from pinger when its socket is closed
1 parent 897f462 commit 71b6717

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

Diff for: ios/PacketTunnelCore/Pinger/TunnelPinger.swift

+21-21
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,12 @@ import WireGuardKit
1414

1515
public final class TunnelPinger: PingerProtocol {
1616
private var sequenceNumber: UInt16 = 0
17-
private let stateLock = NSRecursiveLock()
17+
private let stateLock = NSLock()
1818
private let pingReceiveQueue: DispatchQueue
1919
private let replyQueue: DispatchQueue
2020
private var destAddress: IPv4Address?
21-
private var _onReply: ((PingerReply) -> Void)?
22-
public var onReply: ((PingerReply) -> Void)? {
23-
get {
24-
stateLock.withLock {
25-
return _onReply
26-
}
27-
}
28-
set {
29-
stateLock.withLock {
30-
_onReply = newValue
31-
}
32-
}
33-
}
34-
21+
// Always accessed from the `replyQueue` and is assigned once, on the main thread of the PacketTunnel. It is thread safe.
22+
public var onReply: ((PingerReply) -> Void)?
3523
private var pingProvider: ICMPPingProvider
3624

3725
private let logger: Logger
@@ -49,18 +37,26 @@ public final class TunnelPinger: PingerProtocol {
4937

5038
public func openSocket(bindTo interfaceName: String?, destAddress: IPv4Address) throws {
5139
try pingProvider.openICMP(address: destAddress)
52-
self.destAddress = destAddress
40+
stateLock.withLock {
41+
self.destAddress = destAddress
42+
}
5343
pingReceiveQueue.async { [weak self] in
5444
while let self {
5545
do {
5646
let seq = try pingProvider.receiveICMP()
5747

5848
replyQueue.async { [weak self] in
59-
self?.onReply?(PingerReply.success(destAddress, UInt16(seq)))
49+
self?.stateLock.withLock {
50+
self?.onReply?(PingerReply.success(destAddress, UInt16(seq)))
51+
}
6052
}
6153
} catch {
6254
replyQueue.async { [weak self] in
63-
self?.onReply?(PingerReply.parseError(error))
55+
self?.stateLock.withLock {
56+
if self?.destAddress != nil {
57+
self?.onReply?(PingerReply.parseError(error))
58+
}
59+
}
6460
}
6561
return
6662
}
@@ -69,14 +65,18 @@ public final class TunnelPinger: PingerProtocol {
6965
}
7066

7167
public func closeSocket() {
72-
pingProvider.closeICMP()
73-
self.destAddress = nil
68+
stateLock.withLock {
69+
self.destAddress = nil
70+
pingProvider.closeICMP()
71+
}
7472
}
7573

7674
public func send() throws -> PingerSendResult {
7775
let sequenceNumber = nextSequenceNumber()
7876

79-
guard let destAddress else { throw WireGuardAdapterError.invalidState }
77+
stateLock.lock()
78+
defer { stateLock.unlock() }
79+
guard destAddress != nil else { throw WireGuardAdapterError.invalidState }
8080
// NOTE: we cheat here by returning the destination address we were passed, rather than parsing it from the packet on the other side of the FFI boundary.
8181
try pingProvider.sendICMPPing(seqNumber: sequenceNumber)
8282

0 commit comments

Comments
 (0)