2 * Copyright (c) 2004, 2005, 2009-2011, 2013-2015, 2017 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 /* -------------------- */
46 CFMutableArrayRef sorted
;
48 sets
= SCNetworkSetCopyAll(prefs
);
50 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
54 sorted
= CFArrayCreateMutableCopy(NULL
, 0, sets
);
55 CFArraySortValues(sorted
,
56 CFRangeMake(0, CFArrayGetCount(sorted
)),
61 sets
= CFArrayCreateCopy(NULL
, sorted
);
67 static SCNetworkSetRef
68 _find_set(char *match
)
70 Boolean allowIndex
= TRUE
;
73 CFStringRef select_name
= NULL
;
74 SCNetworkSetRef selected
= NULL
;
78 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
90 // try to select the set by its setID
92 select_name
= CFStringCreateWithCString(NULL
, match
, kCFStringEncodingUTF8
);
94 n
= CFArrayGetCount(sets
);
95 for (i
= 0; i
< n
; i
++) {
99 set
= CFArrayGetValueAtIndex(sets
, i
);
100 setID
= SCNetworkSetGetSetID(set
);
101 if (CFEqual(select_name
, setID
)) {
108 // try to select the set by its name
110 for (i
= 0; i
< n
; i
++) {
114 set
= CFArrayGetValueAtIndex(sets
, i
);
115 setName
= SCNetworkSetGetName(set
);
116 if ((setName
!= NULL
) &&
117 CFEqual(select_name
, setName
)) {
118 if (selected
== NULL
) {
121 // if multiple sets match
123 SCPrint(TRUE
, stdout
, CFSTR("multiple sets match\n"));
129 if (selected
!= NULL
) {
133 // try to select the set by its name (case insensitive)
135 for (i
= 0; i
< n
; i
++) {
139 set
= CFArrayGetValueAtIndex(sets
, i
);
140 setName
= SCNetworkSetGetName(set
);
141 if ((setName
!= NULL
) &&
142 CFStringCompare(select_name
,
144 kCFCompareCaseInsensitive
) == kCFCompareEqualTo
) {
145 if (selected
== NULL
) {
148 // if multiple sets match
150 SCPrint(TRUE
, stdout
, CFSTR("multiple sets match\n"));
156 if (selected
!= NULL
) {
160 // try to select the set by its index
168 val
= strtol(str
, &end
, 10);
169 if ((*str
!= '\0') && (*end
== '\0') && (errno
== 0)) {
170 if ((val
> 0) && (val
<= n
)) {
171 selected
= CFArrayGetValueAtIndex(sets
, val
- 1);
176 if (selected
!= NULL
) {
180 SCPrint(TRUE
, stdout
, CFSTR("no match, which set?\n"));
184 if (select_name
!= NULL
) CFRelease(select_name
);
189 /* -------------------- */
194 create_set(int argc
, char **argv
)
200 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
204 set
= SCNetworkSetCreate(prefs
);
206 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
210 if ((argc
> 0) && (strlen(argv
[0]) > 0)) {
213 setName
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
217 ok
= SCNetworkSetSetName(set
, setName
);
220 SCPrint(TRUE
, stdout
, CFSTR("set not created: %s\n"), SCErrorString(SCError()));
221 (void) SCNetworkSetRemove(set
);
227 _prefs_changed
= TRUE
;
229 if (net_set
!= NULL
) CFRelease(net_set
);
232 setName
= SCNetworkSetGetName(set
);
233 if (setName
!= NULL
) {
234 SCPrint(TRUE
, stdout
,
235 CFSTR("set \"%@\" (%@) created and selected\n"),
237 SCNetworkSetGetSetID(set
));
239 SCPrint(TRUE
, stdout
,
240 CFSTR("set ID \"%@\" created and selected\n"),
241 SCNetworkSetGetSetID(set
));
244 if (net_service
!= NULL
) {
245 CFRelease(net_service
);
247 SCPrint(TRUE
, stdout
, CFSTR("& no service selected\n"));
250 if (net_protocol
!= NULL
) {
251 CFRelease(net_protocol
);
253 SCPrint(TRUE
, stdout
, CFSTR("& no protocol selected\n"));
256 if (net_interface
!= NULL
) {
257 CFRelease(net_interface
);
258 net_interface
= NULL
;
259 SCPrint(TRUE
, stdout
, CFSTR("& no interface selected\n"));
273 remove_set(int argc
, char **argv
)
275 SCNetworkSetRef set
= NULL
;
279 set
= _find_set(argv
[0]);
281 if (net_set
!= NULL
) {
292 if (!SCNetworkSetRemove(set
)) {
293 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
297 _prefs_changed
= TRUE
;
299 setName
= SCNetworkSetGetName(set
);
300 if (setName
!= NULL
) {
301 SCPrint(TRUE
, stdout
, CFSTR("set \"%@\" removed\n"), setName
);
303 SCPrint(TRUE
, stdout
,
304 CFSTR("set ID \"%@\" removed\n"),
305 SCNetworkSetGetSetID(set
));
308 if (CFEqual(set
, net_set
)) {
309 if (net_service
!= NULL
) {
310 CFRelease(net_service
);
312 SCPrint(TRUE
, stdout
, CFSTR("& no service selected\n"));
315 if (net_protocol
!= NULL
) {
316 CFRelease(net_protocol
);
318 SCPrint(TRUE
, stdout
, CFSTR("& no protocol selected\n"));
321 if (net_interface
!= NULL
) {
322 CFRelease(net_interface
);
323 net_interface
= NULL
;
324 SCPrint(TRUE
, stdout
, CFSTR("& no interface selected\n"));
342 select_set(int argc
, char **argv
)
348 set
= _find_set(argv
[0]);
354 if (net_set
!= NULL
) CFRelease(net_set
);
355 net_set
= CFRetain(set
);
357 setName
= SCNetworkSetGetName(set
);
358 if (setName
!= NULL
) {
359 SCPrint(TRUE
, stdout
, CFSTR("set \"%@\" selected\n"), setName
);
361 SCPrint(TRUE
, stdout
,
362 CFSTR("set ID \"%@\" selected\n"),
363 SCNetworkSetGetSetID(set
));
366 if (net_service
!= NULL
) {
367 CFRelease(net_service
);
369 SCPrint(TRUE
, stdout
, CFSTR("& no service selected\n"));
372 if (net_protocol
!= NULL
) {
373 CFRelease(net_protocol
);
375 SCPrint(TRUE
, stdout
, CFSTR("& no protocol selected\n"));
378 if (net_interface
!= NULL
) {
379 CFRelease(net_interface
);
380 net_interface
= NULL
;
381 SCPrint(TRUE
, stdout
, CFSTR("& no interface selected\n"));
390 set_set(int argc
, char **argv
)
394 if (net_set
== NULL
) {
395 SCPrint(TRUE
, stdout
, CFSTR("set not selected\n"));
400 SCPrint(TRUE
, stdout
, CFSTR("set what?\n"));
411 if (strcmp(command
, "name") == 0) {
415 SCPrint(TRUE
, stdout
, CFSTR("name not specified\n"));
419 setName
= (strlen(argv
[0]) > 0)
420 ? CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
)
425 ok
= SCNetworkSetSetName(net_set
, setName
);
426 if (setName
!= NULL
) CFRelease(setName
);
428 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
432 _prefs_changed
= TRUE
;
436 * since the (displayed) ordering may have changed, refresh sets
440 setID
= _SC_cfstring_to_cstring(SCNetworkSetGetSetID(net_set
),
443 kCFStringEncodingUTF8
);
451 net_set
= _find_set(setID
);
452 if (net_set
!= NULL
) {
456 CFAllocatorDeallocate(NULL
, setID
);
458 } else if (strcmp(command
, "current") == 0) {
459 ok
= SCNetworkSetSetCurrent(net_set
);
461 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
465 _prefs_changed
= TRUE
;
466 } else if (strcmp(command
, "id") == 0) {
470 if ((argc
< 1) || (strlen(argv
[0]) == 0)) {
471 SCPrint(TRUE
, stdout
, CFSTR("set id not specified\n"));
476 setID
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
480 ok
= _SCNetworkSetSetSetID(net_set
, setID
);
483 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
487 _prefs_changed
= TRUE
;
491 * since the (displayed) ordering may have changed, refresh sets
499 net_set
= _find_set(newID
);
500 if (net_set
!= NULL
) {
505 SCPrint(TRUE
, stdout
, CFSTR("set what?\n"));
508 if (net_set
== NULL
) {
519 show_set(int argc
, char **argv
)
521 CFArrayRef interfaces
;
523 SCNetworkSetRef set
= NULL
;
527 set
= _find_set(argv
[0]);
529 if (net_set
!= NULL
) {
532 SCPrint(TRUE
, stdout
, CFSTR("set not selected\n"));
541 SCPrint(TRUE
, stdout
, CFSTR("set id = %@\n"), SCNetworkSetGetSetID(set
));
543 setName
= SCNetworkSetGetName(set
);
544 SCPrint(TRUE
, stdout
, CFSTR("name = %@\n"),
545 (setName
!= NULL
) ? setName
: CFSTR(""));
547 services
= SCNetworkSetCopyServices(set
);
548 if (services
!= NULL
) {
554 order
= SCNetworkSetGetServiceOrder(set
);
556 nOrder
= CFArrayGetCount(order
);
559 n
= CFArrayGetCount(services
);
561 CFMutableArrayRef sorted
;
563 sorted
= CFArrayCreateMutableCopy(NULL
, 0, services
);
564 CFArraySortValues(sorted
,
565 CFRangeMake(0, CFArrayGetCount(sorted
)),
566 _SCNetworkServiceCompare
,
572 SCPrint(TRUE
, stdout
, CFSTR("services =\n"));
574 for (i
= 0; i
< n
; i
++) {
575 CFIndex orderIndex
= kCFNotFound
;
576 SCNetworkServiceRef service
;
577 CFStringRef serviceName
;
578 CFStringRef serviceID
;
580 service
= CFArrayGetValueAtIndex(services
, i
);
581 serviceID
= SCNetworkServiceGetServiceID(service
);
582 serviceName
= SCNetworkServiceGetName(service
);
583 if (serviceName
== NULL
) serviceName
= CFSTR("");
586 orderIndex
= CFArrayGetFirstIndexOfValue(order
,
587 CFRangeMake(0, nOrder
),
590 if (orderIndex
!= kCFNotFound
) {
591 SCPrint(TRUE
, stdout
, CFSTR("%c%2ld: %@%-*s (%@)\n"),
592 ((net_service
!= NULL
) && CFEqual(service
, net_service
)) ? '>' : ' ',
595 (int)(30 - CFStringGetLength(serviceName
)),
599 SCPrint(TRUE
, stdout
, CFSTR("%c : %@%-*s (%@)\n"),
600 ((net_service
!= NULL
) && CFEqual(service
, net_service
)) ? '>' : ' ',
602 (int)(30 - CFStringGetLength(serviceName
)),
611 interfaces
= SCNetworkSetCopyAvailableInterfaces(set
);
612 if (interfaces
!= NULL
) {
614 SCNetworkInterfaceRef interface
;
617 SCPrint(TRUE
, stdout
, CFSTR("available interfaces =\n"));
619 n
= CFArrayGetCount(interfaces
);
620 for (i
= 0; i
< n
; i
++) {
621 interface
= CFArrayGetValueAtIndex(interfaces
, i
);
622 SCPrint(TRUE
, stdout
, CFSTR(" %2ld: %@\n"),
624 SCNetworkInterfaceGetLocalizedDisplayName(interface
));
627 CFRelease(interfaces
);
631 SCPrint(TRUE
, stdout
, CFSTR("\n%@\n"), set
);
640 show_sets(int argc
, char **argv
)
644 SCNetworkSetRef current
;
649 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
660 current
= SCNetworkSetCopyCurrent(prefs
);
662 n
= CFArrayGetCount(sets
);
663 for (i
= 0; i
< n
; i
++) {
668 set
= CFArrayGetValueAtIndex(sets
, i
);
669 setID
= SCNetworkSetGetSetID(set
);
670 setName
= SCNetworkSetGetName(set
);
671 if (setName
== NULL
) setName
= CFSTR("");
673 SCPrint(TRUE
, stdout
, CFSTR(" %c%c%2ld: %@%-*s (%@)\n"),
674 ((current
!= NULL
) && CFEqual(set
, current
)) ? '*' : ' ',
675 ((net_set
!= NULL
) && CFEqual(set
, net_set
)) ? '>' : ' ',
678 (int)(30 - CFStringGetLength(setName
)),
683 if (current
!= NULL
) CFRelease(current
);