Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
866 views
in Technique[技术] by (71.8m points)

macos - how to set up a vpn connection programmatically in MAC?

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/Macnetworkprog/2011/May/msg00032.html

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

has anybody done it as shown in the above link or is there any other source available...?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...