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