aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-12-21 17:32:44 +0530
committerRoopesh Chander <roop@roopc.net>2018-12-21 17:32:44 +0530
commitf818cdd9631cdbe5b433aadb3af8ceab22f58c50 (patch)
treeac9f568d6221d9b67a9a54ecd01cd09977fade78 /WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
parentNE: Change handling of bad domain names and Activate On Demand (diff)
downloadwireguard-apple-f818cdd9631cdbe5b433aadb3af8ceab22f58c50.tar.xz
wireguard-apple-f818cdd9631cdbe5b433aadb3af8ceab22f58c50.zip
NE: Update listen port only when first interface changes
When handling network path changes, change the listen port only when the first interface has changed. Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift')
-rw-r--r--WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift19
1 files changed, 17 insertions, 2 deletions
diff --git a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
index f678ca7..8d71f0b 100644
--- a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
+++ b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
@@ -81,17 +81,32 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
var handle: Int32 = -1
+ func interfaceDescription(_ interface: NWInterface?) -> String {
+ if let interface = interface {
+ return "\(interface.name) (\(interface.type))"
+ } else {
+ return "None"
+ }
+ }
+
networkMonitor = NWPathMonitor()
+ var previousPrimaryNetworkPathInterface = networkMonitor?.currentPath.availableInterfaces.first
+ wg_log(.debug, message: "Network path primary interface: \(interfaceDescription(previousPrimaryNetworkPathInterface))")
networkMonitor?.pathUpdateHandler = { path in
guard handle >= 0 else { return }
if path.status == .satisfied {
wg_log(.debug, message: "Network change detected, re-establishing sockets and IPs: \(path.availableInterfaces)")
- let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: wgGetListenPort(handle))
+ let primaryNetworkPathInterface = path.availableInterfaces.first
+ wg_log(.debug, message: "Network path primary interface: \(interfaceDescription(primaryNetworkPathInterface))")
+ let shouldIncludeListenPort = previousPrimaryNetworkPathInterface != primaryNetworkPathInterface
+ let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(shouldIncludeListenPort: shouldIncludeListenPort, currentListenPort: wgGetListenPort(handle))
let err = withStringsAsGoStrings(endpointString, call: { return wgSetConfig(handle, $0.0) })
if err == -EADDRINUSE {
- let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: 0)
+ // We expect this to happen only if shouldIncludeListenPort is true
+ let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(shouldIncludeListenPort: shouldIncludeListenPort, currentListenPort: 0)
_ = withStringsAsGoStrings(endpointString, call: { return wgSetConfig(handle, $0.0) })
}
+ previousPrimaryNetworkPathInterface = primaryNetworkPathInterface
}
}
networkMonitor?.start(queue: DispatchQueue(label: "NetworkMonitor"))