Need your help! create pptp vpn on mac unsuccessfully!
Need your help! create pptp vpn on mac unsuccessfully!
- Subject: Need your help! create pptp vpn on mac unsuccessfully!
- From: 张军 <email@hidden>
- Date: Tue, 21 May 2013 19:41:18 +0800
I want to create vpn / pptp programmatically. but the function SCNetworkConnectionStart() always return false.who can help me? what's wrong with my code.thank you very much!
-(IBAction)beginConnectVpn:(id)sender{
CFStringRef username = CFSTR("MyVpnAccount");
CFStringRef password = CFSTR("MyVpnPassword");
CFStringRef address = CFSTR("MyVpnAddress");
SCNetworkInterfaceRef pptpRef = SCNetworkInterfaceCreateWithInterface(kSCNetworkInterfaceIPv4,kSCNetworkInterfaceTypePPTP);
SCNetworkInterfaceRef pppRef = SCNetworkInterfaceCreateWithInterface(pptpRef, kSCNetworkInterfaceTypePPP);
CFStringRef appName = CFSTR("com.helpnow.newmt");
SCPreferencesRef prefs = SCPreferencesCreate(NULL, appName, NULL);
SCNetworkServiceRef vpnService = SCNetworkServiceCreate(prefs, pppRef);
//4.开始设置
SCNetworkInterfaceRef iface = SCNetworkServiceGetInterface(vpnService);
CFStringRef keysPPP[4] = { NULL, NULL, NULL, NULL };
CFStringRef valsPPP[4] = { NULL, NULL, NULL, NULL };
CFIndex numkeys = 0;
OSStatus err = 0;
CFStringRef kVendorName = appName;
CFStringRef kAppName = appName;
CFDictionaryRef serviceOptions;
SCNetworkConnectionRef connection;
CFStringRef serviceToDial;
struct CallbackParams params;
/***********************/
keysPPP[numkeys] = kSCPropNetPPPAuthName;
valsPPP[numkeys++] = username;
keysPPP[numkeys] = kSCPropNetPPPAuthPassword;
valsPPP[numkeys++] = password;
keysPPP[numkeys] = kSCPropNetPPPCommRemoteAddress;
valsPPP[numkeys++] = address;
keysPPP[numkeys] = kSCPropUserDefinedName;
valsPPP[numkeys++] = kAppName;
//ppp options
CFDictionaryRef pppOptionsForDial = CFDictionaryCreate (NULL,
(const void **)&keysPPP,
(const void **)&valsPPP,
numkeys,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFStringRef keysInt[] = { kSCPropNetInterfaceType, kSCPropNetInterfaceSubType };
CFStringRef valsInt[] = { kSCValNetInterfaceTypePPP, kSCValNetInterfaceSubTypePPTP };
CFDictionaryRef interfaceOptionsForDial = CFDictionaryCreate (NULL,
(const void **)&keysInt,
(const void **)&valsInt,
2,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
/***********************/
// IPv4 options
CFStringRef keysIPv4[] = { kSCPropNetIPv4ConfigMethod, kSCPropNetOverridePrimary };
CFStringRef valsIPv4[] = { kSCValNetIPv4ConfigMethodPPP, CFSTR("1") };
CFDictionaryRef ipv4OptionsForDial = CFDictionaryCreate (NULL,
(const void **)&keysIPv4,
(const void **)&valsIPv4,
2,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
/***********************/
// IPv6 options
CFStringRef keysIPv6[] = { kSCPropNetIPv6ConfigMethod };
CFStringRef valsIPv6[] = { kSCValNetIPv6ConfigMethodAutomatic };
CFDictionaryRef ipv6OptionsForDial = CFDictionaryCreate (NULL,
(const void **)&keysIPv6,
(const void **)&valsIPv6,
1,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFStringRef keys[] = { kSCEntNetInterface, kSCEntNetIPv4, kSCEntNetIPv6, kSCEntNetPPP };
CFDictionaryRef vals[] = { interfaceOptionsForDial, ipv4OptionsForDial, ipv6OptionsForDial, pppOptionsForDial };
CFDictionaryRef AlloptionsForDial = CFDictionaryCreate (NULL,
(const void **) &keys,
(const void **) &vals,
4,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
SCNetworkInterfaceSetConfiguration(iface,AlloptionsForDial);
SCNetworkSetRef set;
if ( SCNetworkServiceEstablishDefaultConfiguration(vpnService) ) {
NSLog(@"estableish_success");
set = SCNetworkSetCopyCurrent(prefs);
if (set && SCNetworkSetAddService(set, vpnService))
{
NSLog(@"set_inside___");
SCPreferencesCommitChanges(prefs);
SCPreferencesApplyChanges(prefs);
SCPreferencesSynchronize(prefs);
serviceToDial = SCNetworkServiceGetServiceID(vpnService);
NSLog(@"serviceToDial_%@",serviceToDial);
}
}else {
NSLog(@"estalbe_error");
}
if (err == 0) {
SCNetworkConnectionContext context;
// Set up the parameters to our callback function.
params.magic = kCallbackParamsMagic;
params.forcePrintStatus = true;
params.lastMinorStatus = kSCNetworkConnectionDisconnected;
params.lastMinorStatus = kSCNetworkConnectionPPPDisconnected;
// Set up the context to reference those parameters.
context.version = 0;
context.info = (void *) ¶ms;
context.retain = NULL;
context.release = NULL;
context.copyDescription = NULL;
connection = SCNetworkConnectionCreateWithServiceID(NULL,
serviceToDial,
MyNetworkConnectionCallBack,
&context);
}
BOOL constr = SCNetworkConnectionStart(connection, AlloptionsForDial, false);
if (constr) {
NSLog(@"yes......");
}else {
NSLog(@"no......");
//this always return flase;
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden