how to set up a vpn connection programmatically in MAC?

前端 未结 1 468
执念已碎
执念已碎 2020-12-11 05:25

after searching for a while i found that the only best source available to establish a vpn connection programmatically in mac is at

http://lists.apple.com/archives/M

1条回答
  •  佛祖请我去吃肉
    2020-12-11 06:06

    but i was struck at the 5th point while coding which says about kSCNetworkProtocolTypePPP and there is no such thing...

    There is no need to fetch the PPP protocol, you can apply the settings directly to the interface you created. If you created the interface like this:

    interface = SCNetworkInterfaceCreateWithInterface(bottomInterface, kSCNetworkInterfaceTypePPP);
    

    you can apply the PPP options directly using:

    SCNetworkInterfaceSetConfiguration(interface, myOptions)
    

    You'll have to apply the shared secret using

    SCNetworkInterfaceSetExtendedConfiguration(interface, CFSTR("IPSec"), myOptions)
    

    And if you want to enable "Send all traffic over VPN", you'll need to apply those settings by fetching the IPv4 protocol first:

    SCNetworkProtocolRef protocol = SCNetworkServiceCopyProtocol(service, kSCNetworkProtocolTypeIPv4);
    SCNetworkProtocolSetConfiguration(protocol, myOptions)
    

    The source code mentioned in this answer was extracted from https://github.com/halo/macosvpn/blob/master/macosvpn/Classes/VPNController.m where you can find it in its entirety.

    0 讨论(0)
提交回复
热议问题