2 * Copyright (c) 2004-2009, 2011 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 /* -------------------- */
49 _compare_protocols(const void *val1
, const void *val2
, void *context
)
51 SCNetworkProtocolRef p1
= (SCNetworkProtocolRef
)val1
;
52 SCNetworkProtocolRef p2
= (SCNetworkProtocolRef
)val2
;
56 type1
= SCNetworkProtocolGetProtocolType(p1
);
57 type2
= SCNetworkProtocolGetProtocolType(p2
);
59 return CFStringCompare(type1
, type2
, 0);
64 __copyIPv4Address(const char *arg
)
67 struct sockaddr_in sin
;
69 if (_SC_string_to_sockaddr(arg
, AF_INET
, (void *)&sin
, sizeof(sin
)) == NULL
) {
73 _SC_sockaddr_to_string((struct sockaddr
*)&sin
, buf
, sizeof(buf
));
74 return CFStringCreateWithCString(NULL
, buf
, kCFStringEncodingUTF8
);
79 __copyIPv6Address(const char *arg
)
82 struct sockaddr_in6 sin6
;
84 if (_SC_string_to_sockaddr(arg
, AF_INET6
, (void *)&sin6
, sizeof(sin6
)) == NULL
) {
88 _SC_sockaddr_to_string((struct sockaddr
*)&sin6
, buf
, sizeof(buf
));
89 return CFStringCreateWithCString(NULL
, buf
, kCFStringEncodingUTF8
);
93 /* -------------------- */
96 static SCNetworkProtocolRef
97 _find_protocol(char *match
)
99 Boolean allowIndex
= TRUE
;
102 CFStringRef select_name
= NULL
;
103 SCNetworkProtocolRef selected
= NULL
;
105 if (protocols
== NULL
) {
106 if (net_service
== NULL
) {
107 SCPrint(TRUE
, stdout
, CFSTR("network service not selected\n"));
111 protocols
= SCNetworkServiceCopyProtocols(net_service
);
112 if (protocols
== NULL
) {
113 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
117 n
= CFArrayGetCount(protocols
);
119 CFMutableArrayRef sorted
;
121 sorted
= CFArrayCreateMutableCopy(NULL
, 0, protocols
);
122 CFArraySortValues(sorted
,
126 CFRelease(protocols
);
133 // try to select the protocol by its protocol type
135 select_name
= CFStringCreateWithCString(NULL
, match
, kCFStringEncodingUTF8
);
137 n
= CFArrayGetCount(protocols
);
138 for (i
= 0; i
< n
; i
++) {
139 SCNetworkProtocolRef protocol
;
142 protocol
= CFArrayGetValueAtIndex(protocols
, i
);
143 type
= SCNetworkProtocolGetProtocolType(protocol
);
144 if (CFStringCompare(select_name
, type
, kCFCompareCaseInsensitive
) == kCFCompareEqualTo
) {
155 // try to select the protocol by its index
158 val
= strtol(str
, &end
, 10);
159 if ((*str
!= '\0') && (*end
== '\0') && (errno
== 0)) {
160 if ((val
> 0) && (val
<= n
)) {
161 selected
= CFArrayGetValueAtIndex(protocols
, val
- 1);
166 if (selected
!= NULL
) {
170 SCPrint(TRUE
, stdout
, CFSTR("no match, which protocol?\n"));
174 if (select_name
!= NULL
) CFRelease(select_name
);
179 /* -------------------- */
184 create_protocol(int argc
, char **argv
)
186 SCNetworkInterfaceRef interface
;
187 CFStringRef protocolType
;
189 if ((argc
< 1) || (strlen(argv
[0]) == 0)) {
190 SCPrint(TRUE
, stdout
, CFSTR("what protocol type?\n"));
194 if (net_service
== NULL
) {
195 SCPrint(TRUE
, stdout
, CFSTR("network service not selected\n"));
199 protocolType
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
201 interface
= SCNetworkServiceGetInterface(net_service
);
202 if (interface
!= NULL
) {
203 CFArrayRef supported
;
207 supported
= SCNetworkInterfaceGetSupportedProtocolTypes(interface
);
208 n
= (supported
!= NULL
) ? CFArrayGetCount(supported
) : 0;
209 for (i
= 0; i
< n
; i
++) {
210 CFStringRef supportedType
;
212 supportedType
= CFArrayGetValueAtIndex(supported
, i
);
213 if (CFStringCompare(protocolType
,
215 kCFCompareCaseInsensitive
) == kCFCompareEqualTo
) {
216 CFRelease(protocolType
);
217 protocolType
= CFRetain(supportedType
);
223 if (!SCNetworkServiceAddProtocolType(net_service
, protocolType
)) {
224 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
228 _prefs_changed
= TRUE
;
230 if (protocols
!= NULL
) {
231 CFRelease(protocols
);
235 if (net_protocol
!= NULL
) CFRelease(net_protocol
);
236 // net_protocol = NULL;
238 net_protocol
= SCNetworkServiceCopyProtocol(net_service
, protocolType
);
239 if (net_protocol
== NULL
) {
240 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
244 SCPrint(TRUE
, stdout
,
245 CFSTR("protocol \"%@\" selected\n"),
250 CFRelease(protocolType
);
255 /* -------------------- */
260 disable_protocol(int argc
, char **argv
)
262 SCNetworkProtocolRef protocol
= NULL
;
265 protocol
= _find_protocol(argv
[0]);
267 if (net_protocol
!= NULL
) {
268 protocol
= net_protocol
;
270 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
275 if (protocol
== NULL
) {
279 if (!SCNetworkProtocolSetEnabled(protocol
, FALSE
)) {
280 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
284 _prefs_changed
= TRUE
;
290 /* -------------------- */
295 enable_protocol(int argc
, char **argv
)
297 SCNetworkProtocolRef protocol
= NULL
;
300 protocol
= _find_protocol(argv
[0]);
302 if (net_protocol
!= NULL
) {
303 protocol
= net_protocol
;
305 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
310 if (protocol
== NULL
) {
314 if (!SCNetworkProtocolSetEnabled(protocol
, TRUE
)) {
315 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
319 _prefs_changed
= TRUE
;
325 /* -------------------- */
330 remove_protocol(int argc
, char **argv
)
332 SCNetworkProtocolRef protocol
= NULL
;
333 CFStringRef protocolType
;
336 protocol
= _find_protocol(argv
[0]);
338 if (net_protocol
!= NULL
) {
339 protocol
= net_protocol
;
341 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
346 if (protocol
== NULL
) {
352 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
353 if (!SCNetworkServiceRemoveProtocolType(net_service
, protocolType
)) {
354 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
358 _prefs_changed
= TRUE
;
360 SCPrint(TRUE
, stdout
,
361 CFSTR("protocol \"%@\" removed\n"),
364 if ((net_protocol
!= NULL
) && CFEqual(protocol
, net_protocol
)) {
365 CFRelease(net_protocol
);
367 SCPrint(TRUE
, stdout
, CFSTR("& no protocol selected\n"));
370 if (protocols
!= NULL
) {
371 CFRelease(protocols
);
382 /* -------------------- */
387 select_protocol(int argc
, char **argv
)
389 SCNetworkProtocolRef protocol
;
391 protocol
= _find_protocol(argv
[0]);
393 if (protocol
== NULL
) {
397 if (net_protocol
!= NULL
) CFRelease(net_protocol
);
398 net_protocol
= CFRetain(protocol
);
400 SCPrint(TRUE
, stdout
,
401 CFSTR("protocol \"%@\" selected\n"),
402 SCNetworkProtocolGetProtocolType(protocol
));
412 static CF_RETURNS_RETAINED CFStringRef
413 __cleanupDomainName(CFStringRef domain
)
415 CFMutableStringRef newDomain
;
417 newDomain
= CFStringCreateMutableCopy(NULL
, 0, domain
);
418 CFStringTrimWhitespace(newDomain
);
419 CFStringTrim(newDomain
, CFSTR("."));
420 if (CFStringGetLength(newDomain
) == 0) {
421 CFRelease(newDomain
);
430 __doDNSDomain(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
433 SCPrint(TRUE
, stdout
, CFSTR("DNS domain name not specified\n"));
437 if (strlen(argv
[0]) > 0) {
441 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
442 domain
= __cleanupDomainName(str
);
445 if (domain
!= NULL
) {
446 CFDictionarySetValue(newConfiguration
, key
, domain
);
449 SCPrint(TRUE
, stdout
, CFSTR("invalid DNS domain name\n"));
453 CFDictionaryRemoveValue(newConfiguration
, key
);
461 __doDNSDomainArray(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
463 CFMutableArrayRef domains
;
466 SCPrint(TRUE
, stdout
, CFSTR("DNS search domain name(s) not specified\n"));
470 domains
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
472 if (strlen(argv
[0]) > 0) {
476 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
477 array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
482 CFIndex n
= CFArrayGetCount(array
);
484 for (i
= 0; i
< n
; i
++) {
487 domain
= __cleanupDomainName(CFArrayGetValueAtIndex(array
, i
));
488 if (domain
!= NULL
) {
489 CFArrayAppendValue(domains
, domain
);
494 SCPrint(TRUE
, stdout
, CFSTR("invalid DNS search domain name\n"));
502 if (CFArrayGetCount(domains
) > 0) {
503 CFDictionarySetValue(newConfiguration
, key
, domains
);
505 CFDictionaryRemoveValue(newConfiguration
, key
);
514 __doDNSServerAddresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
516 CFMutableArrayRef servers
;
519 SCPrint(TRUE
, stdout
, CFSTR("DNS name server address(es) not specified\n"));
523 servers
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
525 if (strlen(argv
[0]) > 0) {
531 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
532 array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
535 n
= (array
!= NULL
) ? CFArrayGetCount(array
) : 0;
536 for (i
= 0; i
< n
; i
++) {
539 if (_SC_cfstring_to_cstring(CFArrayGetValueAtIndex(array
, i
),
542 kCFStringEncodingUTF8
) != NULL
) {
545 server
= __copyIPv4Address(str
);
546 if (server
== NULL
) {
547 server
= __copyIPv6Address(str
);
549 if (server
!= NULL
) {
550 CFArrayAppendValue(servers
, server
);
556 SCPrint(TRUE
, stdout
, CFSTR("invalid DNS name server address\n"));
561 if (array
!= NULL
) CFRelease(array
);
564 if (CFArrayGetCount(servers
) > 0) {
565 CFDictionarySetValue(newConfiguration
, key
, servers
);
567 CFDictionaryRemoveValue(newConfiguration
, key
);
575 static options dnsOptions
[] = {
576 { "DomainName" , "domain" , isOther
, &kSCPropNetDNSDomainName
, __doDNSDomain
, NULL
},
577 { "domain" , "domain" , isOther
, &kSCPropNetDNSDomainName
, __doDNSDomain
, NULL
},
578 { "SearchDomains" , "search" , isOther
, &kSCPropNetDNSSearchDomains
, __doDNSDomainArray
, NULL
},
579 { "search" , "search" , isOther
, &kSCPropNetDNSSearchDomains
, __doDNSDomainArray
, NULL
},
580 { "ServerAddresses", "address", isOther
, &kSCPropNetDNSServerAddresses
, __doDNSServerAddresses
, NULL
},
581 { "nameserver" , "address", isOther
, &kSCPropNetDNSServerAddresses
, __doDNSServerAddresses
, NULL
},
582 { "nameservers" , "address", isOther
, &kSCPropNetDNSServerAddresses
, __doDNSServerAddresses
, NULL
},
583 { "SupplementalMatchDomains",
586 &kSCPropNetDNSSupplementalMatchDomains
,
590 { "?" , NULL
, isHelp
, NULL
, NULL
,
591 "\nDNS configuration commands\n\n"
592 " set protocol search domain-name[,domain-name-2]\n"
593 " set protocol nameserver x1.x1.x1.x1[,x2.x2.x2.x2]\n"
596 #define N_DNS_OPTIONS (sizeof(dnsOptions) / sizeof(dnsOptions[0]))
600 set_protocol_dns(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
604 ok
= _process_options(dnsOptions
, N_DNS_OPTIONS
, argc
, argv
, newConfiguration
);
613 #define allowIPv4Address 1<<1 // allow address
614 #define allowIPv4Netmask 1<<2 // allow subnet mask
615 #define allowIPv4Router 1<<3 // allow router
616 #define allowIPv4DHCPClientID 1<<4 // allow DCHP Client ID
618 static selections ipv4ConfigMethods
[] = {
619 { CFSTR("BOOTP") , &kSCValNetIPv4ConfigMethodBOOTP
, 0 },
620 { CFSTR("DHCP") , &kSCValNetIPv4ConfigMethodDHCP
, allowIPv4DHCPClientID
},
621 { CFSTR("INFORM") , &kSCValNetIPv4ConfigMethodINFORM
, allowIPv4Address
},
622 { CFSTR("LinkLocal"), &kSCValNetIPv4ConfigMethodLinkLocal
, 0 },
623 { CFSTR("Manual") , &kSCValNetIPv4ConfigMethodManual
, allowIPv4Address
|allowIPv4Netmask
|allowIPv4Router
},
624 { CFSTR("PPP") , &kSCValNetIPv4ConfigMethodPPP
, allowIPv4Address
|selectionNotAvailable
},
630 __doIPv4ConfigMethod(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
636 method
= CFDictionaryGetValue(newConfiguration
, key
);
637 methodIndex
= _find_selection(method
, (selections
*)ipv4ConfigMethods
, &flags
);
638 if (methodIndex
!= kCFNotFound
) {
639 if (!(flags
& allowIPv4Address
)) {
640 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv4Addresses
);
642 if (!(flags
& allowIPv4Netmask
)) {
643 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv4SubnetMasks
);
645 if (!(flags
& allowIPv4Router
)) {
646 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv4Router
);
648 if (!(flags
& allowIPv4DHCPClientID
)) {
649 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv4DHCPClientID
);
652 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
661 __doIPv4Addresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
663 Boolean useArray
= (info
== (void *)FALSE
) ? FALSE
: TRUE
;
665 if (strlen(argv
[0]) > 0) {
668 address
= __copyIPv4Address(argv
[0]);
669 if (address
!= NULL
) {
671 CFArrayRef addresses
;
673 addresses
= CFArrayCreate(NULL
, (const void **)&address
, 1, &kCFTypeArrayCallBacks
);
674 CFDictionarySetValue(newConfiguration
, key
, addresses
);
675 CFRelease(addresses
);
677 CFDictionarySetValue(newConfiguration
, key
, address
);
684 CFDictionaryRemoveValue(newConfiguration
, key
);
691 static options ipv4Options
[] = {
692 { "ConfigMethod", "configuration method"
693 , isChooseOne
, &kSCPropNetIPv4ConfigMethod
, __doIPv4ConfigMethod
, (void *)ipv4ConfigMethods
},
694 { "config" , "configuration method"
695 , isChooseOne
, &kSCPropNetIPv4ConfigMethod
, __doIPv4ConfigMethod
, (void *)ipv4ConfigMethods
},
696 { "Addresses" , "address" , isOther
, &kSCPropNetIPv4Addresses
, __doIPv4Addresses
, (void *)TRUE
},
697 { "address" , "address" , isOther
, &kSCPropNetIPv4Addresses
, __doIPv4Addresses
, (void *)TRUE
},
698 { "SubnetMasks" , "netmask" , isOther
, &kSCPropNetIPv4SubnetMasks
, __doIPv4Addresses
, (void *)TRUE
},
699 { "netmask" , "netmask" , isOther
, &kSCPropNetIPv4SubnetMasks
, __doIPv4Addresses
, (void *)TRUE
},
700 { "Router" , "address" , isOther
, &kSCPropNetIPv4Router
, __doIPv4Addresses
, (void *)FALSE
},
701 { "DHCPClientID", "client ID", isString
, &kSCPropNetIPv4DHCPClientID
, NULL
, NULL
},
703 { "?" , NULL
, isHelp
, NULL
, NULL
,
704 "\nIPv4 configuration commands\n\n"
705 " set protocol config {BOOTP|DHCP|INFORM|MANUAL}\n"
706 "\n w/config=BOOTP\n"
709 " set protocol dhcpclientid identifier\n"
710 "\n w/config=INFORM\n"
711 " set protocol address x.x.x.x\n"
712 "\n w/config=MANUAL\n"
713 " set protocol address x.x.x.x\n"
714 " set protocol netmask x.x.x.x\n"
715 " set protocol router x.x.x.x\n"
718 #define N_IPV4_OPTIONS (sizeof(ipv4Options) / sizeof(ipv4Options[0]))
722 set_protocol_ipv4(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
726 ok
= _process_options(ipv4Options
, N_IPV4_OPTIONS
, argc
, argv
, newConfiguration
);
732 // validate configuration
733 method
= CFDictionaryGetValue(newConfiguration
, kSCPropNetIPv4ConfigMethod
);
734 methodIndex
= _find_selection(method
, (selections
*)ipv4ConfigMethods
, &flags
);
735 if (methodIndex
== kCFNotFound
) {
736 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
740 if (!(flags
& allowIPv4Address
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv4Addresses
)) {
741 SCPrint(TRUE
, stdout
,
742 CFSTR("IP address not allowed with %@ configuration\n"),
743 ipv4ConfigMethods
[methodIndex
].selection
);
747 if (!(flags
& allowIPv4Netmask
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv4SubnetMasks
)) {
748 SCPrint(TRUE
, stdout
,
749 CFSTR("Subnet mask not allowed with %@ configuration\n"),
750 ipv4ConfigMethods
[methodIndex
].selection
);
754 if (!(flags
& allowIPv4Router
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv4Router
)) {
755 SCPrint(TRUE
, stdout
,
756 CFSTR("Default route not allowed with %@ configuration\n"),
757 ipv4ConfigMethods
[methodIndex
].selection
);
761 if (!(flags
& allowIPv4DHCPClientID
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv4DHCPClientID
)) {
762 SCPrint(TRUE
, stdout
,
763 CFSTR("DHCP client ID not allowed with %@ configuration\n"),
764 ipv4ConfigMethods
[methodIndex
].selection
);
777 #define allowIPv6Address 1<<1 // allow address
778 #define allowIPv6PrefixLength 1<<2 // allow prefix length
779 #define allowIPv6Router 1<<3 // allow router
781 static selections ipv6ConfigMethods
[] = {
782 { CFSTR("Automatic") , & kSCValNetIPv6ConfigMethodAutomatic
, 0 },
783 { CFSTR("Manual") , & kSCValNetIPv6ConfigMethodManual
, allowIPv6Address
|allowIPv6PrefixLength
|allowIPv6Router
},
784 { CFSTR("RouterAdvertisement"), & kSCValNetIPv6ConfigMethodRouterAdvertisement
, allowIPv6Address
},
785 { CFSTR("6to4") , & kSCValNetIPv6ConfigMethod6to4
, 0 },
791 __doIPv6ConfigMethod(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
797 method
= CFDictionaryGetValue(newConfiguration
, key
);
798 methodIndex
= _find_selection(method
, (selections
*)ipv6ConfigMethods
, &flags
);
799 if (methodIndex
!= kCFNotFound
) {
800 if (!(flags
& allowIPv6Address
)) {
801 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv6Addresses
);
803 if (!(flags
& allowIPv6PrefixLength
)) {
804 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv6PrefixLength
);
806 if (!(flags
& allowIPv6Router
)) {
807 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv6Router
);
810 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
819 __doIPv6Addresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
821 Boolean useArray
= (info
== (void *)FALSE
) ? FALSE
: TRUE
;
823 if (strlen(argv
[0]) > 0) {
826 address
= __copyIPv6Address(argv
[0]);
827 if (address
!= NULL
) {
829 CFArrayRef addresses
;
831 addresses
= CFArrayCreate(NULL
, (const void **)&address
, 1, &kCFTypeArrayCallBacks
);
832 CFDictionarySetValue(newConfiguration
, key
, addresses
);
833 CFRelease(addresses
);
835 CFDictionarySetValue(newConfiguration
, key
, address
);
842 CFDictionaryRemoveValue(newConfiguration
, key
);
849 static options ipv6Options
[] = {
850 { "ConfigMethod", "configuration method"
851 , isChooseOne
, &kSCPropNetIPv6ConfigMethod
, __doIPv6ConfigMethod
, (void *)ipv6ConfigMethods
},
852 { "config" , "configuration method"
853 , isChooseOne
, &kSCPropNetIPv6ConfigMethod
, __doIPv6ConfigMethod
, (void *)ipv6ConfigMethods
},
854 { "Addresses" , "address" , isOther
, &kSCPropNetIPv6Addresses
, __doIPv6Addresses
, (void *)TRUE
},
855 { "address" , "address" , isOther
, &kSCPropNetIPv6Addresses
, __doIPv6Addresses
, (void *)TRUE
},
856 { "PrefixLength", "prefix length", isNumber
, &kSCPropNetIPv6PrefixLength
, NULL
, NULL
},
857 { "Router" , "address" , isOther
, &kSCPropNetIPv6Router
, __doIPv6Addresses
, (void *)FALSE
},
859 { "?" , NULL
, isHelp
, NULL
, NULL
,
860 "\nIPv6 configuration commands\n\n"
861 " set protocol config {Automatic|MANUAL}\n"
862 "\n w/config=Automatic\n"
864 "\n w/config=MANUAL\n"
865 " set protocol address x:x:x:x:x:x\n"
866 " set protocol router x:x:x:x:x:x\n"
867 " set protocol prefixlength n\n"
870 #define N_IPV6_OPTIONS (sizeof(ipv6Options) / sizeof(ipv6Options[0]))
874 set_protocol_ipv6(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
878 ok
= _process_options(ipv6Options
, N_IPV6_OPTIONS
, argc
, argv
, newConfiguration
);
884 // validate configuration
885 method
= CFDictionaryGetValue(newConfiguration
, kSCPropNetIPv6ConfigMethod
);
886 methodIndex
= _find_selection(method
, (selections
*)ipv6ConfigMethods
, &flags
);
887 if (methodIndex
== kCFNotFound
) {
888 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
892 if (!(flags
& allowIPv6Address
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6Addresses
)) {
893 SCPrint(TRUE
, stdout
,
894 CFSTR("IP address not allowed with %@ configuration\n"),
895 ipv6ConfigMethods
[methodIndex
].selection
);
899 if (!(flags
& allowIPv6PrefixLength
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6PrefixLength
)) {
900 SCPrint(TRUE
, stdout
,
901 CFSTR("Prefix length not allowed with %@ configuration\n"),
902 ipv6ConfigMethods
[methodIndex
].selection
);
906 if (!(flags
& allowIPv6Router
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6Router
)) {
907 SCPrint(TRUE
, stdout
,
908 CFSTR("Router not allowed with %@ configuration\n"),
909 ipv6ConfigMethods
[methodIndex
].selection
);
922 typedef const struct {
924 const CFStringRef
*keyEnable
;
925 const CFStringRef
*keyProxy
;
926 const CFStringRef
*keyPort
;
927 const CFStringRef
*keyURL
;
930 static proxyKeys proxyKeys_FTP
= { "FTP" , &kSCPropNetProxiesFTPEnable
, &kSCPropNetProxiesFTPProxy
, &kSCPropNetProxiesFTPPort
, NULL
};
931 static proxyKeys proxyKeys_Gopher
= { "Gopher", &kSCPropNetProxiesGopherEnable
, &kSCPropNetProxiesGopherProxy
, &kSCPropNetProxiesGopherPort
, NULL
};
932 static proxyKeys proxyKeys_HTTP
= { "HTTP" , &kSCPropNetProxiesHTTPEnable
, &kSCPropNetProxiesHTTPProxy
, &kSCPropNetProxiesHTTPPort
, NULL
};
933 static proxyKeys proxyKeys_HTTPS
= { "HTTPS" , &kSCPropNetProxiesHTTPSEnable
, &kSCPropNetProxiesHTTPSProxy
, &kSCPropNetProxiesHTTPSPort
, NULL
};
934 static proxyKeys proxyKeys_RTSP
= { "RTSP" , &kSCPropNetProxiesRTSPEnable
, &kSCPropNetProxiesRTSPProxy
, &kSCPropNetProxiesRTSPPort
, NULL
};
935 static proxyKeys proxyKeys_SOCKS
= { "SOCKS" , &kSCPropNetProxiesSOCKSEnable
, &kSCPropNetProxiesSOCKSProxy
, &kSCPropNetProxiesSOCKSPort
, NULL
};
936 static proxyKeys proxyKeys_PAC
= { ".pac" , &kSCPropNetProxiesProxyAutoConfigEnable
, NULL
, NULL
, &kSCPropNetProxiesProxyAutoConfigURLString
};
937 static proxyKeys proxyKeys_WPAD
= { "WPAD" , &kSCPropNetProxiesProxyAutoDiscoveryEnable
, NULL
, NULL
, NULL
};
939 static proxyKeys
*currentProxy
= NULL
;
942 static int __doProxySelect (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
943 static int __doProxyEnable (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
944 static int __doProxyHost (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
945 static int __doProxyPort (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
946 static int __doProxyURL (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
947 static int __doProxyFTPPassive(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
950 static options proxyOptions
[] = {
952 { "ExceptionsList" , "exceptions", isStringArray
, &kSCPropNetProxiesExceptionsList
, NULL
, NULL
},
953 { "ExcludeSimpleHostnames", NULL
, isBoolean
, &kSCPropNetProxiesExcludeSimpleHostnames
, NULL
, NULL
},
955 { "FTP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_FTP
},
956 { "Gopher" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_Gopher
},
957 { "HTTP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_HTTP
},
958 { "HTTPS" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_HTTPS
},
959 { "RTSP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_RTSP
},
960 { "SOCKS" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_SOCKS
},
961 { "ProxyAutoConfig" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_PAC
},
962 { ".pac" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_PAC
},
963 { "ProxyAutoDiscovery" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_WPAD
},
964 { "WPAD" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_WPAD
},
966 { "disable" , NULL
, isOther
, NULL
, __doProxyEnable
, (void *)FALSE
},
967 { "enable" , NULL
, isOther
, NULL
, __doProxyEnable
, (void *)TRUE
},
968 { "proxy" , NULL
, isOther
, NULL
, __doProxyHost
, NULL
},
969 { "host" , NULL
, isOther
, NULL
, __doProxyHost
, NULL
},
970 { "port" , NULL
, isOther
, NULL
, __doProxyPort
, NULL
},
971 { "url" , NULL
, isOther
, NULL
, __doProxyURL
, NULL
},
972 // (ftp) proxy modifiers
973 { "FTPPassive" , NULL
, isBoolean
, &kSCPropNetProxiesFTPPassive
, __doProxyFTPPassive
, NULL
},
974 { "passive" , NULL
, isBoolean
, &kSCPropNetProxiesFTPPassive
, __doProxyFTPPassive
, NULL
},
976 { "?" , NULL
, isHelp
, NULL
, NULL
,
977 "\nProxy configuration commands\n\n"
978 " set protocol ExceptionsList exception[,exception-2]\n"
979 " set protocol ExcludeSimpleHostnames {enable|disable}\n"
981 " set protocol ftp {enable|disable}\n"
982 " set protocol ftp host proxy-host\n"
983 " set protocol ftp port proxy-port\n"
984 " set protocol ftp passive {enable|disable}\n"
986 " set protocol http {enable|disable}\n"
987 " set protocol http host proxy-host\n"
988 " set protocol http port proxy-port\n"
990 " set protocol https {enable|disable}\n"
991 " set protocol https host proxy-host\n"
992 " set protocol https port proxy-port\n"
994 " set protocol rtsp {enable|disable}\n"
995 " set protocol rtsp host proxy-host\n"
996 " set protocol rtsp port proxy-port\n"
998 " set protocol socks {enable|disable}\n"
999 " set protocol socks host proxy-host\n"
1000 " set protocol socks port proxy-port\n"
1002 " set protocol .pac {enable|disable}\n"
1003 " set protocol .pac url .pac-url\n"
1005 " set protocol wpad {enable|disable}\n"
1008 #define N_PROXY_OPTIONS (sizeof(proxyOptions) / sizeof(proxyOptions[0]))
1012 __doProxySelect(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1017 SCPrint(TRUE
, stdout
, CFSTR("proxy option[s] not specified\n"));
1021 currentProxy
= (proxyKeys
*)info
;
1023 nextOption
= _find_option(argv
[0], proxyOptions
, N_PROXY_OPTIONS
);
1024 if ((nextOption
== kCFNotFound
) ||
1025 (proxyOptions
[nextOption
].handler
== __doProxySelect
)) {
1026 SCPrint(TRUE
, stdout
, CFSTR("%s proxy option[s] not specified\n"), currentProxy
->proxy
);
1035 __doProxyEnable(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1037 Boolean enabled
= (info
== (void *)FALSE
) ? FALSE
: TRUE
;
1039 if (currentProxy
== NULL
) {
1040 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1044 if (currentProxy
->keyEnable
== NULL
) {
1045 SCPrint(TRUE
, stdout
, CFSTR("%s proxy cannot be %s\n"),
1046 currentProxy
->proxy
,
1047 enabled
? "enabled" : "disabled");
1053 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyEnable
), CFNumberRef_1
);
1055 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyEnable
));
1057 if (currentProxy
->keyProxy
!= NULL
) {
1058 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyProxy
));
1061 if (currentProxy
->keyPort
!= NULL
) {
1062 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyPort
));
1065 if (currentProxy
->keyURL
!= NULL
) {
1066 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyURL
));
1075 __proxy_enabled(CFDictionaryRef configuration
, const CFStringRef
*enableKey
)
1080 if (enableKey
== NULL
) {
1081 return TRUE
; // if proxy does not need to be enabled
1084 num
= CFDictionaryGetValue(configuration
, *enableKey
);
1085 if (!isA_CFNumber(num
) ||
1086 !CFNumberGetValue(num
, kCFNumberIntType
, &val
) ||
1088 return FALSE
; // if not enabled
1096 __doProxyHost(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1098 if (currentProxy
== NULL
) {
1099 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1103 if (currentProxy
->keyProxy
== NULL
) {
1104 SCPrint(TRUE
, stdout
, CFSTR("%s proxy host cannot be specified\n"), currentProxy
->proxy
);
1108 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1109 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1114 SCPrint(TRUE
, stdout
, CFSTR("%s proxy host not specified\n"), currentProxy
->proxy
);
1118 if (strlen(argv
[0]) > 0) {
1121 host
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1122 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyProxy
), host
);
1125 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyProxy
));
1133 __doProxyPort(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1135 if (currentProxy
== NULL
) {
1136 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1140 if (currentProxy
->keyPort
== NULL
) {
1141 SCPrint(TRUE
, stdout
, CFSTR("%s proxy port cannot be specified\n"), currentProxy
->proxy
);
1145 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1146 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1151 SCPrint(TRUE
, stdout
, CFSTR("%s proxy port not specified\n"), currentProxy
->proxy
);
1155 if (strlen(argv
[0]) > 0) {
1159 num
= _copy_number(argv
[0]);
1160 if (!isA_CFNumber(num
) ||
1161 !CFNumberGetValue(num
, kCFNumberIntType
, &port
) ||
1162 (port
< 0) || (port
> 65535)) {
1163 SCPrint(TRUE
, stdout
, CFSTR("invalid %s proxy port number\n"), currentProxy
->proxy
);
1164 if (num
!= NULL
) CFRelease(num
);
1168 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyPort
), num
);
1171 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyPort
));
1179 __doProxyURL(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1181 if (currentProxy
== NULL
) {
1182 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1186 if (currentProxy
->keyURL
== NULL
) {
1187 SCPrint(TRUE
, stdout
, CFSTR("%s proxy URL cannot be specified\n"), currentProxy
->proxy
);
1191 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1192 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1197 SCPrint(TRUE
, stdout
, CFSTR("%s proxy URL not specified\n"), currentProxy
->proxy
);
1201 if (strlen(argv
[0]) > 0) {
1204 url
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1205 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyURL
), url
);
1208 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyURL
));
1216 __doProxyFTPPassive(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1218 if (currentProxy
== NULL
) {
1219 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1223 if (currentProxy
!= &proxyKeys_FTP
) {
1224 SCPrint(TRUE
, stdout
, CFSTR("passive can only be enable for FTP proxy\n"));
1233 set_protocol_proxies(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1237 ok
= _process_options(proxyOptions
, N_PROXY_OPTIONS
, argc
, argv
, newConfiguration
);
1246 #if !TARGET_OS_IPHONE
1249 static CF_RETURNS_RETAINED CFStringRef
1250 __cleanupName(CFStringRef name
)
1252 CFMutableStringRef newName
;
1254 newName
= CFStringCreateMutableCopy(NULL
, 0, name
);
1255 CFStringTrimWhitespace(newName
);
1256 if (CFStringGetLength(newName
) == 0) {
1266 __doSMBName(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1269 SCPrint(TRUE
, stdout
, CFSTR("NetBIOS name not specified\n"));
1273 if (strlen(argv
[0]) > 0) {
1277 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1278 name
= __cleanupName(str
);
1282 CFDictionarySetValue(newConfiguration
, key
, name
);
1285 SCPrint(TRUE
, stdout
, CFSTR("invalid NetBIOS name\n"));
1289 CFDictionaryRemoveValue(newConfiguration
, key
);
1297 __doSMBWorkgroup(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1300 SCPrint(TRUE
, stdout
, CFSTR("Workgroup not specified\n"));
1304 if (strlen(argv
[0]) > 0) {
1308 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1309 name
= __cleanupName(str
);
1313 CFDictionarySetValue(newConfiguration
, key
, name
);
1316 SCPrint(TRUE
, stdout
, CFSTR("invalid Workgroup\n"));
1320 CFDictionaryRemoveValue(newConfiguration
, key
);
1328 __doSMBWINSAddresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1330 CFMutableArrayRef servers
;
1333 SCPrint(TRUE
, stdout
, CFSTR("WINS address(es) not specified\n"));
1337 servers
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1339 if (strlen(argv
[0]) > 0) {
1345 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1346 array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
1349 n
= (array
!= NULL
) ? CFArrayGetCount(array
) : 0;
1350 for (i
= 0; i
< n
; i
++) {
1353 if (_SC_cfstring_to_cstring(CFArrayGetValueAtIndex(array
, i
),
1356 kCFStringEncodingUTF8
) != NULL
) {
1359 server
= __copyIPv4Address(str
);
1360 //if (server == NULL) {
1361 // server = __copyIPv6Address(str);
1363 if (server
!= NULL
) {
1364 CFArrayAppendValue(servers
, server
);
1370 SCPrint(TRUE
, stdout
, CFSTR("invalid WINS address\n"));
1375 if (array
!= NULL
) CFRelease(array
);
1378 if (CFArrayGetCount(servers
) > 0) {
1379 CFDictionarySetValue(newConfiguration
, key
, servers
);
1381 CFDictionaryRemoveValue(newConfiguration
, key
);
1389 static selections smbNodeTypes
[] = {
1390 { CFSTR("Broadcast"), &kSCValNetSMBNetBIOSNodeTypeBroadcast
, 0 },
1391 { CFSTR("Peer") , &kSCValNetSMBNetBIOSNodeTypePeer
, 0 },
1392 { CFSTR("Mixed") , &kSCValNetSMBNetBIOSNodeTypeMixed
, 0 },
1393 { CFSTR("Hybrid") , &kSCValNetSMBNetBIOSNodeTypeHybrid
, 0 },
1398 static options smbOptions
[] = {
1399 { "NetBIOSName" , "name" , isOther
, &kSCPropNetSMBNetBIOSName
, __doSMBName
, NULL
},
1400 { "name" , "name" , isOther
, &kSCPropNetSMBNetBIOSName
, __doSMBName
, NULL
},
1401 { "NetBIOSNodeType", "type" , isChooseOne
, &kSCPropNetSMBNetBIOSNodeType
, NULL
, (void *)smbNodeTypes
},
1402 { "type", "type" , isChooseOne
, &kSCPropNetSMBNetBIOSNodeType
, NULL
, (void *)smbNodeTypes
},
1403 { "Workgroup" , "workgroup", isOther
, &kSCPropNetSMBWorkgroup
, __doSMBWorkgroup
, NULL
},
1404 { "WINSAddresses" , "wins" , isOther
, &kSCPropNetSMBWINSAddresses
, __doSMBWINSAddresses
, NULL
},
1405 { "wins" , "wins" , isOther
, &kSCPropNetSMBWINSAddresses
, __doSMBWINSAddresses
, NULL
},
1407 { "?" , NULL
, isHelp
, NULL
, NULL
,
1408 "\nSMB configuration commands\n\n"
1409 " set protocol name NetBIOS-name\n"
1410 " set protocol type (Broadcast|Peer|Mixed|Hybrid)\n"
1411 " set protocol workgroup SMB-workgroup\n"
1412 " set protocol wins x1.x1.x1.x1[,x2.x2.x2.x2]\n"
1415 #define N_SMB_OPTIONS (sizeof(smbOptions) / sizeof(smbOptions[0]))
1419 set_protocol_smb(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1423 ok
= _process_options(smbOptions
, N_SMB_OPTIONS
, argc
, argv
, newConfiguration
);
1428 #endif // !TARGET_OS_IPHONE
1432 #pragma mark *Protocol*
1437 set_protocol(int argc
, char **argv
)
1439 CFDictionaryRef configuration
;
1440 CFMutableDictionaryRef newConfiguration
= NULL
;
1442 CFStringRef protocolType
;
1444 if (net_protocol
== NULL
) {
1445 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
1450 SCPrint(TRUE
, stdout
, CFSTR("set what?\n"));
1454 configuration
= SCNetworkProtocolGetConfiguration(net_protocol
);
1455 if (configuration
== NULL
) {
1456 newConfiguration
= CFDictionaryCreateMutable(NULL
,
1458 &kCFTypeDictionaryKeyCallBacks
,
1459 &kCFTypeDictionaryValueCallBacks
);
1461 newConfiguration
= CFDictionaryCreateMutableCopy(NULL
, 0, configuration
);
1462 CFDictionaryRemoveValue(newConfiguration
, kSCResvInactive
);
1465 protocolType
= SCNetworkProtocolGetProtocolType(net_protocol
);
1466 if (CFEqual(protocolType
, kSCNetworkProtocolTypeDNS
)) {
1467 ok
= set_protocol_dns(argc
, argv
, newConfiguration
);
1468 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv4
)) {
1469 ok
= set_protocol_ipv4(argc
, argv
, newConfiguration
);
1470 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv6
)) {
1471 ok
= set_protocol_ipv6(argc
, argv
, newConfiguration
);
1472 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeProxies
)) {
1473 ok
= set_protocol_proxies(argc
, argv
, newConfiguration
);
1474 #if !TARGET_OS_IPHONE
1475 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeSMB
)) {
1476 ok
= set_protocol_smb(argc
, argv
, newConfiguration
);
1477 #endif // !TARGET_OS_IPHONE
1479 SCPrint(TRUE
, stdout
, CFSTR("this protocols configuration cannot be changed\n"));
1486 if (((configuration
== NULL
) && (CFDictionaryGetCount(newConfiguration
) > 0)) ||
1487 ((configuration
!= NULL
) && !CFEqual(configuration
, newConfiguration
))) {
1488 if (!SCNetworkProtocolSetConfiguration(net_protocol
, newConfiguration
)) {
1489 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
1493 _prefs_changed
= TRUE
;
1498 if (newConfiguration
!= NULL
) CFRelease(newConfiguration
);
1503 /* -------------------- */
1508 show_protocol(int argc
, char **argv
)
1510 CFDictionaryRef configuration
;
1511 SCNetworkProtocolRef protocol
= NULL
;
1512 CFStringRef protocolType
;
1515 protocol
= _find_protocol(argv
[0]);
1517 if (net_protocol
!= NULL
) {
1518 protocol
= net_protocol
;
1520 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
1525 if (protocol
== NULL
) {
1529 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1530 SCPrint(TRUE
, stdout
, CFSTR("protocol type = %@\n"), protocolType
);
1532 configuration
= SCNetworkProtocolGetConfiguration(protocol
);
1533 if (configuration
!= NULL
) {
1534 SCPrint(TRUE
, stdout
, CFSTR("\n protocol configuration\n"));
1535 _show_entity(configuration
, CFSTR(""));
1539 SCPrint(TRUE
, stdout
, CFSTR("\n%@\n"), protocol
);
1546 /* -------------------- */
1551 show_protocols(int argc
, char **argv
)
1556 if (prefs
== NULL
) {
1557 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
1561 if (net_service
== NULL
) {
1562 SCPrint(TRUE
, stdout
, CFSTR("service not selected\n"));
1566 if (protocols
!= NULL
) CFRelease(protocols
);
1567 protocols
= SCNetworkServiceCopyProtocols(net_service
);
1568 if (protocols
== NULL
) {
1569 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
1573 n
= CFArrayGetCount(protocols
);
1575 CFMutableArrayRef sorted
;
1577 sorted
= CFArrayCreateMutableCopy(NULL
, 0, protocols
);
1578 CFArraySortValues(sorted
,
1582 CFRelease(protocols
);
1586 for (i
= 0; i
< n
; i
++) {
1587 SCNetworkProtocolRef protocol
;
1588 CFStringRef protocolType
;
1590 protocol
= CFArrayGetValueAtIndex(protocols
, i
);
1591 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1593 SCPrint(TRUE
, stdout
, CFSTR("%c%2d: %@%*s :"),
1594 ((net_protocol
!= NULL
) && CFEqual(protocol
, net_protocol
)) ? '>' : ' ',
1597 sizeof("AppleTalk") - CFStringGetLength(protocolType
) - 1,
1600 if (SCNetworkProtocolGetEnabled(protocol
)) {
1601 CFStringRef description
;
1603 description
= _protocol_description(protocol
, FALSE
);
1604 SCPrint(TRUE
, stdout
, CFSTR(" %@"), description
);
1605 CFRelease(description
);
1607 SCPrint(TRUE
, stdout
, CFSTR(" *DISABLED*"));
1609 SCPrint(TRUE
, stdout
, CFSTR("\n"));
1616 /* -------------------- */
1620 CF_RETURNS_RETAINED CFStringRef
1621 _protocol_description(SCNetworkProtocolRef protocol
, Boolean skipEmpty
)
1623 CFDictionaryRef configuration
;
1624 CFMutableStringRef description
= NULL
;
1625 CFStringRef protocolType
;
1627 description
= CFStringCreateMutable(NULL
, 0);
1629 if (!SCNetworkProtocolGetEnabled(protocol
)) {
1633 configuration
= SCNetworkProtocolGetConfiguration(protocol
);
1634 if (configuration
== NULL
) {
1638 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1639 if (CFEqual(protocolType
, kSCNetworkProtocolTypeDNS
)) {
1644 domain
= CFDictionaryGetValue(configuration
, kSCPropNetDNSDomainName
);
1645 if (isA_CFString(domain
)) {
1646 CFStringAppendFormat(description
,
1652 search
= CFDictionaryGetValue(configuration
, kSCPropNetDNSSearchDomains
);
1653 if (isA_CFArray(search
)) {
1656 str
= CFStringCreateByCombiningStrings(NULL
, search
, CFSTR(","));
1657 CFStringAppendFormat(description
,
1659 CFSTR("%ssearch=%@"),
1660 CFStringGetLength(description
) > 0 ? ", " : "",
1665 servers
= CFDictionaryGetValue(configuration
, kSCPropNetDNSServerAddresses
);
1666 if (isA_CFArray(servers
)) {
1669 str
= CFStringCreateByCombiningStrings(NULL
, servers
, CFSTR(","));
1670 CFStringAppendFormat(description
,
1672 CFSTR("%sservers=%@"),
1673 CFStringGetLength(description
) > 0 ? ", " : "",
1677 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv4
)) {
1680 method
= CFDictionaryGetValue(configuration
, kSCPropNetIPv4ConfigMethod
);
1681 if (isA_CFString(method
)) {
1682 CFArrayRef addresses
;
1684 addresses
= CFDictionaryGetValue(configuration
, kSCPropNetIPv4Addresses
);
1685 if (CFEqual(method
, kSCValNetIPv4ConfigMethodINFORM
) &&
1686 isA_CFArray(addresses
)) {
1687 CFStringAppendFormat(description
,
1689 CFSTR("%@, address=%@"),
1691 CFArrayGetValueAtIndex(addresses
, 0));
1692 } else if (CFEqual(method
, kSCValNetIPv4ConfigMethodManual
) &&
1693 isA_CFArray(addresses
)) {
1694 CFStringAppendFormat(description
,
1696 CFSTR("%@, address=%@"),
1698 CFArrayGetValueAtIndex(addresses
, 0));
1700 CFStringAppendFormat(description
,
1706 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv6
)) {
1709 method
= CFDictionaryGetValue(configuration
, kSCPropNetIPv6ConfigMethod
);
1710 if (isA_CFString(method
)) {
1711 CFStringAppendFormat(description
,
1716 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeProxies
)) {
1718 static proxyKeys
*keys
[] = { &proxyKeys_FTP
, &proxyKeys_Gopher
, &proxyKeys_HTTP
, &proxyKeys_HTTPS
,
1719 &proxyKeys_RTSP
, &proxyKeys_SOCKS
, &proxyKeys_PAC
, &proxyKeys_WPAD
};
1721 for (i
= 0; i
< sizeof(keys
)/sizeof(keys
[0]); i
++) {
1722 proxyKeys
*currentProxy
= keys
[i
];
1724 if (!__proxy_enabled(configuration
, currentProxy
->keyEnable
)) {
1728 if (((currentProxy
->keyProxy
!= NULL
) &&
1729 !CFDictionaryContainsKey(configuration
, *(currentProxy
->keyProxy
))) ||
1730 ((currentProxy
->keyURL
!= NULL
) &&
1731 !CFDictionaryContainsKey(configuration
, *(currentProxy
->keyURL
)))) {
1735 CFStringAppendFormat(description
,
1738 CFStringGetLength(description
) > 0 ? ", " : "",
1739 currentProxy
->proxy
);
1741 #if !TARGET_OS_IPHONE
1742 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeSMB
)) {
1745 CFStringRef workgroup
;
1747 name
= CFDictionaryGetValue(configuration
, kSCPropNetSMBNetBIOSName
);
1748 if (isA_CFString(name
)) {
1749 CFStringAppendFormat(description
,
1751 CFSTR("NetBIOS name=%@"),
1755 workgroup
= CFDictionaryGetValue(configuration
, kSCPropNetSMBWorkgroup
);
1756 if (isA_CFString(workgroup
)) {
1757 CFStringAppendFormat(description
,
1759 CFSTR("Workgroup=%@"),
1763 servers
= CFDictionaryGetValue(configuration
, kSCPropNetSMBWINSAddresses
);
1764 if (isA_CFArray(servers
)) {
1767 str
= CFStringCreateByCombiningStrings(NULL
, servers
, CFSTR(","));
1768 CFStringAppendFormat(description
,
1770 CFSTR("%sWINS servers=%@"),
1771 CFStringGetLength(description
) > 0 ? ", " : "",
1775 #endif // !TARGET_OS_IPHONE
1780 if (skipEmpty
&& CFStringGetLength(description
) == 0) {
1781 CFRelease(description
);