Exemple #1
0
FskErr FskNetInterfaceInitialize(void) {
	FskErr err;

	err = FskListMutexNew(&interfaceChangeCBList, "interfaceChangeCBLIst");
	if (err) return err;

#if TARGET_OS_WIN32
	installWindowsNetworkInterfaceChangeTracker();
#elif TARGET_OS_IPHONE
	installIOSNetworkInterfaceChangeTracker();
#elif TARGET_OS_MAC
	installMacNetworkInterfaceChangeTracker();
#endif /* TARGET_OS */

#if TARGET_OS_KPL
	KplNetInterfaceInitialize();
	KplNetInterfaceSetChangedCallback(KplNetInterfaceChanged, 0L);
#endif /* TARGET_OS_KPL */

	err = FskListMutexNew(&gNetworkInterfaceList, "gNetworkInterfaceList");
	if (err) return err;
	FskMutexAcquire(gNetworkInterfaceList->mutex);
	sFskNetInterfaceEnumerate((FskNetInterfaceRecord**)(void*)&gNetworkInterfaceList->list);
	FskMutexRelease(gNetworkInterfaceList->mutex);

	return err;
}
void KprNetworkInterfaceSetup()
{
#if TARGET_OS_IPHONE
	struct sockaddr_in localWifiAddress;
	SCNetworkReachabilityContext context = {0, NULL, NULL, NULL, NULL};
	SCNetworkReachabilityFlags flags;
	bzero(&localWifiAddress, sizeof(localWifiAddress));
	localWifiAddress.sin_len = sizeof(localWifiAddress);
	localWifiAddress.sin_family = AF_INET;
	localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM); // IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0
	gNetworkInterfaceReachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)&localWifiAddress);
	SCNetworkReachabilitySetCallback(gNetworkInterfaceReachability, KprNetworkInterfaceReachabilityCallback, &context);
	SCNetworkReachabilityScheduleWithRunLoop(gNetworkInterfaceReachability, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
	SCNetworkReachabilityGetFlags(gNetworkInterfaceReachability, &flags);
	KprNetworkInterfaceReachabilityCallback(gNetworkInterfaceReachability, flags, NULL);
#elif TARGET_OS_MAC
	FskErr err = kFskErrNone;
    SCDynamicStoreContext context = {0, NULL, NULL, NULL, NULL};
    CFStringRef pattern = NULL;
    CFArrayRef patternList = NULL;
	pattern = SCDynamicStoreKeyCreateNetworkServiceEntity(NULL, kSCDynamicStoreDomainState, kSCCompAnyRegex, kSCEntNetIPv4);
    if (!pattern) { err = kFskErrOperationFailed; goto bail; }
    patternList = CFArrayCreate(NULL, (const void **)&pattern, 1, &kCFTypeArrayCallBacks);
    if (!patternList) { err = kFskErrOperationFailed; goto bail; }
    gNetworkInterfaceStore = SCDynamicStoreCreate(NULL, CFSTR("KprNetWorkInterface"), KprNetworkInterfaceStoreCallback, &context);
    if (!gNetworkInterfaceStore) { err = kFskErrOperationFailed; goto bail; }
	if (!SCDynamicStoreSetNotificationKeys(gNetworkInterfaceStore, NULL, patternList)) { err = kFskErrOperationFailed; goto bail; }
    gNetworkInterfaceSource = SCDynamicStoreCreateRunLoopSource(NULL, gNetworkInterfaceStore, 0);
    if (!gNetworkInterfaceSource) { err = kFskErrOperationFailed; goto bail; }
    CFRunLoopAddSource(CFRunLoopGetCurrent(), gNetworkInterfaceSource, kCFRunLoopCommonModes);
    KprNetworkInterfaceStoreCallback(gNetworkInterfaceStore, NULL, NULL);
bail:
    if (err != noErr) {
    	if (gNetworkInterfaceSource) {
        	CFRelease(gNetworkInterfaceSource);
        	gNetworkInterfaceSource = NULL;
    	}
    	if (gNetworkInterfaceStore) {
        	CFRelease(gNetworkInterfaceStore);
        	gNetworkInterfaceStore = NULL;
    	}
    }
	if (patternList)
		CFRelease(patternList);
	if (pattern) 
		CFRelease(pattern);
#elif TARGET_OS_WIN32
	NETIOAPI_API status = NotifyIpInterfaceChange(AF_INET,  &KprNetworkInterfaceCallback, NULL, TRUE, &gNetworkInterfaceHandle);
#elif TARGET_OS_KPL
	#ifdef KPR_CONFIG
	KplNetInterfaceInitialize();
	KplNetInterfaceSetChangedCallback(KprNetworkInterfaceCallback, 0L);
	KprNetworkInterfaceCallback(0);
	#endif
#endif
}