I've implemented a VPN app with Packet Tunnel Provider for MacOS and iOS.
I have two questions regarding the Extension's sleep/wake functions:
1. If the VPN configuration is set with disconnectOnSleep = false, and at the extension I'm sending keep-alives every X seconds,
What would happen when the device enters sleep mode? Will it keep sending keep-alive (because the VPN is configured with disconnectOnSleep=false) ?
2. If the VPN configuration is set with disconnectOnSleep = true, and also isOnDemandEnabled = true.
When the device enters sleep mode, do I need to disconnect the VPN myself? Or the OS would take care of it?
And if I should disconnect it myself, the on-demand won't try to turn it on again (because the on-demand) ?
I recently investigated the interaction between VPN and sleep as part of a DTS incident. I’ve included the resulting info below. I think that’ll answer a bunch of your questions. If there’s any remaining, please post back with the specifics.
Your tunnel provider can work in one of two modes:
If
is set (disconnectOnSleep
), the system will automatically stop the tunnel as the device goes to sleep. In that case the tunnel won’t automatically start again on wake (unless triggered by an on demand rule).<NetworkExtension/NEVPNProtocol.h>
If
is false, the system does not automatically stop the tunnel on sleep. In that case:disconnectOnSleep
Your provider should override the
and-sleepWithCompletionHandler:
methods (-wake
).<NetworkExtension/NEProvider.h>
In the ‘sleep’ override it should quiesce the tunnel as appropriate for your protocol. In the ‘wake’ override it should reactivate the tunnel.
If the reactivation fails (for example, because the network environment changed in a way that prevents reactivation) your provider should stop the tunnel by calling one of the ‘cancel’ methods (for example,
for a packet tunnel provider).-cancelTunnelWithError
Between the tunnel quiesce and reactivate your provider should set
to true (reasserting
) so that the system knows it’s not connected.<NetworkExtension/NETunnelProvider.h>
While reactivating you can choose to clear out your tunnel settings by calling
with nil for the-setTunnelNetworkSettings:completionHandler:
parameter (tunnelNetworkSettings
). Doing this will stop traffic flowing into the tunnel, which might be a nice thing to do for the user if the reactivation takes a long time.<NetworkExtension/NETunnelProvider.h>
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"