summaryrefslogtreecommitdiffhomepage
path: root/ios
diff options
context:
space:
mode:
authorBug Magnet <marco.nikic@mullvad.net>2024-10-09 16:05:46 +0200
committerBug Magnet <marco.nikic@mullvad.net>2024-10-11 12:32:10 +0200
commit67dfd8d808e3aea7b5c46f5c5e4e9ece7632017f (patch)
treef59df7d70b00650cd1ad7ab1235320eb711e2cb7 /ios
parentc082cbaa1df91796859b5e732f67868a80b6966d (diff)
downloadmullvadvpn-67dfd8d808e3aea7b5c46f5c5e4e9ece7632017f.tar.xz
mullvadvpn-67dfd8d808e3aea7b5c46f5c5e4e9ece7632017f.zip
Ignore parse errors from pinger when its socket is closed
Diffstat (limited to 'ios')
-rw-r--r--ios/PacketTunnelCore/Pinger/TunnelPinger.swift42
1 files changed, 21 insertions, 21 deletions
diff --git a/ios/PacketTunnelCore/Pinger/TunnelPinger.swift b/ios/PacketTunnelCore/Pinger/TunnelPinger.swift
index 9b9162f2e5..47c5d8734a 100644
--- a/ios/PacketTunnelCore/Pinger/TunnelPinger.swift
+++ b/ios/PacketTunnelCore/Pinger/TunnelPinger.swift
@@ -14,24 +14,12 @@ import WireGuardKit
public final class TunnelPinger: PingerProtocol {
private var sequenceNumber: UInt16 = 0
- private let stateLock = NSRecursiveLock()
+ private let stateLock = NSLock()
private let pingReceiveQueue: DispatchQueue
private let replyQueue: DispatchQueue
private var destAddress: IPv4Address?
- private var _onReply: ((PingerReply) -> Void)?
- public var onReply: ((PingerReply) -> Void)? {
- get {
- stateLock.withLock {
- return _onReply
- }
- }
- set {
- stateLock.withLock {
- _onReply = newValue
- }
- }
- }
-
+ /// Always accessed from the `replyQueue` and is assigned once, on the main thread of the PacketTunnel. It is thread safe.
+ public var onReply: ((PingerReply) -> Void)?
private var pingProvider: ICMPPingProvider
private let logger: Logger
@@ -49,18 +37,26 @@ public final class TunnelPinger: PingerProtocol {
public func openSocket(bindTo interfaceName: String?, destAddress: IPv4Address) throws {
try pingProvider.openICMP(address: destAddress)
- self.destAddress = destAddress
+ stateLock.withLock {
+ self.destAddress = destAddress
+ }
pingReceiveQueue.async { [weak self] in
while let self {
do {
let seq = try pingProvider.receiveICMP()
replyQueue.async { [weak self] in
- self?.onReply?(PingerReply.success(destAddress, UInt16(seq)))
+ self?.stateLock.withLock {
+ self?.onReply?(PingerReply.success(destAddress, UInt16(seq)))
+ }
}
} catch {
replyQueue.async { [weak self] in
- self?.onReply?(PingerReply.parseError(error))
+ self?.stateLock.withLock {
+ if self?.destAddress != nil {
+ self?.onReply?(PingerReply.parseError(error))
+ }
+ }
}
return
}
@@ -69,14 +65,18 @@ public final class TunnelPinger: PingerProtocol {
}
public func closeSocket() {
- pingProvider.closeICMP()
- self.destAddress = nil
+ stateLock.withLock {
+ self.destAddress = nil
+ pingProvider.closeICMP()
+ }
}
public func send() throws -> PingerSendResult {
let sequenceNumber = nextSequenceNumber()
- guard let destAddress else { throw WireGuardAdapterError.invalidState }
+ stateLock.lock()
+ defer { stateLock.unlock() }
+ guard destAddress != nil else { throw WireGuardAdapterError.invalidState }
// 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.
try pingProvider.sendICMPPing(seqNumber: sequenceNumber)