2 * Copyright (c) 2004-2009, 2011, 2014, 2017 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * August 5, 2004 Allan Nathanson <ajn@apple.com>
34 #include "net_protocol.h"
37 #include <sys/types.h>
38 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
44 /* -------------------- */
48 __copyIPv4Address(const char *arg
)
51 struct sockaddr_in sin
;
53 if (_SC_string_to_sockaddr(arg
, AF_INET
, (void *)&sin
, sizeof(sin
)) == NULL
) {
57 _SC_sockaddr_to_string((struct sockaddr
*)&sin
, buf
, sizeof(buf
));
58 return CFStringCreateWithCString(NULL
, buf
, kCFStringEncodingUTF8
);
63 __copyIPv6Address(const char *arg
)
66 struct sockaddr_in6 sin6
;
68 if (_SC_string_to_sockaddr(arg
, AF_INET6
, (void *)&sin6
, sizeof(sin6
)) == NULL
) {
72 _SC_sockaddr_to_string((struct sockaddr
*)&sin6
, buf
, sizeof(buf
));
73 return CFStringCreateWithCString(NULL
, buf
, kCFStringEncodingUTF8
);
77 /* -------------------- */
80 static SCNetworkProtocolRef
81 _find_protocol(char *match
)
83 Boolean allowIndex
= TRUE
;
86 CFStringRef select_name
= NULL
;
87 SCNetworkProtocolRef selected
= NULL
;
89 if (protocols
== NULL
) {
90 if (net_service
== NULL
) {
91 SCPrint(TRUE
, stdout
, CFSTR("network service not selected\n"));
95 protocols
= SCNetworkServiceCopyProtocols(net_service
);
96 if (protocols
== NULL
) {
97 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
101 n
= CFArrayGetCount(protocols
);
103 CFMutableArrayRef sorted
;
105 sorted
= CFArrayCreateMutableCopy(NULL
, 0, protocols
);
106 CFArraySortValues(sorted
,
108 _SCNetworkProtocolCompare
,
110 CFRelease(protocols
);
117 // try to select the protocol by its protocol type
119 select_name
= CFStringCreateWithCString(NULL
, match
, kCFStringEncodingUTF8
);
121 n
= CFArrayGetCount(protocols
);
122 for (i
= 0; i
< n
; i
++) {
123 SCNetworkProtocolRef protocol
;
126 protocol
= CFArrayGetValueAtIndex(protocols
, i
);
127 type
= SCNetworkProtocolGetProtocolType(protocol
);
128 if (CFStringCompare(select_name
, type
, kCFCompareCaseInsensitive
) == kCFCompareEqualTo
) {
139 // try to select the protocol by its index
142 val
= strtol(str
, &end
, 10);
143 if ((*str
!= '\0') && (*end
== '\0') && (errno
== 0)) {
144 if ((val
> 0) && (val
<= n
)) {
145 selected
= CFArrayGetValueAtIndex(protocols
, val
- 1);
150 if (selected
!= NULL
) {
154 SCPrint(TRUE
, stdout
, CFSTR("no match, which protocol?\n"));
158 if (select_name
!= NULL
) CFRelease(select_name
);
163 /* -------------------- */
168 create_protocol(int argc
, char **argv
)
170 SCNetworkInterfaceRef interface
;
171 CFStringRef protocolType
;
173 if ((argc
< 1) || (strlen(argv
[0]) == 0)) {
174 SCPrint(TRUE
, stdout
, CFSTR("what protocol type?\n"));
178 if (net_service
== NULL
) {
179 SCPrint(TRUE
, stdout
, CFSTR("network service not selected\n"));
183 protocolType
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
185 interface
= SCNetworkServiceGetInterface(net_service
);
186 if (interface
!= NULL
) {
187 CFArrayRef supported
;
191 supported
= SCNetworkInterfaceGetSupportedProtocolTypes(interface
);
192 n
= (supported
!= NULL
) ? CFArrayGetCount(supported
) : 0;
193 for (i
= 0; i
< n
; i
++) {
194 CFStringRef supportedType
;
196 supportedType
= CFArrayGetValueAtIndex(supported
, i
);
197 if (CFStringCompare(protocolType
,
199 kCFCompareCaseInsensitive
) == kCFCompareEqualTo
) {
200 CFRelease(protocolType
);
201 protocolType
= CFRetain(supportedType
);
207 if (!SCNetworkServiceAddProtocolType(net_service
, protocolType
)) {
208 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
212 _prefs_changed
= TRUE
;
214 if (protocols
!= NULL
) {
215 CFRelease(protocols
);
219 if (net_protocol
!= NULL
) CFRelease(net_protocol
);
220 // net_protocol = NULL;
222 net_protocol
= SCNetworkServiceCopyProtocol(net_service
, protocolType
);
223 if (net_protocol
== NULL
) {
224 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
228 SCPrint(TRUE
, stdout
,
229 CFSTR("protocol \"%@\" selected\n"),
234 CFRelease(protocolType
);
239 /* -------------------- */
244 disable_protocol(int argc
, char **argv
)
246 SCNetworkProtocolRef protocol
= NULL
;
249 protocol
= _find_protocol(argv
[0]);
251 if (net_protocol
!= NULL
) {
252 protocol
= net_protocol
;
254 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
259 if (protocol
== NULL
) {
263 if (!SCNetworkProtocolSetEnabled(protocol
, FALSE
)) {
264 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
268 _prefs_changed
= TRUE
;
274 /* -------------------- */
279 enable_protocol(int argc
, char **argv
)
281 SCNetworkProtocolRef protocol
= NULL
;
284 protocol
= _find_protocol(argv
[0]);
286 if (net_protocol
!= NULL
) {
287 protocol
= net_protocol
;
289 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
294 if (protocol
== NULL
) {
298 if (!SCNetworkProtocolSetEnabled(protocol
, TRUE
)) {
299 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
303 _prefs_changed
= TRUE
;
309 /* -------------------- */
314 remove_protocol(int argc
, char **argv
)
316 SCNetworkProtocolRef protocol
= NULL
;
317 CFStringRef protocolType
;
320 protocol
= _find_protocol(argv
[0]);
322 if (net_protocol
!= NULL
) {
323 protocol
= net_protocol
;
325 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
330 if (protocol
== NULL
) {
336 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
337 if (!SCNetworkServiceRemoveProtocolType(net_service
, protocolType
)) {
338 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
342 _prefs_changed
= TRUE
;
344 SCPrint(TRUE
, stdout
,
345 CFSTR("protocol \"%@\" removed\n"),
348 if ((net_protocol
!= NULL
) && CFEqual(protocol
, net_protocol
)) {
349 CFRelease(net_protocol
);
351 SCPrint(TRUE
, stdout
, CFSTR("& no protocol selected\n"));
354 if (protocols
!= NULL
) {
355 CFRelease(protocols
);
366 /* -------------------- */
371 select_protocol(int argc
, char **argv
)
374 SCNetworkProtocolRef protocol
;
376 protocol
= _find_protocol(argv
[0]);
378 if (protocol
== NULL
) {
382 if (net_protocol
!= NULL
) CFRelease(net_protocol
);
383 net_protocol
= CFRetain(protocol
);
385 SCPrint(TRUE
, stdout
,
386 CFSTR("protocol \"%@\" selected\n"),
387 SCNetworkProtocolGetProtocolType(protocol
));
397 static CF_RETURNS_RETAINED CFStringRef
398 __cleanupDomainName(CFStringRef domain
)
400 CFMutableStringRef newDomain
;
402 newDomain
= CFStringCreateMutableCopy(NULL
, 0, domain
);
403 CFStringTrimWhitespace(newDomain
);
404 CFStringTrim(newDomain
, CFSTR("."));
405 if (CFStringGetLength(newDomain
) == 0) {
406 CFRelease(newDomain
);
415 __doDNSDomain(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
417 #pragma unused(description)
420 SCPrint(TRUE
, stdout
, CFSTR("DNS domain name not specified\n"));
424 if (strlen(argv
[0]) > 0) {
428 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
429 domain
= __cleanupDomainName(str
);
432 if (domain
!= NULL
) {
433 CFDictionarySetValue(newConfiguration
, key
, domain
);
436 SCPrint(TRUE
, stdout
, CFSTR("invalid DNS domain name\n"));
440 CFDictionaryRemoveValue(newConfiguration
, key
);
448 __doDNSDomainArray(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
450 #pragma unused(description)
452 CFMutableArrayRef domains
;
455 SCPrint(TRUE
, stdout
, CFSTR("DNS search domain name(s) not specified\n"));
459 domains
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
461 if (strlen(argv
[0]) > 0) {
465 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
466 array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
471 CFIndex n
= CFArrayGetCount(array
);
473 for (i
= 0; i
< n
; i
++) {
476 domain
= __cleanupDomainName(CFArrayGetValueAtIndex(array
, i
));
477 if (domain
!= NULL
) {
478 CFArrayAppendValue(domains
, domain
);
483 SCPrint(TRUE
, stdout
, CFSTR("invalid DNS search domain name\n"));
491 if (CFArrayGetCount(domains
) > 0) {
492 CFDictionarySetValue(newConfiguration
, key
, domains
);
494 CFDictionaryRemoveValue(newConfiguration
, key
);
503 __doDNSServerAddresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
505 #pragma unused(description)
507 CFMutableArrayRef servers
;
510 SCPrint(TRUE
, stdout
, CFSTR("DNS name server address(es) not specified\n"));
514 servers
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
516 if (strlen(argv
[0]) > 0) {
522 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
523 array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
526 n
= (array
!= NULL
) ? CFArrayGetCount(array
) : 0;
527 for (i
= 0; i
< n
; i
++) {
530 if (_SC_cfstring_to_cstring(CFArrayGetValueAtIndex(array
, i
),
533 kCFStringEncodingUTF8
) != NULL
) {
536 server
= __copyIPv4Address(str
);
537 if (server
== NULL
) {
538 server
= __copyIPv6Address(str
);
540 if (server
!= NULL
) {
541 CFArrayAppendValue(servers
, server
);
547 SCPrint(TRUE
, stdout
, CFSTR("invalid DNS name server address\n"));
552 if (array
!= NULL
) CFRelease(array
);
555 if (CFArrayGetCount(servers
) > 0) {
556 CFDictionarySetValue(newConfiguration
, key
, servers
);
558 CFDictionaryRemoveValue(newConfiguration
, key
);
566 static options dnsOptions
[] = {
567 { "DomainName" , "domain" , isOther
, &kSCPropNetDNSDomainName
, __doDNSDomain
, NULL
},
568 { "domain" , "domain" , isOther
, &kSCPropNetDNSDomainName
, __doDNSDomain
, NULL
},
569 { "SearchDomains" , "search" , isOther
, &kSCPropNetDNSSearchDomains
, __doDNSDomainArray
, NULL
},
570 { "search" , "search" , isOther
, &kSCPropNetDNSSearchDomains
, __doDNSDomainArray
, NULL
},
571 { "ServerAddresses", "address", isOther
, &kSCPropNetDNSServerAddresses
, __doDNSServerAddresses
, NULL
},
572 { "nameserver" , "address", isOther
, &kSCPropNetDNSServerAddresses
, __doDNSServerAddresses
, NULL
},
573 { "nameservers" , "address", isOther
, &kSCPropNetDNSServerAddresses
, __doDNSServerAddresses
, NULL
},
574 { "SupplementalMatchDomains",
577 &kSCPropNetDNSSupplementalMatchDomains
,
581 { "?" , NULL
, isHelp
, NULL
, NULL
,
582 "\nDNS configuration commands\n\n"
583 " set protocol search domain-name[,domain-name-2]\n"
584 " set protocol nameserver x1.x1.x1.x1[,x2.x2.x2.x2]\n"
587 #define N_DNS_OPTIONS (sizeof(dnsOptions) / sizeof(dnsOptions[0]))
591 set_protocol_dns(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
595 ok
= _process_options(dnsOptions
, N_DNS_OPTIONS
, argc
, argv
, newConfiguration
);
604 #define allowIPv4Address 1<<1 // allow address
605 #define allowIPv4Netmask 1<<2 // allow subnet mask
606 #define allowIPv4Router 1<<3 // allow router
607 #define allowIPv4DHCPClientID 1<<4 // allow DCHP Client ID
609 static selections ipv4ConfigMethods
[] = {
610 { CFSTR("BOOTP") , &kSCValNetIPv4ConfigMethodBOOTP
, 0 },
611 { CFSTR("DHCP") , &kSCValNetIPv4ConfigMethodDHCP
, allowIPv4DHCPClientID
},
612 { CFSTR("INFORM") , &kSCValNetIPv4ConfigMethodINFORM
, allowIPv4Address
},
613 { CFSTR("LinkLocal"), &kSCValNetIPv4ConfigMethodLinkLocal
, 0 },
614 { CFSTR("Manual") , &kSCValNetIPv4ConfigMethodManual
, allowIPv4Address
|allowIPv4Netmask
|allowIPv4Router
},
615 { CFSTR("PPP") , &kSCValNetIPv4ConfigMethodPPP
, allowIPv4Address
|selectionNotAvailable
},
621 __doIPv4ConfigMethod(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
623 #pragma unused(description)
631 method
= CFDictionaryGetValue(newConfiguration
, key
);
632 methodIndex
= _find_selection(method
, (selections
*)ipv4ConfigMethods
, &flags
);
633 if (methodIndex
!= kCFNotFound
) {
634 if (!(flags
& allowIPv4Address
)) {
635 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv4Addresses
);
637 if (!(flags
& allowIPv4Netmask
)) {
638 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv4SubnetMasks
);
640 if (!(flags
& allowIPv4Router
)) {
641 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv4Router
);
643 if (!(flags
& allowIPv4DHCPClientID
)) {
644 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv4DHCPClientID
);
647 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
656 __doIPv4Addresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
658 #pragma unused(description)
660 Boolean useArray
= (info
== (void *)FALSE
) ? FALSE
: TRUE
;
662 if (strlen(argv
[0]) > 0) {
665 address
= __copyIPv4Address(argv
[0]);
666 if (address
!= NULL
) {
668 CFArrayRef addresses
;
670 addresses
= CFArrayCreate(NULL
, (const void **)&address
, 1, &kCFTypeArrayCallBacks
);
671 CFDictionarySetValue(newConfiguration
, key
, addresses
);
672 CFRelease(addresses
);
674 CFDictionarySetValue(newConfiguration
, key
, address
);
681 CFDictionaryRemoveValue(newConfiguration
, key
);
688 static options ipv4Options
[] = {
689 { "ConfigMethod", "configuration method"
690 , isChooseOne
, &kSCPropNetIPv4ConfigMethod
, __doIPv4ConfigMethod
, (void *)ipv4ConfigMethods
},
691 { "config" , "configuration method"
692 , isChooseOne
, &kSCPropNetIPv4ConfigMethod
, __doIPv4ConfigMethod
, (void *)ipv4ConfigMethods
},
693 { "Addresses" , "address" , isOther
, &kSCPropNetIPv4Addresses
, __doIPv4Addresses
, (void *)TRUE
},
694 { "address" , "address" , isOther
, &kSCPropNetIPv4Addresses
, __doIPv4Addresses
, (void *)TRUE
},
695 { "SubnetMasks" , "netmask" , isOther
, &kSCPropNetIPv4SubnetMasks
, __doIPv4Addresses
, (void *)TRUE
},
696 { "netmask" , "netmask" , isOther
, &kSCPropNetIPv4SubnetMasks
, __doIPv4Addresses
, (void *)TRUE
},
697 { "Router" , "address" , isOther
, &kSCPropNetIPv4Router
, __doIPv4Addresses
, (void *)FALSE
},
698 { "DHCPClientID", "client ID", isString
, &kSCPropNetIPv4DHCPClientID
, NULL
, NULL
},
700 { "?" , NULL
, isHelp
, NULL
, NULL
,
701 "\nIPv4 configuration commands\n\n"
702 " set protocol config {BOOTP|DHCP|INFORM|MANUAL}\n"
703 "\n w/config=BOOTP\n"
706 " set protocol dhcpclientid identifier\n"
707 "\n w/config=INFORM\n"
708 " set protocol address x.x.x.x\n"
709 "\n w/config=MANUAL\n"
710 " set protocol address x.x.x.x\n"
711 " set protocol netmask x.x.x.x\n"
712 " set protocol router x.x.x.x\n"
715 #define N_IPV4_OPTIONS (sizeof(ipv4Options) / sizeof(ipv4Options[0]))
719 set_protocol_ipv4(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
723 ok
= _process_options(ipv4Options
, N_IPV4_OPTIONS
, argc
, argv
, newConfiguration
);
729 // validate configuration
730 method
= CFDictionaryGetValue(newConfiguration
, kSCPropNetIPv4ConfigMethod
);
731 methodIndex
= _find_selection(method
, (selections
*)ipv4ConfigMethods
, &flags
);
732 if (methodIndex
== kCFNotFound
) {
733 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
737 if (!(flags
& allowIPv4Address
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv4Addresses
)) {
738 SCPrint(TRUE
, stdout
,
739 CFSTR("IP address not allowed with %@ configuration\n"),
740 ipv4ConfigMethods
[methodIndex
].selection
);
744 if (!(flags
& allowIPv4Netmask
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv4SubnetMasks
)) {
745 SCPrint(TRUE
, stdout
,
746 CFSTR("Subnet mask not allowed with %@ configuration\n"),
747 ipv4ConfigMethods
[methodIndex
].selection
);
751 if (!(flags
& allowIPv4Router
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv4Router
)) {
752 SCPrint(TRUE
, stdout
,
753 CFSTR("Default route not allowed with %@ configuration\n"),
754 ipv4ConfigMethods
[methodIndex
].selection
);
758 if (!(flags
& allowIPv4DHCPClientID
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv4DHCPClientID
)) {
759 SCPrint(TRUE
, stdout
,
760 CFSTR("DHCP client ID not allowed with %@ configuration\n"),
761 ipv4ConfigMethods
[methodIndex
].selection
);
774 #define allowIPv6Address 1<<1 // allow address
775 #define allowIPv6PrefixLength 1<<2 // allow prefix length
776 #define allowIPv6Router 1<<3 // allow router
778 static selections ipv6ConfigMethods
[] = {
779 { CFSTR("Automatic") , & kSCValNetIPv6ConfigMethodAutomatic
, 0 },
780 { CFSTR("Manual") , & kSCValNetIPv6ConfigMethodManual
, allowIPv6Address
|allowIPv6PrefixLength
|allowIPv6Router
},
781 { CFSTR("RouterAdvertisement"), & kSCValNetIPv6ConfigMethodRouterAdvertisement
, allowIPv6Address
},
782 { CFSTR("6to4") , & kSCValNetIPv6ConfigMethod6to4
, 0 },
788 __doIPv6ConfigMethod(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
790 #pragma unused(description)
798 method
= CFDictionaryGetValue(newConfiguration
, key
);
799 methodIndex
= _find_selection(method
, (selections
*)ipv6ConfigMethods
, &flags
);
800 if (methodIndex
!= kCFNotFound
) {
801 if (!(flags
& allowIPv6Address
)) {
802 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv6Addresses
);
804 if (!(flags
& allowIPv6PrefixLength
)) {
805 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv6PrefixLength
);
807 if (!(flags
& allowIPv6Router
)) {
808 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv6Router
);
811 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
820 __doIPv6Addresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
822 #pragma unused(description)
824 Boolean useArray
= (info
== (void *)FALSE
) ? FALSE
: TRUE
;
826 if (strlen(argv
[0]) > 0) {
829 address
= __copyIPv6Address(argv
[0]);
830 if (address
!= NULL
) {
832 CFArrayRef addresses
;
834 addresses
= CFArrayCreate(NULL
, (const void **)&address
, 1, &kCFTypeArrayCallBacks
);
835 CFDictionarySetValue(newConfiguration
, key
, addresses
);
836 CFRelease(addresses
);
838 CFDictionarySetValue(newConfiguration
, key
, address
);
845 CFDictionaryRemoveValue(newConfiguration
, key
);
852 static options ipv6Options
[] = {
853 { "ConfigMethod", "configuration method"
854 , isChooseOne
, &kSCPropNetIPv6ConfigMethod
, __doIPv6ConfigMethod
, (void *)ipv6ConfigMethods
},
855 { "config" , "configuration method"
856 , isChooseOne
, &kSCPropNetIPv6ConfigMethod
, __doIPv6ConfigMethod
, (void *)ipv6ConfigMethods
},
857 { "Addresses" , "address" , isOther
, &kSCPropNetIPv6Addresses
, __doIPv6Addresses
, (void *)TRUE
},
858 { "address" , "address" , isOther
, &kSCPropNetIPv6Addresses
, __doIPv6Addresses
, (void *)TRUE
},
859 { "PrefixLength", "prefix length", isNumber
, &kSCPropNetIPv6PrefixLength
, NULL
, NULL
},
860 { "Router" , "address" , isOther
, &kSCPropNetIPv6Router
, __doIPv6Addresses
, (void *)FALSE
},
862 { "?" , NULL
, isHelp
, NULL
, NULL
,
863 "\nIPv6 configuration commands\n\n"
864 " set protocol config {Automatic|MANUAL}\n"
865 "\n w/config=Automatic\n"
867 "\n w/config=MANUAL\n"
868 " set protocol address x:x:x:x:x:x\n"
869 " set protocol router x:x:x:x:x:x\n"
870 " set protocol prefixlength n\n"
873 #define N_IPV6_OPTIONS (sizeof(ipv6Options) / sizeof(ipv6Options[0]))
877 set_protocol_ipv6(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
881 ok
= _process_options(ipv6Options
, N_IPV6_OPTIONS
, argc
, argv
, newConfiguration
);
887 // validate configuration
888 method
= CFDictionaryGetValue(newConfiguration
, kSCPropNetIPv6ConfigMethod
);
889 methodIndex
= _find_selection(method
, (selections
*)ipv6ConfigMethods
, &flags
);
890 if (methodIndex
== kCFNotFound
) {
891 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
895 if (!(flags
& allowIPv6Address
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6Addresses
)) {
896 SCPrint(TRUE
, stdout
,
897 CFSTR("IP address not allowed with %@ configuration\n"),
898 ipv6ConfigMethods
[methodIndex
].selection
);
902 if (!(flags
& allowIPv6PrefixLength
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6PrefixLength
)) {
903 SCPrint(TRUE
, stdout
,
904 CFSTR("Prefix length not allowed with %@ configuration\n"),
905 ipv6ConfigMethods
[methodIndex
].selection
);
909 if (!(flags
& allowIPv6Router
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6Router
)) {
910 SCPrint(TRUE
, stdout
,
911 CFSTR("Router not allowed with %@ configuration\n"),
912 ipv6ConfigMethods
[methodIndex
].selection
);
925 typedef const struct {
927 const CFStringRef
*keyEnable
;
928 const CFStringRef
*keyProxy
;
929 const CFStringRef
*keyPort
;
930 const CFStringRef
*keyURL
;
933 static proxyKeys proxyKeys_FTP
= { "FTP" , &kSCPropNetProxiesFTPEnable
, &kSCPropNetProxiesFTPProxy
, &kSCPropNetProxiesFTPPort
, NULL
};
934 static proxyKeys proxyKeys_Gopher
= { "Gopher", &kSCPropNetProxiesGopherEnable
, &kSCPropNetProxiesGopherProxy
, &kSCPropNetProxiesGopherPort
, NULL
};
935 static proxyKeys proxyKeys_HTTP
= { "HTTP" , &kSCPropNetProxiesHTTPEnable
, &kSCPropNetProxiesHTTPProxy
, &kSCPropNetProxiesHTTPPort
, NULL
};
936 static proxyKeys proxyKeys_HTTPS
= { "HTTPS" , &kSCPropNetProxiesHTTPSEnable
, &kSCPropNetProxiesHTTPSProxy
, &kSCPropNetProxiesHTTPSPort
, NULL
};
937 static proxyKeys proxyKeys_RTSP
= { "RTSP" , &kSCPropNetProxiesRTSPEnable
, &kSCPropNetProxiesRTSPProxy
, &kSCPropNetProxiesRTSPPort
, NULL
};
938 static proxyKeys proxyKeys_SOCKS
= { "SOCKS" , &kSCPropNetProxiesSOCKSEnable
, &kSCPropNetProxiesSOCKSProxy
, &kSCPropNetProxiesSOCKSPort
, NULL
};
939 static proxyKeys proxyKeys_PAC
= { ".pac" , &kSCPropNetProxiesProxyAutoConfigEnable
, NULL
, NULL
, &kSCPropNetProxiesProxyAutoConfigURLString
};
940 static proxyKeys proxyKeys_WPAD
= { "WPAD" , &kSCPropNetProxiesProxyAutoDiscoveryEnable
, NULL
, NULL
, NULL
};
942 static proxyKeys
*currentProxy
= NULL
;
945 static int __doProxySelect (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
946 static int __doProxyEnable (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
947 static int __doProxyHost (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
948 static int __doProxyPort (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
949 static int __doProxyURL (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
950 static int __doProxyFTPPassive(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
953 static options proxyOptions
[] = {
955 { "ExceptionsList" , "exceptions", isStringArray
, &kSCPropNetProxiesExceptionsList
, NULL
, NULL
},
956 { "ExcludeSimpleHostnames", NULL
, isBoolean
, &kSCPropNetProxiesExcludeSimpleHostnames
, NULL
, NULL
},
958 { "FTP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_FTP
},
959 { "Gopher" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_Gopher
},
960 { "HTTP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_HTTP
},
961 { "HTTPS" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_HTTPS
},
962 { "RTSP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_RTSP
},
963 { "SOCKS" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_SOCKS
},
964 { "ProxyAutoConfig" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_PAC
},
965 { ".pac" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_PAC
},
966 { "ProxyAutoDiscovery" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_WPAD
},
967 { "WPAD" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_WPAD
},
969 { "disable" , NULL
, isOther
, NULL
, __doProxyEnable
, (void *)FALSE
},
970 { "enable" , NULL
, isOther
, NULL
, __doProxyEnable
, (void *)TRUE
},
971 { "proxy" , NULL
, isOther
, NULL
, __doProxyHost
, NULL
},
972 { "host" , NULL
, isOther
, NULL
, __doProxyHost
, NULL
},
973 { "port" , NULL
, isOther
, NULL
, __doProxyPort
, NULL
},
974 { "url" , NULL
, isOther
, NULL
, __doProxyURL
, NULL
},
975 // (ftp) proxy modifiers
976 { "FTPPassive" , NULL
, isBoolean
, &kSCPropNetProxiesFTPPassive
, __doProxyFTPPassive
, NULL
},
977 { "passive" , NULL
, isBoolean
, &kSCPropNetProxiesFTPPassive
, __doProxyFTPPassive
, NULL
},
979 { "?" , NULL
, isHelp
, NULL
, NULL
,
980 "\nProxy configuration commands\n\n"
981 " set protocol ExceptionsList exception[,exception-2]\n"
982 " set protocol ExcludeSimpleHostnames {enable|disable}\n"
984 " set protocol ftp {enable|disable}\n"
985 " set protocol ftp host proxy-host\n"
986 " set protocol ftp port proxy-port\n"
987 " set protocol ftp passive {enable|disable}\n"
989 " set protocol http {enable|disable}\n"
990 " set protocol http host proxy-host\n"
991 " set protocol http port proxy-port\n"
993 " set protocol https {enable|disable}\n"
994 " set protocol https host proxy-host\n"
995 " set protocol https port proxy-port\n"
997 " set protocol rtsp {enable|disable}\n"
998 " set protocol rtsp host proxy-host\n"
999 " set protocol rtsp port proxy-port\n"
1001 " set protocol socks {enable|disable}\n"
1002 " set protocol socks host proxy-host\n"
1003 " set protocol socks port proxy-port\n"
1005 " set protocol .pac {enable|disable}\n"
1006 " set protocol .pac url .pac-url\n"
1008 " set protocol wpad {enable|disable}\n"
1011 #define N_PROXY_OPTIONS (sizeof(proxyOptions) / sizeof(proxyOptions[0]))
1015 __doProxySelect(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1018 #pragma unused(description)
1019 #pragma unused(newConfiguration)
1023 SCPrint(TRUE
, stdout
, CFSTR("proxy option[s] not specified\n"));
1027 currentProxy
= (proxyKeys
*)info
;
1029 nextOption
= _find_option(argv
[0], proxyOptions
, N_PROXY_OPTIONS
);
1030 if ((nextOption
== kCFNotFound
) ||
1031 (proxyOptions
[nextOption
].handler
== __doProxySelect
)) {
1032 SCPrint(TRUE
, stdout
, CFSTR("%s proxy option[s] not specified\n"), currentProxy
->proxy
);
1041 __doProxyEnable(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1044 #pragma unused(description)
1045 #pragma unused(argc)
1046 #pragma unused(argv)
1047 Boolean enabled
= (info
== (void *)FALSE
) ? FALSE
: TRUE
;
1049 if (currentProxy
== NULL
) {
1050 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1054 if (currentProxy
->keyEnable
== NULL
) {
1055 SCPrint(TRUE
, stdout
, CFSTR("%s proxy cannot be %s\n"),
1056 currentProxy
->proxy
,
1057 enabled
? "enabled" : "disabled");
1063 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyEnable
), CFNumberRef_1
);
1065 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyEnable
));
1067 if (currentProxy
->keyProxy
!= NULL
) {
1068 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyProxy
));
1071 if (currentProxy
->keyPort
!= NULL
) {
1072 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyPort
));
1075 if (currentProxy
->keyURL
!= NULL
) {
1076 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyURL
));
1085 __proxy_enabled(CFDictionaryRef configuration
, const CFStringRef
*enableKey
)
1090 if (enableKey
== NULL
) {
1091 return TRUE
; // if proxy does not need to be enabled
1094 num
= CFDictionaryGetValue(configuration
, *enableKey
);
1095 if (!isA_CFNumber(num
) ||
1096 !CFNumberGetValue(num
, kCFNumberIntType
, &val
) ||
1098 return FALSE
; // if not enabled
1106 __doProxyHost(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1109 #pragma unused(description)
1110 #pragma unused(info)
1111 if (currentProxy
== NULL
) {
1112 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1116 if (currentProxy
->keyProxy
== NULL
) {
1117 SCPrint(TRUE
, stdout
, CFSTR("%s proxy host cannot be specified\n"), currentProxy
->proxy
);
1121 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1122 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1127 SCPrint(TRUE
, stdout
, CFSTR("%s proxy host not specified\n"), currentProxy
->proxy
);
1131 if (strlen(argv
[0]) > 0) {
1134 host
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1135 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyProxy
), host
);
1138 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyProxy
));
1146 __doProxyPort(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1149 #pragma unused(description)
1150 #pragma unused(info)
1151 if (currentProxy
== NULL
) {
1152 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1156 if (currentProxy
->keyPort
== NULL
) {
1157 SCPrint(TRUE
, stdout
, CFSTR("%s proxy port cannot be specified\n"), currentProxy
->proxy
);
1161 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1162 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1167 SCPrint(TRUE
, stdout
, CFSTR("%s proxy port not specified\n"), currentProxy
->proxy
);
1171 if (strlen(argv
[0]) > 0) {
1175 num
= _copy_number(argv
[0]);
1176 if (!isA_CFNumber(num
) ||
1177 !CFNumberGetValue(num
, kCFNumberIntType
, &port
) ||
1178 (port
< 0) || (port
> 65535)) {
1179 SCPrint(TRUE
, stdout
, CFSTR("invalid %s proxy port number\n"), currentProxy
->proxy
);
1180 if (num
!= NULL
) CFRelease(num
);
1184 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyPort
), num
);
1187 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyPort
));
1195 __doProxyURL(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1198 #pragma unused(description)
1199 #pragma unused(info)
1200 if (currentProxy
== NULL
) {
1201 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1205 if (currentProxy
->keyURL
== NULL
) {
1206 SCPrint(TRUE
, stdout
, CFSTR("%s proxy URL cannot be specified\n"), currentProxy
->proxy
);
1210 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1211 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1216 SCPrint(TRUE
, stdout
, CFSTR("%s proxy URL not specified\n"), currentProxy
->proxy
);
1220 if (strlen(argv
[0]) > 0) {
1223 url
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1224 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyURL
), url
);
1227 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyURL
));
1235 __doProxyFTPPassive(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1238 #pragma unused(description)
1239 #pragma unused(info)
1240 #pragma unused(argc)
1241 #pragma unused(argv)
1242 #pragma unused(newConfiguration)
1243 if (currentProxy
== NULL
) {
1244 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1248 if (currentProxy
!= &proxyKeys_FTP
) {
1249 SCPrint(TRUE
, stdout
, CFSTR("passive can only be enable for FTP proxy\n"));
1258 set_protocol_proxies(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1262 ok
= _process_options(proxyOptions
, N_PROXY_OPTIONS
, argc
, argv
, newConfiguration
);
1271 #if !TARGET_OS_IPHONE
1274 static CF_RETURNS_RETAINED CFStringRef
1275 __cleanupName(CFStringRef name
)
1277 CFMutableStringRef newName
;
1279 newName
= CFStringCreateMutableCopy(NULL
, 0, name
);
1280 CFStringTrimWhitespace(newName
);
1281 if (CFStringGetLength(newName
) == 0) {
1291 __doSMBName(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1293 #pragma unused(description)
1294 #pragma unused(info)
1296 SCPrint(TRUE
, stdout
, CFSTR("NetBIOS name not specified\n"));
1300 if (strlen(argv
[0]) > 0) {
1304 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1305 name
= __cleanupName(str
);
1309 CFDictionarySetValue(newConfiguration
, key
, name
);
1312 SCPrint(TRUE
, stdout
, CFSTR("invalid NetBIOS name\n"));
1316 CFDictionaryRemoveValue(newConfiguration
, key
);
1324 __doSMBWorkgroup(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1326 #pragma unused(description)
1327 #pragma unused(info)
1329 SCPrint(TRUE
, stdout
, CFSTR("Workgroup not specified\n"));
1333 if (strlen(argv
[0]) > 0) {
1337 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1338 name
= __cleanupName(str
);
1342 CFDictionarySetValue(newConfiguration
, key
, name
);
1345 SCPrint(TRUE
, stdout
, CFSTR("invalid Workgroup\n"));
1349 CFDictionaryRemoveValue(newConfiguration
, key
);
1357 __doSMBWINSAddresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1359 #pragma unused(description)
1360 #pragma unused(info)
1361 CFMutableArrayRef servers
;
1364 SCPrint(TRUE
, stdout
, CFSTR("WINS address(es) not specified\n"));
1368 servers
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1370 if (strlen(argv
[0]) > 0) {
1376 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1377 array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
1380 n
= (array
!= NULL
) ? CFArrayGetCount(array
) : 0;
1381 for (i
= 0; i
< n
; i
++) {
1384 if (_SC_cfstring_to_cstring(CFArrayGetValueAtIndex(array
, i
),
1387 kCFStringEncodingUTF8
) != NULL
) {
1390 server
= __copyIPv4Address(str
);
1391 //if (server == NULL) {
1392 // server = __copyIPv6Address(str);
1394 if (server
!= NULL
) {
1395 CFArrayAppendValue(servers
, server
);
1401 SCPrint(TRUE
, stdout
, CFSTR("invalid WINS address\n"));
1406 if (array
!= NULL
) CFRelease(array
);
1409 if (CFArrayGetCount(servers
) > 0) {
1410 CFDictionarySetValue(newConfiguration
, key
, servers
);
1412 CFDictionaryRemoveValue(newConfiguration
, key
);
1420 static selections smbNodeTypes
[] = {
1421 { CFSTR("Broadcast"), &kSCValNetSMBNetBIOSNodeTypeBroadcast
, 0 },
1422 { CFSTR("Peer") , &kSCValNetSMBNetBIOSNodeTypePeer
, 0 },
1423 { CFSTR("Mixed") , &kSCValNetSMBNetBIOSNodeTypeMixed
, 0 },
1424 { CFSTR("Hybrid") , &kSCValNetSMBNetBIOSNodeTypeHybrid
, 0 },
1429 static options smbOptions
[] = {
1430 { "NetBIOSName" , "name" , isOther
, &kSCPropNetSMBNetBIOSName
, __doSMBName
, NULL
},
1431 { "name" , "name" , isOther
, &kSCPropNetSMBNetBIOSName
, __doSMBName
, NULL
},
1432 { "NetBIOSNodeType", "type" , isChooseOne
, &kSCPropNetSMBNetBIOSNodeType
, NULL
, (void *)smbNodeTypes
},
1433 { "type", "type" , isChooseOne
, &kSCPropNetSMBNetBIOSNodeType
, NULL
, (void *)smbNodeTypes
},
1434 { "Workgroup" , "workgroup", isOther
, &kSCPropNetSMBWorkgroup
, __doSMBWorkgroup
, NULL
},
1435 { "WINSAddresses" , "wins" , isOther
, &kSCPropNetSMBWINSAddresses
, __doSMBWINSAddresses
, NULL
},
1436 { "wins" , "wins" , isOther
, &kSCPropNetSMBWINSAddresses
, __doSMBWINSAddresses
, NULL
},
1438 { "?" , NULL
, isHelp
, NULL
, NULL
,
1439 "\nSMB configuration commands\n\n"
1440 " set protocol name NetBIOS-name\n"
1441 " set protocol type (Broadcast|Peer|Mixed|Hybrid)\n"
1442 " set protocol workgroup SMB-workgroup\n"
1443 " set protocol wins x1.x1.x1.x1[,x2.x2.x2.x2]\n"
1446 #define N_SMB_OPTIONS (sizeof(smbOptions) / sizeof(smbOptions[0]))
1450 set_protocol_smb(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1454 ok
= _process_options(smbOptions
, N_SMB_OPTIONS
, argc
, argv
, newConfiguration
);
1459 #endif // !TARGET_OS_IPHONE
1463 #pragma mark *Protocol*
1468 set_protocol(int argc
, char **argv
)
1470 CFDictionaryRef configuration
;
1471 CFMutableDictionaryRef newConfiguration
= NULL
;
1473 CFStringRef protocolType
;
1475 if (net_protocol
== NULL
) {
1476 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
1481 SCPrint(TRUE
, stdout
, CFSTR("set what?\n"));
1485 configuration
= SCNetworkProtocolGetConfiguration(net_protocol
);
1486 if (configuration
== NULL
) {
1487 newConfiguration
= CFDictionaryCreateMutable(NULL
,
1489 &kCFTypeDictionaryKeyCallBacks
,
1490 &kCFTypeDictionaryValueCallBacks
);
1492 newConfiguration
= CFDictionaryCreateMutableCopy(NULL
, 0, configuration
);
1493 CFDictionaryRemoveValue(newConfiguration
, kSCResvInactive
);
1496 protocolType
= SCNetworkProtocolGetProtocolType(net_protocol
);
1497 if (CFEqual(protocolType
, kSCNetworkProtocolTypeDNS
)) {
1498 ok
= set_protocol_dns(argc
, argv
, newConfiguration
);
1499 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv4
)) {
1500 ok
= set_protocol_ipv4(argc
, argv
, newConfiguration
);
1501 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv6
)) {
1502 ok
= set_protocol_ipv6(argc
, argv
, newConfiguration
);
1503 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeProxies
)) {
1504 ok
= set_protocol_proxies(argc
, argv
, newConfiguration
);
1505 #if !TARGET_OS_IPHONE
1506 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeSMB
)) {
1507 ok
= set_protocol_smb(argc
, argv
, newConfiguration
);
1508 #endif // !TARGET_OS_IPHONE
1510 SCPrint(TRUE
, stdout
, CFSTR("this protocols configuration cannot be changed\n"));
1517 if (((configuration
== NULL
) && (CFDictionaryGetCount(newConfiguration
) > 0)) ||
1518 ((configuration
!= NULL
) && !CFEqual(configuration
, newConfiguration
))) {
1519 if (!SCNetworkProtocolSetConfiguration(net_protocol
, newConfiguration
)) {
1520 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
1524 _prefs_changed
= TRUE
;
1529 if (newConfiguration
!= NULL
) CFRelease(newConfiguration
);
1534 /* -------------------- */
1539 show_protocol(int argc
, char **argv
)
1541 CFDictionaryRef configuration
;
1542 SCNetworkProtocolRef protocol
= NULL
;
1543 CFStringRef protocolType
;
1546 protocol
= _find_protocol(argv
[0]);
1548 if (net_protocol
!= NULL
) {
1549 protocol
= net_protocol
;
1551 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
1556 if (protocol
== NULL
) {
1560 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1561 SCPrint(TRUE
, stdout
, CFSTR("protocol type = %@\n"), protocolType
);
1563 configuration
= SCNetworkProtocolGetConfiguration(protocol
);
1564 if (configuration
!= NULL
) {
1565 SCPrint(TRUE
, stdout
, CFSTR("\n protocol configuration\n"));
1566 _show_entity(configuration
, CFSTR(""));
1570 SCPrint(TRUE
, stdout
, CFSTR("\n%@\n"), protocol
);
1577 /* -------------------- */
1582 show_protocols(int argc
, char **argv
)
1584 #pragma unused(argc)
1585 #pragma unused(argv)
1589 if (prefs
== NULL
) {
1590 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
1594 if (net_service
== NULL
) {
1595 SCPrint(TRUE
, stdout
, CFSTR("service not selected\n"));
1599 if (protocols
!= NULL
) CFRelease(protocols
);
1600 protocols
= SCNetworkServiceCopyProtocols(net_service
);
1601 if (protocols
== NULL
) {
1602 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
1606 n
= CFArrayGetCount(protocols
);
1608 CFMutableArrayRef sorted
;
1610 sorted
= CFArrayCreateMutableCopy(NULL
, 0, protocols
);
1611 CFArraySortValues(sorted
,
1613 _SCNetworkProtocolCompare
,
1615 CFRelease(protocols
);
1619 for (i
= 0; i
< n
; i
++) {
1620 SCNetworkProtocolRef protocol
;
1621 CFStringRef protocolType
;
1623 protocol
= CFArrayGetValueAtIndex(protocols
, i
);
1624 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1626 SCPrint(TRUE
, stdout
, CFSTR("%c%2ld: %@%*s :"),
1627 ((net_protocol
!= NULL
) && CFEqual(protocol
, net_protocol
)) ? '>' : ' ',
1630 (int)(sizeof("Proxies") - CFStringGetLength(protocolType
) - 1),
1633 if (SCNetworkProtocolGetEnabled(protocol
)) {
1634 CFStringRef description
;
1636 description
= _protocol_description(protocol
, FALSE
);
1637 SCPrint(TRUE
, stdout
, CFSTR(" %@"), description
);
1638 CFRelease(description
);
1640 SCPrint(TRUE
, stdout
, CFSTR(" *DISABLED*"));
1642 SCPrint(TRUE
, stdout
, CFSTR("\n"));
1649 /* -------------------- */
1653 CF_RETURNS_RETAINED CFStringRef
1654 _protocol_description(SCNetworkProtocolRef protocol
, Boolean skipEmpty
)
1656 CFDictionaryRef configuration
;
1657 CFMutableStringRef description
= NULL
;
1658 CFStringRef protocolType
;
1660 description
= CFStringCreateMutable(NULL
, 0);
1662 if (!SCNetworkProtocolGetEnabled(protocol
)) {
1666 configuration
= SCNetworkProtocolGetConfiguration(protocol
);
1667 if (configuration
== NULL
) {
1671 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1672 if (CFEqual(protocolType
, kSCNetworkProtocolTypeDNS
)) {
1677 domain
= CFDictionaryGetValue(configuration
, kSCPropNetDNSDomainName
);
1678 if (isA_CFString(domain
)) {
1679 CFStringAppendFormat(description
,
1685 search
= CFDictionaryGetValue(configuration
, kSCPropNetDNSSearchDomains
);
1686 if (isA_CFArray(search
)) {
1689 str
= CFStringCreateByCombiningStrings(NULL
, search
, CFSTR(","));
1690 CFStringAppendFormat(description
,
1692 CFSTR("%ssearch=%@"),
1693 CFStringGetLength(description
) > 0 ? ", " : "",
1698 servers
= CFDictionaryGetValue(configuration
, kSCPropNetDNSServerAddresses
);
1699 if (isA_CFArray(servers
)) {
1702 str
= CFStringCreateByCombiningStrings(NULL
, servers
, CFSTR(","));
1703 CFStringAppendFormat(description
,
1705 CFSTR("%sservers=%@"),
1706 CFStringGetLength(description
) > 0 ? ", " : "",
1710 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv4
)) {
1713 method
= CFDictionaryGetValue(configuration
, kSCPropNetIPv4ConfigMethod
);
1714 if (isA_CFString(method
)) {
1715 CFArrayRef addresses
;
1717 addresses
= CFDictionaryGetValue(configuration
, kSCPropNetIPv4Addresses
);
1718 if (CFEqual(method
, kSCValNetIPv4ConfigMethodINFORM
) &&
1719 isA_CFArray(addresses
)) {
1720 CFStringAppendFormat(description
,
1722 CFSTR("%@, address=%@"),
1724 CFArrayGetValueAtIndex(addresses
, 0));
1725 } else if (CFEqual(method
, kSCValNetIPv4ConfigMethodManual
) &&
1726 isA_CFArray(addresses
)) {
1729 CFStringAppendFormat(description
,
1731 CFSTR("%@, address=%@"),
1733 CFArrayGetValueAtIndex(addresses
, 0));
1735 if (CFDictionaryGetValueIfPresent(configuration
,
1736 kSCPropNetIPv4Router
,
1737 (const void **)&router
) &&
1738 isA_CFString(router
)) {
1739 CFStringAppendFormat(description
,
1741 CFSTR(", router=%@"),
1745 CFStringAppendFormat(description
,
1751 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv6
)) {
1754 method
= CFDictionaryGetValue(configuration
, kSCPropNetIPv6ConfigMethod
);
1755 if (isA_CFString(method
)) {
1756 CFStringAppendFormat(description
,
1761 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeProxies
)) {
1762 static proxyKeys
*keys
[] = { &proxyKeys_FTP
, &proxyKeys_Gopher
, &proxyKeys_HTTP
, &proxyKeys_HTTPS
,
1763 &proxyKeys_RTSP
, &proxyKeys_SOCKS
, &proxyKeys_PAC
, &proxyKeys_WPAD
};
1765 for (size_t i
= 0; i
< sizeof(keys
)/sizeof(keys
[0]); i
++) {
1766 proxyKeys
*currentProxy
= keys
[i
];
1768 if (!__proxy_enabled(configuration
, currentProxy
->keyEnable
)) {
1772 if (((currentProxy
->keyProxy
!= NULL
) &&
1773 !CFDictionaryContainsKey(configuration
, *(currentProxy
->keyProxy
))) ||
1774 ((currentProxy
->keyURL
!= NULL
) &&
1775 !CFDictionaryContainsKey(configuration
, *(currentProxy
->keyURL
)))) {
1779 CFStringAppendFormat(description
,
1782 CFStringGetLength(description
) > 0 ? ", " : "",
1783 currentProxy
->proxy
);
1785 #if !TARGET_OS_IPHONE
1786 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeSMB
)) {
1789 CFStringRef workgroup
;
1791 name
= CFDictionaryGetValue(configuration
, kSCPropNetSMBNetBIOSName
);
1792 if (isA_CFString(name
)) {
1793 CFStringAppendFormat(description
,
1795 CFSTR("NetBIOS name=%@"),
1799 workgroup
= CFDictionaryGetValue(configuration
, kSCPropNetSMBWorkgroup
);
1800 if (isA_CFString(workgroup
)) {
1801 CFStringAppendFormat(description
,
1803 CFSTR("Workgroup=%@"),
1807 servers
= CFDictionaryGetValue(configuration
, kSCPropNetSMBWINSAddresses
);
1808 if (isA_CFArray(servers
)) {
1811 str
= CFStringCreateByCombiningStrings(NULL
, servers
, CFSTR(","));
1812 CFStringAppendFormat(description
,
1814 CFSTR("%sWINS servers=%@"),
1815 CFStringGetLength(description
) > 0 ? ", " : "",
1819 #endif // !TARGET_OS_IPHONE
1824 if (skipEmpty
&& CFStringGetLength(description
) == 0) {
1825 CFRelease(description
);