1

Need create VPN connection L2TP on osx without Shared Secret

NSString *server = @"serverIP";
        NSString *username = @"user";
        NSString *password = @"pass";  
        const void* passwordData = [[password dataUsingEncoding:NSUTF8StringEncoding] bytes];
    [vpnManager loadFromPreferencesWithCompletionHandler:^(NSError *error) {
        if (error) {
              NSLog(@"Load config failed [%@]", error.localizedDescription);
                return;
            }

    NEVPNProtocol *p = (NEVPNProtocol *)vpnManager.protocolConfiguration;

    if (!p) {
        p = [[NEVPNProtocol alloc] init];
    }
    p.username = username;
    p.serverAddress = server;
    p.passwordReference = (__bridge NSData * _Nullable)(passwordData);
    p.disconnectOnSleep = NO;

    vpnManager.protocolConfiguration = p;
    vpnManager.localizedDescription = @"L2TPOverIPSec";
    vpnManager.enabled = YES;

    [vpnManager saveToPreferencesWithCompletionHandler:^(NSError *error) {
        if (error) {
            NSLog(@"Save config failed [%@]", error.localizedDescription);
        }
    }];

}];

NEVPNConnection *connect = [vpnManager connection];
NSError *error1;
if ([connect startVPNTunnelAndReturnError:  &error1]) {
    NSLog(@"connect");
} else {
    NSLog(@"not connect");
}

after building i get this error Missing protocol or protocol has invalid type, and other 2 protocols use IKE tunnel, what can u advice to me? other option is run terminal from code and add this string networksetup -connectpppoeservice VPNConnect but i dont know if it possible

Eugenie
  • 131
  • 1
  • 2
  • 11

2 Answers2

0

I know this is the old question, but I'm here to note, that Network Extension framework (where NEVPNProtocol is from) can't go on with OSI level 2 protocols, which is L2TP. (https://forums.developer.apple.com/thread/29909)

It seems now (starting from iOS 8 and os x 10.10) the recommended way on Apple devices is to use built-in protocols, or implement your own but on L3 / L4: https://developer.apple.com/documentation/networkextension

(And so there is no public API for using L2TP)

Arthur Bulakaiev
  • 1,207
  • 8
  • 17
0

No, you need use SCNetwork and Helper Tool (to get root access) With this link you will be able to create L2TP Protocol and connect with it.

This works even on the latest version MacOS 11 "Big Sure"

neskafesha
  • 191
  • 5