2 * Copyright (c) 2004, 2005, 2009-2011, 2013-2015 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * August 5, 2004 Allan Nathanson <ajn@apple.com>
35 #include "net_service.h"
39 /* -------------------- */
42 static CFComparisonResult
43 _compare_sets(const void *val1
, const void *val2
, void *context
)
49 SCNetworkSetRef s1
= (SCNetworkSetRef
)val1
;
50 SCNetworkSetRef s2
= (SCNetworkSetRef
)val2
;
52 name1
= SCNetworkSetGetName(s1
);
53 name2
= SCNetworkSetGetName(s2
);
57 return CFStringCompare(name1
, name2
, 0);
59 return kCFCompareLessThan
;
64 return kCFCompareGreaterThan
;
67 id1
= SCNetworkSetGetSetID(s1
);
68 id2
= SCNetworkSetGetSetID(s2
);
69 return CFStringCompare(id1
, id2
, 0);
77 CFMutableArrayRef sorted
;
79 sets
= SCNetworkSetCopyAll(prefs
);
81 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
85 sorted
= CFArrayCreateMutableCopy(NULL
, 0, sets
);
86 CFArraySortValues(sorted
,
87 CFRangeMake(0, CFArrayGetCount(sorted
)),
92 sets
= CFArrayCreateCopy(NULL
, sorted
);
98 static SCNetworkSetRef
99 _find_set(char *match
)
101 Boolean allowIndex
= TRUE
;
104 CFStringRef select_name
= NULL
;
105 SCNetworkSetRef selected
= NULL
;
109 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
121 // try to select the set by its setID
123 select_name
= CFStringCreateWithCString(NULL
, match
, kCFStringEncodingUTF8
);
125 n
= CFArrayGetCount(sets
);
126 for (i
= 0; i
< n
; i
++) {
130 set
= CFArrayGetValueAtIndex(sets
, i
);
131 setID
= SCNetworkSetGetSetID(set
);
132 if (CFEqual(select_name
, setID
)) {
139 // try to select the set by its name
141 for (i
= 0; i
< n
; i
++) {
145 set
= CFArrayGetValueAtIndex(sets
, i
);
146 setName
= SCNetworkSetGetName(set
);
147 if ((setName
!= NULL
) &&
148 CFEqual(select_name
, setName
)) {
149 if (selected
== NULL
) {
152 // if multiple sets match
154 SCPrint(TRUE
, stdout
, CFSTR("multiple sets match\n"));
160 if (selected
!= NULL
) {
164 // try to select the set by its name (case insensitive)
166 for (i
= 0; i
< n
; i
++) {
170 set
= CFArrayGetValueAtIndex(sets
, i
);
171 setName
= SCNetworkSetGetName(set
);
172 if ((setName
!= NULL
) &&
173 CFStringCompare(select_name
,
175 kCFCompareCaseInsensitive
) == kCFCompareEqualTo
) {
176 if (selected
== NULL
) {
179 // if multiple sets match
181 SCPrint(TRUE
, stdout
, CFSTR("multiple sets match\n"));
187 if (selected
!= NULL
) {
191 // try to select the set by its index
199 val
= strtol(str
, &end
, 10);
200 if ((*str
!= '\0') && (*end
== '\0') && (errno
== 0)) {
201 if ((val
> 0) && (val
<= n
)) {
202 selected
= CFArrayGetValueAtIndex(sets
, val
- 1);
207 if (selected
!= NULL
) {
211 SCPrint(TRUE
, stdout
, CFSTR("no match, which set?\n"));
215 if (select_name
!= NULL
) CFRelease(select_name
);
220 /* -------------------- */
225 create_set(int argc
, char **argv
)
231 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
235 set
= SCNetworkSetCreate(prefs
);
237 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
241 if ((argc
> 0) && (strlen(argv
[0]) > 0)) {
244 setName
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
248 ok
= SCNetworkSetSetName(set
, setName
);
251 SCPrint(TRUE
, stdout
, CFSTR("set not created: %s\n"), SCErrorString(SCError()));
252 (void) SCNetworkSetRemove(set
);
258 _prefs_changed
= TRUE
;
260 if (net_set
!= NULL
) CFRelease(net_set
);
263 setName
= SCNetworkSetGetName(set
);
264 if (setName
!= NULL
) {
265 SCPrint(TRUE
, stdout
,
266 CFSTR("set \"%@\" (%@) created and selected\n"),
268 SCNetworkSetGetSetID(set
));
270 SCPrint(TRUE
, stdout
,
271 CFSTR("set ID \"%@\" created and selected\n"),
272 SCNetworkSetGetSetID(set
));
275 if (net_service
!= NULL
) {
276 CFRelease(net_service
);
278 SCPrint(TRUE
, stdout
, CFSTR("& no service selected\n"));
281 if (net_protocol
!= NULL
) {
282 CFRelease(net_protocol
);
284 SCPrint(TRUE
, stdout
, CFSTR("& no protocol selected\n"));
287 if (net_interface
!= NULL
) {
288 CFRelease(net_interface
);
289 net_interface
= NULL
;
290 SCPrint(TRUE
, stdout
, CFSTR("& no interface selected\n"));
304 remove_set(int argc
, char **argv
)
306 SCNetworkSetRef set
= NULL
;
310 set
= _find_set(argv
[0]);
312 if (net_set
!= NULL
) {
323 if (!SCNetworkSetRemove(set
)) {
324 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
328 _prefs_changed
= TRUE
;
330 setName
= SCNetworkSetGetName(set
);
331 if (setName
!= NULL
) {
332 SCPrint(TRUE
, stdout
, CFSTR("set \"%@\" removed\n"), setName
);
334 SCPrint(TRUE
, stdout
,
335 CFSTR("set ID \"%@\" removed\n"),
336 SCNetworkSetGetSetID(set
));
339 if (CFEqual(set
, net_set
)) {
340 if (net_service
!= NULL
) {
341 CFRelease(net_service
);
343 SCPrint(TRUE
, stdout
, CFSTR("& no service selected\n"));
346 if (net_protocol
!= NULL
) {
347 CFRelease(net_protocol
);
349 SCPrint(TRUE
, stdout
, CFSTR("& no protocol selected\n"));
352 if (net_interface
!= NULL
) {
353 CFRelease(net_interface
);
354 net_interface
= NULL
;
355 SCPrint(TRUE
, stdout
, CFSTR("& no interface selected\n"));
373 select_set(int argc
, char **argv
)
378 set
= _find_set(argv
[0]);
384 if (net_set
!= NULL
) CFRelease(net_set
);
385 net_set
= CFRetain(set
);
387 setName
= SCNetworkSetGetName(set
);
388 if (setName
!= NULL
) {
389 SCPrint(TRUE
, stdout
, CFSTR("set \"%@\" selected\n"), setName
);
391 SCPrint(TRUE
, stdout
,
392 CFSTR("set ID \"%@\" selected\n"),
393 SCNetworkSetGetSetID(set
));
396 if (net_service
!= NULL
) {
397 CFRelease(net_service
);
399 SCPrint(TRUE
, stdout
, CFSTR("& no service selected\n"));
402 if (net_protocol
!= NULL
) {
403 CFRelease(net_protocol
);
405 SCPrint(TRUE
, stdout
, CFSTR("& no protocol selected\n"));
408 if (net_interface
!= NULL
) {
409 CFRelease(net_interface
);
410 net_interface
= NULL
;
411 SCPrint(TRUE
, stdout
, CFSTR("& no interface selected\n"));
420 set_set(int argc
, char **argv
)
424 if (net_set
== NULL
) {
425 SCPrint(TRUE
, stdout
, CFSTR("set not selected\n"));
430 SCPrint(TRUE
, stdout
, CFSTR("set what?\n"));
441 if (strcmp(command
, "name") == 0) {
445 SCPrint(TRUE
, stdout
, CFSTR("name not specified\n"));
449 setName
= (strlen(argv
[0]) > 0)
450 ? CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
)
455 ok
= SCNetworkSetSetName(net_set
, setName
);
456 if (setName
!= NULL
) CFRelease(setName
);
458 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
462 _prefs_changed
= TRUE
;
466 * since the (displayed) ordering may have changed, refresh sets
470 setID
= _SC_cfstring_to_cstring(SCNetworkSetGetSetID(net_set
),
473 kCFStringEncodingUTF8
);
481 net_set
= _find_set(setID
);
482 if (net_set
!= NULL
) {
486 CFAllocatorDeallocate(NULL
, setID
);
488 } else if (strcmp(command
, "current") == 0) {
489 ok
= SCNetworkSetSetCurrent(net_set
);
491 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
495 _prefs_changed
= TRUE
;
496 } else if (strcmp(command
, "id") == 0) {
500 if ((argc
< 1) || (strlen(argv
[0]) == 0)) {
501 SCPrint(TRUE
, stdout
, CFSTR("set id not specified\n"));
506 setID
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
510 ok
= _SCNetworkSetSetSetID(net_set
, setID
);
513 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
517 _prefs_changed
= TRUE
;
521 * since the (displayed) ordering may have changed, refresh sets
529 net_set
= _find_set(newID
);
530 if (net_set
!= NULL
) {
535 SCPrint(TRUE
, stdout
, CFSTR("set what?\n"));
538 if (net_set
== NULL
) {
549 show_set(int argc
, char **argv
)
551 CFArrayRef interfaces
;
553 SCNetworkSetRef set
= NULL
;
557 set
= _find_set(argv
[0]);
559 if (net_set
!= NULL
) {
562 SCPrint(TRUE
, stdout
, CFSTR("set not selected\n"));
571 SCPrint(TRUE
, stdout
, CFSTR("set id = %@\n"), SCNetworkSetGetSetID(set
));
573 setName
= SCNetworkSetGetName(set
);
574 SCPrint(TRUE
, stdout
, CFSTR("name = %@\n"),
575 (setName
!= NULL
) ? setName
: CFSTR(""));
577 services
= SCNetworkSetCopyServices(set
);
578 if (services
!= NULL
) {
584 order
= SCNetworkSetGetServiceOrder(set
);
586 nOrder
= CFArrayGetCount(order
);
589 n
= CFArrayGetCount(services
);
591 CFMutableArrayRef sorted
;
593 sorted
= CFArrayCreateMutableCopy(NULL
, 0, services
);
594 CFArraySortValues(sorted
,
595 CFRangeMake(0, CFArrayGetCount(sorted
)),
596 _SCNetworkServiceCompare
,
602 SCPrint(TRUE
, stdout
, CFSTR("services =\n"));
604 for (i
= 0; i
< n
; i
++) {
605 CFIndex orderIndex
= kCFNotFound
;
606 SCNetworkServiceRef service
;
607 CFStringRef serviceName
;
608 CFStringRef serviceID
;
610 service
= CFArrayGetValueAtIndex(services
, i
);
611 serviceID
= SCNetworkServiceGetServiceID(service
);
612 serviceName
= SCNetworkServiceGetName(service
);
613 if (serviceName
== NULL
) serviceName
= CFSTR("");
616 orderIndex
= CFArrayGetFirstIndexOfValue(order
,
617 CFRangeMake(0, nOrder
),
620 if (orderIndex
!= kCFNotFound
) {
621 SCPrint(TRUE
, stdout
, CFSTR("%c%2ld: %@%-*s (%@)\n"),
622 ((net_service
!= NULL
) && CFEqual(service
, net_service
)) ? '>' : ' ',
625 (int)(30 - CFStringGetLength(serviceName
)),
629 SCPrint(TRUE
, stdout
, CFSTR("%c : %@%-*s (%@)\n"),
630 ((net_service
!= NULL
) && CFEqual(service
, net_service
)) ? '>' : ' ',
632 (int)(30 - CFStringGetLength(serviceName
)),
641 interfaces
= SCNetworkSetCopyAvailableInterfaces(set
);
642 if (interfaces
!= NULL
) {
644 SCNetworkInterfaceRef interface
;
647 SCPrint(TRUE
, stdout
, CFSTR("available interfaces =\n"));
649 n
= CFArrayGetCount(interfaces
);
650 for (i
= 0; i
< n
; i
++) {
651 interface
= CFArrayGetValueAtIndex(interfaces
, i
);
652 SCPrint(TRUE
, stdout
, CFSTR(" %2ld: %@ \n"),
654 SCNetworkInterfaceGetLocalizedDisplayName(interface
));
657 CFRelease(interfaces
);
661 SCPrint(TRUE
, stdout
, CFSTR("\n%@\n"), set
);
670 show_sets(int argc
, char **argv
)
672 SCNetworkSetRef current
;
677 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
688 current
= SCNetworkSetCopyCurrent(prefs
);
690 n
= CFArrayGetCount(sets
);
691 for (i
= 0; i
< n
; i
++) {
696 set
= CFArrayGetValueAtIndex(sets
, i
);
697 setID
= SCNetworkSetGetSetID(set
);
698 setName
= SCNetworkSetGetName(set
);
699 if (setName
== NULL
) setName
= CFSTR("");
701 SCPrint(TRUE
, stdout
, CFSTR(" %c%c%2ld: %@%-*s (%@)\n"),
702 ((current
!= NULL
) && CFEqual(set
, current
)) ? '*' : ' ',
703 ((net_set
!= NULL
) && CFEqual(set
, net_set
)) ? '>' : ' ',
706 (int)(30 - CFStringGetLength(setName
)),
711 if (current
!= NULL
) CFRelease(current
);