2 * Copyright (c) 2004 Apple Computer, 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"
38 /* -------------------- */
41 static CFComparisonResult
42 _compare_sets(const void *val1
, const void *val2
, void *context
)
48 SCNetworkSetRef s1
= (SCNetworkSetRef
)val1
;
49 SCNetworkSetRef s2
= (SCNetworkSetRef
)val2
;
51 name1
= SCNetworkSetGetName(s1
);
52 name2
= SCNetworkSetGetName(s2
);
56 return CFStringCompare(name1
, name2
, 0);
58 return kCFCompareLessThan
;
63 return kCFCompareGreaterThan
;
66 id1
= SCNetworkSetGetSetID(s1
);
67 id2
= SCNetworkSetGetSetID(s2
);
68 return CFStringCompare(id1
, id2
, 0);
76 CFMutableArrayRef sorted
;
78 sets
= SCNetworkSetCopyAll(prefs
);
80 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
84 sorted
= CFArrayCreateMutableCopy(NULL
, 0, sets
);
85 CFArraySortValues(sorted
,
86 CFRangeMake(0, CFArrayGetCount(sorted
)),
91 sets
= CFArrayCreateCopy(NULL
, sorted
);
97 static SCNetworkSetRef
98 _find_set(char *match
)
100 Boolean allowIndex
= TRUE
;
103 CFStringRef select_name
= NULL
;
104 SCNetworkSetRef selected
= NULL
;
108 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
120 // try to select the set by its setID
122 select_name
= CFStringCreateWithCString(NULL
, match
, kCFStringEncodingUTF8
);
124 n
= CFArrayGetCount(sets
);
125 for (i
= 0; i
< n
; i
++) {
129 set
= CFArrayGetValueAtIndex(sets
, i
);
130 setID
= SCNetworkSetGetSetID(set
);
131 if (CFEqual(select_name
, setID
)) {
138 // try to select the set by its name
140 for (i
= 0; i
< n
; i
++) {
144 set
= CFArrayGetValueAtIndex(sets
, i
);
145 setName
= SCNetworkSetGetName(set
);
146 if ((setName
!= NULL
) &&
147 CFEqual(select_name
, setName
)) {
148 if (selected
== NULL
) {
151 // if multiple sets match
153 SCPrint(TRUE
, stdout
, CFSTR("multiple sets match\n"));
159 if (selected
!= NULL
) {
163 // try to select the set by its name (case insensitive)
165 for (i
= 0; i
< n
; i
++) {
169 set
= CFArrayGetValueAtIndex(sets
, i
);
170 setName
= SCNetworkSetGetName(set
);
171 if ((setName
!= NULL
) &&
172 CFStringCompare(select_name
,
174 kCFCompareCaseInsensitive
) == kCFCompareEqualTo
) {
175 if (selected
== NULL
) {
178 // if multiple sets match
180 SCPrint(TRUE
, stdout
, CFSTR("multiple sets match\n"));
186 if (selected
!= NULL
) {
190 // try to select the set by its index
198 val
= strtol(str
, &end
, 10);
199 if ((*str
!= '\0') && (*end
== '\0') && (errno
== 0)) {
200 if ((val
> 0) && (val
<= n
)) {
201 selected
= CFArrayGetValueAtIndex(sets
, val
- 1);
206 if (selected
!= NULL
) {
210 SCPrint(TRUE
, stdout
, CFSTR("no match, which set?\n"));
214 if (select_name
!= NULL
) CFRelease(select_name
);
219 /* -------------------- */
224 create_set(int argc
, char **argv
)
230 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
234 set
= SCNetworkSetCreate(prefs
);
236 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
240 if ((argc
> 0) && (strlen(argv
[0]) > 0)) {
243 setName
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
247 ok
= SCNetworkSetSetName(set
, setName
);
250 SCPrint(TRUE
, stdout
, CFSTR("set not created: %s\n"), SCErrorString(SCError()));
251 (void) SCNetworkSetRemove(set
);
259 if (net_set
!= NULL
) CFRelease(net_set
);
262 setName
= SCNetworkSetGetName(set
);
263 if (setName
!= NULL
) {
264 SCPrint(TRUE
, stdout
,
265 CFSTR("set \"%@\" (%@) created and selected\n"),
267 SCNetworkSetGetSetID(set
));
269 SCPrint(TRUE
, stdout
,
270 CFSTR("set ID \"%@\" created and selected\n"),
271 SCNetworkSetGetSetID(set
));
274 if (net_service
!= NULL
) {
275 CFRelease(net_service
);
277 SCPrint(TRUE
, stdout
, CFSTR("& no service selected\n"));
280 if (net_protocol
!= NULL
) {
281 CFRelease(net_protocol
);
283 SCPrint(TRUE
, stdout
, CFSTR("& no protocol selected\n"));
286 if (net_interface
!= NULL
) {
287 CFRelease(net_interface
);
288 net_interface
= NULL
;
289 SCPrint(TRUE
, stdout
, CFSTR("& no interface selected\n"));
303 remove_set(int argc
, char **argv
)
305 SCNetworkSetRef set
= NULL
;
309 set
= _find_set(argv
[0]);
311 if (net_set
!= NULL
) {
322 if (!SCNetworkSetRemove(set
)) {
323 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
329 setName
= SCNetworkSetGetName(set
);
330 if (setName
!= NULL
) {
331 SCPrint(TRUE
, stdout
, CFSTR("set \"%@\" removed\n"), setName
);
333 SCPrint(TRUE
, stdout
,
334 CFSTR("set ID \"%@\" removed\n"),
335 SCNetworkSetGetSetID(set
));
338 if (CFEqual(set
, net_set
)) {
339 if (net_service
!= NULL
) {
340 CFRelease(net_service
);
342 SCPrint(TRUE
, stdout
, CFSTR("& no service selected\n"));
345 if (net_protocol
!= NULL
) {
346 CFRelease(net_protocol
);
348 SCPrint(TRUE
, stdout
, CFSTR("& no protocol selected\n"));
351 if (net_interface
!= NULL
) {
352 CFRelease(net_interface
);
353 net_interface
= NULL
;
354 SCPrint(TRUE
, stdout
, CFSTR("& no interface selected\n"));
372 select_set(int argc
, char **argv
)
377 set
= _find_set(argv
[0]);
383 if (net_set
!= NULL
) CFRelease(net_set
);
384 net_set
= CFRetain(set
);
386 setName
= SCNetworkSetGetName(set
);
387 if (setName
!= NULL
) {
388 SCPrint(TRUE
, stdout
, CFSTR("set \"%@\" selected\n"), setName
);
390 SCPrint(TRUE
, stdout
,
391 CFSTR("set ID \"%@\" selected\n"),
392 SCNetworkSetGetSetID(set
));
395 if (net_service
!= NULL
) {
396 CFRelease(net_service
);
398 SCPrint(TRUE
, stdout
, CFSTR("& no service selected\n"));
401 if (net_protocol
!= NULL
) {
402 CFRelease(net_protocol
);
404 SCPrint(TRUE
, stdout
, CFSTR("& no protocol selected\n"));
407 if (net_interface
!= NULL
) {
408 CFRelease(net_interface
);
409 net_interface
= NULL
;
410 SCPrint(TRUE
, stdout
, CFSTR("& no interface selected\n"));
419 set_set(int argc
, char **argv
)
423 if (net_set
== NULL
) {
424 SCPrint(TRUE
, stdout
, CFSTR("set not selected\n"));
429 SCPrint(TRUE
, stdout
, CFSTR("set what?\n"));
440 if (strcmp(command
, "name") == 0) {
444 SCPrint(TRUE
, stdout
, CFSTR("name not specified\n"));
448 setName
= (strlen(argv
[0]) > 0)
449 ? CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
)
454 ok
= SCNetworkSetSetName(net_set
, setName
);
455 if (setName
!= NULL
) CFRelease(setName
);
457 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
465 * since the (displayed) ordering may have changed, refresh sets
469 setID
= _SC_cfstring_to_cstring(SCNetworkSetGetSetID(net_set
),
472 kCFStringEncodingUTF8
);
480 net_set
= _find_set(setID
);
481 if (net_set
!= NULL
) {
485 CFAllocatorDeallocate(NULL
, setID
);
488 SCPrint(TRUE
, stdout
, CFSTR("set what?\n"));
498 show_set(int argc
, char **argv
)
501 SCNetworkSetRef set
= NULL
;
505 set
= _find_set(argv
[0]);
507 if (net_set
!= NULL
) {
510 SCPrint(TRUE
, stdout
, CFSTR("set not selected\n"));
519 SCPrint(TRUE
, stdout
, CFSTR("set id = %@\n"), SCNetworkSetGetSetID(set
));
521 setName
= SCNetworkSetGetName(set
);
522 SCPrint(TRUE
, stdout
, CFSTR("name = %@\n"),
523 (setName
!= NULL
) ? setName
: CFSTR(""));
525 services
= SCNetworkSetCopyServices(set
);
526 if (services
!= NULL
) {
531 CFMutableArrayRef sorted
= (CFMutableArrayRef
)services
;
533 order
= SCNetworkSetGetServiceOrder(set
);
535 nOrder
= CFArrayGetCount(order
);
538 n
= CFArrayGetCount(services
);
540 sorted
= CFArrayCreateMutableCopy(NULL
, 0, services
);
541 CFArraySortValues(sorted
,
542 CFRangeMake(0, CFArrayGetCount(sorted
)),
547 SCPrint(TRUE
, stdout
, CFSTR("services =\n"));
549 for (i
= 0; i
< n
; i
++) {
550 CFIndex orderIndex
= kCFNotFound
;
551 SCNetworkServiceRef service
;
552 CFStringRef serviceName
;
553 CFStringRef serviceID
;
555 service
= CFArrayGetValueAtIndex(sorted
, i
);
556 serviceID
= SCNetworkServiceGetServiceID(service
);
557 serviceName
= SCNetworkServiceGetName(service
);
558 if (serviceName
== NULL
) serviceName
= CFSTR("");
561 orderIndex
= CFArrayGetFirstIndexOfValue(order
,
562 CFRangeMake(0, nOrder
),
565 if (orderIndex
!= kCFNotFound
) {
566 SCPrint(TRUE
, stdout
, CFSTR("%c%2d: %@%-*s (%@)\n"),
567 ((net_service
!= NULL
) && CFEqual(service
, net_service
)) ? '>' : ' ',
570 30 - CFStringGetLength(serviceName
),
574 SCPrint(TRUE
, stdout
, CFSTR("%c : %@%-*s (%@)\n"),
575 ((net_service
!= NULL
) && CFEqual(service
, net_service
)) ? '>' : ' ',
577 30 - CFStringGetLength(serviceName
),
585 if (sorted
!= services
) CFRelease(sorted
);
589 SCPrint(TRUE
, stdout
, CFSTR("\n%@\n"), set
);
598 show_sets(int argc
, char **argv
)
600 SCNetworkSetRef current
;
605 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
616 current
= SCNetworkSetCopyCurrent(prefs
);
618 n
= CFArrayGetCount(sets
);
619 for (i
= 0; i
< n
; i
++) {
624 set
= CFArrayGetValueAtIndex(sets
, i
);
625 setID
= SCNetworkSetGetSetID(set
);
626 setName
= SCNetworkSetGetName(set
);
627 if (setName
== NULL
) setName
= CFSTR("");
629 SCPrint(TRUE
, stdout
, CFSTR(" %c%c%2d: %@%-*s (%@)\n"),
630 ((current
!= NULL
) && CFEqual(set
, current
)) ? '*' : ' ',
631 ((net_set
!= NULL
) && CFEqual(set
, net_set
)) ? '>' : ' ',
634 30 - CFStringGetLength(setName
),
639 if (current
!= NULL
) CFRelease(current
);