Bug 44874 - Problems with NetworkExtension.NETunnelProviderManager
Summary: Problems with NetworkExtension.NETunnelProviderManager
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 10.0 (iOS10)
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: (C10)
Assignee: Vincent Dondain [MSFT]
Depends on:
Reported: 2016-09-29 11:57 UTC by Per Allansson
Modified: 2016-10-03 22:00 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Per Allansson 2016-09-29 11:57:27 UTC
Normally when using the NETunnelProviderManager in for example Xcode/Swift, you would do something like this to create a new vpn configuration (assuming one has the correct entitlements, which I do have):

  NETunnelProviderManager.loadAllFromPreferences() { ... }
  let newMgr = NETunnelProviderManager()
  newMgr.localizedDescription = "My VPN"
  newMgr.protocolConfiguration = NETunnelProviderProtocol()
  newMgr.protocolConfiguration?.serverAddress = "vpn.server.address"
  newMgr.saveToPreferences() { ... }

now this should translate to, in Xamarin/C#:

  NETunnelProviderManager.loadAllFromPreferences((action, err) => { ... });
  var newMgr = new NETunnelProviderManager(); // FAIL!
  newMgr.LocalizedDescription = "My VPN";
  newMgr.ProtocolConfiguration = new NETunnelProviderProtocol();
  newMgr.ProtocolConfiguration.serverAddress = "vpn.server.address";
  newMgr.SaveToPreferences((err) => { ...});

Unfortunately there is no public (or protected) () constructor for NETunnelProviderManager, so I tried a workaround by creating a subclass:

  class MyNETunnelProviderManager : NETunnelProviderManager 
       public MyNETunnelProviderManager() : base(NSObjectFlag.Empty) {}

  NETunnelProviderManager.loadAllFromPreferences((action, err) => { ... });
  var newMgr = new MyNETunnelProviderManager();
  newMgr.LocalizedDescription = "My VPN";
  newMgr.ProtocolConfiguration = new NETunnelProviderProtocol();
  newMgr.ProtocolConfiguration.serverAddress = "vpn.server.address"; // FAIL!
  newMgr.SaveToPreferences((err) => { ...});

but for some reason the property newMgr.ProtocolConfiguration will always be null even though I have set it. 

So, to summarize:

 1) constructor public NETunnelProviderManager() should exist.

 2) and if 1) doesn't solve the problem with the ProtocolConfiguration not being set, that should be looked into as well (or tell me what I am doing wrong :) )
Comment 1 Vincent Dondain [MSFT] 2016-09-29 12:23:11 UTC

There is a comment in our bindings to explain why the default constructor has been disabled: https://github.com/xamarin/xamarin-macios/blob/master/src/networkextension.cs#L738

As NETunnelProviderManager is a subclass of NEVpnManager I believe you should be using the static "SharedManager" property (https://github.com/xamarin/xamarin-macios/blob/master/src/networkextension.cs#L790-L791).

I'm marking this as resolved answered but feel free to reopen if using SharedManager doesn't solve your issues.
Comment 2 Per Allansson 2016-09-29 12:50:56 UTC
SharedManager seems to be for NEVPNManager only - you cannot use it together with NETunnelProviderManager. I just tried it with Xcode/Swift and it fails to save the preferences if I replace

 let newMgr = NETunnelProviderManager()


 let newMgr = NETunnelProviderManager.shared()
Comment 3 Sebastien Pouliot 2016-09-29 14:05:19 UTC
The bindings were done before Apple release any documentation and before we got the required entitlements.

From a quick look the `init` call seems to be valid only with the entitlement. We'll confirm this asap and update the bindings.
Comment 4 Vincent Dondain [MSFT] 2016-10-03 22:00:18 UTC
Fixed in https://github.com/xamarin/xamarin-macios/pull/936

Note: ProtocolConfiguration can now be set.