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