]> git.saurik.com Git - apple/configd.git/blob - scutil.tproj/net_service.c
configd-293.4.tar.gz
[apple/configd.git] / scutil.tproj / net_service.c
1 /*
2 * Copyright (c) 2004-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * August 5, 2004 Allan Nathanson <ajn@apple.com>
28 * - initial revision
29 */
30
31
32 #include "scutil.h"
33 #include "net.h"
34 #include "net_service.h"
35 #include "net_interface.h"
36 #include "net_protocol.h"
37 #include "prefs.h"
38
39
40 /* -------------------- */
41
42
43 __private_extern__
44 CFComparisonResult
45 _compare_services(const void *val1, const void *val2, void *context)
46 {
47 CFStringRef id1;
48 CFStringRef id2;
49 CFArrayRef order = (CFArrayRef)context;
50 SCNetworkServiceRef s1 = (SCNetworkServiceRef)val1;
51 SCNetworkServiceRef s2 = (SCNetworkServiceRef)val2;
52
53 id1 = SCNetworkServiceGetServiceID(s1);
54 id2 = SCNetworkServiceGetServiceID(s2);
55
56 if (order != NULL) {
57 CFIndex o1;
58 CFIndex o2;
59 CFRange range;
60
61 range = CFRangeMake(0, CFArrayGetCount(order));
62 o1 = CFArrayGetFirstIndexOfValue(order, range, id1);
63 o2 = CFArrayGetFirstIndexOfValue(order, range, id2);
64
65 if (o1 > o2) {
66 return (o2 != kCFNotFound) ? kCFCompareGreaterThan : kCFCompareLessThan;
67 } else if (o1 < o2) {
68 return (o1 != kCFNotFound) ? kCFCompareLessThan : kCFCompareGreaterThan;
69 }
70 }
71
72 return CFStringCompare(id1, id2, 0);
73 }
74
75
76 static SCNetworkServiceRef
77 _find_service(char *match)
78 {
79 Boolean allowIndex = TRUE;
80 CFIndex i;
81 CFIndex n;
82 CFStringRef select_name = NULL;
83 SCNetworkServiceRef selected = NULL;
84
85 if (services == NULL) {
86 if (net_set == NULL) {
87 SCPrint(TRUE, stdout, CFSTR("set not selected\n"));
88 return NULL;
89 }
90
91 services = SCNetworkSetCopyServices(net_set);
92 if (services == NULL) {
93 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
94 return NULL;
95 }
96
97 allowIndex = FALSE;
98 }
99
100 // try to select the service by its serviceID
101
102 select_name = CFStringCreateWithCString(NULL, match, kCFStringEncodingUTF8);
103
104 n = CFArrayGetCount(services);
105 for (i = 0; i < n; i++) {
106 SCNetworkServiceRef service;
107 CFStringRef serviceID;
108
109 service = CFArrayGetValueAtIndex(services, i);
110 serviceID = SCNetworkServiceGetServiceID(service);
111 if (CFEqual(select_name, serviceID)) {
112 selected = service;
113 goto done;
114 }
115 }
116
117 // try to select the service by its name
118
119 for (i = 0; i < n; i++) {
120 SCNetworkServiceRef service;
121 CFStringRef serviceName;
122
123 service = CFArrayGetValueAtIndex(services, i);
124 serviceName = SCNetworkServiceGetName(service);
125 if ((serviceName != NULL) && CFEqual(select_name, serviceName)) {
126 if (selected == NULL) {
127 selected = service;
128 } else {
129 // if multiple services match
130 selected = NULL;
131 SCPrint(TRUE, stdout, CFSTR("multiple services match\n"));
132 goto done;
133 }
134 }
135 }
136
137 if (selected != NULL) {
138 goto done;
139 }
140
141 // try to select the service by its name (case insensitive)
142
143 for (i = 0; i < n; i++) {
144 SCNetworkServiceRef service;
145 CFStringRef serviceName;
146
147 service = CFArrayGetValueAtIndex(services, i);
148 serviceName = SCNetworkServiceGetName(service);
149 if ((serviceName != NULL) &&
150 CFStringCompare(select_name,
151 serviceName,
152 kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
153 if (selected == NULL) {
154 selected = service;
155 } else {
156 // if multiple services match
157 selected = NULL;
158 SCPrint(TRUE, stdout, CFSTR("multiple services match\n"));
159 goto done;
160 }
161 }
162 }
163
164 if (selected != NULL) {
165 goto done;
166 }
167
168 // try to select the service by its [BSD] interface name
169
170 for (i = 0; i < n; i++) {
171 SCNetworkInterfaceRef interface;
172 CFStringRef interfaceName = NULL;
173 SCNetworkServiceRef service;
174
175 service = CFArrayGetValueAtIndex(services, i);
176
177 interface = SCNetworkServiceGetInterface(service);
178 while ((interface != NULL) && (interfaceName == NULL)) {
179 interfaceName = SCNetworkInterfaceGetBSDName(interface);
180 if (interfaceName == NULL) {
181 interface = SCNetworkInterfaceGetInterface(interface);
182 }
183 }
184
185 if (interfaceName == NULL) {
186 continue;
187 }
188
189 if (CFStringCompare(select_name,
190 interfaceName,
191 kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
192 if (selected == NULL) {
193 selected = service;
194 } else {
195 // if multiple services match
196 selected = NULL;
197 SCPrint(TRUE, stdout, CFSTR("multiple services match\n"));
198 goto done;
199 }
200 }
201 }
202
203 if (selected != NULL) {
204 goto done;
205 }
206
207 // try to select the service by its index
208
209 if (allowIndex) {
210 char *end;
211 char *str = match;
212 long val;
213
214 errno = 0;
215 val = strtol(str, &end, 10);
216 if ((*str != '\0') && (*end == '\0') && (errno == 0)) {
217 if ((val > 0) && (val <= n)) {
218 selected = CFArrayGetValueAtIndex(services, val - 1);
219 }
220 }
221 }
222
223 if (selected != NULL) {
224 goto done;
225 }
226
227 SCPrint(TRUE, stdout, CFSTR("no match, which service?\n"));
228
229 done :
230
231 if (select_name != NULL) CFRelease(select_name);
232 return selected;
233 }
234
235
236 /* -------------------- */
237
238
239 __private_extern__
240 void
241 create_service(int argc, char **argv)
242 {
243 SCNetworkInterfaceRef interface;
244 CFStringRef interfaceName;
245 Boolean ok;
246 SCNetworkServiceRef service = NULL;
247 CFStringRef serviceName;
248 CFStringRef setName;
249 CFArrayRef supported;
250
251 if (prefs == NULL) {
252 SCPrint(TRUE, stdout, CFSTR("network configuration not open\n"));
253 return;
254 }
255
256 if (net_set == NULL) {
257 SCPrint(TRUE, stdout, CFSTR("set not selected\n"));
258 return;
259 }
260
261 if (argc < 1) {
262 if (net_interface == NULL) {
263 SCPrint(TRUE, stdout, CFSTR("no network interface selected\n"));
264 return;
265 }
266
267 interface = net_interface;
268 } else {
269 int nArgs;
270
271 interface = _find_interface(argc, argv, &nArgs);
272 argv += nArgs;
273 argc -= nArgs;
274 }
275
276 if (interface == NULL) {
277 return;
278 }
279
280 supported = SCNetworkInterfaceGetSupportedProtocolTypes(interface);
281 if (supported == NULL) {
282 SCPrint(TRUE, stdout, CFSTR("no network protocols are supported over this interface\n"));
283 return;
284 }
285
286 service = SCNetworkServiceCreate(prefs, interface);
287 if (service == NULL) {
288 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
289 goto done;
290 }
291
292 if ((argc > 0) && (strlen(argv[0]) > 0)) {
293 Boolean ok;
294
295 serviceName = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
296 // argv++;
297 // argc--;
298
299 ok = SCNetworkServiceSetName(service, serviceName);
300 CFRelease(serviceName);
301 if (!ok) {
302 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
303 (void)SCNetworkServiceRemove(service);
304 goto done;
305 }
306 }
307
308 ok = SCNetworkServiceEstablishDefaultConfiguration(service);
309 if (!ok) {
310 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
311 (void)SCNetworkServiceRemove(service);
312 goto done;
313 }
314
315 ok = SCNetworkSetAddService(net_set, service);
316 if (!ok) {
317 SCPrint(TRUE, stdout, CFSTR("service not created: %s\n"), SCErrorString(SCError()));
318 (void)SCNetworkServiceRemove(service);
319 goto done;
320 }
321
322 _prefs_changed = TRUE;
323
324 if (net_service != NULL) CFRelease(net_service);
325 net_service = CFRetain(service);
326
327 serviceName = SCNetworkServiceGetName(service);
328 if (serviceName != NULL) {
329 SCPrint(TRUE, stdout,
330 CFSTR("service \"%@\" (%@) created and selected\n"),
331 serviceName,
332 SCNetworkServiceGetServiceID(service));
333 } else {
334 SCPrint(TRUE, stdout,
335 CFSTR("service ID \"%@\" created and selected\n"),
336 SCNetworkServiceGetServiceID(service));
337 }
338
339 setName = SCNetworkSetGetName(net_set);
340 if (setName != NULL) {
341 SCPrint(TRUE, stdout, CFSTR("& added to set \"%@\"\n"), setName);
342 } else {
343 SCPrint(TRUE, stdout, CFSTR("& added to set ID \"%@\"\n"),
344 SCNetworkSetGetSetID(net_set));
345 }
346
347 if (net_interface != NULL) CFRelease(net_interface);
348 net_interface = SCNetworkServiceGetInterface(net_service);
349 if (net_interface != NULL) {
350 CFRetain(net_interface);
351 }
352
353 interfaceName = SCNetworkInterfaceGetLocalizedDisplayName(interface);
354 if (interfaceName == NULL) {
355 interfaceName = SCNetworkInterfaceGetBSDName(interface);
356 }
357 if (interfaceName == NULL) {
358 interfaceName = SCNetworkInterfaceGetInterfaceType(interface);
359 }
360
361 SCPrint(TRUE, stdout,
362 CFSTR("& interface \"%@\" selected\n"),
363 interfaceName);
364
365 if (protocols != NULL) {
366 CFRelease(protocols);
367 protocols = NULL;
368 }
369
370 if (net_protocol != NULL) {
371 CFRelease(net_protocol);
372 net_protocol = NULL;
373 SCPrint(TRUE, stdout, CFSTR("& no protocol selected\n"));
374 }
375
376 if (services != NULL) {
377 CFRelease(services);
378 services = NULL;
379 }
380
381 done :
382
383 if (service != NULL) CFRelease(service);
384 return;
385 }
386
387
388 __private_extern__
389 void
390 disable_service(int argc, char **argv)
391 {
392 SCNetworkServiceRef service;
393
394 if (argc == 1) {
395 service = _find_service(argv[0]);
396 } else {
397 if (net_service != NULL) {
398 service = net_service;
399 } else {
400 SCPrint(TRUE, stdout, CFSTR("service not selected\n"));
401 return;
402 }
403 }
404
405 if (service == NULL) {
406 return;
407 }
408
409 if (!SCNetworkServiceSetEnabled(service, FALSE)) {
410 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
411 return;
412 }
413
414 _prefs_changed = TRUE;
415 return;
416 }
417
418
419 __private_extern__
420 void
421 enable_service(int argc, char **argv)
422 {
423 SCNetworkServiceRef service;
424
425 if (argc == 1) {
426 service = _find_service(argv[0]);
427 } else {
428 if (net_service != NULL) {
429 service = net_service;
430 } else {
431 SCPrint(TRUE, stdout, CFSTR("service not selected\n"));
432 return;
433 }
434 }
435
436 if (service == NULL) {
437 return;
438 }
439
440 if (!SCNetworkServiceSetEnabled(service, TRUE)) {
441 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
442 return;
443 }
444
445 _prefs_changed = TRUE;
446 return;
447 }
448
449
450 __private_extern__
451 void
452 remove_service(int argc, char **argv)
453 {
454 SCNetworkServiceRef service = NULL;
455 CFStringRef serviceName;
456
457 if (argc == 1) {
458 service = _find_service(argv[0]);
459 } else {
460 if (net_service != NULL) {
461 service = net_service;
462 }
463 }
464
465 if (service == NULL) {
466 return;
467 }
468
469 CFRetain(service);
470
471 if (!SCNetworkServiceRemove(service)) {
472 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
473 goto done;
474 }
475
476 _prefs_changed = TRUE;
477
478 serviceName = SCNetworkServiceGetName(service);
479 if (serviceName != NULL) {
480 SCPrint(TRUE, stdout, CFSTR("service \"%@\" removed\n"), serviceName);
481 } else {
482 SCPrint(TRUE, stdout,
483 CFSTR("service ID \"%@\" removed\n"),
484 SCNetworkServiceGetServiceID(service));
485 }
486
487 if ((net_service != NULL) && CFEqual(service, net_service)) {
488 CFRelease(net_service);
489 net_service = NULL;
490 SCPrint(TRUE, stdout, CFSTR("& no service selected\n"));
491
492 if (protocols != NULL) {
493 CFRelease(protocols);
494 protocols = NULL;
495 }
496
497 if (net_protocol != NULL) {
498 CFRelease(net_protocol);
499 net_protocol = NULL;
500 SCPrint(TRUE, stdout, CFSTR("& no protocol selected\n"));
501 }
502
503 if (net_interface != NULL) {
504 CFRelease(net_interface);
505 net_interface = NULL;
506 SCPrint(TRUE, stdout, CFSTR("& no interface selected\n"));
507 }
508 }
509
510 if (services != NULL) {
511 CFRelease(services);
512 services = NULL;
513 }
514
515 done :
516
517 CFRelease(service);
518 return;
519 }
520
521
522 __private_extern__
523 void
524 select_service(int argc, char **argv)
525 {
526 SCNetworkInterfaceRef interface;
527 SCNetworkServiceRef service;
528 CFStringRef serviceName;
529
530 service = _find_service(argv[0]);
531
532 if (service == NULL) {
533 return;
534 }
535
536 if (net_service != NULL) CFRelease(net_service);
537 net_service = CFRetain(service);
538
539 serviceName = SCNetworkServiceGetName(service);
540 if (serviceName != NULL) {
541 SCPrint(TRUE, stdout, CFSTR("service \"%@\" selected\n"), serviceName);
542 } else {
543 SCPrint(TRUE, stdout,
544 CFSTR("service ID \"%@\" selected\n"),
545 SCNetworkServiceGetServiceID(service));
546 }
547
548 interface = SCNetworkServiceGetInterface(service);
549 if (interface != NULL) {
550 CFStringRef interfaceName;
551
552 if (net_interface != NULL) CFRelease(net_interface);
553 net_interface = CFRetain(interface);
554
555 interfaceName = SCNetworkInterfaceGetLocalizedDisplayName(interface);
556 if (interfaceName == NULL) {
557 interfaceName = SCNetworkInterfaceGetBSDName(interface);
558 }
559 if (interfaceName == NULL) {
560 interfaceName = SCNetworkInterfaceGetInterfaceType(interface);
561 }
562
563 SCPrint(TRUE, stdout,
564 CFSTR("& interface \"%@\" selected\n"),
565 interfaceName);
566 } else {
567 if (net_interface != NULL) {
568 CFRelease(net_interface);
569 net_interface = NULL;
570 SCPrint(TRUE, stdout, CFSTR("& no interface selected\n"));
571 }
572 }
573
574 if (protocols != NULL) {
575 CFRelease(protocols);
576 protocols = NULL;
577 }
578
579 if (net_protocol != NULL) {
580 CFRelease(net_protocol);
581 net_protocol = NULL;
582 SCPrint(TRUE, stdout, CFSTR("& no protocol selected\n"));
583 }
584
585 return;
586 }
587
588
589 __private_extern__
590 void
591 set_service(int argc, char **argv)
592 {
593 Boolean ok;
594
595 if (net_service == NULL) {
596 SCPrint(TRUE, stdout, CFSTR("service not selected\n"));
597 return;
598 }
599
600 if (argc < 1) {
601 SCPrint(TRUE, stdout, CFSTR("set what?\n"));
602 return;
603 }
604
605 while (argc > 0) {
606 char *command;
607
608 command = argv[0];
609 argv++;
610 argc--;
611
612 if (strcmp(command, "name") == 0) {
613 CFStringRef serviceName;
614
615 if (argc < 1) {
616 SCPrint(TRUE, stdout, CFSTR("name not specified\n"));
617 return;
618 }
619
620 serviceName = (strlen(argv[0]) > 0)
621 ? CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8)
622 : NULL;
623 argv++;
624 argc--;
625
626 ok = SCNetworkServiceSetName(net_service, serviceName);
627 if (serviceName != NULL) CFRelease(serviceName);
628 if (!ok) {
629 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
630 return;
631 }
632
633 _prefs_changed = TRUE;
634 } else if (strcmp(command, "order") == 0) {
635
636 char *end;
637 long newIndex;
638 CFIndex nServices;
639 char *str;
640 CFArrayRef services;
641
642 services = SCNetworkSetCopyServices(net_set);
643 nServices = CFArrayGetCount(services);
644 CFRelease(services);
645
646 if (argc < 1) {
647 SCPrint(TRUE, stdout, CFSTR("order not specified\n"));
648 return;
649 }
650
651 if (net_set == NULL) {
652 SCPrint(TRUE, stdout, CFSTR("set not selected\n"));
653 return;
654 }
655
656 str = argv[0];
657 argv++;
658 argc--;
659
660 errno = 0;
661 newIndex = strtol(str, &end, 10);
662 if ((*str != '\0') && (*end == '\0') && (errno == 0)) {
663 if ((newIndex > 0) && (newIndex <= nServices)) {
664 CFIndex curIndex;
665 CFMutableArrayRef newOrder;
666 CFArrayRef order;
667 CFStringRef serviceID = SCNetworkServiceGetServiceID(net_service);
668
669 order = SCNetworkSetGetServiceOrder(net_set);
670 if (order == NULL) {
671 newOrder = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
672 } else {
673 newOrder = CFArrayCreateMutableCopy(NULL, 0, order);
674 }
675
676 curIndex = CFArrayGetFirstIndexOfValue(newOrder,
677 CFRangeMake(0, CFArrayGetCount(newOrder)),
678 serviceID);
679 if (curIndex != kCFNotFound) {
680 CFArrayRemoveValueAtIndex(newOrder, curIndex);
681 }
682
683 if (newIndex <= CFArrayGetCount(newOrder)) {
684 CFArrayInsertValueAtIndex(newOrder, newIndex - 1, serviceID);
685 } else {
686 CFArrayAppendValue(newOrder, serviceID);
687 }
688
689 ok = SCNetworkSetSetServiceOrder(net_set, newOrder);
690 CFRelease(newOrder);
691 if (!ok) {
692 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
693 return;
694 }
695
696 _prefs_changed = TRUE;
697 } else {
698 SCPrint(TRUE, stdout, CFSTR("set order to what?\n"));
699 return;
700 }
701 } else {
702 SCPrint(TRUE, stdout, CFSTR("set what?\n"));
703 return;
704 }
705 } else if (strcmp(command, "rank") == 0) {
706 SCNetworkServicePrimaryRank rank = kSCNetworkServicePrimaryRankDefault;
707 SCNetworkServiceRef service = (argc < 2) ? net_service : NULL;
708
709 if (argc < 1) {
710 SCPrint(TRUE, stdout, CFSTR("rank not specified\n"));
711 return;
712 }
713
714 if (strlen(argv[0]) > 0) {
715 if (strcasecmp(argv[0], "Never") == 0) {
716 rank = kSCNetworkServicePrimaryRankNever;
717 } else if ((service != net_service) && (strcasecmp(argv[0], "First") == 0)) {
718 rank = kSCNetworkServicePrimaryRankFirst;
719 } else if ((service != net_service) && (strcasecmp(argv[0], "Last") == 0)) {
720 rank = kSCNetworkServicePrimaryRankLast;
721 } else {
722 SCPrint(TRUE, stdout, CFSTR("rank not valid\n"));
723 return;
724 }
725 }
726 argv++;
727 argc--;
728
729 if (service == NULL) {
730 CFStringRef serviceID;
731 SCDynamicStoreRef store;
732
733 store = SCDynamicStoreCreate(NULL,
734 CFSTR("scutil (set primary rank)"),
735 NULL,
736 NULL);
737 serviceID = SCNetworkServiceGetServiceID(net_service);
738 service = _SCNetworkServiceCopyActive(store, serviceID);
739 CFRelease(store);
740
741 argv++;
742 argc--;
743 }
744
745 ok = SCNetworkServiceSetPrimaryRank(service, rank);
746 if (service != net_service) CFRelease(service);
747 if (ok) {
748 if (service == net_service) _prefs_changed = TRUE;
749 } else {
750 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
751 return;
752 }
753 } else {
754 SCPrint(TRUE, stdout, CFSTR("set what?\n"));
755 }
756 }
757
758 return;
759 }
760
761
762 static void
763 __show_service_interface(SCNetworkServiceRef service, const char *prefix)
764 {
765 CFStringRef description;
766 SCNetworkInterfaceRef interface;
767
768 interface = SCNetworkServiceGetInterface(service);
769 if (interface == NULL) {
770 return;
771 }
772
773 description = _interface_description(interface);
774 SCPrint(TRUE, stdout, CFSTR("%s%@\n"), prefix, description);
775 CFRelease(description);
776
777 return;
778 }
779
780 static void
781 __show_service_protocols(SCNetworkServiceRef service, const char *prefix, Boolean skipEmpty)
782 {
783 CFIndex i;
784 CFIndex n;
785 CFArrayRef protocols;
786
787 protocols = SCNetworkServiceCopyProtocols(service);
788 if (protocols == NULL) {
789 return;
790 }
791
792 n = CFArrayGetCount(protocols);
793 if (n > 1) {
794 CFMutableArrayRef sorted;
795
796 sorted = CFArrayCreateMutableCopy(NULL, 0, protocols);
797 CFArraySortValues(sorted,
798 CFRangeMake(0, n),
799 _compare_protocols,
800 NULL);
801 CFRelease(protocols);
802 protocols = sorted;
803 }
804
805 for (i = 0; i < n; i++) {
806 CFStringRef description;
807 SCNetworkProtocolRef protocol;
808
809 protocol = CFArrayGetValueAtIndex(protocols, i);
810 description = _protocol_description(protocol, skipEmpty);
811 if (description != NULL) {
812 CFStringRef protocolType;
813
814 protocolType = SCNetworkProtocolGetProtocolType(protocol);
815 SCPrint(TRUE, stdout,
816 CFSTR("%s%@%*s : %@\n"),
817 prefix,
818 protocolType,
819 sizeof("Interface") - CFStringGetLength(protocolType) - 1,
820 "",
821 description);
822 CFRelease(description);
823 }
824 }
825
826 CFRelease(protocols);
827 return;
828 }
829
830
831 __private_extern__
832 void
833 show_service(int argc, char **argv)
834 {
835 SCNetworkInterfaceRef interface;
836 CFArrayRef protocols;
837 SCNetworkServiceRef service;
838 CFStringRef serviceName;
839 SCNetworkServicePrimaryRank serviceRank;
840
841 if (argc == 1) {
842 service = _find_service(argv[0]);
843 } else {
844 if (net_service != NULL) {
845 service = net_service;
846 } else {
847 SCPrint(TRUE, stdout, CFSTR("service not selected\n"));
848 return;
849 }
850 }
851
852 if (service == NULL) {
853 return;
854 }
855
856 SCPrint(TRUE, stdout, CFSTR("service id = %@\n"), SCNetworkServiceGetServiceID(service));
857
858 serviceName = SCNetworkServiceGetName(service);
859 SCPrint(TRUE, stdout, CFSTR("name = %@\n"),
860 (serviceName != NULL) ? serviceName : CFSTR(""));
861
862 serviceRank = SCNetworkServiceGetPrimaryRank(service);
863 switch (serviceRank) {
864 case kSCNetworkServicePrimaryRankDefault :
865 // nothing to report
866 break;
867 case kSCNetworkServicePrimaryRankFirst :
868 SCPrint(TRUE, stdout, CFSTR("primary rank = FIRST\n"));
869 break;
870 case kSCNetworkServicePrimaryRankLast :
871 SCPrint(TRUE, stdout, CFSTR("primary rank = LAST\n"));
872 break;
873 case kSCNetworkServicePrimaryRankNever :
874 SCPrint(TRUE, stdout, CFSTR("primary rank = NEVER\n"));
875 break;
876 default :
877 SCPrint(TRUE, stdout, CFSTR("primary rank = %d\n"), serviceRank);
878 break;
879 }
880
881 interface = SCNetworkServiceGetInterface(service);
882 if (interface != NULL) {
883 CFStringRef interfaceName;
884
885 interfaceName = SCNetworkInterfaceGetLocalizedDisplayName(interface);
886 if (interfaceName != NULL) {
887 CFRetain(interfaceName);
888 } else {
889 interfaceName = _interface_description(interface);
890 }
891 if (interfaceName != NULL) {
892 SCPrint(TRUE, stdout, CFSTR("interface = %@\n"), interfaceName);
893 CFRelease(interfaceName);
894 }
895 } else {
896 SCPrint(TRUE, stdout, CFSTR("\n No interface!\n\n"));
897 }
898
899 protocols = SCNetworkServiceCopyProtocols(service);
900 if (protocols != NULL) {
901 CFIndex n;
902
903 n = CFArrayGetCount(protocols);
904 if (n > 1) {
905 CFMutableArrayRef sorted;
906
907 sorted = CFArrayCreateMutableCopy(NULL, 0, protocols);
908 CFArraySortValues(sorted,
909 CFRangeMake(0, n),
910 _compare_protocols,
911 NULL);
912 CFRelease(protocols);
913 protocols = sorted;
914 }
915
916 if (n > 0) {
917 CFIndex i;
918
919 SCPrint(TRUE, stdout, CFSTR("configured protocols = "));
920 for (i = 0; i < n; i++) {
921 SCNetworkProtocolRef protocol;
922
923 protocol = CFArrayGetValueAtIndex(protocols, i);
924 SCPrint(TRUE, stdout, CFSTR("%s%@"),
925 (i == 0) ? "" : ", ",
926 SCNetworkProtocolGetProtocolType(protocol));
927 }
928 SCPrint(TRUE, stdout, CFSTR("\n"));
929
930 __show_service_protocols(service, " ", FALSE);
931 } else {
932 SCPrint(TRUE, stdout, CFSTR("no configured protocols\n"));
933 }
934
935 CFRelease(protocols);
936 }
937
938 if (_sc_debug) {
939 SCPrint(TRUE, stdout, CFSTR("\n%@\n"), service);
940 }
941
942 return;
943 }
944
945
946 __private_extern__
947 void
948 show_services(int argc, char **argv)
949 {
950 CFIndex i;
951 CFIndex n;
952
953 if (prefs == NULL) {
954 SCPrint(TRUE, stdout, CFSTR("network configuration not open\n"));
955 return;
956 }
957
958 if (argc == 1) {
959 if (services != NULL) CFRelease(services);
960 services = SCNetworkServiceCopyAll(prefs);
961 } else {
962 if (net_set == NULL) {
963 SCPrint(TRUE, stdout, CFSTR("set not selected\n"));
964 return;
965 }
966
967 if (services != NULL) CFRelease(services);
968 services = SCNetworkSetCopyServices(net_set);
969 n = (services != NULL) ? CFArrayGetCount(services) : 0;
970 if (n > 1) {
971 CFArrayRef order;
972 CFMutableArrayRef sorted;
973
974 order = SCNetworkSetGetServiceOrder(net_set);
975 sorted = CFArrayCreateMutableCopy(NULL, 0, services);
976 CFArraySortValues(sorted,
977 CFRangeMake(0, CFArrayGetCount(sorted)),
978 _compare_services,
979 (void *)order);
980 CFRelease(services);
981 services = sorted;
982 }
983 }
984
985 if (services == NULL) {
986 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
987 return;
988 }
989
990 n = CFArrayGetCount(services);
991 for (i = 0; i < n; i++) {
992 SCNetworkServiceRef service;
993 CFStringRef serviceName;
994 CFStringRef serviceID;
995
996 service = CFArrayGetValueAtIndex(services, i);
997 serviceID = SCNetworkServiceGetServiceID(service);
998 serviceName = SCNetworkServiceGetName(service);
999 if (serviceName == NULL) serviceName = CFSTR("");
1000
1001 SCPrint(TRUE, stdout, CFSTR("%c%2d: %@%-*s (%@)%s\n"),
1002 ((net_service != NULL) && CFEqual(service, net_service)) ? '>' : ' ',
1003 i + 1,
1004 serviceName,
1005 30 - CFStringGetLength(serviceName),
1006 " ",
1007 serviceID,
1008 SCNetworkServiceGetEnabled(service) ? "" : " *DISABLED*");
1009
1010 __show_service_interface(service, " Interface : ");
1011 __show_service_protocols(service, " ", TRUE);
1012 }
1013
1014 return;
1015 }