2 * Copyright (c) 2004-2009, 2011, 2014, 2017, 2019 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 { "EnableCGA" , NULL
, isBoolean
, &kSCPropNetIPv6EnableCGA
, NULL
, NULL
},
860 { "PrefixLength", "prefix length", isNumber
, &kSCPropNetIPv6PrefixLength
, NULL
, NULL
},
861 { "Router" , "address" , isOther
, &kSCPropNetIPv6Router
, __doIPv6Addresses
, (void *)FALSE
},
863 { "?" , NULL
, isHelp
, NULL
, NULL
,
864 "\nIPv6 configuration commands\n\n"
865 " set protocol config {Automatic|MANUAL}\n"
866 "\n w/config=Automatic\n"
868 "\n w/config=MANUAL\n"
869 " set protocol address x:x:x:x:x:x\n"
870 " set protocol router x:x:x:x:x:x\n"
871 " set protocol prefixlength n\n"
874 #define N_IPV6_OPTIONS (sizeof(ipv6Options) / sizeof(ipv6Options[0]))
878 set_protocol_ipv6(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
882 ok
= _process_options(ipv6Options
, N_IPV6_OPTIONS
, argc
, argv
, newConfiguration
);
888 // validate configuration
889 method
= CFDictionaryGetValue(newConfiguration
, kSCPropNetIPv6ConfigMethod
);
890 methodIndex
= _find_selection(method
, (selections
*)ipv6ConfigMethods
, &flags
);
891 if (methodIndex
== kCFNotFound
) {
892 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
896 if (!(flags
& allowIPv6Address
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6Addresses
)) {
897 SCPrint(TRUE
, stdout
,
898 CFSTR("IP address not allowed with %@ configuration\n"),
899 ipv6ConfigMethods
[methodIndex
].selection
);
903 if (!(flags
& allowIPv6PrefixLength
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6PrefixLength
)) {
904 SCPrint(TRUE
, stdout
,
905 CFSTR("Prefix length not allowed with %@ configuration\n"),
906 ipv6ConfigMethods
[methodIndex
].selection
);
910 if (!(flags
& allowIPv6Router
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6Router
)) {
911 SCPrint(TRUE
, stdout
,
912 CFSTR("Router not allowed with %@ configuration\n"),
913 ipv6ConfigMethods
[methodIndex
].selection
);
926 typedef const struct {
928 const CFStringRef
*keyEnable
;
929 const CFStringRef
*keyProxy
;
930 const CFStringRef
*keyPort
;
931 const CFStringRef
*keyURL
;
934 static proxyKeys proxyKeys_FTP
= { "FTP" , &kSCPropNetProxiesFTPEnable
, &kSCPropNetProxiesFTPProxy
, &kSCPropNetProxiesFTPPort
, NULL
};
935 static proxyKeys proxyKeys_Gopher
= { "Gopher", &kSCPropNetProxiesGopherEnable
, &kSCPropNetProxiesGopherProxy
, &kSCPropNetProxiesGopherPort
, NULL
};
936 static proxyKeys proxyKeys_HTTP
= { "HTTP" , &kSCPropNetProxiesHTTPEnable
, &kSCPropNetProxiesHTTPProxy
, &kSCPropNetProxiesHTTPPort
, NULL
};
937 static proxyKeys proxyKeys_HTTPS
= { "HTTPS" , &kSCPropNetProxiesHTTPSEnable
, &kSCPropNetProxiesHTTPSProxy
, &kSCPropNetProxiesHTTPSPort
, NULL
};
938 static proxyKeys proxyKeys_RTSP
= { "RTSP" , &kSCPropNetProxiesRTSPEnable
, &kSCPropNetProxiesRTSPProxy
, &kSCPropNetProxiesRTSPPort
, NULL
};
939 static proxyKeys proxyKeys_SOCKS
= { "SOCKS" , &kSCPropNetProxiesSOCKSEnable
, &kSCPropNetProxiesSOCKSProxy
, &kSCPropNetProxiesSOCKSPort
, NULL
};
940 static proxyKeys proxyKeys_PAC
= { ".pac" , &kSCPropNetProxiesProxyAutoConfigEnable
, NULL
, NULL
, &kSCPropNetProxiesProxyAutoConfigURLString
};
941 static proxyKeys proxyKeys_WPAD
= { "WPAD" , &kSCPropNetProxiesProxyAutoDiscoveryEnable
, NULL
, NULL
, NULL
};
943 static proxyKeys
*currentProxy
= NULL
;
946 static int __doProxySelect (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
947 static int __doProxyEnable (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
948 static int __doProxyHost (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
949 static int __doProxyPort (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
950 static int __doProxyURL (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
951 static int __doProxyFTPPassive(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
954 static options proxyOptions
[] = {
956 { "ExceptionsList" , "exceptions", isStringArray
, &kSCPropNetProxiesExceptionsList
, NULL
, NULL
},
957 { "ExcludeSimpleHostnames", NULL
, isBoolean
, &kSCPropNetProxiesExcludeSimpleHostnames
, NULL
, NULL
},
959 { "FTP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_FTP
},
960 { "Gopher" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_Gopher
},
961 { "HTTP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_HTTP
},
962 { "HTTPS" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_HTTPS
},
963 { "RTSP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_RTSP
},
964 { "SOCKS" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_SOCKS
},
965 { "ProxyAutoConfig" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_PAC
},
966 { ".pac" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_PAC
},
967 { "ProxyAutoDiscovery" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_WPAD
},
968 { "WPAD" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_WPAD
},
970 { "disable" , NULL
, isOther
, NULL
, __doProxyEnable
, (void *)FALSE
},
971 { "enable" , NULL
, isOther
, NULL
, __doProxyEnable
, (void *)TRUE
},
972 { "proxy" , NULL
, isOther
, NULL
, __doProxyHost
, NULL
},
973 { "host" , NULL
, isOther
, NULL
, __doProxyHost
, NULL
},
974 { "port" , NULL
, isOther
, NULL
, __doProxyPort
, NULL
},
975 { "url" , NULL
, isOther
, NULL
, __doProxyURL
, NULL
},
976 // (ftp) proxy modifiers
977 { "FTPPassive" , NULL
, isBoolean
, &kSCPropNetProxiesFTPPassive
, __doProxyFTPPassive
, NULL
},
978 { "passive" , NULL
, isBoolean
, &kSCPropNetProxiesFTPPassive
, __doProxyFTPPassive
, NULL
},
980 { "?" , NULL
, isHelp
, NULL
, NULL
,
981 "\nProxy configuration commands\n\n"
982 " set protocol ExceptionsList exception[,exception-2]\n"
983 " set protocol ExcludeSimpleHostnames {enable|disable}\n"
985 " set protocol ftp {enable|disable}\n"
986 " set protocol ftp host proxy-host\n"
987 " set protocol ftp port proxy-port\n"
988 " set protocol ftp passive {enable|disable}\n"
990 " set protocol http {enable|disable}\n"
991 " set protocol http host proxy-host\n"
992 " set protocol http port proxy-port\n"
994 " set protocol https {enable|disable}\n"
995 " set protocol https host proxy-host\n"
996 " set protocol https port proxy-port\n"
998 " set protocol rtsp {enable|disable}\n"
999 " set protocol rtsp host proxy-host\n"
1000 " set protocol rtsp port proxy-port\n"
1002 " set protocol socks {enable|disable}\n"
1003 " set protocol socks host proxy-host\n"
1004 " set protocol socks port proxy-port\n"
1006 " set protocol .pac {enable|disable}\n"
1007 " set protocol .pac url .pac-url\n"
1009 " set protocol wpad {enable|disable}\n"
1012 #define N_PROXY_OPTIONS (sizeof(proxyOptions) / sizeof(proxyOptions[0]))
1016 __doProxySelect(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1019 #pragma unused(description)
1020 #pragma unused(newConfiguration)
1024 SCPrint(TRUE
, stdout
, CFSTR("proxy option[s] not specified\n"));
1028 currentProxy
= (proxyKeys
*)info
;
1030 nextOption
= _find_option(argv
[0], proxyOptions
, N_PROXY_OPTIONS
);
1031 if ((nextOption
== kCFNotFound
) ||
1032 (proxyOptions
[nextOption
].handler
== __doProxySelect
)) {
1033 SCPrint(TRUE
, stdout
, CFSTR("%s proxy option[s] not specified\n"), currentProxy
->proxy
);
1042 __doProxyEnable(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1045 #pragma unused(description)
1046 #pragma unused(argc)
1047 #pragma unused(argv)
1048 Boolean enabled
= (info
== (void *)FALSE
) ? FALSE
: TRUE
;
1050 if (currentProxy
== NULL
) {
1051 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1055 if (currentProxy
->keyEnable
== NULL
) {
1056 SCPrint(TRUE
, stdout
, CFSTR("%s proxy cannot be %s\n"),
1057 currentProxy
->proxy
,
1058 enabled
? "enabled" : "disabled");
1064 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyEnable
), CFNumberRef_1
);
1066 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyEnable
));
1068 if (currentProxy
->keyProxy
!= NULL
) {
1069 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyProxy
));
1072 if (currentProxy
->keyPort
!= NULL
) {
1073 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyPort
));
1076 if (currentProxy
->keyURL
!= NULL
) {
1077 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyURL
));
1086 __proxy_enabled(CFDictionaryRef configuration
, const CFStringRef
*enableKey
)
1091 if (enableKey
== NULL
) {
1092 return TRUE
; // if proxy does not need to be enabled
1095 num
= CFDictionaryGetValue(configuration
, *enableKey
);
1096 if (!isA_CFNumber(num
) ||
1097 !CFNumberGetValue(num
, kCFNumberIntType
, &val
) ||
1099 return FALSE
; // if not enabled
1107 __doProxyHost(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1110 #pragma unused(description)
1111 #pragma unused(info)
1112 if (currentProxy
== NULL
) {
1113 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1117 if (currentProxy
->keyProxy
== NULL
) {
1118 SCPrint(TRUE
, stdout
, CFSTR("%s proxy host cannot be specified\n"), currentProxy
->proxy
);
1122 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1123 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1128 SCPrint(TRUE
, stdout
, CFSTR("%s proxy host not specified\n"), currentProxy
->proxy
);
1132 if (strlen(argv
[0]) > 0) {
1135 host
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1136 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyProxy
), host
);
1139 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyProxy
));
1147 __doProxyPort(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1150 #pragma unused(description)
1151 #pragma unused(info)
1152 if (currentProxy
== NULL
) {
1153 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1157 if (currentProxy
->keyPort
== NULL
) {
1158 SCPrint(TRUE
, stdout
, CFSTR("%s proxy port cannot be specified\n"), currentProxy
->proxy
);
1162 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1163 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1168 SCPrint(TRUE
, stdout
, CFSTR("%s proxy port not specified\n"), currentProxy
->proxy
);
1172 if (strlen(argv
[0]) > 0) {
1176 num
= _copy_number(argv
[0]);
1177 if (!isA_CFNumber(num
) ||
1178 !CFNumberGetValue(num
, kCFNumberIntType
, &port
) ||
1179 (port
< 0) || (port
> 65535)) {
1180 SCPrint(TRUE
, stdout
, CFSTR("invalid %s proxy port number\n"), currentProxy
->proxy
);
1181 if (num
!= NULL
) CFRelease(num
);
1185 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyPort
), num
);
1188 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyPort
));
1196 __doProxyURL(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1199 #pragma unused(description)
1200 #pragma unused(info)
1201 if (currentProxy
== NULL
) {
1202 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1206 if (currentProxy
->keyURL
== NULL
) {
1207 SCPrint(TRUE
, stdout
, CFSTR("%s proxy URL cannot be specified\n"), currentProxy
->proxy
);
1211 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1212 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1217 SCPrint(TRUE
, stdout
, CFSTR("%s proxy URL not specified\n"), currentProxy
->proxy
);
1221 if (strlen(argv
[0]) > 0) {
1224 url
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1225 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyURL
), url
);
1228 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyURL
));
1236 __doProxyFTPPassive(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1239 #pragma unused(description)
1240 #pragma unused(info)
1241 #pragma unused(argc)
1242 #pragma unused(argv)
1243 #pragma unused(newConfiguration)
1244 if (currentProxy
== NULL
) {
1245 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1249 if (currentProxy
!= &proxyKeys_FTP
) {
1250 SCPrint(TRUE
, stdout
, CFSTR("passive can only be enable for FTP proxy\n"));
1259 set_protocol_proxies(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1263 ok
= _process_options(proxyOptions
, N_PROXY_OPTIONS
, argc
, argv
, newConfiguration
);
1272 #if !TARGET_OS_IPHONE
1275 static CF_RETURNS_RETAINED CFStringRef
1276 __cleanupName(CFStringRef name
)
1278 CFMutableStringRef newName
;
1280 newName
= CFStringCreateMutableCopy(NULL
, 0, name
);
1281 CFStringTrimWhitespace(newName
);
1282 if (CFStringGetLength(newName
) == 0) {
1292 __doSMBName(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1294 #pragma unused(description)
1295 #pragma unused(info)
1297 SCPrint(TRUE
, stdout
, CFSTR("NetBIOS name not specified\n"));
1301 if (strlen(argv
[0]) > 0) {
1305 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1306 name
= __cleanupName(str
);
1310 CFDictionarySetValue(newConfiguration
, key
, name
);
1313 SCPrint(TRUE
, stdout
, CFSTR("invalid NetBIOS name\n"));
1317 CFDictionaryRemoveValue(newConfiguration
, key
);
1325 __doSMBWorkgroup(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1327 #pragma unused(description)
1328 #pragma unused(info)
1330 SCPrint(TRUE
, stdout
, CFSTR("Workgroup not specified\n"));
1334 if (strlen(argv
[0]) > 0) {
1338 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1339 name
= __cleanupName(str
);
1343 CFDictionarySetValue(newConfiguration
, key
, name
);
1346 SCPrint(TRUE
, stdout
, CFSTR("invalid Workgroup\n"));
1350 CFDictionaryRemoveValue(newConfiguration
, key
);
1358 __doSMBWINSAddresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1360 #pragma unused(description)
1361 #pragma unused(info)
1362 CFMutableArrayRef servers
;
1365 SCPrint(TRUE
, stdout
, CFSTR("WINS address(es) not specified\n"));
1369 servers
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1371 if (strlen(argv
[0]) > 0) {
1377 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1378 array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
1381 n
= (array
!= NULL
) ? CFArrayGetCount(array
) : 0;
1382 for (i
= 0; i
< n
; i
++) {
1385 if (_SC_cfstring_to_cstring(CFArrayGetValueAtIndex(array
, i
),
1388 kCFStringEncodingUTF8
) != NULL
) {
1391 server
= __copyIPv4Address(str
);
1392 //if (server == NULL) {
1393 // server = __copyIPv6Address(str);
1395 if (server
!= NULL
) {
1396 CFArrayAppendValue(servers
, server
);
1402 SCPrint(TRUE
, stdout
, CFSTR("invalid WINS address\n"));
1407 if (array
!= NULL
) CFRelease(array
);
1410 if (CFArrayGetCount(servers
) > 0) {
1411 CFDictionarySetValue(newConfiguration
, key
, servers
);
1413 CFDictionaryRemoveValue(newConfiguration
, key
);
1421 static selections smbNodeTypes
[] = {
1422 { CFSTR("Broadcast"), &kSCValNetSMBNetBIOSNodeTypeBroadcast
, 0 },
1423 { CFSTR("Peer") , &kSCValNetSMBNetBIOSNodeTypePeer
, 0 },
1424 { CFSTR("Mixed") , &kSCValNetSMBNetBIOSNodeTypeMixed
, 0 },
1425 { CFSTR("Hybrid") , &kSCValNetSMBNetBIOSNodeTypeHybrid
, 0 },
1430 static options smbOptions
[] = {
1431 { "NetBIOSName" , "name" , isOther
, &kSCPropNetSMBNetBIOSName
, __doSMBName
, NULL
},
1432 { "name" , "name" , isOther
, &kSCPropNetSMBNetBIOSName
, __doSMBName
, NULL
},
1433 { "NetBIOSNodeType", "type" , isChooseOne
, &kSCPropNetSMBNetBIOSNodeType
, NULL
, (void *)smbNodeTypes
},
1434 { "type", "type" , isChooseOne
, &kSCPropNetSMBNetBIOSNodeType
, NULL
, (void *)smbNodeTypes
},
1435 { "Workgroup" , "workgroup", isOther
, &kSCPropNetSMBWorkgroup
, __doSMBWorkgroup
, NULL
},
1436 { "WINSAddresses" , "wins" , isOther
, &kSCPropNetSMBWINSAddresses
, __doSMBWINSAddresses
, NULL
},
1437 { "wins" , "wins" , isOther
, &kSCPropNetSMBWINSAddresses
, __doSMBWINSAddresses
, NULL
},
1439 { "?" , NULL
, isHelp
, NULL
, NULL
,
1440 "\nSMB configuration commands\n\n"
1441 " set protocol name NetBIOS-name\n"
1442 " set protocol type (Broadcast|Peer|Mixed|Hybrid)\n"
1443 " set protocol workgroup SMB-workgroup\n"
1444 " set protocol wins x1.x1.x1.x1[,x2.x2.x2.x2]\n"
1447 #define N_SMB_OPTIONS (sizeof(smbOptions) / sizeof(smbOptions[0]))
1451 set_protocol_smb(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1455 ok
= _process_options(smbOptions
, N_SMB_OPTIONS
, argc
, argv
, newConfiguration
);
1460 #endif // !TARGET_OS_IPHONE
1464 #pragma mark *Protocol*
1469 set_protocol(int argc
, char **argv
)
1471 CFDictionaryRef configuration
;
1472 CFMutableDictionaryRef newConfiguration
= NULL
;
1474 CFStringRef protocolType
;
1476 if (net_protocol
== NULL
) {
1477 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
1482 SCPrint(TRUE
, stdout
, CFSTR("set what?\n"));
1486 configuration
= SCNetworkProtocolGetConfiguration(net_protocol
);
1487 if (configuration
== NULL
) {
1488 newConfiguration
= CFDictionaryCreateMutable(NULL
,
1490 &kCFTypeDictionaryKeyCallBacks
,
1491 &kCFTypeDictionaryValueCallBacks
);
1493 newConfiguration
= CFDictionaryCreateMutableCopy(NULL
, 0, configuration
);
1494 CFDictionaryRemoveValue(newConfiguration
, kSCResvInactive
);
1497 protocolType
= SCNetworkProtocolGetProtocolType(net_protocol
);
1498 if (CFEqual(protocolType
, kSCNetworkProtocolTypeDNS
)) {
1499 ok
= set_protocol_dns(argc
, argv
, newConfiguration
);
1500 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv4
)) {
1501 ok
= set_protocol_ipv4(argc
, argv
, newConfiguration
);
1502 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv6
)) {
1503 ok
= set_protocol_ipv6(argc
, argv
, newConfiguration
);
1504 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeProxies
)) {
1505 ok
= set_protocol_proxies(argc
, argv
, newConfiguration
);
1506 #if !TARGET_OS_IPHONE
1507 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeSMB
)) {
1508 ok
= set_protocol_smb(argc
, argv
, newConfiguration
);
1509 #endif // !TARGET_OS_IPHONE
1511 SCPrint(TRUE
, stdout
, CFSTR("this protocols configuration cannot be changed\n"));
1518 if (((configuration
== NULL
) && (CFDictionaryGetCount(newConfiguration
) > 0)) ||
1519 ((configuration
!= NULL
) && !CFEqual(configuration
, newConfiguration
))) {
1520 if (!SCNetworkProtocolSetConfiguration(net_protocol
, newConfiguration
)) {
1521 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
1525 _prefs_changed
= TRUE
;
1530 if (newConfiguration
!= NULL
) CFRelease(newConfiguration
);
1535 /* -------------------- */
1540 show_protocol(int argc
, char **argv
)
1542 CFDictionaryRef configuration
;
1543 SCNetworkProtocolRef protocol
= NULL
;
1544 CFStringRef protocolType
;
1547 protocol
= _find_protocol(argv
[0]);
1549 if (net_protocol
!= NULL
) {
1550 protocol
= net_protocol
;
1552 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
1557 if (protocol
== NULL
) {
1561 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1562 SCPrint(TRUE
, stdout
, CFSTR("protocol type = %@\n"), protocolType
);
1564 configuration
= SCNetworkProtocolGetConfiguration(protocol
);
1565 if (configuration
!= NULL
) {
1566 SCPrint(TRUE
, stdout
, CFSTR("\n protocol configuration\n"));
1567 _show_entity(configuration
, CFSTR(""));
1571 SCPrint(TRUE
, stdout
, CFSTR("\n%@\n"), protocol
);
1578 /* -------------------- */
1583 show_protocols(int argc
, char **argv
)
1585 #pragma unused(argc)
1586 #pragma unused(argv)
1590 if (prefs
== NULL
) {
1591 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
1595 if (net_service
== NULL
) {
1596 SCPrint(TRUE
, stdout
, CFSTR("service not selected\n"));
1600 if (protocols
!= NULL
) CFRelease(protocols
);
1601 protocols
= SCNetworkServiceCopyProtocols(net_service
);
1602 if (protocols
== NULL
) {
1603 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
1607 n
= CFArrayGetCount(protocols
);
1609 CFMutableArrayRef sorted
;
1611 sorted
= CFArrayCreateMutableCopy(NULL
, 0, protocols
);
1612 CFArraySortValues(sorted
,
1614 _SCNetworkProtocolCompare
,
1616 CFRelease(protocols
);
1620 for (i
= 0; i
< n
; i
++) {
1621 SCNetworkProtocolRef protocol
;
1622 CFStringRef protocolType
;
1624 protocol
= CFArrayGetValueAtIndex(protocols
, i
);
1625 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1627 SCPrint(TRUE
, stdout
, CFSTR("%c%2ld: %@%*s :"),
1628 ((net_protocol
!= NULL
) && CFEqual(protocol
, net_protocol
)) ? '>' : ' ',
1631 (int)(sizeof("Proxies") - CFStringGetLength(protocolType
) - 1),
1634 if (SCNetworkProtocolGetEnabled(protocol
)) {
1635 CFStringRef description
;
1637 description
= _protocol_description(protocol
, FALSE
);
1638 SCPrint(TRUE
, stdout
, CFSTR(" %@"), description
);
1639 CFRelease(description
);
1641 SCPrint(TRUE
, stdout
, CFSTR(" *DISABLED*"));
1643 SCPrint(TRUE
, stdout
, CFSTR("\n"));
1650 /* -------------------- */
1654 CF_RETURNS_RETAINED CFStringRef
1655 _protocol_description(SCNetworkProtocolRef protocol
, Boolean skipEmpty
)
1657 CFDictionaryRef configuration
;
1658 CFMutableStringRef description
= NULL
;
1659 CFStringRef protocolType
;
1661 description
= CFStringCreateMutable(NULL
, 0);
1663 if (!SCNetworkProtocolGetEnabled(protocol
)) {
1667 configuration
= SCNetworkProtocolGetConfiguration(protocol
);
1668 if (configuration
== NULL
) {
1672 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1673 if (CFEqual(protocolType
, kSCNetworkProtocolTypeDNS
)) {
1678 domain
= CFDictionaryGetValue(configuration
, kSCPropNetDNSDomainName
);
1679 if (isA_CFString(domain
)) {
1680 CFStringAppendFormat(description
,
1686 search
= CFDictionaryGetValue(configuration
, kSCPropNetDNSSearchDomains
);
1687 if (isA_CFArray(search
)) {
1690 str
= CFStringCreateByCombiningStrings(NULL
, search
, CFSTR(","));
1691 CFStringAppendFormat(description
,
1693 CFSTR("%ssearch=%@"),
1694 CFStringGetLength(description
) > 0 ? ", " : "",
1699 servers
= CFDictionaryGetValue(configuration
, kSCPropNetDNSServerAddresses
);
1700 if (isA_CFArray(servers
)) {
1703 str
= CFStringCreateByCombiningStrings(NULL
, servers
, CFSTR(","));
1704 CFStringAppendFormat(description
,
1706 CFSTR("%sservers=%@"),
1707 CFStringGetLength(description
) > 0 ? ", " : "",
1711 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv4
)) {
1714 method
= CFDictionaryGetValue(configuration
, kSCPropNetIPv4ConfigMethod
);
1715 if (isA_CFString(method
)) {
1716 CFArrayRef addresses
;
1718 addresses
= CFDictionaryGetValue(configuration
, kSCPropNetIPv4Addresses
);
1719 if (CFEqual(method
, kSCValNetIPv4ConfigMethodINFORM
) &&
1720 isA_CFArray(addresses
)) {
1721 CFStringAppendFormat(description
,
1723 CFSTR("%@, address=%@"),
1725 CFArrayGetValueAtIndex(addresses
, 0));
1726 } else if (CFEqual(method
, kSCValNetIPv4ConfigMethodManual
) &&
1727 isA_CFArray(addresses
)) {
1731 CFStringAppendFormat(description
,
1733 CFSTR("%@, address=%@"),
1735 CFArrayGetValueAtIndex(addresses
, 0));
1737 if (CFDictionaryGetValueIfPresent(configuration
,
1738 kSCPropNetIPv4SubnetMasks
,
1739 (const void **)&masks
) &&
1740 isA_CFArray(masks
)) {
1741 CFStringAppendFormat(description
,
1744 CFArrayGetValueAtIndex(masks
, 0));
1747 if (CFDictionaryGetValueIfPresent(configuration
,
1748 kSCPropNetIPv4Router
,
1749 (const void **)&router
) &&
1750 isA_CFString(router
)) {
1751 CFStringAppendFormat(description
,
1753 CFSTR(", router=%@"),
1757 CFStringAppendFormat(description
,
1763 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv6
)) {
1766 method
= CFDictionaryGetValue(configuration
, kSCPropNetIPv6ConfigMethod
);
1767 if (isA_CFString(method
)) {
1768 CFStringAppendFormat(description
,
1773 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeProxies
)) {
1774 static proxyKeys
*keys
[] = { &proxyKeys_FTP
, &proxyKeys_Gopher
, &proxyKeys_HTTP
, &proxyKeys_HTTPS
,
1775 &proxyKeys_RTSP
, &proxyKeys_SOCKS
, &proxyKeys_PAC
, &proxyKeys_WPAD
};
1777 for (size_t i
= 0; i
< sizeof(keys
)/sizeof(keys
[0]); i
++) {
1778 proxyKeys
*currentProxy
= keys
[i
];
1780 if (!__proxy_enabled(configuration
, currentProxy
->keyEnable
)) {
1784 if (((currentProxy
->keyProxy
!= NULL
) &&
1785 !CFDictionaryContainsKey(configuration
, *(currentProxy
->keyProxy
))) ||
1786 ((currentProxy
->keyURL
!= NULL
) &&
1787 !CFDictionaryContainsKey(configuration
, *(currentProxy
->keyURL
)))) {
1791 CFStringAppendFormat(description
,
1794 CFStringGetLength(description
) > 0 ? ", " : "",
1795 currentProxy
->proxy
);
1797 #if !TARGET_OS_IPHONE
1798 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeSMB
)) {
1801 CFStringRef workgroup
;
1803 name
= CFDictionaryGetValue(configuration
, kSCPropNetSMBNetBIOSName
);
1804 if (isA_CFString(name
)) {
1805 CFStringAppendFormat(description
,
1807 CFSTR("NetBIOS name=%@"),
1811 workgroup
= CFDictionaryGetValue(configuration
, kSCPropNetSMBWorkgroup
);
1812 if (isA_CFString(workgroup
)) {
1813 CFStringAppendFormat(description
,
1815 CFSTR("Workgroup=%@"),
1819 servers
= CFDictionaryGetValue(configuration
, kSCPropNetSMBWINSAddresses
);
1820 if (isA_CFArray(servers
)) {
1823 str
= CFStringCreateByCombiningStrings(NULL
, servers
, CFSTR(","));
1824 CFStringAppendFormat(description
,
1826 CFSTR("%sWINS servers=%@"),
1827 CFStringGetLength(description
) > 0 ? ", " : "",
1831 #endif // !TARGET_OS_IPHONE
1836 if (skipEmpty
&& CFStringGetLength(description
) == 0) {
1837 CFRelease(description
);