2 * Copyright (c) 2004-2007, 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>
36 #include "net_interface.h"
37 #include "net_protocol.h"
38 #include "net_service.h"
44 __private_extern__ CFMutableArrayRef new_interfaces
= NULL
;
46 __private_extern__ CFArrayRef interfaces
= NULL
;
47 __private_extern__ CFArrayRef services
= NULL
;
48 __private_extern__ CFArrayRef protocols
= NULL
;
49 __private_extern__ CFArrayRef sets
= NULL
;
51 __private_extern__ SCNetworkInterfaceRef net_interface
= NULL
;
52 __private_extern__ SCNetworkServiceRef net_service
= NULL
;
53 __private_extern__ SCNetworkProtocolRef net_protocol
= NULL
;
54 __private_extern__ SCNetworkSetRef net_set
= NULL
;
56 __private_extern__ CFNumberRef CFNumberRef_0
= NULL
;
57 __private_extern__ CFNumberRef CFNumberRef_1
= NULL
;
60 /* -------------------- */
65 _copy_number(const char *arg
)
69 if (sscanf(arg
, "%d", &val
) != 1) {
73 return CFNumberCreate(NULL
, kCFNumberIntType
, &val
);
77 /* -------------------- */
82 _find_option(const char *option
, optionsRef options
, const int nOptions
)
86 for (i
= 0; i
< nOptions
; i
++) {
87 if (strcasecmp(option
, options
[i
].option
) == 0) {
98 _find_selection(CFStringRef choice
, selections choices
[], unsigned int *flags
)
103 while (choices
[i
].selection
!= NULL
) {
104 if (CFStringCompare(choice
,
105 choices
[i
].selection
,
106 kCFCompareCaseInsensitive
) == kCFCompareEqualTo
) {
108 *flags
= choices
[i
].flags
;
121 _process_options(optionsRef options
, int nOptions
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
124 CFIndex optionIndex
= kCFNotFound
;
126 optionIndex
= _find_option(argv
[0], options
, nOptions
);
127 if (optionIndex
== kCFNotFound
) {
128 SCPrint(TRUE
, stdout
, CFSTR("set what?\n"));
134 switch (options
[optionIndex
].type
) {
136 // all option processing is managed by the "handler"
139 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), options
[optionIndex
].info
);
143 selections
*choices
= (selections
*)options
[optionIndex
].info
;
148 SCPrint(TRUE
, stdout
,
149 CFSTR("%s not specified\n"),
150 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "selection");
154 choice
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
155 i
= _find_selection(choice
, choices
, &flags
);
158 if (i
!= kCFNotFound
) {
159 if (choices
[i
].flags
& selectionNotAvailable
) {
160 SCPrint(TRUE
, stdout
,
161 CFSTR("cannot select %s\n"),
162 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "selection");
166 CFDictionarySetValue(newConfiguration
,
167 *(options
[optionIndex
].key
),
170 SCPrint(TRUE
, stdout
,
171 CFSTR("invalid %s\n"),
172 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "selection");
180 case isChooseMultiple
:
182 SCPrint(TRUE
, stdout
,
183 CFSTR("%s(s) not specified\n"),
184 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "selection");
188 if (strlen(argv
[0]) > 0) {
191 CFMutableArrayRef chosen
;
193 CFArrayRef str_array
;
195 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
196 str_array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
199 chosen
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
201 n
= CFArrayGetCount(str_array
);
202 for (i
= 0; i
< n
; i
++) {
204 selections
*choices
= (selections
*)options
[optionIndex
].info
;
208 choice
= CFArrayGetValueAtIndex(str_array
, i
);
209 j
= _find_selection(choice
, choices
, &flags
);
211 if (j
!= kCFNotFound
) {
212 if (choices
[j
].flags
& selectionNotAvailable
) {
213 SCPrint(TRUE
, stdout
,
214 CFSTR("cannot select %s\n"),
215 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "selection");
216 CFArrayRemoveAllValues(chosen
);
220 CFArrayAppendValue(chosen
, *(choices
[j
].key
));
222 SCPrint(TRUE
, stdout
,
223 CFSTR("invalid %s\n"),
224 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "selection");
225 CFArrayRemoveAllValues(chosen
);
229 CFRelease(str_array
);
231 if (CFArrayGetCount(chosen
) > 0) {
232 CFDictionarySetValue(newConfiguration
, *(options
[optionIndex
].key
), chosen
);
234 CFDictionaryRemoveValue(newConfiguration
, *(options
[optionIndex
].key
));
238 CFDictionaryRemoveValue(newConfiguration
, *(options
[optionIndex
].key
));
246 SCPrint(TRUE
, stdout
,
247 CFSTR("%s not specified\n"),
248 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "enable/disable");
252 if ((strcasecmp(argv
[0], "disable") == 0) ||
253 (strcasecmp(argv
[0], "no" ) == 0) ||
254 (strcasecmp(argv
[0], "0" ) == 0)) {
255 CFDictionarySetValue(newConfiguration
, *(options
[optionIndex
].key
), CFNumberRef_0
);
256 } else if ((strcasecmp(argv
[0], "enable") == 0) ||
257 (strcasecmp(argv
[0], "yes" ) == 0) ||
258 (strcasecmp(argv
[0], "1" ) == 0)) {
259 CFDictionarySetValue(newConfiguration
, *(options
[optionIndex
].key
), CFNumberRef_1
);
261 SCPrint(TRUE
, stdout
, CFSTR("invalid value\n"));
270 SCPrint(TRUE
, stdout
,
271 CFSTR("%s not specified\n"),
272 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "value");
276 if (strlen(argv
[0]) > 0) {
279 num
= _copy_number(argv
[0]);
281 CFDictionarySetValue(newConfiguration
, *(options
[optionIndex
].key
), num
);
284 SCPrint(TRUE
, stdout
, CFSTR("invalid value\n"));
288 CFDictionaryRemoveValue(newConfiguration
, *(options
[optionIndex
].key
));
296 SCPrint(TRUE
, stdout
,
297 CFSTR("%s not specified\n"),
298 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "value");
302 if (strlen(argv
[0]) > 0) {
305 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
306 CFDictionarySetValue(newConfiguration
, *(options
[optionIndex
].key
), str
);
309 CFDictionaryRemoveValue(newConfiguration
, *(options
[optionIndex
].key
));
317 SCPrint(TRUE
, stdout
,
318 CFSTR("%s(s) not specified\n"),
319 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "value");
323 if (strlen(argv
[0]) > 0) {
325 CFArrayRef str_array
;
327 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
328 str_array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
331 CFDictionarySetValue(newConfiguration
, *(options
[optionIndex
].key
), str_array
);
332 CFRelease(str_array
);
334 CFDictionaryRemoveValue(newConfiguration
, *(options
[optionIndex
].key
));
342 if (options
[optionIndex
].handler
!= NULL
) {
346 key
= options
[optionIndex
].key
!= NULL
? *(options
[optionIndex
].key
) : NULL
;
347 nArgs
= (*options
[optionIndex
].handler
)(key
,
348 options
[optionIndex
].description
,
349 options
[optionIndex
].info
,
366 /* -------------------- */
373 _show_entity(CFDictionaryRef entity
, CFStringRef prefix
)
376 const void * keys_q
[N_QUICK
];
377 const void ** keys
= keys_q
;
380 CFMutableArrayRef sorted
;
382 n
= CFDictionaryGetCount(entity
);
383 if (n
> (CFIndex
)(sizeof(keys_q
) / sizeof(CFTypeRef
))) {
384 keys
= CFAllocatorAllocate(NULL
, n
* sizeof(CFTypeRef
), 0);
386 CFDictionaryGetKeysAndValues(entity
, keys
, NULL
);
388 array
= CFArrayCreate(NULL
, keys
, n
, &kCFTypeArrayCallBacks
);
389 sorted
= CFArrayCreateMutableCopy(NULL
, n
, array
);
391 CFArraySortValues(sorted
,
393 (CFComparatorFunction
)CFStringCompare
,
397 for (i
= 0; i
< n
; i
++) {
401 key
= CFArrayGetValueAtIndex(sorted
, i
);
402 value
= CFDictionaryGetValue(entity
, key
);
403 if (isA_CFArray(value
)) {
405 CFIndex n
= CFArrayGetCount(value
);
407 SCPrint(TRUE
, stdout
, CFSTR("%@ %@ = ("), prefix
, key
);
408 for (i
= 0; i
< n
; i
++) {
411 val
= CFArrayGetValueAtIndex(value
, i
);
412 SCPrint(TRUE
, stdout
,
417 SCPrint(TRUE
, stdout
, CFSTR(")\n"));
419 SCPrint(TRUE
, stdout
, CFSTR("%@ %@ = %@\n"), prefix
, key
, value
);
425 if (keys
!= keys_q
) {
426 CFAllocatorDeallocate(NULL
, keys
);
433 /* -------------------- */
439 if (net_interface
!= NULL
) {
440 CFRelease(net_interface
);
441 net_interface
= NULL
;
444 if (net_service
!= NULL
) {
445 CFRelease(net_service
);
449 if (net_protocol
!= NULL
) {
450 CFRelease(net_protocol
);
454 if (net_set
!= NULL
) {
459 if (interfaces
!= NULL
) {
460 CFRelease(interfaces
);
464 if (services
!= NULL
) {
469 if (protocols
!= NULL
) {
470 CFRelease(protocols
);
479 if (new_interfaces
!= NULL
) {
480 CFRelease(new_interfaces
);
481 new_interfaces
= NULL
;
495 CFNumberRef_0
= CFNumberCreate(NULL
, kCFNumberIntType
, &zero
);
496 CFNumberRef_1
= CFNumberCreate(NULL
, kCFNumberIntType
, &one
);
504 do_net_open(int argc
, char **argv
)
507 CFStringRef prefsID
= NULL
;
510 if (_prefs_commitRequired(argc
, argv
, "close")) {
519 prefsID
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
522 ok
= _prefs_open(CFSTR("scutil --net"), prefsID
);
523 if (prefsID
!= NULL
) CFRelease(prefsID
);
527 CFSTR("Could not open prefs: %s\n"),
528 SCErrorString(SCError()));
532 net_set
= SCNetworkSetCopyCurrent(prefs
);
533 if (net_set
!= NULL
) {
536 setName
= SCNetworkSetGetName(net_set
);
537 if (setName
!= NULL
) {
538 SCPrint(TRUE
, stdout
, CFSTR("set \"%@\" selected\n"), setName
);
540 SCPrint(TRUE
, stdout
,
541 CFSTR("set ID \"%@\" selected\n"),
542 SCNetworkSetGetSetID(net_set
));
552 do_net_commit(int argc
, char **argv
)
554 if (!SCPreferencesCommitChanges(prefs
)) {
555 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
559 _prefs_changed
= FALSE
;
566 do_net_apply(int argc
, char **argv
)
568 if (!SCPreferencesApplyChanges(prefs
)) {
569 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
577 do_net_close(int argc
, char **argv
)
579 if (_prefs_commitRequired(argc
, argv
, "close")) {
592 do_net_quit(int argc
, char **argv
)
594 if (_prefs_commitRequired(argc
, argv
, "quit")) {
601 termRequested
= TRUE
;
606 /* -------------------- */
609 typedef void (*net_func
) (int argc
, char **argv
);
611 static const struct {
622 { "interfaces", NULL
, NULL
, NULL
,
623 NULL
, NULL
, show_interfaces
,
626 { "interface", create_interface
, NULL
, NULL
,
627 select_interface
, set_interface
, show_interface
,
630 { "services", NULL
, NULL
, NULL
,
631 NULL
, NULL
, show_services
,
634 { "service", create_service
, disable_service
, enable_service
,
635 select_service
, set_service
, show_service
,
638 { "protocols", NULL
, NULL
, NULL
,
639 NULL
, NULL
, show_protocols
,
642 { "protocol", create_protocol
, disable_protocol
, enable_protocol
,
643 select_protocol
, set_protocol
, show_protocol
,
646 { "sets", NULL
, NULL
, NULL
,
647 NULL
, NULL
, show_sets
,
650 { "set", create_set
, NULL
, NULL
,
651 select_set
, set_set
, show_set
,
655 #define N_NET_KEYS (sizeof(net_keys) / sizeof(net_keys[0]))
659 findNetKey(char *key
)
663 for (i
= 0; i
< (int)N_NET_KEYS
; i
++) {
664 if (strcmp(key
, net_keys
[i
].key
) == 0) {
673 /* -------------------- */
678 do_net_create(int argc
, char **argv
)
689 SCPrint(TRUE
, stderr
, CFSTR("create what?\n"));
693 if (*net_keys
[i
].create
== NULL
) {
694 SCPrint(TRUE
, stderr
, CFSTR("create what?\n"));
697 (*net_keys
[i
].create
)(argc
, argv
);
704 do_net_disable(int argc
, char **argv
)
715 SCPrint(TRUE
, stderr
, CFSTR("disable what?\n"));
719 if (*net_keys
[i
].disable
== NULL
) {
720 SCPrint(TRUE
, stderr
, CFSTR("disable what?\n"));
723 (*net_keys
[i
].disable
)(argc
, argv
);
730 do_net_enable(int argc
, char **argv
)
741 SCPrint(TRUE
, stderr
, CFSTR("enable what?\n"));
745 if (*net_keys
[i
].enable
== NULL
) {
746 SCPrint(TRUE
, stderr
, CFSTR("enable what?\n"));
749 (*net_keys
[i
].enable
)(argc
, argv
);
756 do_net_remove(int argc
, char **argv
)
767 SCPrint(TRUE
, stderr
, CFSTR("remove what?\n"));
771 if (*net_keys
[i
].remove
== NULL
) {
772 SCPrint(TRUE
, stderr
, CFSTR("remove what?\n"));
775 (*net_keys
[i
].remove
)(argc
, argv
);
782 do_net_select(int argc
, char **argv
)
793 SCPrint(TRUE
, stderr
, CFSTR("select what?\n"));
797 if (*net_keys
[i
].select
== NULL
) {
798 SCPrint(TRUE
, stderr
, CFSTR("select what?\n"));
801 (*net_keys
[i
].select
)(argc
, argv
);
808 do_net_set(int argc
, char **argv
)
819 SCPrint(TRUE
, stderr
, CFSTR("set what?\n"));
823 (*net_keys
[i
].set
)(argc
, argv
);
830 do_net_show(int argc
, char **argv
)
841 SCPrint(TRUE
, stderr
, CFSTR("show what?\n"));
845 (*net_keys
[i
].show
)(argc
, argv
);
852 do_net_update(int argc
, char **argv
)
855 Boolean setCreated
= FALSE
;
856 Boolean setUpdated
= FALSE
;
859 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
863 if (net_set
!= NULL
) {
864 set
= CFRetain(net_set
);
866 set
= SCNetworkSetCopyCurrent(prefs
);
871 CFStringRef setName
= NULL
;
873 sets
= SCNetworkSetCopyAll(prefs
);
877 n
= CFArrayGetCount(sets
);
880 SCPrint(TRUE
, stdout
, CFSTR("no current set\n"));
885 bundle
= _SC_CFBundleGet();
886 if (bundle
!= NULL
) {
887 setName
= CFBundleCopyLocalizedString(bundle
,
888 CFSTR("DEFAULT_SET_NAME"),
892 if (setName
== NULL
) {
893 setName
= CFSTR("Automatic");
897 set
= SCNetworkSetCreate(prefs
);
899 SCPrint(TRUE
, stdout
,
900 CFSTR("could not initialize \"%@\": %s\n"),
902 SCErrorString(SCError()));
907 (void) SCNetworkSetSetName(set
, setName
);
909 ok
= SCNetworkSetSetCurrent(set
);
911 SCPrint(TRUE
, stdout
,
912 CFSTR("could not initialize \"%@\": %s\n"),
914 SCErrorString(SCError()));
915 (void) SCNetworkSetRemove(set
);
921 if (net_set
!= NULL
) CFRelease(net_set
);
931 setUpdated
= SCNetworkSetEstablishDefaultConfiguration(set
);
935 _prefs_changed
= TRUE
;
937 setName
= SCNetworkSetGetName(set
);
938 if (setName
!= NULL
) {
939 SCPrint(TRUE
, stdout
,
940 CFSTR("set \"%@\" (%@) %supdated\n"),
942 SCNetworkSetGetSetID(set
),
943 setCreated
? "created, selected, and " : "");
945 SCPrint(TRUE
, stdout
,
946 CFSTR("set ID \"%@\" %supdated\n"),
947 SCNetworkSetGetSetID(set
),
948 setCreated
? "created, selected, and " : "");
957 #include "SCPreferencesInternal.h"
962 do_net_snapshot(int argc
, char **argv
)
965 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
970 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
972 if (prefsPrivate
->prefs
!= NULL
) {
974 static int n_snapshot
= 0;
978 asprintf(&path
, "/tmp/prefs_snapshot_%d", n_snapshot
++);
980 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
, 0644);
983 SCPrint(TRUE
, stdout
, CFSTR("could not write snapshot: open() failed : %s\n"), strerror(errno
));
987 xmlData
= CFPropertyListCreateXMLData(NULL
, prefsPrivate
->prefs
);
988 if (xmlData
!= NULL
) {
989 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
992 SCPrint(TRUE
, stdout
, CFSTR("could not write snapshot: CFPropertyListCreateXMLData() failed\n"));
997 SCPrint(TRUE
, stdout
, CFSTR("prefs have not been accessed\n"));