]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Kit: Adapter: ignore error when updating tunnel settings in offline am/ignore-set-network-settings-error-when-offline
authorAndrej Mihajlov <and@mullvad.net>
Mon, 20 Dec 2021 11:19:17 +0000 (12:19 +0100)
committerAndrej Mihajlov <and@mullvad.net>
Mon, 20 Dec 2021 15:21:34 +0000 (16:21 +0100)
Since around iOS 15.1, calling `setNetworkSettings()` when in
airplane mode, throws an unknown error:

Error Domain=NEAgentErrorDomain Code=1

Signed-off-by: Andrej Mihajlov <and@mullvad.net>
Sources/WireGuardKit/WireGuardAdapter.swift

index 4cb2e2ea69dfb5b902e33811d3593a61655564d7..0f00b8982580c848ed14f282bc9b7246eb6003c1 100644 (file)
@@ -253,35 +253,58 @@ public class WireGuardAdapter {
                 self.packetTunnelProvider?.reasserting = false
             }
 
+            let settingsGenerator: PacketTunnelSettingsGenerator
             do {
-                let settingsGenerator = try self.makeSettingsGenerator(with: tunnelConfiguration)
-                try self.setNetworkSettings(settingsGenerator.generateNetworkSettings())
+                settingsGenerator = try self.makeSettingsGenerator(with: tunnelConfiguration)
+            } catch let error as WireGuardAdapterError {
+                completionHandler(error)
+                return
+            } catch {
+                fatalError()
+            }
 
-                switch self.state {
-                case .started(let handle, _):
-                    let (wgConfig, resolutionResults) = settingsGenerator.uapiConfiguration()
-                    self.logEndpointResolutionResults(resolutionResults)
+            switch self.state {
+            case .started(let handle, _):
+                do {
+                    try self.setNetworkSettings(settingsGenerator.generateNetworkSettings())
+                } catch let error as WireGuardAdapterError {
+                    completionHandler(error)
+                    return
+                } catch {
+                    fatalError()
+                }
 
-                    wgSetConfig(handle, wgConfig)
-                    #if os(iOS)
-                    wgDisableSomeRoamingForBrokenMobileSemantics(handle)
-                    #endif
+                let (wgConfig, resolutionResults) = settingsGenerator.uapiConfiguration()
+                self.logEndpointResolutionResults(resolutionResults)
 
-                    self.state = .started(handle, settingsGenerator)
+                wgSetConfig(handle, wgConfig)
+                #if os(iOS)
+                wgDisableSomeRoamingForBrokenMobileSemantics(handle)
+                #endif
 
-                case .temporaryShutdown:
-                    self.state = .temporaryShutdown(settingsGenerator)
+                self.state = .started(handle, settingsGenerator)
 
-                case .stopped:
+            case .temporaryShutdown:
+                // On iOS 15.1 or newer, updating network settings may fail when in airplane mode.
+                // Network path monitor will retry updating settings later when connectivity is
+                // back online.
+                do {
+                    try self.setNetworkSettings(settingsGenerator.generateNetworkSettings())
+                } catch let error as WireGuardAdapterError {
+                    if case .setNetworkSettings(let systemError) = error {
+                        self.logHandler(.verbose, "Failed to set network settings while offline. Will retry when connectivity is back online. Error: \(systemError.localizedDescription)")
+                    }
+                } catch {
                     fatalError()
                 }
 
-                completionHandler(nil)
-            } catch let error as WireGuardAdapterError {
-                completionHandler(error)
-            } catch {
+                self.state = .temporaryShutdown(settingsGenerator)
+
+            case .stopped:
                 fatalError()
             }
+
+            completionHandler(nil)
         }
     }