2 * Copyright (c) 2004-2009, 2011, 2014, 2017, 2019, 2020 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 __doIPv6PrefixLength(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
854 #pragma unused(description)
857 char * prefix
= argv
[0];
858 if (*prefix
!= '\0') {
862 prefixLength
= (int)strtol(prefix
, &end
, 10);
864 if (*end
== '\0' && errno
== 0 && prefixLength
> 0 && prefixLength
<= 128) {
868 num
= CFNumberCreate(NULL
, kCFNumberIntType
, &prefixLength
);
869 prefixes
= CFArrayCreate(NULL
, (const void **)&num
, 1, &kCFTypeArrayCallBacks
);
871 CFDictionarySetValue(newConfiguration
, key
, prefixes
);
874 SCPrint(TRUE
, stdout
, CFSTR("Invalid prefix length '%s' (valid range 1..128)\n"), prefix
);
878 CFDictionaryRemoveValue(newConfiguration
, key
);
885 static options ipv6Options
[] = {
886 { "ConfigMethod", "configuration method"
887 , isChooseOne
, &kSCPropNetIPv6ConfigMethod
, __doIPv6ConfigMethod
, (void *)ipv6ConfigMethods
},
888 { "config" , "configuration method"
889 , isChooseOne
, &kSCPropNetIPv6ConfigMethod
, __doIPv6ConfigMethod
, (void *)ipv6ConfigMethods
},
890 { "Addresses" , "address" , isOther
, &kSCPropNetIPv6Addresses
, __doIPv6Addresses
, (void *)TRUE
},
891 { "address" , "address" , isOther
, &kSCPropNetIPv6Addresses
, __doIPv6Addresses
, (void *)TRUE
},
892 { "EnableCGA" , NULL
, isBoolean
, &kSCPropNetIPv6EnableCGA
, NULL
, NULL
},
893 { "PrefixLength", "prefix length", isOther
, &kSCPropNetIPv6PrefixLength
, __doIPv6PrefixLength
, NULL
},
894 { "Router" , "address" , isOther
, &kSCPropNetIPv6Router
, __doIPv6Addresses
, (void *)FALSE
},
896 { "?" , NULL
, isHelp
, NULL
, NULL
,
897 "\nIPv6 configuration commands\n\n"
898 " set protocol config {Automatic|MANUAL}\n"
899 "\n w/config=Automatic\n"
901 "\n w/config=MANUAL\n"
902 " set protocol address x:x:x:x:x:x\n"
903 " set protocol router x:x:x:x:x:x\n"
904 " set protocol prefixlength n\n"
907 #define N_IPV6_OPTIONS (sizeof(ipv6Options) / sizeof(ipv6Options[0]))
911 set_protocol_ipv6(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
915 ok
= _process_options(ipv6Options
, N_IPV6_OPTIONS
, argc
, argv
, newConfiguration
);
921 // validate configuration
922 method
= CFDictionaryGetValue(newConfiguration
, kSCPropNetIPv6ConfigMethod
);
923 methodIndex
= _find_selection(method
, (selections
*)ipv6ConfigMethods
, &flags
);
924 if (methodIndex
== kCFNotFound
) {
925 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
929 if (!(flags
& allowIPv6Address
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6Addresses
)) {
930 SCPrint(TRUE
, stdout
,
931 CFSTR("IP address not allowed with %@ configuration\n"),
932 ipv6ConfigMethods
[methodIndex
].selection
);
936 if (!(flags
& allowIPv6PrefixLength
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6PrefixLength
)) {
937 SCPrint(TRUE
, stdout
,
938 CFSTR("Prefix length not allowed with %@ configuration\n"),
939 ipv6ConfigMethods
[methodIndex
].selection
);
943 if (!(flags
& allowIPv6Router
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6Router
)) {
944 SCPrint(TRUE
, stdout
,
945 CFSTR("Router not allowed with %@ configuration\n"),
946 ipv6ConfigMethods
[methodIndex
].selection
);
959 typedef const struct {
961 const CFStringRef
*keyEnable
;
962 const CFStringRef
*keyProxy
;
963 const CFStringRef
*keyPort
;
964 const CFStringRef
*keyURL
;
967 static proxyKeys proxyKeys_FTP
= { "FTP" , &kSCPropNetProxiesFTPEnable
, &kSCPropNetProxiesFTPProxy
, &kSCPropNetProxiesFTPPort
, NULL
};
968 static proxyKeys proxyKeys_Gopher
= { "Gopher", &kSCPropNetProxiesGopherEnable
, &kSCPropNetProxiesGopherProxy
, &kSCPropNetProxiesGopherPort
, NULL
};
969 static proxyKeys proxyKeys_HTTP
= { "HTTP" , &kSCPropNetProxiesHTTPEnable
, &kSCPropNetProxiesHTTPProxy
, &kSCPropNetProxiesHTTPPort
, NULL
};
970 static proxyKeys proxyKeys_HTTPS
= { "HTTPS" , &kSCPropNetProxiesHTTPSEnable
, &kSCPropNetProxiesHTTPSProxy
, &kSCPropNetProxiesHTTPSPort
, NULL
};
971 static proxyKeys proxyKeys_RTSP
= { "RTSP" , &kSCPropNetProxiesRTSPEnable
, &kSCPropNetProxiesRTSPProxy
, &kSCPropNetProxiesRTSPPort
, NULL
};
972 static proxyKeys proxyKeys_SOCKS
= { "SOCKS" , &kSCPropNetProxiesSOCKSEnable
, &kSCPropNetProxiesSOCKSProxy
, &kSCPropNetProxiesSOCKSPort
, NULL
};
973 static proxyKeys proxyKeys_PAC
= { ".pac" , &kSCPropNetProxiesProxyAutoConfigEnable
, NULL
, NULL
, &kSCPropNetProxiesProxyAutoConfigURLString
};
974 static proxyKeys proxyKeys_WPAD
= { "WPAD" , &kSCPropNetProxiesProxyAutoDiscoveryEnable
, NULL
, NULL
, NULL
};
976 static proxyKeys
*currentProxy
= NULL
;
979 static int __doProxySelect (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
980 static int __doProxyEnable (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
981 static int __doProxyHost (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
982 static int __doProxyPort (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
983 static int __doProxyURL (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
984 static int __doProxyFTPPassive(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
987 static options proxyOptions
[] = {
989 { "ExceptionsList" , "exceptions", isStringArray
, &kSCPropNetProxiesExceptionsList
, NULL
, NULL
},
990 { "ExcludeSimpleHostnames", NULL
, isBoolean
, &kSCPropNetProxiesExcludeSimpleHostnames
, NULL
, NULL
},
992 { "FTP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_FTP
},
993 { "Gopher" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_Gopher
},
994 { "HTTP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_HTTP
},
995 { "HTTPS" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_HTTPS
},
996 { "RTSP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_RTSP
},
997 { "SOCKS" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_SOCKS
},
998 { "ProxyAutoConfig" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_PAC
},
999 { ".pac" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_PAC
},
1000 { "ProxyAutoDiscovery" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_WPAD
},
1001 { "WPAD" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_WPAD
},
1003 { "disable" , NULL
, isOther
, NULL
, __doProxyEnable
, (void *)FALSE
},
1004 { "enable" , NULL
, isOther
, NULL
, __doProxyEnable
, (void *)TRUE
},
1005 { "proxy" , NULL
, isOther
, NULL
, __doProxyHost
, NULL
},
1006 { "host" , NULL
, isOther
, NULL
, __doProxyHost
, NULL
},
1007 { "port" , NULL
, isOther
, NULL
, __doProxyPort
, NULL
},
1008 { "url" , NULL
, isOther
, NULL
, __doProxyURL
, NULL
},
1009 // (ftp) proxy modifiers
1010 { "FTPPassive" , NULL
, isBoolean
, &kSCPropNetProxiesFTPPassive
, __doProxyFTPPassive
, NULL
},
1011 { "passive" , NULL
, isBoolean
, &kSCPropNetProxiesFTPPassive
, __doProxyFTPPassive
, NULL
},
1013 { "?" , NULL
, isHelp
, NULL
, NULL
,
1014 "\nProxy configuration commands\n\n"
1015 " set protocol ExceptionsList exception[,exception-2]\n"
1016 " set protocol ExcludeSimpleHostnames {enable|disable}\n"
1018 " set protocol ftp {enable|disable}\n"
1019 " set protocol ftp host proxy-host\n"
1020 " set protocol ftp port proxy-port\n"
1021 " set protocol ftp passive {enable|disable}\n"
1023 " set protocol http {enable|disable}\n"
1024 " set protocol http host proxy-host\n"
1025 " set protocol http port proxy-port\n"
1027 " set protocol https {enable|disable}\n"
1028 " set protocol https host proxy-host\n"
1029 " set protocol https port proxy-port\n"
1031 " set protocol rtsp {enable|disable}\n"
1032 " set protocol rtsp host proxy-host\n"
1033 " set protocol rtsp port proxy-port\n"
1035 " set protocol socks {enable|disable}\n"
1036 " set protocol socks host proxy-host\n"
1037 " set protocol socks port proxy-port\n"
1039 " set protocol .pac {enable|disable}\n"
1040 " set protocol .pac url .pac-url\n"
1042 " set protocol wpad {enable|disable}\n"
1045 #define N_PROXY_OPTIONS (sizeof(proxyOptions) / sizeof(proxyOptions[0]))
1049 __doProxySelect(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1052 #pragma unused(description)
1053 #pragma unused(newConfiguration)
1057 SCPrint(TRUE
, stdout
, CFSTR("proxy option[s] not specified\n"));
1061 currentProxy
= (proxyKeys
*)info
;
1063 nextOption
= _find_option(argv
[0], proxyOptions
, N_PROXY_OPTIONS
);
1064 if ((nextOption
== kCFNotFound
) ||
1065 (proxyOptions
[nextOption
].handler
== __doProxySelect
)) {
1066 SCPrint(TRUE
, stdout
, CFSTR("%s proxy option[s] not specified\n"), currentProxy
->proxy
);
1075 __doProxyEnable(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1078 #pragma unused(description)
1079 #pragma unused(argc)
1080 #pragma unused(argv)
1081 Boolean enabled
= (info
== (void *)FALSE
) ? FALSE
: TRUE
;
1083 if (currentProxy
== NULL
) {
1084 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1088 if (currentProxy
->keyEnable
== NULL
) {
1089 SCPrint(TRUE
, stdout
, CFSTR("%s proxy cannot be %s\n"),
1090 currentProxy
->proxy
,
1091 enabled
? "enabled" : "disabled");
1097 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyEnable
), CFNumberRef_1
);
1099 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyEnable
));
1101 if (currentProxy
->keyProxy
!= NULL
) {
1102 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyProxy
));
1105 if (currentProxy
->keyPort
!= NULL
) {
1106 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyPort
));
1109 if (currentProxy
->keyURL
!= NULL
) {
1110 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyURL
));
1119 __proxy_enabled(CFDictionaryRef configuration
, const CFStringRef
*enableKey
)
1124 if (enableKey
== NULL
) {
1125 return TRUE
; // if proxy does not need to be enabled
1128 num
= CFDictionaryGetValue(configuration
, *enableKey
);
1129 if (!isA_CFNumber(num
) ||
1130 !CFNumberGetValue(num
, kCFNumberIntType
, &val
) ||
1132 return FALSE
; // if not enabled
1140 __doProxyHost(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1143 #pragma unused(description)
1144 #pragma unused(info)
1145 if (currentProxy
== NULL
) {
1146 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1150 if (currentProxy
->keyProxy
== NULL
) {
1151 SCPrint(TRUE
, stdout
, CFSTR("%s proxy host cannot be specified\n"), currentProxy
->proxy
);
1155 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1156 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1161 SCPrint(TRUE
, stdout
, CFSTR("%s proxy host not specified\n"), currentProxy
->proxy
);
1165 if (strlen(argv
[0]) > 0) {
1168 host
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1169 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyProxy
), host
);
1172 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyProxy
));
1180 __doProxyPort(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1183 #pragma unused(description)
1184 #pragma unused(info)
1185 if (currentProxy
== NULL
) {
1186 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1190 if (currentProxy
->keyPort
== NULL
) {
1191 SCPrint(TRUE
, stdout
, CFSTR("%s proxy port cannot be specified\n"), currentProxy
->proxy
);
1195 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1196 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1201 SCPrint(TRUE
, stdout
, CFSTR("%s proxy port not specified\n"), currentProxy
->proxy
);
1205 if (strlen(argv
[0]) > 0) {
1209 num
= _copy_number(argv
[0]);
1210 if (!isA_CFNumber(num
) ||
1211 !CFNumberGetValue(num
, kCFNumberIntType
, &port
) ||
1212 (port
< 0) || (port
> 65535)) {
1213 SCPrint(TRUE
, stdout
, CFSTR("invalid %s proxy port number\n"), currentProxy
->proxy
);
1214 if (num
!= NULL
) CFRelease(num
);
1218 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyPort
), num
);
1221 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyPort
));
1229 __doProxyURL(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1232 #pragma unused(description)
1233 #pragma unused(info)
1234 if (currentProxy
== NULL
) {
1235 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1239 if (currentProxy
->keyURL
== NULL
) {
1240 SCPrint(TRUE
, stdout
, CFSTR("%s proxy URL cannot be specified\n"), currentProxy
->proxy
);
1244 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1245 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1250 SCPrint(TRUE
, stdout
, CFSTR("%s proxy URL not specified\n"), currentProxy
->proxy
);
1254 if (strlen(argv
[0]) > 0) {
1257 url
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1258 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyURL
), url
);
1261 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyURL
));
1269 __doProxyFTPPassive(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1272 #pragma unused(description)
1273 #pragma unused(info)
1274 #pragma unused(argc)
1275 #pragma unused(argv)
1276 #pragma unused(newConfiguration)
1277 if (currentProxy
== NULL
) {
1278 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1282 if (currentProxy
!= &proxyKeys_FTP
) {
1283 SCPrint(TRUE
, stdout
, CFSTR("passive can only be enable for FTP proxy\n"));
1292 set_protocol_proxies(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1296 ok
= _process_options(proxyOptions
, N_PROXY_OPTIONS
, argc
, argv
, newConfiguration
);
1305 #if !TARGET_OS_IPHONE
1308 static CF_RETURNS_RETAINED CFStringRef
1309 __cleanupName(CFStringRef name
)
1311 CFMutableStringRef newName
;
1313 newName
= CFStringCreateMutableCopy(NULL
, 0, name
);
1314 CFStringTrimWhitespace(newName
);
1315 if (CFStringGetLength(newName
) == 0) {
1325 __doSMBName(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("NetBIOS name 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 NetBIOS name\n"));
1350 CFDictionaryRemoveValue(newConfiguration
, key
);
1358 __doSMBWorkgroup(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1360 #pragma unused(description)
1361 #pragma unused(info)
1363 SCPrint(TRUE
, stdout
, CFSTR("Workgroup not specified\n"));
1367 if (strlen(argv
[0]) > 0) {
1371 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1372 name
= __cleanupName(str
);
1376 CFDictionarySetValue(newConfiguration
, key
, name
);
1379 SCPrint(TRUE
, stdout
, CFSTR("invalid Workgroup\n"));
1383 CFDictionaryRemoveValue(newConfiguration
, key
);
1391 __doSMBWINSAddresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1393 #pragma unused(description)
1394 #pragma unused(info)
1395 CFMutableArrayRef servers
;
1398 SCPrint(TRUE
, stdout
, CFSTR("WINS address(es) not specified\n"));
1402 servers
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1404 if (strlen(argv
[0]) > 0) {
1410 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1411 array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
1414 n
= (array
!= NULL
) ? CFArrayGetCount(array
) : 0;
1415 for (i
= 0; i
< n
; i
++) {
1418 if (_SC_cfstring_to_cstring(CFArrayGetValueAtIndex(array
, i
),
1421 kCFStringEncodingUTF8
) != NULL
) {
1424 server
= __copyIPv4Address(str
);
1425 //if (server == NULL) {
1426 // server = __copyIPv6Address(str);
1428 if (server
!= NULL
) {
1429 CFArrayAppendValue(servers
, server
);
1435 SCPrint(TRUE
, stdout
, CFSTR("invalid WINS address\n"));
1440 if (array
!= NULL
) CFRelease(array
);
1443 if (CFArrayGetCount(servers
) > 0) {
1444 CFDictionarySetValue(newConfiguration
, key
, servers
);
1446 CFDictionaryRemoveValue(newConfiguration
, key
);
1454 static selections smbNodeTypes
[] = {
1455 { CFSTR("Broadcast"), &kSCValNetSMBNetBIOSNodeTypeBroadcast
, 0 },
1456 { CFSTR("Peer") , &kSCValNetSMBNetBIOSNodeTypePeer
, 0 },
1457 { CFSTR("Mixed") , &kSCValNetSMBNetBIOSNodeTypeMixed
, 0 },
1458 { CFSTR("Hybrid") , &kSCValNetSMBNetBIOSNodeTypeHybrid
, 0 },
1463 static options smbOptions
[] = {
1464 { "NetBIOSName" , "name" , isOther
, &kSCPropNetSMBNetBIOSName
, __doSMBName
, NULL
},
1465 { "name" , "name" , isOther
, &kSCPropNetSMBNetBIOSName
, __doSMBName
, NULL
},
1466 { "NetBIOSNodeType", "type" , isChooseOne
, &kSCPropNetSMBNetBIOSNodeType
, NULL
, (void *)smbNodeTypes
},
1467 { "type", "type" , isChooseOne
, &kSCPropNetSMBNetBIOSNodeType
, NULL
, (void *)smbNodeTypes
},
1468 { "Workgroup" , "workgroup", isOther
, &kSCPropNetSMBWorkgroup
, __doSMBWorkgroup
, NULL
},
1469 { "WINSAddresses" , "wins" , isOther
, &kSCPropNetSMBWINSAddresses
, __doSMBWINSAddresses
, NULL
},
1470 { "wins" , "wins" , isOther
, &kSCPropNetSMBWINSAddresses
, __doSMBWINSAddresses
, NULL
},
1472 { "?" , NULL
, isHelp
, NULL
, NULL
,
1473 "\nSMB configuration commands\n\n"
1474 " set protocol name NetBIOS-name\n"
1475 " set protocol type (Broadcast|Peer|Mixed|Hybrid)\n"
1476 " set protocol workgroup SMB-workgroup\n"
1477 " set protocol wins x1.x1.x1.x1[,x2.x2.x2.x2]\n"
1480 #define N_SMB_OPTIONS (sizeof(smbOptions) / sizeof(smbOptions[0]))
1484 set_protocol_smb(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1488 ok
= _process_options(smbOptions
, N_SMB_OPTIONS
, argc
, argv
, newConfiguration
);
1493 #endif // !TARGET_OS_IPHONE
1497 #pragma mark *Protocol*
1502 set_protocol(int argc
, char **argv
)
1504 CFDictionaryRef configuration
;
1505 CFMutableDictionaryRef newConfiguration
= NULL
;
1507 CFStringRef protocolType
;
1509 if (net_protocol
== NULL
) {
1510 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
1515 SCPrint(TRUE
, stdout
, CFSTR("set what?\n"));
1519 configuration
= SCNetworkProtocolGetConfiguration(net_protocol
);
1520 if (configuration
== NULL
) {
1521 newConfiguration
= CFDictionaryCreateMutable(NULL
,
1523 &kCFTypeDictionaryKeyCallBacks
,
1524 &kCFTypeDictionaryValueCallBacks
);
1526 newConfiguration
= CFDictionaryCreateMutableCopy(NULL
, 0, configuration
);
1527 CFDictionaryRemoveValue(newConfiguration
, kSCResvInactive
);
1530 protocolType
= SCNetworkProtocolGetProtocolType(net_protocol
);
1531 if (CFEqual(protocolType
, kSCNetworkProtocolTypeDNS
)) {
1532 ok
= set_protocol_dns(argc
, argv
, newConfiguration
);
1533 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv4
)) {
1534 ok
= set_protocol_ipv4(argc
, argv
, newConfiguration
);
1535 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv6
)) {
1536 ok
= set_protocol_ipv6(argc
, argv
, newConfiguration
);
1537 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeProxies
)) {
1538 ok
= set_protocol_proxies(argc
, argv
, newConfiguration
);
1539 #if !TARGET_OS_IPHONE
1540 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeSMB
)) {
1541 ok
= set_protocol_smb(argc
, argv
, newConfiguration
);
1542 #endif // !TARGET_OS_IPHONE
1544 SCPrint(TRUE
, stdout
, CFSTR("this protocols configuration cannot be changed\n"));
1551 if (((configuration
== NULL
) && (CFDictionaryGetCount(newConfiguration
) > 0)) ||
1552 ((configuration
!= NULL
) && !CFEqual(configuration
, newConfiguration
))) {
1553 if (!SCNetworkProtocolSetConfiguration(net_protocol
, newConfiguration
)) {
1554 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
1558 _prefs_changed
= TRUE
;
1563 if (newConfiguration
!= NULL
) CFRelease(newConfiguration
);
1568 /* -------------------- */
1573 show_protocol(int argc
, char **argv
)
1575 CFDictionaryRef configuration
;
1576 SCNetworkProtocolRef protocol
= NULL
;
1577 CFStringRef protocolType
;
1580 protocol
= _find_protocol(argv
[0]);
1582 if (net_protocol
!= NULL
) {
1583 protocol
= net_protocol
;
1585 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
1590 if (protocol
== NULL
) {
1594 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1595 SCPrint(TRUE
, stdout
, CFSTR("protocol type = %@\n"), protocolType
);
1597 configuration
= SCNetworkProtocolGetConfiguration(protocol
);
1598 if (configuration
!= NULL
) {
1599 SCPrint(TRUE
, stdout
, CFSTR("\n protocol configuration\n"));
1600 _show_entity(configuration
, CFSTR(""));
1604 SCPrint(TRUE
, stdout
, CFSTR("\n%@\n"), protocol
);
1611 /* -------------------- */
1616 show_protocols(int argc
, char **argv
)
1618 #pragma unused(argc)
1619 #pragma unused(argv)
1623 if (prefs
== NULL
) {
1624 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
1628 if (net_service
== NULL
) {
1629 SCPrint(TRUE
, stdout
, CFSTR("service not selected\n"));
1633 if (protocols
!= NULL
) CFRelease(protocols
);
1634 protocols
= SCNetworkServiceCopyProtocols(net_service
);
1635 if (protocols
== NULL
) {
1636 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
1640 n
= CFArrayGetCount(protocols
);
1642 CFMutableArrayRef sorted
;
1644 sorted
= CFArrayCreateMutableCopy(NULL
, 0, protocols
);
1645 CFArraySortValues(sorted
,
1647 _SCNetworkProtocolCompare
,
1649 CFRelease(protocols
);
1653 for (i
= 0; i
< n
; i
++) {
1654 SCNetworkProtocolRef protocol
;
1655 CFStringRef protocolType
;
1657 protocol
= CFArrayGetValueAtIndex(protocols
, i
);
1658 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1660 SCPrint(TRUE
, stdout
, CFSTR("%c%2ld: %@%*s :"),
1661 ((net_protocol
!= NULL
) && CFEqual(protocol
, net_protocol
)) ? '>' : ' ',
1664 (int)(sizeof("Proxies") - CFStringGetLength(protocolType
) - 1),
1667 if (SCNetworkProtocolGetEnabled(protocol
)) {
1668 CFStringRef description
;
1670 description
= _protocol_description(protocol
, FALSE
);
1671 SCPrint(TRUE
, stdout
, CFSTR(" %@"), description
);
1672 CFRelease(description
);
1674 SCPrint(TRUE
, stdout
, CFSTR(" *DISABLED*"));
1676 SCPrint(TRUE
, stdout
, CFSTR("\n"));
1683 /* -------------------- */
1687 CF_RETURNS_RETAINED CFStringRef
1688 _protocol_description(SCNetworkProtocolRef protocol
, Boolean skipEmpty
)
1690 CFDictionaryRef configuration
;
1691 CFMutableStringRef description
= NULL
;
1692 CFStringRef protocolType
;
1694 description
= CFStringCreateMutable(NULL
, 0);
1696 if (!SCNetworkProtocolGetEnabled(protocol
)) {
1700 configuration
= SCNetworkProtocolGetConfiguration(protocol
);
1701 if (configuration
== NULL
) {
1705 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1706 if (CFEqual(protocolType
, kSCNetworkProtocolTypeDNS
)) {
1711 domain
= CFDictionaryGetValue(configuration
, kSCPropNetDNSDomainName
);
1712 if (isA_CFString(domain
)) {
1713 CFStringAppendFormat(description
,
1719 search
= CFDictionaryGetValue(configuration
, kSCPropNetDNSSearchDomains
);
1720 if (isA_CFArray(search
)) {
1723 str
= CFStringCreateByCombiningStrings(NULL
, search
, CFSTR(","));
1724 CFStringAppendFormat(description
,
1726 CFSTR("%ssearch=%@"),
1727 CFStringGetLength(description
) > 0 ? ", " : "",
1732 servers
= CFDictionaryGetValue(configuration
, kSCPropNetDNSServerAddresses
);
1733 if (isA_CFArray(servers
)) {
1736 str
= CFStringCreateByCombiningStrings(NULL
, servers
, CFSTR(","));
1737 CFStringAppendFormat(description
,
1739 CFSTR("%sservers=%@"),
1740 CFStringGetLength(description
) > 0 ? ", " : "",
1744 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv4
)) {
1747 method
= CFDictionaryGetValue(configuration
, kSCPropNetIPv4ConfigMethod
);
1748 if (isA_CFString(method
)) {
1749 CFArrayRef addresses
;
1751 addresses
= CFDictionaryGetValue(configuration
, kSCPropNetIPv4Addresses
);
1752 if (CFEqual(method
, kSCValNetIPv4ConfigMethodINFORM
) &&
1753 isA_CFArray(addresses
)) {
1754 CFStringAppendFormat(description
,
1756 CFSTR("%@, address=%@"),
1758 CFArrayGetValueAtIndex(addresses
, 0));
1759 } else if (CFEqual(method
, kSCValNetIPv4ConfigMethodManual
) &&
1760 isA_CFArray(addresses
)) {
1764 CFStringAppendFormat(description
,
1766 CFSTR("%@, address=%@"),
1768 CFArrayGetValueAtIndex(addresses
, 0));
1770 if (CFDictionaryGetValueIfPresent(configuration
,
1771 kSCPropNetIPv4SubnetMasks
,
1772 (const void **)&masks
) &&
1773 isA_CFArray(masks
)) {
1774 CFStringAppendFormat(description
,
1777 CFArrayGetValueAtIndex(masks
, 0));
1780 if (CFDictionaryGetValueIfPresent(configuration
,
1781 kSCPropNetIPv4Router
,
1782 (const void **)&router
) &&
1783 isA_CFString(router
)) {
1784 CFStringAppendFormat(description
,
1786 CFSTR(", router=%@"),
1790 CFStringAppendFormat(description
,
1796 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv6
)) {
1799 method
= CFDictionaryGetValue(configuration
, kSCPropNetIPv6ConfigMethod
);
1800 if (isA_CFString(method
)) {
1801 CFStringAppendFormat(description
,
1806 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeProxies
)) {
1807 static proxyKeys
*keys
[] = { &proxyKeys_FTP
, &proxyKeys_Gopher
, &proxyKeys_HTTP
, &proxyKeys_HTTPS
,
1808 &proxyKeys_RTSP
, &proxyKeys_SOCKS
, &proxyKeys_PAC
, &proxyKeys_WPAD
};
1810 for (size_t i
= 0; i
< sizeof(keys
)/sizeof(keys
[0]); i
++) {
1811 proxyKeys
*currentProxy
= keys
[i
];
1813 if (!__proxy_enabled(configuration
, currentProxy
->keyEnable
)) {
1817 if (((currentProxy
->keyProxy
!= NULL
) &&
1818 !CFDictionaryContainsKey(configuration
, *(currentProxy
->keyProxy
))) ||
1819 ((currentProxy
->keyURL
!= NULL
) &&
1820 !CFDictionaryContainsKey(configuration
, *(currentProxy
->keyURL
)))) {
1824 CFStringAppendFormat(description
,
1827 CFStringGetLength(description
) > 0 ? ", " : "",
1828 currentProxy
->proxy
);
1830 #if !TARGET_OS_IPHONE
1831 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeSMB
)) {
1834 CFStringRef workgroup
;
1836 name
= CFDictionaryGetValue(configuration
, kSCPropNetSMBNetBIOSName
);
1837 if (isA_CFString(name
)) {
1838 CFStringAppendFormat(description
,
1840 CFSTR("NetBIOS name=%@"),
1844 workgroup
= CFDictionaryGetValue(configuration
, kSCPropNetSMBWorkgroup
);
1845 if (isA_CFString(workgroup
)) {
1846 CFStringAppendFormat(description
,
1848 CFSTR("Workgroup=%@"),
1852 servers
= CFDictionaryGetValue(configuration
, kSCPropNetSMBWINSAddresses
);
1853 if (isA_CFArray(servers
)) {
1856 str
= CFStringCreateByCombiningStrings(NULL
, servers
, CFSTR(","));
1857 CFStringAppendFormat(description
,
1859 CFSTR("%sWINS servers=%@"),
1860 CFStringGetLength(description
) > 0 ? ", " : "",
1864 #endif // !TARGET_OS_IPHONE
1869 if (skipEmpty
&& CFStringGetLength(description
) == 0) {
1870 CFRelease(description
);