SCNetworkSignature.c [plain text]
#include <netinet/in.h>
#include <CoreFoundation/CFDictionary.h>
#include <CoreFoundation/CFString.h>
#include <CoreFoundation/CFArray.h>
#include <CoreFoundation/CFRuntime.h>
#include <SystemConfiguration/SCDynamicStore.h>
#include <SystemConfiguration/SCValidation.h>
#include <SystemConfiguration/SCPrivate.h>
#include "SCNetworkSignature.h"
#include "SCNetworkSignaturePrivate.h"
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1050
#error MAC_OS_X_VERSION_MIN_REQUIRED < 1050, Check MACOSX_DEPLOYMENT_TARGET
#endif
const char * kSCNetworkSignatureActiveChangedNotifyName = NETWORK_ID_KEY ".active";
#pragma mark SCNetworkSignature support routines
static __inline__ SCDynamicStoreRef
store_create(CFAllocatorRef alloc)
{
return (SCDynamicStoreCreate(alloc, CFSTR("SCNetworkSignature"),
NULL, NULL));
}
static CFDictionaryRef
store_copy_id_dict(CFAllocatorRef alloc, SCDynamicStoreRef store)
{
CFDictionaryRef id_dict = NULL;
Boolean release_store = FALSE;
if (store == NULL) {
store = store_create(alloc);
if (store == NULL) {
goto done;
}
release_store = TRUE;
}
id_dict = SCDynamicStoreCopyValue(store,
kSCNetworkIdentificationStoreKey);
if (isA_CFDictionary(id_dict) == NULL) {
if (id_dict != NULL) {
CFRelease(id_dict);
id_dict = NULL;
}
goto done;
}
done:
if (release_store) {
CFRelease(store);
}
return (id_dict);
}
#pragma -
#pragma mark SCNetworkSignature APIs
CFStringRef
SCNetworkSignatureCopyActiveIdentifierForAddress(CFAllocatorRef alloc,
const struct sockaddr * addr)
{
CFDictionaryRef id_dict = NULL;
CFStringRef ident = NULL;
struct sockaddr_in * sin_p;
sin_p = (struct sockaddr_in *)addr;
if (addr == NULL
|| addr->sa_family != AF_INET
|| addr->sa_len != sizeof(struct sockaddr_in)
|| sin_p->sin_addr.s_addr != 0) {
_SCErrorSet(kSCStatusInvalidArgument);
goto done;
}
id_dict = store_copy_id_dict(alloc, NULL);
if (id_dict == NULL) {
_SCErrorSet(kSCStatusFailed);
goto done;
}
ident = CFDictionaryGetValue(id_dict, kStoreKeyPrimaryIPv4Identifier);
if (isA_CFString(ident) != NULL) {
CFRetain(ident);
}
else {
_SCErrorSet(kSCStatusFailed);
}
done:
if (id_dict != NULL) {
CFRelease(id_dict);
}
return (ident);
}
CFArrayRef
SCNetworkSignatureCopyActiveIdentifiers(CFAllocatorRef alloc)
{
CFArrayRef active = NULL;
int i;
int count = 0;
CFDictionaryRef id_dict = NULL;
id_dict = store_copy_id_dict(alloc, NULL);
if (id_dict == NULL) {
goto done;
}
active = CFDictionaryGetValue(id_dict, kStoreKeyActiveIdentifiers);
if (isA_CFArray(active) != NULL) {
count = CFArrayGetCount(active);
}
if (count == 0) {
active = NULL;
goto done;
}
for (i = 0; i < count; i++) {
CFStringRef ident = CFArrayGetValueAtIndex(active, i);
if (isA_CFString(ident) == NULL) {
active = NULL;
goto done;
}
}
CFRetain(active);
done:
if (id_dict != NULL) {
CFRelease(id_dict);
}
if (active == NULL) {
_SCErrorSet(kSCStatusFailed);
}
return (active);
}
#pragma mark -