]> git.saurik.com Git - apple/configd.git/blame - IPMonitorControl/main.c
configd-699.1.5.tar.gz
[apple/configd.git] / IPMonitorControl / main.c
CommitLineData
78403150
A
1/*
2 * Copyright (c) 2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24/*
25 * main.c
26 * - test harness to test IPMonitorControl client and server
27 */
28
29/*
30 * Modification History
31 *
32 * December 16, 2013 Dieter Siegmund (dieter@apple.com)
33 * - initial revision
34 */
35
36#include <stdlib.h>
37#include <CoreFoundation/CoreFoundation.h>
38#include <SystemConfiguration/SCPrivate.h>
39
40#include "IPMonitorControl.h"
41#include "IPMonitorControlServer.h"
42#include "symbol_scope.h"
43
44STATIC void
45AssertionsChanged(void * info)
46{
47 CFDictionaryRef assertions = NULL;
48 CFArrayRef changes;
49
50 changes = IPMonitorControlServerCopyInterfaceRankInformation(&assertions);
51 SCPrint(TRUE, stdout, CFSTR("Changed interfaces %@\n"), changes);
52 if (assertions == NULL) {
53 SCPrint(TRUE, stdout, CFSTR("No assertions\n"));
54 }
55 else {
56 SCPrint(TRUE, stdout, CFSTR("Assertions = %@\n"), assertions);
57 CFRelease(assertions);
58 }
59 if (changes != NULL) {
60 CFRelease(changes);
61 }
62 return;
63}
64
65int
66main(int argc, char * argv[])
67{
68 if (argc >= 2) {
69 int ch;
70 IPMonitorControlRef control;
71 SCNetworkServicePrimaryRank rank;
72 Boolean rank_set = FALSE;
73
74 rank = kSCNetworkServicePrimaryRankDefault;
75 control = IPMonitorControlCreate();
76 if (control == NULL) {
77 fprintf(stderr, "failed to allocate IPMonitorControl\n");
78 exit(1);
79 }
80
81 while ((ch = getopt(argc, argv, "i:r:")) != EOF) {
82 CFStringRef ifname;
83 SCNetworkServicePrimaryRank existing_rank;
84
85 switch ((char)ch) {
86 case 'i':
87 ifname = CFStringCreateWithCString(NULL, optarg,
88 kCFStringEncodingUTF8);
89 existing_rank = IPMonitorControlGetInterfacePrimaryRank(control,
90 ifname);
91 printf("%s rank was %u\n", optarg, existing_rank);
92 if (IPMonitorControlSetInterfacePrimaryRank(control,
93 ifname,
94 rank)) {
95 printf("%s rank set to %u\n", optarg, rank);
96 rank_set = TRUE;
97 }
98 else {
99 fprintf(stderr, "failed to set rank\n");
100 }
101 CFRelease(ifname);
102 break;
103 case 'r':
104 rank = strtoul(optarg, NULL, 0);
105 break;
106 default:
107 fprintf(stderr, "unexpected option '%c'\n", (char)ch);
108 exit(1);
109 break;
110 }
111 }
112 argc -= optind;
113 argv += optind;
114 if (argc > 0) {
115 fprintf(stderr, "ignoring additional parameters\n");
116 }
117 if (rank_set == FALSE) {
118 exit(1);
119 }
120 }
121 else {
122 CFRunLoopSourceContext context;
123 CFRunLoopSourceRef rls;
124 STATIC Boolean verbose = TRUE;
125
126 bzero(&context, sizeof(context));
127 context.info = (void *)NULL;
128 context.perform = AssertionsChanged;
129 rls = CFRunLoopSourceCreate(NULL, 0, &context);
130 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls,
131 kCFRunLoopDefaultMode);
132 if (IPMonitorControlServerStart(CFRunLoopGetCurrent(), rls,
133 &verbose) == FALSE) {
134 fprintf(stderr, "failed to create connection\n");
135 exit(1);
136 }
137 }
138 CFRunLoopRun();
139 exit(0);
140 return (0);
141}
142