aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-05-30 15:31:09 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2019-05-31 17:29:29 +0200
commit168ba2da8ad9876f8a7602352fa0e7e4c180d325 (patch)
treefc8a329df7c6e07503941bedd73fe210cc459633 /WireGuard
parentmacOS: Dismiss modals correctly (diff)
downloadwireguard-apple-168ba2da8ad9876f8a7602352fa0e7e4c180d325.tar.xz
wireguard-apple-168ba2da8ad9876f8a7602352fa0e7e4c180d325.zip
NetworkExtension: bump sockets on path change
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'WireGuard')
-rw-r--r--WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift22
1 files changed, 12 insertions, 10 deletions
diff --git a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
index 46d5c33..0618b19 100644
--- a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
+++ b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
@@ -9,17 +9,14 @@ import os.log
class PacketTunnelProvider: NEPacketTunnelProvider {
private var handle: Int32?
- #if os(iOS)
private var networkMonitor: NWPathMonitor?
- #endif
private var ifname: String?
+ private var lastSeenInterfaces: [String] = []
private var packetTunnelSettingsGenerator: PacketTunnelSettingsGenerator?
- #if os(iOS)
deinit {
networkMonitor?.cancel()
}
- #endif
override func startTunnel(options: [String: NSObject]?, completionHandler startTunnelCompletionHandler: @escaping (Error?) -> Void) {
let activationAttemptId = options?["activationAttemptId"] as? String
@@ -55,11 +52,9 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
errorNotifier.notify(PacketTunnelProviderError.couldNotSetNetworkSettings)
startTunnelCompletionHandler(PacketTunnelProviderError.couldNotSetNetworkSettings)
} else {
- #if os(iOS)
self.networkMonitor = NWPathMonitor()
self.networkMonitor!.pathUpdateHandler = self.pathUpdate
self.networkMonitor!.start(queue: DispatchQueue(label: "NetworkMonitor"))
- #endif
let fileDescriptor = (self.packetFlow.value(forKeyPath: "socket.fileDescriptor") as? Int32) ?? -1
if fileDescriptor < 0 {
@@ -90,10 +85,8 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
}
override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
- #if os(iOS)
networkMonitor?.cancel()
networkMonitor = nil
- #endif
ErrorNotifier.removeLastErrorFile()
@@ -148,14 +141,23 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
}
}
- #if os(iOS)
private func pathUpdate(path: Network.NWPath) {
guard let handle = handle else { return }
+ guard let ifname = ifname else { return }
+ wg_log(.debug, message: "Network change detected with \(path.status) route and interface order \(path.availableInterfaces)")
+ guard path.status == .satisfied else { return }
+
+ #if os(iOS)
if let packetTunnelSettingsGenerator = packetTunnelSettingsGenerator {
_ = packetTunnelSettingsGenerator.endpointUapiConfiguration().withGoString { return wgSetConfig(handle, $0) }
}
+ #endif
+ let interfaces = path.availableInterfaces.filter { $0.name != ifname }.compactMap { $0.name }
+ if !interfaces.elementsEqual(lastSeenInterfaces) {
+ lastSeenInterfaces = interfaces
+ wgBumpSockets(handle)
+ }
}
- #endif
}
extension String {