]>
Commit | Line | Data |
---|---|---|
dbf6a266 | 1 | /* |
edebe297 | 2 | * Copyright (c) 2004-2007 Apple Inc. All rights reserved. |
dbf6a266 A |
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" | |
edebe297 | 37 | #include "prefs.h" |
dbf6a266 A |
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; | |
dbf6a266 A |
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 | interface = _find_interface(argv[0]); | |
270 | argv++; | |
271 | argc--; | |
272 | } | |
273 | ||
274 | if (interface == NULL) { | |
275 | return; | |
276 | } | |
277 | ||
278 | supported = SCNetworkInterfaceGetSupportedProtocolTypes(interface); | |
279 | if (supported == NULL) { | |
280 | SCPrint(TRUE, stdout, CFSTR("no network protocols are supported over this interface\n")); | |
281 | return; | |
282 | } | |
283 | ||
284 | service = SCNetworkServiceCreate(prefs, interface); | |
285 | if (service == NULL) { | |
286 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); | |
287 | goto done; | |
288 | } | |
289 | ||
290 | if ((argc > 0) && (strlen(argv[0]) > 0)) { | |
291 | Boolean ok; | |
292 | ||
293 | serviceName = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8); | |
294 | argv++; | |
295 | argc--; | |
296 | ||
297 | ok = SCNetworkServiceSetName(service, serviceName); | |
298 | CFRelease(serviceName); | |
299 | if (!ok) { | |
edebe297 A |
300 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); |
301 | (void)SCNetworkServiceRemove(service); | |
302 | goto done; | |
dbf6a266 A |
303 | } |
304 | } | |
305 | ||
306 | ok = SCNetworkSetAddService(net_set, service); | |
307 | if (!ok) { | |
edebe297 | 308 | SCPrint(TRUE, stdout, CFSTR("service not created: %s\n"), SCErrorString(SCError())); |
dbf6a266 A |
309 | (void)SCNetworkServiceRemove(service); |
310 | goto done; | |
311 | } | |
312 | ||
edebe297 | 313 | _prefs_changed = TRUE; |
dbf6a266 A |
314 | |
315 | if (net_service != NULL) CFRelease(net_service); | |
316 | net_service = CFRetain(service); | |
317 | ||
318 | serviceName = SCNetworkServiceGetName(service); | |
319 | if (serviceName != NULL) { | |
320 | SCPrint(TRUE, stdout, | |
321 | CFSTR("service \"%@\" (%@) created and selected\n"), | |
322 | serviceName, | |
323 | SCNetworkServiceGetServiceID(service)); | |
324 | } else { | |
325 | SCPrint(TRUE, stdout, | |
326 | CFSTR("service ID \"%@\" created and selected\n"), | |
327 | SCNetworkServiceGetServiceID(service)); | |
328 | } | |
329 | ||
330 | setName = SCNetworkSetGetName(net_set); | |
331 | if (setName != NULL) { | |
332 | SCPrint(TRUE, stdout, CFSTR("& added to set \"%@\"\n"), setName); | |
333 | } else { | |
334 | SCPrint(TRUE, stdout, CFSTR("& added to set ID \"%@\"\n"), | |
335 | SCNetworkSetGetSetID(net_set)); | |
336 | } | |
337 | ||
338 | if (net_interface != NULL) CFRelease(net_interface); | |
339 | net_interface = CFRetain(interface); | |
340 | ||
341 | interfaceName = SCNetworkInterfaceGetLocalizedDisplayName(interface); | |
342 | if (interfaceName == NULL) { | |
343 | interfaceName = SCNetworkInterfaceGetBSDName(interface); | |
344 | } | |
345 | if (interfaceName == NULL) { | |
346 | interfaceName = SCNetworkInterfaceGetInterfaceType(interface); | |
347 | } | |
348 | ||
349 | SCPrint(TRUE, stdout, | |
350 | CFSTR("& interface \"%@\" selected\n"), | |
351 | interfaceName); | |
352 | ||
353 | if (protocols != NULL) { | |
354 | CFRelease(protocols); | |
355 | protocols = NULL; | |
356 | } | |
357 | ||
358 | if (net_protocol != NULL) { | |
359 | CFRelease(net_protocol); | |
360 | net_protocol = NULL; | |
361 | SCPrint(TRUE, stdout, CFSTR("& no protocol selected\n")); | |
362 | } | |
363 | ||
364 | if (services != NULL) { | |
365 | CFRelease(services); | |
366 | services = NULL; | |
367 | } | |
368 | ||
369 | done : | |
370 | ||
371 | if (service != NULL) CFRelease(service); | |
372 | return; | |
373 | } | |
374 | ||
375 | ||
376 | __private_extern__ | |
377 | void | |
378 | disable_service(int argc, char **argv) | |
379 | { | |
380 | SCNetworkServiceRef service; | |
381 | ||
382 | if (argc == 1) { | |
383 | service = _find_service(argv[0]); | |
384 | } else { | |
385 | if (net_service != NULL) { | |
386 | service = net_service; | |
387 | } else { | |
388 | SCPrint(TRUE, stdout, CFSTR("service not selected\n")); | |
389 | return; | |
390 | } | |
391 | } | |
392 | ||
393 | if (service == NULL) { | |
394 | return; | |
395 | } | |
396 | ||
397 | if (!SCNetworkServiceSetEnabled(service, FALSE)) { | |
398 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); | |
399 | return; | |
400 | } | |
401 | ||
edebe297 | 402 | _prefs_changed = TRUE; |
dbf6a266 A |
403 | return; |
404 | } | |
405 | ||
406 | ||
407 | __private_extern__ | |
408 | void | |
409 | enable_service(int argc, char **argv) | |
410 | { | |
411 | SCNetworkServiceRef service; | |
412 | ||
413 | if (argc == 1) { | |
414 | service = _find_service(argv[0]); | |
415 | } else { | |
416 | if (net_service != NULL) { | |
417 | service = net_service; | |
418 | } else { | |
419 | SCPrint(TRUE, stdout, CFSTR("service not selected\n")); | |
420 | return; | |
421 | } | |
422 | } | |
423 | ||
424 | if (service == NULL) { | |
425 | return; | |
426 | } | |
427 | ||
428 | if (!SCNetworkServiceSetEnabled(service, TRUE)) { | |
429 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); | |
430 | return; | |
431 | } | |
432 | ||
edebe297 | 433 | _prefs_changed = TRUE; |
dbf6a266 A |
434 | return; |
435 | } | |
436 | ||
437 | ||
438 | __private_extern__ | |
439 | void | |
440 | remove_service(int argc, char **argv) | |
441 | { | |
442 | SCNetworkServiceRef service = NULL; | |
443 | CFStringRef serviceName; | |
444 | ||
445 | if (argc == 1) { | |
446 | service = _find_service(argv[0]); | |
447 | } else { | |
448 | if (net_service != NULL) { | |
449 | service = net_service; | |
450 | } | |
451 | } | |
452 | ||
453 | if (service == NULL) { | |
454 | return; | |
455 | } | |
456 | ||
457 | CFRetain(service); | |
458 | ||
459 | if (!SCNetworkServiceRemove(service)) { | |
460 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); | |
461 | goto done; | |
462 | } | |
463 | ||
edebe297 | 464 | _prefs_changed = TRUE; |
dbf6a266 A |
465 | |
466 | serviceName = SCNetworkServiceGetName(service); | |
467 | if (serviceName != NULL) { | |
468 | SCPrint(TRUE, stdout, CFSTR("service \"%@\" removed\n"), serviceName); | |
469 | } else { | |
470 | SCPrint(TRUE, stdout, | |
471 | CFSTR("service ID \"%@\" removed\n"), | |
472 | SCNetworkServiceGetServiceID(service)); | |
473 | } | |
474 | ||
475 | if ((net_service != NULL) && CFEqual(service, net_service)) { | |
476 | CFRelease(net_service); | |
477 | net_service = NULL; | |
478 | SCPrint(TRUE, stdout, CFSTR("& no service selected\n")); | |
479 | ||
480 | if (protocols != NULL) { | |
481 | CFRelease(protocols); | |
482 | protocols = NULL; | |
483 | } | |
484 | ||
485 | if (net_protocol != NULL) { | |
486 | CFRelease(net_protocol); | |
487 | net_protocol = NULL; | |
488 | SCPrint(TRUE, stdout, CFSTR("& no protocol selected\n")); | |
489 | } | |
490 | ||
491 | if (net_interface != NULL) { | |
492 | CFRelease(net_interface); | |
493 | net_interface = NULL; | |
494 | SCPrint(TRUE, stdout, CFSTR("& no interface selected\n")); | |
495 | } | |
496 | } | |
497 | ||
498 | if (services != NULL) { | |
499 | CFRelease(services); | |
500 | services = NULL; | |
501 | } | |
502 | ||
503 | done : | |
504 | ||
505 | CFRelease(service); | |
506 | return; | |
507 | } | |
508 | ||
509 | ||
510 | __private_extern__ | |
511 | void | |
512 | select_service(int argc, char **argv) | |
513 | { | |
514 | SCNetworkInterfaceRef interface; | |
515 | SCNetworkServiceRef service; | |
516 | CFStringRef serviceName; | |
517 | ||
518 | service = _find_service(argv[0]); | |
519 | ||
520 | if (service == NULL) { | |
521 | return; | |
522 | } | |
523 | ||
524 | if (net_service != NULL) CFRelease(net_service); | |
525 | net_service = CFRetain(service); | |
526 | ||
527 | serviceName = SCNetworkServiceGetName(service); | |
528 | if (serviceName != NULL) { | |
529 | SCPrint(TRUE, stdout, CFSTR("service \"%@\" selected\n"), serviceName); | |
530 | } else { | |
531 | SCPrint(TRUE, stdout, | |
532 | CFSTR("service ID \"%@\" selected\n"), | |
533 | SCNetworkServiceGetServiceID(service)); | |
534 | } | |
535 | ||
536 | interface = SCNetworkServiceGetInterface(service); | |
537 | if (interface != NULL) { | |
538 | CFStringRef interfaceName; | |
539 | ||
540 | if (net_interface != NULL) CFRelease(net_interface); | |
541 | net_interface = CFRetain(interface); | |
542 | ||
543 | interfaceName = SCNetworkInterfaceGetLocalizedDisplayName(interface); | |
544 | if (interfaceName == NULL) { | |
545 | interfaceName = SCNetworkInterfaceGetBSDName(interface); | |
546 | } | |
547 | if (interfaceName == NULL) { | |
548 | interfaceName = SCNetworkInterfaceGetInterfaceType(interface); | |
549 | } | |
550 | ||
551 | SCPrint(TRUE, stdout, | |
552 | CFSTR("& interface \"%@\" selected\n"), | |
553 | interfaceName); | |
554 | } else { | |
555 | if (net_interface != NULL) { | |
556 | CFRelease(net_interface); | |
557 | net_interface = NULL; | |
558 | SCPrint(TRUE, stdout, CFSTR("& no interface selected\n")); | |
559 | } | |
560 | } | |
561 | ||
562 | if (protocols != NULL) { | |
563 | CFRelease(protocols); | |
564 | protocols = NULL; | |
565 | } | |
566 | ||
567 | if (net_protocol != NULL) { | |
568 | CFRelease(net_protocol); | |
569 | net_protocol = NULL; | |
570 | SCPrint(TRUE, stdout, CFSTR("& no protocol selected\n")); | |
571 | } | |
572 | ||
573 | return; | |
574 | } | |
575 | ||
576 | ||
577 | __private_extern__ | |
578 | void | |
579 | set_service(int argc, char **argv) | |
580 | { | |
581 | Boolean ok; | |
582 | ||
583 | if (net_service == NULL) { | |
584 | SCPrint(TRUE, stdout, CFSTR("service not selected\n")); | |
585 | return; | |
586 | } | |
587 | ||
588 | if (argc < 1) { | |
589 | SCPrint(TRUE, stdout, CFSTR("set what?\n")); | |
590 | return; | |
591 | } | |
592 | ||
593 | while (argc > 0) { | |
594 | char *command; | |
595 | ||
596 | command = argv[0]; | |
597 | argv++; | |
598 | argc--; | |
599 | ||
600 | if (strcmp(command, "name") == 0) { | |
601 | CFStringRef serviceName; | |
602 | ||
603 | if (argc < 1) { | |
604 | SCPrint(TRUE, stdout, CFSTR("name not specified\n")); | |
605 | return; | |
606 | } | |
607 | ||
608 | serviceName = (strlen(argv[0]) > 0) | |
609 | ? CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8) | |
610 | : NULL; | |
611 | argv++; | |
612 | argc--; | |
613 | ||
614 | ok = SCNetworkServiceSetName(net_service, serviceName); | |
615 | if (serviceName != NULL) CFRelease(serviceName); | |
616 | if (!ok) { | |
617 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); | |
618 | return; | |
619 | } | |
620 | ||
edebe297 | 621 | _prefs_changed = TRUE; |
dbf6a266 A |
622 | } else if (strcmp(command, "order") == 0) { |
623 | ||
624 | char *end; | |
625 | long newIndex; | |
626 | CFIndex nServices; | |
627 | char *str; | |
628 | CFArrayRef services; | |
629 | ||
630 | services = SCNetworkSetCopyServices(net_set); | |
631 | nServices = CFArrayGetCount(services); | |
632 | CFRelease(services); | |
633 | ||
634 | if (argc < 1) { | |
635 | SCPrint(TRUE, stdout, CFSTR("order not specified\n")); | |
636 | return; | |
637 | } | |
638 | ||
639 | if (net_set == NULL) { | |
640 | SCPrint(TRUE, stdout, CFSTR("set not selected\n")); | |
641 | return; | |
642 | } | |
643 | ||
644 | str = argv[0]; | |
645 | argv++; | |
646 | argc--; | |
647 | ||
648 | errno = 0; | |
649 | newIndex = strtol(str, &end, 10); | |
650 | if ((*str != '\0') && (*end == '\0') && (errno == 0)) { | |
651 | if ((newIndex > 0) && (newIndex <= nServices)) { | |
652 | CFIndex curIndex; | |
653 | CFMutableArrayRef newOrder; | |
654 | CFArrayRef order; | |
655 | CFStringRef serviceID = SCNetworkServiceGetServiceID(net_service); | |
656 | ||
657 | order = SCNetworkSetGetServiceOrder(net_set); | |
658 | if (order == NULL) { | |
659 | newOrder = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); | |
660 | } else { | |
661 | newOrder = CFArrayCreateMutableCopy(NULL, 0, order); | |
662 | } | |
663 | ||
664 | curIndex = CFArrayGetFirstIndexOfValue(newOrder, | |
665 | CFRangeMake(0, CFArrayGetCount(newOrder)), | |
666 | serviceID); | |
667 | if (curIndex != kCFNotFound) { | |
668 | CFArrayRemoveValueAtIndex(newOrder, curIndex); | |
669 | } | |
670 | ||
671 | if (newIndex <= CFArrayGetCount(newOrder)) { | |
672 | CFArrayInsertValueAtIndex(newOrder, newIndex - 1, serviceID); | |
673 | } else { | |
674 | CFArrayAppendValue(newOrder, serviceID); | |
675 | } | |
676 | ||
677 | ok = SCNetworkSetSetServiceOrder(net_set, newOrder); | |
678 | CFRelease(newOrder); | |
679 | if (!ok) { | |
680 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); | |
681 | return; | |
682 | } | |
683 | ||
edebe297 | 684 | _prefs_changed = TRUE; |
dbf6a266 A |
685 | } else { |
686 | SCPrint(TRUE, stdout, CFSTR("set order to what?\n")); | |
687 | return; | |
688 | } | |
689 | } else { | |
690 | SCPrint(TRUE, stdout, CFSTR("set what?\n")); | |
691 | return; | |
692 | } | |
693 | } else { | |
694 | SCPrint(TRUE, stdout, CFSTR("set what?\n")); | |
695 | } | |
696 | } | |
697 | ||
698 | return; | |
699 | } | |
700 | ||
701 | ||
702 | static void | |
703 | __show_service_interface(SCNetworkServiceRef service, const char *prefix) | |
704 | { | |
705 | CFStringRef description; | |
706 | SCNetworkInterfaceRef interface; | |
707 | ||
708 | interface = SCNetworkServiceGetInterface(service); | |
709 | if (interface == NULL) { | |
710 | return; | |
711 | } | |
712 | ||
713 | description = _interface_description(interface); | |
714 | SCPrint(TRUE, stdout, CFSTR("%s%@\n"), prefix, description); | |
715 | CFRelease(description); | |
716 | ||
717 | return; | |
718 | } | |
719 | ||
720 | static void | |
721 | __show_service_protocols(SCNetworkServiceRef service, const char *prefix, Boolean skipEmpty) | |
722 | { | |
723 | CFIndex i; | |
724 | CFIndex n; | |
725 | CFArrayRef protocols; | |
726 | ||
727 | protocols = SCNetworkServiceCopyProtocols(service); | |
728 | if (protocols == NULL) { | |
729 | return; | |
730 | } | |
731 | ||
732 | n = CFArrayGetCount(protocols); | |
733 | if (n > 1) { | |
734 | CFMutableArrayRef sorted; | |
735 | ||
736 | sorted = CFArrayCreateMutableCopy(NULL, 0, protocols); | |
737 | CFArraySortValues(sorted, | |
738 | CFRangeMake(0, n), | |
739 | _compare_protocols, | |
740 | NULL); | |
741 | CFRelease(protocols); | |
742 | protocols = sorted; | |
743 | } | |
744 | ||
745 | for (i = 0; i < n; i++) { | |
746 | CFStringRef description; | |
747 | SCNetworkProtocolRef protocol; | |
748 | ||
749 | protocol = CFArrayGetValueAtIndex(protocols, i); | |
750 | description = _protocol_description(protocol, skipEmpty); | |
751 | if (description != NULL) { | |
752 | CFStringRef protocolType; | |
753 | ||
754 | protocolType = SCNetworkProtocolGetProtocolType(protocol); | |
755 | SCPrint(TRUE, stdout, | |
756 | CFSTR("%s%@%*s : %@\n"), | |
757 | prefix, | |
758 | protocolType, | |
759 | sizeof("Interface") - CFStringGetLength(protocolType) - 1, | |
760 | "", | |
761 | description); | |
762 | CFRelease(description); | |
763 | } | |
764 | } | |
765 | ||
766 | CFRelease(protocols); | |
767 | return; | |
768 | } | |
769 | ||
770 | ||
771 | __private_extern__ | |
772 | void | |
773 | show_service(int argc, char **argv) | |
774 | { | |
775 | SCNetworkInterfaceRef interface; | |
776 | CFArrayRef protocols; | |
777 | SCNetworkServiceRef service; | |
778 | CFStringRef serviceName; | |
779 | ||
780 | if (argc == 1) { | |
781 | service = _find_service(argv[0]); | |
782 | } else { | |
783 | if (net_service != NULL) { | |
784 | service = net_service; | |
785 | } else { | |
786 | SCPrint(TRUE, stdout, CFSTR("service not selected\n")); | |
787 | return; | |
788 | } | |
789 | } | |
790 | ||
791 | if (service == NULL) { | |
792 | return; | |
793 | } | |
794 | ||
795 | SCPrint(TRUE, stdout, CFSTR("service id = %@\n"), SCNetworkServiceGetServiceID(service)); | |
796 | ||
797 | serviceName = SCNetworkServiceGetName(service); | |
798 | SCPrint(TRUE, stdout, CFSTR("name = %@\n"), | |
799 | (serviceName != NULL) ? serviceName : CFSTR("")); | |
800 | ||
801 | interface = SCNetworkServiceGetInterface(service); | |
802 | if (interface != NULL) { | |
803 | CFStringRef interfaceName; | |
804 | ||
805 | interfaceName = SCNetworkInterfaceGetLocalizedDisplayName(interface); | |
806 | if (interfaceName != NULL) { | |
807 | CFRetain(interfaceName); | |
808 | } else { | |
809 | interfaceName = _interface_description(interface); | |
810 | } | |
811 | if (interfaceName != NULL) { | |
812 | SCPrint(TRUE, stdout, CFSTR("interface = %@\n"), interfaceName); | |
813 | CFRelease(interfaceName); | |
814 | } | |
815 | } else { | |
816 | SCPrint(TRUE, stdout, CFSTR("\n No interface!\n\n")); | |
817 | } | |
818 | ||
819 | protocols = SCNetworkServiceCopyProtocols(service); | |
820 | if (protocols != NULL) { | |
821 | CFIndex n; | |
822 | ||
823 | n = CFArrayGetCount(protocols); | |
824 | if (n > 1) { | |
825 | CFMutableArrayRef sorted; | |
826 | ||
827 | sorted = CFArrayCreateMutableCopy(NULL, 0, protocols); | |
828 | CFArraySortValues(sorted, | |
829 | CFRangeMake(0, n), | |
830 | _compare_protocols, | |
831 | NULL); | |
832 | CFRelease(protocols); | |
833 | protocols = sorted; | |
834 | } | |
835 | ||
836 | if (n > 0) { | |
837 | CFIndex i; | |
838 | ||
839 | SCPrint(TRUE, stdout, CFSTR("configured protocols = ")); | |
840 | for (i = 0; i < n; i++) { | |
841 | SCNetworkProtocolRef protocol; | |
842 | ||
843 | protocol = CFArrayGetValueAtIndex(protocols, i); | |
844 | SCPrint(TRUE, stdout, CFSTR("%s%@"), | |
845 | (i == 0) ? "" : ", ", | |
846 | SCNetworkProtocolGetProtocolType(protocol)); | |
847 | } | |
848 | SCPrint(TRUE, stdout, CFSTR("\n")); | |
849 | ||
850 | __show_service_protocols(service, " ", FALSE); | |
851 | } else { | |
852 | SCPrint(TRUE, stdout, CFSTR("no configured protocols\n")); | |
853 | } | |
854 | ||
855 | CFRelease(protocols); | |
856 | } | |
857 | ||
858 | if (_sc_debug) { | |
859 | SCPrint(TRUE, stdout, CFSTR("\n%@\n"), service); | |
860 | } | |
861 | ||
862 | return; | |
863 | } | |
864 | ||
865 | ||
866 | __private_extern__ | |
867 | void | |
868 | show_services(int argc, char **argv) | |
869 | { | |
870 | CFIndex i; | |
871 | CFIndex n; | |
872 | ||
873 | if (prefs == NULL) { | |
874 | SCPrint(TRUE, stdout, CFSTR("network configuration not open\n")); | |
875 | return; | |
876 | } | |
877 | ||
878 | if (argc == 1) { | |
879 | if (services != NULL) CFRelease(services); | |
880 | services = SCNetworkServiceCopyAll(prefs); | |
881 | } else { | |
882 | if (net_set == NULL) { | |
883 | SCPrint(TRUE, stdout, CFSTR("set not selected\n")); | |
884 | return; | |
885 | } | |
886 | ||
887 | if (services != NULL) CFRelease(services); | |
888 | services = SCNetworkSetCopyServices(net_set); | |
889 | n = (services != NULL) ? CFArrayGetCount(services) : 0; | |
890 | if (n > 1) { | |
891 | CFArrayRef order; | |
892 | CFMutableArrayRef sorted; | |
893 | ||
894 | order = SCNetworkSetGetServiceOrder(net_set); | |
895 | sorted = CFArrayCreateMutableCopy(NULL, 0, services); | |
896 | CFArraySortValues(sorted, | |
897 | CFRangeMake(0, CFArrayGetCount(sorted)), | |
898 | _compare_services, | |
899 | (void *)order); | |
900 | CFRelease(services); | |
901 | services = sorted; | |
902 | } | |
903 | } | |
904 | ||
905 | if (services == NULL) { | |
906 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); | |
907 | return; | |
908 | } | |
909 | ||
910 | n = CFArrayGetCount(services); | |
911 | for (i = 0; i < n; i++) { | |
912 | SCNetworkServiceRef service; | |
913 | CFStringRef serviceName; | |
914 | CFStringRef serviceID; | |
915 | ||
916 | service = CFArrayGetValueAtIndex(services, i); | |
917 | serviceID = SCNetworkServiceGetServiceID(service); | |
918 | serviceName = SCNetworkServiceGetName(service); | |
919 | if (serviceName == NULL) serviceName = CFSTR(""); | |
920 | ||
921 | SCPrint(TRUE, stdout, CFSTR("%c%2d: %@%-*s (%@)%s\n"), | |
922 | ((net_service != NULL) && CFEqual(service, net_service)) ? '>' : ' ', | |
923 | i + 1, | |
924 | serviceName, | |
925 | 30 - CFStringGetLength(serviceName), | |
926 | " ", | |
927 | serviceID, | |
928 | SCNetworkServiceGetEnabled(service) ? "" : " *DISABLED*"); | |
929 | ||
930 | __show_service_interface(service, " Interface : "); | |
931 | __show_service_protocols(service, " ", TRUE); | |
932 | } | |
933 | ||
934 | return; | |
935 | } |