#include <stdlib.h>
#include <CoreFoundation/CoreFoundation.h>
#include <SystemConfiguration/SCPrivate.h>
#include "IPMonitorControl.h"
#include "IPMonitorControlServer.h"
#include "symbol_scope.h"
STATIC void
AssertionsChanged(void * info)
{
CFDictionaryRef assertions = NULL;
CFArrayRef changes;
changes = IPMonitorControlServerCopyInterfaceRankInformation(&assertions);
SCPrint(TRUE, stdout, CFSTR("Changed interfaces %@\n"), changes);
if (assertions == NULL) {
SCPrint(TRUE, stdout, CFSTR("No assertions\n"));
}
else {
SCPrint(TRUE, stdout, CFSTR("Assertions = %@\n"), assertions);
CFRelease(assertions);
}
if (changes != NULL) {
CFRelease(changes);
}
return;
}
int
main(int argc, char * argv[])
{
if (argc >= 2) {
int ch;
IPMonitorControlRef control;
SCNetworkServicePrimaryRank rank;
Boolean rank_set = FALSE;
Boolean wait = FALSE;
rank = kSCNetworkServicePrimaryRankDefault;
control = IPMonitorControlCreate();
if (control == NULL) {
fprintf(stderr, "failed to allocate IPMonitorControl\n");
exit(1);
}
while ((ch = getopt(argc, argv, "i:r:w")) != EOF) {
CFStringRef ifname;
SCNetworkServicePrimaryRank existing_rank;
switch ((char)ch) {
case 'i':
ifname = CFStringCreateWithCString(NULL, optarg,
kCFStringEncodingUTF8);
existing_rank = IPMonitorControlGetInterfacePrimaryRank(control,
ifname);
printf("%s rank was %u\n", optarg, existing_rank);
if (IPMonitorControlSetInterfacePrimaryRank(control,
ifname,
rank)) {
printf("%s rank set to %u\n", optarg, rank);
rank_set = TRUE;
}
else {
fprintf(stderr, "failed to set rank\n");
}
CFRelease(ifname);
break;
case 'r':
rank = strtoul(optarg, NULL, 0);
break;
case 'w':
wait = TRUE;
break;
default:
fprintf(stderr, "unexpected option '%c'\n", (char)ch);
exit(1);
break;
}
}
argc -= optind;
argv += optind;
if (argc > 0) {
fprintf(stderr, "ignoring additional parameters\n");
}
if (!rank_set) {
exit(1);
}
if (wait) {
CFRunLoopRun();
}
}
else {
CFRunLoopSourceContext context;
CFRunLoopSourceRef rls;
STATIC Boolean verbose = TRUE;
memset(&context, 0, sizeof(context));
context.info = (void *)NULL;
context.perform = AssertionsChanged;
rls = CFRunLoopSourceCreate(NULL, 0, &context);
CFRunLoopAddSource(CFRunLoopGetCurrent(), rls,
kCFRunLoopDefaultMode);
if (!IPMonitorControlServerStart(CFRunLoopGetCurrent(), rls, &verbose)) {
fprintf(stderr, "failed to create connection\n");
exit(1);
}
CFRunLoopRun();
}
exit(0);
return (0);
}