Skip to content

Commit

Permalink
Use newer wg-apple and adjust pinger code
Browse files Browse the repository at this point in the history
  • Loading branch information
pinkisemils committed Oct 2, 2024
1 parent 45fc880 commit a09ff42
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion ios/MullvadVPN.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9232,7 +9232,7 @@
repositoryURL = "https://github.com/mullvad/wireguard-apple.git";
requirement = {
kind = revision;
revision = 5d5fbf1af490c2ec893cae908f5a204ac6f0da46;
revision = f1401d43f9d03438a81ca806b9f0c20269b116cb;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/mullvad/wireguard-apple.git",
"state" : {
"revision" : "5d5fbf1af490c2ec893cae908f5a204ac6f0da46"
"revision" : "f1401d43f9d03438a81ca806b9f0c20269b116cb"
}
}
],
Expand Down
41 changes: 21 additions & 20 deletions ios/PacketTunnelCore/Pinger/TunnelPinger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import WireGuardKit
public final class TunnelPinger: PingerProtocol {
private var sequenceNumber: UInt16 = 0
private let stateLock = NSRecursiveLock()
private let pingQueue: DispatchQueue
private let pingReceiveQueue: DispatchQueue
private let replyQueue: DispatchQueue
private var destAddress: IPv4Address?
private var _onReply: ((PingerReply) -> Void)?
Expand All @@ -39,7 +39,7 @@ public final class TunnelPinger: PingerProtocol {
init(pingProvider: ICMPPingProvider, replyQueue: DispatchQueue) {
self.pingProvider = pingProvider
self.replyQueue = replyQueue
self.pingQueue = DispatchQueue(label: "PacketTunnel.icmp")
self.pingReceiveQueue = DispatchQueue(label: "PacketTunnel.Receive.icmp")
self.logger = Logger(label: "TunnelPinger")
}

Expand All @@ -50,6 +50,22 @@ public final class TunnelPinger: PingerProtocol {
public func openSocket(bindTo interfaceName: String?, destAddress: IPv4Address) throws {
try pingProvider.openICMP(address: destAddress)
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)))
}
} catch {
replyQueue.async { [weak self] in
self?.onReply?(PingerReply.parseError(error))
}
return
}
}
}
}

public func closeSocket() {
Expand All @@ -59,25 +75,10 @@ public final class TunnelPinger: PingerProtocol {

public func send() throws -> PingerSendResult {
let sequenceNumber = nextSequenceNumber()
logger.debug("*** sending ping \(sequenceNumber)")

pingQueue.async { [weak self] in
guard let self, let destAddress else { return }
let reply: PingerReply
do {
try pingProvider.sendICMPPing(seqNumber: sequenceNumber)
// 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.
reply = .success(destAddress, sequenceNumber)
} catch {
reply = .parseError(error)
}
self.logger.debug("--- Pinger reply: \(reply)")

replyQueue.async { [weak self] in
guard let self else { return }
self.onReply?(reply)
}
}
guard let destAddress 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)

return PingerSendResult(sequenceNumber: UInt16(sequenceNumber))
}
Expand Down

0 comments on commit a09ff42

Please sign in to comment.