#import <Cocoa/Cocoa.h>

#include <CoreFoundation/CoreFoundation.h>
#include <SystemConfiguration/SystemConfiguration.h>

// function called when something changed in the System Configuration
void myCallBack(SCDynamicStoreRef store, CFArrayRef changedKeys, void *info) {
    NSLog(@"Changed Keys: %@",(NSArray *)changedKeys);
}


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    SCDynamicStoreRef dynRef=SCDynamicStoreCreate(kCFAllocatorSystemDefault, (CFStringRef)@"Whatever you want", (SCDynamicStoreCallBack)&myCallBack, NULL);
    // Get all available interfaces IPv4 addresses
    NSArray *keyList=(NSArray *)SCDynamicStoreCopyKeyList(dynRef,(CFStringRef)@".*");
    NSDictionary *objects=(NSDictionary *)SCDynamicStoreCopyValue(dynRef,(CFStringRef)@"State:/Network/Global/IPv4");
    // An other example for the same price: watch for modification in the network configuration
    CFRunLoopSourceRef runLoopSource=SCDynamicStoreCreateRunLoopSource(kCFAllocatorSystemDefault,dynRef,0);

    // Show the available interfaces
    NSLog(@"%@",keyList);
    NSLog(@"%@",objects);

    [objects release];
    [keyList release];
    
    // Listen to Network changes
    SCDynamicStoreSetNotificationKeys(dynRef,NULL,(CFArrayRef)[NSArray arrayWithObject:@"State:/Network/.*"]);
    // Add the runLoopSource to the NSRunLoop
    CFRunLoopAddSource([[NSRunLoop currentRunLoop] getCFRunLoop], runLoopSource, kCFRunLoopCommonModes);

    [[NSRunLoop currentRunLoop] run];
    
    [pool release];
    
    return 0;
}
