]> git.saurik.com Git - apple/configd.git/blob - scutil.tproj/net_service.c
configd-289.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 CFRetain(net_interface);
350
351 interfaceName = SCNetworkInterfaceGetLocalizedDisplayName(interface);
352 if (interfaceName == NULL) {
353 interfaceName = SCNetworkInterfaceGetBSDName(interface);
354 }
355 if (interfaceName == NULL) {
356 interfaceName = SCNetworkInterfaceGetInterfaceType(interface);
357 }
358
359 SCPrint(TRUE, stdout,
360 CFSTR("& interface \"%@\" selected\n"),
361 interfaceName);
362
363 if (protocols != NULL) {
364 CFRelease(protocols);
365 protocols = NULL;
366 }
367
368 if (net_protocol != NULL) {
369 CFRelease(net_protocol);
370 net_protocol = NULL;
371 SCPrint(TRUE, stdout, CFSTR("& no protocol selected\n"));
372 }
373
374 if (services != NULL) {
375 CFRelease(services);
376 services = NULL;
377 }
378
379 done :
380
381 if (service != NULL) CFRelease(service);
382 return;
383 }
384
385
386 __private_extern__
387 void
388 disable_service(int argc, char **argv)
389 {
390 SCNetworkServiceRef service;
391
392 if (argc == 1) {
393 service = _find_service(argv[0]);
394 } else {
395 if (net_service != NULL) {
396 service = net_service;
397 } else {
398 SCPrint(TRUE, stdout, CFSTR("service not selected\n"));
399 return;
400 }
401 }
402
403 if (service == NULL) {
404 return;
405 }
406
407 if (!SCNetworkServiceSetEnabled(service, FALSE)) {
408 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
409 return;
410 }
411
412 _prefs_changed = TRUE;
413 return;
414 }
415
416
417 __private_extern__
418 void
419 enable_service(int argc, char **argv)
420 {
421 SCNetworkServiceRef service;
422
423 if (argc == 1) {
424 service = _find_service(argv[0]);
425 } else {
426 if (net_service != NULL) {
427 service = net_service;
428 } else {
429 SCPrint(TRUE, stdout, CFSTR("service not selected\n"));
430 return;
431 }
432 }
433
434 if (service == NULL) {
435 return;
436 }
437
438 if (!SCNetworkServiceSetEnabled(service, TRUE)) {
439 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
440 return;
441 }
442
443 _prefs_changed = TRUE;
444 return;
445 }
446
447
448 __private_extern__
449 void
450 remove_service(int argc, char **argv)
451 {
452 SCNetworkServiceRef service = NULL;
453 CFStringRef serviceName;
454
455 if (argc == 1) {
456 service = _find_service(argv[0]);
457 } else {
458 if (net_service != NULL) {
459 service = net_service;
460 }
461 }
462
463 if (service == NULL) {
464 return;
465 }
466
467 CFRetain(service);
468
469 if (!SCNetworkServiceRemove(service)) {
470 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
471 goto done;
472 }
473
474 _prefs_changed = TRUE;
475
476 serviceName = SCNetworkServiceGetName(service);
477 if (serviceName != NULL) {
478 SCPrint(TRUE, stdout, CFSTR("service \"%@\" removed\n"), serviceName);
479 } else {
480 SCPrint(TRUE, stdout,
481 CFSTR("service ID \"%@\" removed\n"),
482 SCNetworkServiceGetServiceID(service));
483 }
484
485 if ((net_service != NULL) && CFEqual(service, net_service)) {
486 CFRelease(net_service);
487 net_service = NULL;
488 SCPrint(TRUE, stdout, CFSTR("& no service selected\n"));
489
490 if (protocols != NULL) {
491 CFRelease(protocols);
492 protocols = NULL;
493 }
494
495 if (net_protocol != NULL) {
496 CFRelease(net_protocol);
497 net_protocol = NULL;
498 SCPrint(TRUE, stdout, CFSTR("& no protocol selected\n"));
499 }
500
501 if (net_interface != NULL) {
502 CFRelease(net_interface);
503 net_interface = NULL;
504 SCPrint(TRUE, stdout, CFSTR("& no interface selected\n"));
505 }
506 }
507
508 if (services != NULL) {
509 CFRelease(services);
510 services = NULL;
511 }
512
513 done :
514
515 CFRelease(service);
516 return;
517 }
518
519
520 __private_extern__
521 void
522 select_service(int argc, char **argv)
523 {
524 SCNetworkInterfaceRef interface;
525 SCNetworkServiceRef service;
526 CFStringRef serviceName;
527
528 service = _find_service(argv[0]);
529
530 if (service == NULL) {
531 return;
532 }
533
534 if (net_service != NULL) CFRelease(net_service);
535 net_service = CFRetain(service);
536
537 serviceName = SCNetworkServiceGetName(service);
538 if (serviceName != NULL) {
539 SCPrint(TRUE, stdout, CFSTR("service \"%@\" selected\n"), serviceName);
540 } else {
541 SCPrint(TRUE, stdout,
542 CFSTR("service ID \"%@\" selected\n"),
543 SCNetworkServiceGetServiceID(service));
544 }
545
546 interface = SCNetworkServiceGetInterface(service);
547 if (interface != NULL) {
548 CFStringRef interfaceName;
549
550 if (net_interface != NULL) CFRelease(net_interface);
551 net_interface = CFRetain(interface);
552
553 interfaceName = SCNetworkInterfaceGetLocalizedDisplayName(interface);
554 if (interfaceName == NULL) {
555 interfaceName = SCNetworkInterfaceGetBSDName(interface);
556 }
557 if (interfaceName == NULL) {
558 interfaceName = SCNetworkInterfaceGetInterfaceType(interface);
559 }
560
561 SCPrint(TRUE, stdout,
562 CFSTR("& interface \"%@\" selected\n"),
563 interfaceName);
564 } else {
565 if (net_interface != NULL) {
566 CFRelease(net_interface);
567 net_interface = NULL;
568 SCPrint(TRUE, stdout, CFSTR("& no interface selected\n"));
569 }
570 }
571
572 if (protocols != NULL) {
573 CFRelease(protocols);
574 protocols = NULL;
575 }
576
577 if (net_protocol != NULL) {
578 CFRelease(net_protocol);
579 net_protocol = NULL;
580 SCPrint(TRUE, stdout, CFSTR("& no protocol selected\n"));
581 }
582
583 return;
584 }
585
586
587 __private_extern__
588 void
589 set_service(int argc, char **argv)
590 {
591 Boolean ok;
592
593 if (net_service == NULL) {
594 SCPrint(TRUE, stdout, CFSTR("service not selected\n"));
595 return;
596 }
597
598 if (argc < 1) {
599 SCPrint(TRUE, stdout, CFSTR("set what?\n"));
600 return;
601 }
602
603 while (argc > 0) {
604 char *command;
605
606 command = argv[0];
607 argv++;
608 argc--;
609
610 if (strcmp(command, "name") == 0) {
611 CFStringRef serviceName;
612
613 if (argc < 1) {
614 SCPrint(TRUE, stdout, CFSTR("name not specified\n"));
615 return;
616 }
617
618 serviceName = (strlen(argv[0]) > 0)
619 ? CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8)
620 : NULL;
621 argv++;
622 argc--;
623
624 ok = SCNetworkServiceSetName(net_service, serviceName);
625 if (serviceName != NULL) CFRelease(serviceName);
626 if (!ok) {
627 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
628 return;
629 }
630
631 _prefs_changed = TRUE;
632 } else if (strcmp(command, "order") == 0) {
633
634 char *end;
635 long newIndex;
636 CFIndex nServices;
637 char *str;
638 CFArrayRef services;
639
640 services = SCNetworkSetCopyServices(net_set);
641 nServices = CFArrayGetCount(services);
642 CFRelease(services);
643
644 if (argc < 1) {
645 SCPrint(TRUE, stdout, CFSTR("order not specified\n"));
646 return;
647 }
648
649 if (net_set == NULL) {
650 SCPrint(TRUE, stdout, CFSTR("set not selected\n"));
651 return;
652 }
653
654 str = argv[0];
655 argv++;
656 argc--;
657
658 errno = 0;
659 newIndex = strtol(str, &end, 10);
660 if ((*str != '\0') && (*end == '\0') && (errno == 0)) {
661 if ((newIndex > 0) && (newIndex <= nServices)) {
662 CFIndex curIndex;
663 CFMutableArrayRef newOrder;
664 CFArrayRef order;
665 CFStringRef serviceID = SCNetworkServiceGetServiceID(net_service);
666
667 order = SCNetworkSetGetServiceOrder(net_set);
668 if (order == NULL) {
669 newOrder = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
670 } else {
671 newOrder = CFArrayCreateMutableCopy(NULL, 0, order);
672 }
673
674 curIndex = CFArrayGetFirstIndexOfValue(newOrder,
675 CFRangeMake(0, CFArrayGetCount(newOrder)),
676 serviceID);
677 if (curIndex != kCFNotFound) {
678 CFArrayRemoveValueAtIndex(newOrder, curIndex);
679 }
680
681 if (newIndex <= CFArrayGetCount(newOrder)) {
682 CFArrayInsertValueAtIndex(newOrder, newIndex - 1, serviceID);
683 } else {
684 CFArrayAppendValue(newOrder, serviceID);
685 }
686
687 ok = SCNetworkSetSetServiceOrder(net_set, newOrder);
688 CFRelease(newOrder);
689 if (!ok) {
690 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
691 return;
692 }
693
694 _prefs_changed = TRUE;
695 } else {
696 SCPrint(TRUE, stdout, CFSTR("set order to what?\n"));
697 return;
698 }
699 } else {
700 SCPrint(TRUE, stdout, CFSTR("set what?\n"));
701 return;
702 }
703 } else if (strcmp(command, "rank") == 0) {
704 SCNetworkServicePrimaryRank rank = kSCNetworkServicePrimaryRankDefault;
705 SCNetworkServiceRef service = (argc < 2) ? net_service : NULL;
706
707 if (argc < 1) {
708 SCPrint(TRUE, stdout, CFSTR("rank not specified\n"));
709 return;
710 }
711
712 if (strlen(argv[0]) > 0) {
713 if (strcasecmp(argv[0], "Never") == 0) {
714 rank = kSCNetworkServicePrimaryRankNever;
715 } else if ((service != net_service) && (strcasecmp(argv[0], "First") == 0)) {
716 rank = kSCNetworkServicePrimaryRankFirst;
717 } else if ((service != net_service) && (strcasecmp(argv[0], "Last") == 0)) {
718 rank = kSCNetworkServicePrimaryRankLast;
719 } else {
720 SCPrint(TRUE, stdout, CFSTR("rank not valid\n"));
721 return;
722 }
723 }
724 argv++;
725 argc--;
726
727 if (service == NULL) {
728 CFStringRef serviceID;
729 SCDynamicStoreRef store;
730
731 store = SCDynamicStoreCreate(NULL,
732 CFSTR("scutil (set primary rank)"),
733 NULL,
734 NULL);
735 serviceID = SCNetworkServiceGetServiceID(net_service);
736 service = _SCNetworkServiceCopyActive(store, serviceID);
737 CFRelease(store);
738
739 argv++;
740 argc--;
741 }
742
743 ok = SCNetworkServiceSetPrimaryRank(service, rank);
744 if (service != net_service) CFRelease(service);
745 if (ok) {
746 if (service == net_service) _prefs_changed = TRUE;
747 } else {
748 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
749 return;
750 }
751 } else {
752 SCPrint(TRUE, stdout, CFSTR("set what?\n"));
753 }
754 }
755
756 return;
757 }
758
759
760 static void
761 __show_service_interface(SCNetworkServiceRef service, const char *prefix)
762 {
763 CFStringRef description;
764 SCNetworkInterfaceRef interface;
765
766 interface = SCNetworkServiceGetInterface(service);
767 if (interface == NULL) {
768 return;
769 }
770
771 description = _interface_description(interface);
772 SCPrint(TRUE, stdout, CFSTR("%s%@\n"), prefix, description);
773 CFRelease(description);
774
775 return;
776 }
777
778 static void
779 __show_service_protocols(SCNetworkServiceRef service, const char *prefix, Boolean skipEmpty)
780 {
781 CFIndex i;
782 CFIndex n;
783 CFArrayRef protocols;
784
785 protocols = SCNetworkServiceCopyProtocols(service);
786 if (protocols == NULL) {
787 return;
788 }
789
790 n = CFArrayGetCount(protocols);
791 if (n > 1) {
792 CFMutableArrayRef sorted;
793
794 sorted = CFArrayCreateMutableCopy(NULL, 0, protocols);
795 CFArraySortValues(sorted,
796 CFRangeMake(0, n),
797 _compare_protocols,
798 NULL);
799 CFRelease(protocols);
800 protocols = sorted;
801 }
802
803 for (i = 0; i < n; i++) {
804 CFStringRef description;
805 SCNetworkProtocolRef protocol;
806
807 protocol = CFArrayGetValueAtIndex(protocols, i);
808 description = _protocol_description(protocol, skipEmpty);
809 if (description != NULL) {
810 CFStringRef protocolType;
811
812 protocolType = SCNetworkProtocolGetProtocolType(protocol);
813 SCPrint(TRUE, stdout,
814 CFSTR("%s%@%*s : %@\n"),
815 prefix,
816 protocolType,
817 sizeof("Interface") - CFStringGetLength(protocolType) - 1,
818 "",
819 description);
820 CFRelease(description);
821 }
822 }
823
824 CFRelease(protocols);
825 return;
826 }
827
828
829 __private_extern__
830 void
831 show_service(int argc, char **argv)
832 {
833 SCNetworkInterfaceRef interface;
834 CFArrayRef protocols;
835 SCNetworkServiceRef service;
836 CFStringRef serviceName;
837 SCNetworkServicePrimaryRank serviceRank;
838
839 if (argc == 1) {
840 service = _find_service(argv[0]);
841 } else {
842 if (net_service != NULL) {
843 service = net_service;
844 } else {
845 SCPrint(TRUE, stdout, CFSTR("service not selected\n"));
846 return;
847 }
848 }
849
850 if (service == NULL) {
851 return;
852 }
853
854 SCPrint(TRUE, stdout, CFSTR("service id = %@\n"), SCNetworkServiceGetServiceID(service));
855
856 serviceName = SCNetworkServiceGetName(service);
857 SCPrint(TRUE, stdout, CFSTR("name = %@\n"),
858 (serviceName != NULL) ? serviceName : CFSTR(""));
859
860 serviceRank = SCNetworkServiceGetPrimaryRank(service);
861 switch (serviceRank) {
862 case kSCNetworkServicePrimaryRankDefault :
863 // nothing to report
864 break;
865 case kSCNetworkServicePrimaryRankFirst :
866 SCPrint(TRUE, stdout, CFSTR("primary rank = FIRST\n"));
867 break;
868 case kSCNetworkServicePrimaryRankLast :
869 SCPrint(TRUE, stdout, CFSTR("primary rank = LAST\n"));
870 break;
871 case kSCNetworkServicePrimaryRankNever :
872 SCPrint(TRUE, stdout, CFSTR("primary rank = NEVER\n"));
873 break;
874 default :
875 SCPrint(TRUE, stdout, CFSTR("primary rank = %d\n"), serviceRank);
876 break;
877 }
878
879 interface = SCNetworkServiceGetInterface(service);
880 if (interface != NULL) {
881 CFStringRef interfaceName;
882
883 interfaceName = SCNetworkInterfaceGetLocalizedDisplayName(interface);
884 if (interfaceName != NULL) {
885 CFRetain(interfaceName);
886 } else {
887 interfaceName = _interface_description(interface);
888 }
889 if (interfaceName != NULL) {
890 SCPrint(TRUE, stdout, CFSTR("interface = %@\n"), interfaceName);
891 CFRelease(interfaceName);
892 }
893 } else {
894 SCPrint(TRUE, stdout, CFSTR("\n No interface!\n\n"));
895 }
896
897 protocols = SCNetworkServiceCopyProtocols(service);
898 if (protocols != NULL) {
899 CFIndex n;
900
901 n = CFArrayGetCount(protocols);
902 if (n > 1) {
903 CFMutableArrayRef sorted;
904
905 sorted = CFArrayCreateMutableCopy(NULL, 0, protocols);
906 CFArraySortValues(sorted,
907 CFRangeMake(0, n),
908 _compare_protocols,
909 NULL);
910 CFRelease(protocols);
911 protocols = sorted;
912 }
913
914 if (n > 0) {
915 CFIndex i;
916
917 SCPrint(TRUE, stdout, CFSTR("configured protocols = "));
918 for (i = 0; i < n; i++) {
919 SCNetworkProtocolRef protocol;
920
921 protocol = CFArrayGetValueAtIndex(protocols, i);
922 SCPrint(TRUE, stdout, CFSTR("%s%@"),
923 (i == 0) ? "" : ", ",
924 SCNetworkProtocolGetProtocolType(protocol));
925 }
926 SCPrint(TRUE, stdout, CFSTR("\n"));
927
928 __show_service_protocols(service, " ", FALSE);
929 } else {
930 SCPrint(TRUE, stdout, CFSTR("no configured protocols\n"));
931 }
932
933 CFRelease(protocols);
934 }
935
936 if (_sc_debug) {
937 SCPrint(TRUE, stdout, CFSTR("\n%@\n"), service);
938 }
939
940 return;
941 }
942
943
944 __private_extern__
945 void
946 show_services(int argc, char **argv)
947 {
948 CFIndex i;
949 CFIndex n;
950
951 if (prefs == NULL) {
952 SCPrint(TRUE, stdout, CFSTR("network configuration not open\n"));
953 return;
954 }
955
956 if (argc == 1) {
957 if (services != NULL) CFRelease(services);
958 services = SCNetworkServiceCopyAll(prefs);
959 } else {
960 if (net_set == NULL) {
961 SCPrint(TRUE, stdout, CFSTR("set not selected\n"));
962 return;
963 }
964
965 if (services != NULL) CFRelease(services);
966 services = SCNetworkSetCopyServices(net_set);
967 n = (services != NULL) ? CFArrayGetCount(services) : 0;
968 if (n > 1) {
969 CFArrayRef order;
970 CFMutableArrayRef sorted;
971
972 order = SCNetworkSetGetServiceOrder(net_set);
973 sorted = CFArrayCreateMutableCopy(NULL, 0, services);
974 CFArraySortValues(sorted,
975 CFRangeMake(0, CFArrayGetCount(sorted)),
976 _compare_services,
977 (void *)order);
978 CFRelease(services);
979 services = sorted;
980 }
981 }
982
983 if (services == NULL) {
984 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
985 return;
986 }
987
988 n = CFArrayGetCount(services);
989 for (i = 0; i < n; i++) {
990 SCNetworkServiceRef service;
991 CFStringRef serviceName;
992 CFStringRef serviceID;
993
994 service = CFArrayGetValueAtIndex(services, i);
995 serviceID = SCNetworkServiceGetServiceID(service);
996 serviceName = SCNetworkServiceGetName(service);
997 if (serviceName == NULL) serviceName = CFSTR("");
998
999 SCPrint(TRUE, stdout, CFSTR("%c%2d: %@%-*s (%@)%s\n"),
1000 ((net_service != NULL) && CFEqual(service, net_service)) ? '>' : ' ',
1001 i + 1,
1002 serviceName,
1003 30 - CFStringGetLength(serviceName),
1004 " ",
1005 serviceID,
1006 SCNetworkServiceGetEnabled(service) ? "" : " *DISABLED*");
1007
1008 __show_service_interface(service, " Interface : ");
1009 __show_service_protocols(service, " ", TRUE);
1010 }
1011
1012 return;
1013 }