]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Supply missing pieces of path change
authorJason A. Donenfeld <Jason@zx2c4.com>
Tue, 11 Dec 2018 23:45:50 +0000 (00:45 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Wed, 12 Dec 2018 00:11:43 +0000 (01:11 +0100)
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
WireGuard/WireGuardNetworkExtension/PacketTunnelSettingsGenerator.swift
wireguard-go-bridge/src/api-ios.go
wireguard-go-bridge/wireguard.h

index 04e96379dc4c67e99c32b66618e9ad759851eb75..f5f0296ac29484cf843275c3b9f1302a6ac9d69b 100644 (file)
@@ -95,16 +95,18 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
         networkMonitor = NWPathMonitor()
         networkMonitor?.pathUpdateHandler = { path in
             guard handle >= 0 else { return }
-            
             if path.status == .satisfied {
-                let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration()
-                
-                let endpointGoString = endpointString.withCString {
-                    gostring_t(p: $0, n: endpointString.utf8.count)
+                wg_log(.debug, message: "Network change detected, re-establishing sockets and IPs: \(path.availableInterfaces)")
+                let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: wgGetListenPort(handle))
+                let err = endpointString.withCString {
+                    wgSetConfig(handle,  gostring_t(p: $0, n: endpointString.utf8.count))
+                }
+                if err == -EADDRINUSE {
+                    let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: 0)
+                    _ = endpointString.withCString {
+                        wgSetConfig(handle,  gostring_t(p: $0, n: endpointString.utf8.count))
+                    }
                 }
-                
-                wg_log(.debug, staticMessage: "Network change detected, calling wgSetConfig")
-                wgSetConfig(handle, endpointGoString)
             }
         }
         networkMonitor?.start(queue: DispatchQueue(label: "NetworkMonitor"))
index 0eea95aacfe8762178d1e5f8ee8309ab84cb2c91..61e161eb31dba7dc4a99cdcf7e4300f614082e7c 100644 (file)
@@ -15,8 +15,8 @@ class PacketTunnelSettingsGenerator {
         self.resolvedEndpoints = resolvedEndpoints
     }
 
-    func endpointUapiConfiguration() -> String {
-        var wgSettings = "listen_port=\(tunnelConfiguration.interface.listenPort ?? 0)\n"
+    func endpointUapiConfiguration(currentListenPort: UInt16) -> String {
+        var wgSettings = "listen_port=\(tunnelConfiguration.interface.listenPort ?? currentListenPort)\n"
 
         for (i, peer) in tunnelConfiguration.peers.enumerated() {
             wgSettings.append("public_key=\(peer.publicKey.hexEncodedString())\n")
index ff7aab2dcb9d2b9e90fac194f54e2a41def97869..3d35d1e34aaaa64b258db2709ddeb126bb7c2930 100644 (file)
@@ -128,16 +128,27 @@ func wgTurnOff(tunnelHandle int32) {
 }
 
 //export wgSetConfig
-func wgSetConfig(tunnelHandle int32, settings string) {
+func wgSetConfig(tunnelHandle int32, settings string) int64 {
        device, ok := tunnelHandles[tunnelHandle]
        if !ok {
-               return
+               return 0
        }
        bufferedSettings := bufio.NewReadWriter(bufio.NewReader(strings.NewReader(settings)), bufio.NewWriter(ioutil.Discard))
        err := ipcSetOperation(device, bufferedSettings)
        if err != nil {
                device.log.Error.Println(err)
+               return err.Code
+       }
+       return 0
+}
+
+//export wgGetListenPort
+func wgGetListenPort(tunnelHandle int32) uint16 {
+       device, ok := tunnelHandles[tunnelHandle]
+       if !ok {
+               return 0
        }
+       return device.net.port
 }
 
 //export wgVersion
index 54cb9289f78d7c673215aa93356f4e0b90dcafe4..fec352d57c9a07d3b6fc4f25c4f5e74c49128bf8 100644 (file)
@@ -14,7 +14,8 @@ typedef void(*logger_fn_t)(int level, const char *msg);
 extern void wgSetLogger(logger_fn_t logger_fn);
 extern int wgTurnOn(gostring_t ifname, gostring_t settings, int32_t tun_fd);
 extern void wgTurnOff(int handle);
-extern void wgSetConfig(int handle, gostring_t settings);
+extern int64_t wgSetConfig(int handle, gostring_t settings);
+extern uint16_t wgGetListenPort(int handle);
 extern char *wgVersion();
 
 #endif