2 * Copyright (c) 2004, 2005, 2009 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
);
489 SCPrint(TRUE
, stdout
, CFSTR("set what?\n"));
499 show_set(int argc
, char **argv
)
502 SCNetworkSetRef set
= NULL
;
506 set
= _find_set(argv
[0]);
508 if (net_set
!= NULL
) {
511 SCPrint(TRUE
, stdout
, CFSTR("set not selected\n"));
520 SCPrint(TRUE
, stdout
, CFSTR("set id = %@\n"), SCNetworkSetGetSetID(set
));
522 setName
= SCNetworkSetGetName(set
);
523 SCPrint(TRUE
, stdout
, CFSTR("name = %@\n"),
524 (setName
!= NULL
) ? setName
: CFSTR(""));
526 services
= SCNetworkSetCopyServices(set
);
527 if (services
!= NULL
) {
533 order
= SCNetworkSetGetServiceOrder(set
);
535 nOrder
= CFArrayGetCount(order
);
538 n
= CFArrayGetCount(services
);
540 CFMutableArrayRef sorted
;
542 sorted
= CFArrayCreateMutableCopy(NULL
, 0, services
);
543 CFArraySortValues(sorted
,
544 CFRangeMake(0, CFArrayGetCount(sorted
)),
551 SCPrint(TRUE
, stdout
, CFSTR("services =\n"));
553 for (i
= 0; i
< n
; i
++) {
554 CFIndex orderIndex
= kCFNotFound
;
555 SCNetworkServiceRef service
;
556 CFStringRef serviceName
;
557 CFStringRef serviceID
;
559 service
= CFArrayGetValueAtIndex(services
, i
);
560 serviceID
= SCNetworkServiceGetServiceID(service
);
561 serviceName
= SCNetworkServiceGetName(service
);
562 if (serviceName
== NULL
) serviceName
= CFSTR("");
565 orderIndex
= CFArrayGetFirstIndexOfValue(order
,
566 CFRangeMake(0, nOrder
),
569 if (orderIndex
!= kCFNotFound
) {
570 SCPrint(TRUE
, stdout
, CFSTR("%c%2d: %@%-*s (%@)\n"),
571 ((net_service
!= NULL
) && CFEqual(service
, net_service
)) ? '>' : ' ',
574 30 - CFStringGetLength(serviceName
),
578 SCPrint(TRUE
, stdout
, CFSTR("%c : %@%-*s (%@)\n"),
579 ((net_service
!= NULL
) && CFEqual(service
, net_service
)) ? '>' : ' ',
581 30 - CFStringGetLength(serviceName
),
591 SCPrint(TRUE
, stdout
, CFSTR("\n%@\n"), set
);
600 show_sets(int argc
, char **argv
)
602 SCNetworkSetRef current
;
607 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
618 current
= SCNetworkSetCopyCurrent(prefs
);
620 n
= CFArrayGetCount(sets
);
621 for (i
= 0; i
< n
; i
++) {
626 set
= CFArrayGetValueAtIndex(sets
, i
);
627 setID
= SCNetworkSetGetSetID(set
);
628 setName
= SCNetworkSetGetName(set
);
629 if (setName
== NULL
) setName
= CFSTR("");
631 SCPrint(TRUE
, stdout
, CFSTR(" %c%c%2d: %@%-*s (%@)\n"),
632 ((current
!= NULL
) && CFEqual(set
, current
)) ? '*' : ' ',
633 ((net_set
!= NULL
) && CFEqual(set
, net_set
)) ? '>' : ' ',
636 30 - CFStringGetLength(setName
),
641 if (current
!= NULL
) CFRelease(current
);