2 * Copyright (c) 2004-2009 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 bzero(&sin
, sizeof(sin
));
70 sin
.sin_len
= sizeof(sin
);
71 sin
.sin_family
= AF_INET
;
72 if (inet_aton(arg
, &sin
.sin_addr
) != 1) {
76 _SC_sockaddr_to_string((struct sockaddr
*)&sin
, buf
, sizeof(buf
));
77 return CFStringCreateWithCString(NULL
, buf
, kCFStringEncodingUTF8
);
82 __copyIPv6Address(const char *arg
)
86 struct sockaddr_in6 sin6
;
88 bzero(&sin6
, sizeof(sin6
));
89 sin6
.sin6_len
= sizeof(sin6
);
90 sin6
.sin6_family
= AF_INET6
;
91 if (inet_pton(AF_INET6
, arg
, &sin6
.sin6_addr
) != 1) {
97 sin6
.sin6_scope_id
= if_nametoindex(p
+ 1);
100 _SC_sockaddr_to_string((struct sockaddr
*)&sin6
, buf
, sizeof(buf
));
101 return CFStringCreateWithCString(NULL
, buf
, kCFStringEncodingUTF8
);
105 /* -------------------- */
108 static SCNetworkProtocolRef
109 _find_protocol(char *match
)
111 Boolean allowIndex
= TRUE
;
114 CFStringRef select_name
= NULL
;
115 SCNetworkProtocolRef selected
= NULL
;
117 if (protocols
== NULL
) {
118 if (net_service
== NULL
) {
119 SCPrint(TRUE
, stdout
, CFSTR("network service not selected\n"));
123 protocols
= SCNetworkServiceCopyProtocols(net_service
);
124 if (protocols
== NULL
) {
125 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
129 n
= CFArrayGetCount(protocols
);
131 CFMutableArrayRef sorted
;
133 sorted
= CFArrayCreateMutableCopy(NULL
, 0, protocols
);
134 CFArraySortValues(sorted
,
138 CFRelease(protocols
);
145 // try to select the protocol by its protocol type
147 select_name
= CFStringCreateWithCString(NULL
, match
, kCFStringEncodingUTF8
);
149 n
= CFArrayGetCount(protocols
);
150 for (i
= 0; i
< n
; i
++) {
151 SCNetworkProtocolRef protocol
;
154 protocol
= CFArrayGetValueAtIndex(protocols
, i
);
155 type
= SCNetworkProtocolGetProtocolType(protocol
);
156 if (CFStringCompare(select_name
, type
, kCFCompareCaseInsensitive
) == kCFCompareEqualTo
) {
167 // try to select the protocol by its index
170 val
= strtol(str
, &end
, 10);
171 if ((*str
!= '\0') && (*end
== '\0') && (errno
== 0)) {
172 if ((val
> 0) && (val
<= n
)) {
173 selected
= CFArrayGetValueAtIndex(protocols
, val
- 1);
178 if (selected
!= NULL
) {
182 SCPrint(TRUE
, stdout
, CFSTR("no match, which protocol?\n"));
186 if (select_name
!= NULL
) CFRelease(select_name
);
191 /* -------------------- */
196 create_protocol(int argc
, char **argv
)
198 SCNetworkInterfaceRef interface
;
199 CFStringRef protocolType
;
201 if ((argc
< 1) || (strlen(argv
[0]) == 0)) {
202 SCPrint(TRUE
, stdout
, CFSTR("what protocol type?\n"));
206 if (net_service
== NULL
) {
207 SCPrint(TRUE
, stdout
, CFSTR("network service not selected\n"));
211 protocolType
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
213 interface
= SCNetworkServiceGetInterface(net_service
);
214 if (interface
!= NULL
) {
215 CFArrayRef supported
;
219 supported
= SCNetworkInterfaceGetSupportedProtocolTypes(interface
);
220 n
= (supported
!= NULL
) ? CFArrayGetCount(supported
) : 0;
221 for (i
= 0; i
< n
; i
++) {
222 CFStringRef supportedType
;
224 supportedType
= CFArrayGetValueAtIndex(supported
, i
);
225 if (CFStringCompare(protocolType
,
227 kCFCompareCaseInsensitive
) == kCFCompareEqualTo
) {
228 CFRelease(protocolType
);
229 protocolType
= CFRetain(supportedType
);
235 if (!SCNetworkServiceAddProtocolType(net_service
, protocolType
)) {
236 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
240 _prefs_changed
= TRUE
;
242 if (protocols
!= NULL
) {
243 CFRelease(protocols
);
247 if (net_protocol
!= NULL
) CFRelease(net_protocol
);
248 // net_protocol = NULL;
250 net_protocol
= SCNetworkServiceCopyProtocol(net_service
, protocolType
);
251 if (net_protocol
== NULL
) {
252 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
256 SCPrint(TRUE
, stdout
,
257 CFSTR("protocol \"%@\" selected\n"),
262 CFRelease(protocolType
);
267 /* -------------------- */
272 disable_protocol(int argc
, char **argv
)
274 SCNetworkProtocolRef protocol
= NULL
;
277 protocol
= _find_protocol(argv
[0]);
279 if (net_protocol
!= NULL
) {
280 protocol
= net_protocol
;
282 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
287 if (protocol
== NULL
) {
291 if (!SCNetworkProtocolSetEnabled(protocol
, FALSE
)) {
292 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
296 _prefs_changed
= TRUE
;
302 /* -------------------- */
307 enable_protocol(int argc
, char **argv
)
309 SCNetworkProtocolRef protocol
= NULL
;
312 protocol
= _find_protocol(argv
[0]);
314 if (net_protocol
!= NULL
) {
315 protocol
= net_protocol
;
317 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
322 if (protocol
== NULL
) {
326 if (!SCNetworkProtocolSetEnabled(protocol
, TRUE
)) {
327 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
331 _prefs_changed
= TRUE
;
337 /* -------------------- */
342 remove_protocol(int argc
, char **argv
)
344 SCNetworkProtocolRef protocol
= NULL
;
345 CFStringRef protocolType
;
348 protocol
= _find_protocol(argv
[0]);
350 if (net_protocol
!= NULL
) {
351 protocol
= net_protocol
;
353 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
358 if (protocol
== NULL
) {
364 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
365 if (!SCNetworkServiceRemoveProtocolType(net_service
, protocolType
)) {
366 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
370 _prefs_changed
= TRUE
;
372 SCPrint(TRUE
, stdout
,
373 CFSTR("protocol \"%@\" removed\n"),
376 if ((net_protocol
!= NULL
) && CFEqual(protocol
, net_protocol
)) {
377 CFRelease(net_protocol
);
379 SCPrint(TRUE
, stdout
, CFSTR("& no protocol selected\n"));
382 if (protocols
!= NULL
) {
383 CFRelease(protocols
);
394 /* -------------------- */
399 select_protocol(int argc
, char **argv
)
401 SCNetworkProtocolRef protocol
;
403 protocol
= _find_protocol(argv
[0]);
405 if (protocol
== NULL
) {
409 if (net_protocol
!= NULL
) CFRelease(net_protocol
);
410 net_protocol
= CFRetain(protocol
);
412 SCPrint(TRUE
, stdout
,
413 CFSTR("protocol \"%@\" selected\n"),
414 SCNetworkProtocolGetProtocolType(protocol
));
425 __cleanupDomainName(CFStringRef domain
)
427 CFMutableStringRef newDomain
;
429 newDomain
= CFStringCreateMutableCopy(NULL
, 0, domain
);
430 CFStringTrimWhitespace(newDomain
);
431 CFStringTrim(newDomain
, CFSTR("."));
432 if (CFStringGetLength(newDomain
) == 0) {
433 CFRelease(newDomain
);
442 __doDNSDomain(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
445 SCPrint(TRUE
, stdout
, CFSTR("DNS domain name not specified\n"));
449 if (strlen(argv
[0]) > 0) {
453 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
454 domain
= __cleanupDomainName(str
);
457 if (domain
!= NULL
) {
458 CFDictionarySetValue(newConfiguration
, key
, domain
);
461 SCPrint(TRUE
, stdout
, CFSTR("invalid DNS domain name\n"));
465 CFDictionaryRemoveValue(newConfiguration
, key
);
473 __doDNSDomainArray(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
475 CFMutableArrayRef domains
;
478 SCPrint(TRUE
, stdout
, CFSTR("DNS search domain name(s) not specified\n"));
482 domains
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
484 if (strlen(argv
[0]) > 0) {
488 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
489 array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
494 CFIndex n
= CFArrayGetCount(array
);
496 for (i
= 0; i
< n
; i
++) {
499 domain
= __cleanupDomainName(CFArrayGetValueAtIndex(array
, i
));
500 if (domain
!= NULL
) {
501 CFArrayAppendValue(domains
, domain
);
506 SCPrint(TRUE
, stdout
, CFSTR("invalid DNS search domain name\n"));
514 if (CFArrayGetCount(domains
) > 0) {
515 CFDictionarySetValue(newConfiguration
, key
, domains
);
517 CFDictionaryRemoveValue(newConfiguration
, key
);
526 __doDNSServerAddresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
528 CFMutableArrayRef servers
;
531 SCPrint(TRUE
, stdout
, CFSTR("DNS name server address(es) not specified\n"));
535 servers
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
537 if (strlen(argv
[0]) > 0) {
543 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
544 array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
547 n
= (array
!= NULL
) ? CFArrayGetCount(array
) : 0;
548 for (i
= 0; i
< n
; i
++) {
551 if (_SC_cfstring_to_cstring(CFArrayGetValueAtIndex(array
, i
),
554 kCFStringEncodingUTF8
) != NULL
) {
557 server
= __copyIPv4Address(str
);
558 if (server
== NULL
) {
559 server
= __copyIPv6Address(str
);
561 if (server
!= NULL
) {
562 CFArrayAppendValue(servers
, server
);
568 SCPrint(TRUE
, stdout
, CFSTR("invalid DNS name server address\n"));
573 if (array
!= NULL
) CFRelease(array
);
576 if (CFArrayGetCount(servers
) > 0) {
577 CFDictionarySetValue(newConfiguration
, key
, servers
);
579 CFDictionaryRemoveValue(newConfiguration
, key
);
587 static options dnsOptions
[] = {
588 { "DomainName" , "domain" , isOther
, &kSCPropNetDNSDomainName
, __doDNSDomain
, NULL
},
589 { "domain" , "domain" , isOther
, &kSCPropNetDNSDomainName
, __doDNSDomain
, NULL
},
590 { "SearchDomains" , "search" , isOther
, &kSCPropNetDNSSearchDomains
, __doDNSDomainArray
, NULL
},
591 { "search" , "search" , isOther
, &kSCPropNetDNSSearchDomains
, __doDNSDomainArray
, NULL
},
592 { "ServerAddresses", "address", isOther
, &kSCPropNetDNSServerAddresses
, __doDNSServerAddresses
, NULL
},
593 { "nameserver" , "address", isOther
, &kSCPropNetDNSServerAddresses
, __doDNSServerAddresses
, NULL
},
594 { "nameservers" , "address", isOther
, &kSCPropNetDNSServerAddresses
, __doDNSServerAddresses
, NULL
},
595 { "SupplementalMatchDomains",
598 &kSCPropNetDNSSupplementalMatchDomains
,
602 { "?" , NULL
, isHelp
, NULL
, NULL
,
603 "\nDNS configuration commands\n\n"
604 " set protocol search domain-name[,domain-name-2]\n"
605 " set protocol nameserver x1.x1.x1.x1[,x2.x2.x2.x2]\n"
608 #define N_DNS_OPTIONS (sizeof(dnsOptions) / sizeof(dnsOptions[0]))
612 set_protocol_dns(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
616 ok
= _process_options(dnsOptions
, N_DNS_OPTIONS
, argc
, argv
, newConfiguration
);
625 #define allowIPv4Address 1<<1 // allow address
626 #define allowIPv4Netmask 1<<2 // allow subnet mask
627 #define allowIPv4Router 1<<3 // allow router
628 #define allowIPv4DHCPClientID 1<<4 // allow DCHP Client ID
630 static selections ipv4ConfigMethods
[] = {
631 { CFSTR("BOOTP") , &kSCValNetIPv4ConfigMethodBOOTP
, 0 },
632 { CFSTR("DHCP") , &kSCValNetIPv4ConfigMethodDHCP
, allowIPv4DHCPClientID
},
633 { CFSTR("INFORM") , &kSCValNetIPv4ConfigMethodINFORM
, allowIPv4Address
},
634 { CFSTR("LinkLocal"), &kSCValNetIPv4ConfigMethodLinkLocal
, 0 },
635 { CFSTR("Manual") , &kSCValNetIPv4ConfigMethodManual
, allowIPv4Address
|allowIPv4Netmask
|allowIPv4Router
},
636 { CFSTR("PPP") , &kSCValNetIPv4ConfigMethodPPP
, allowIPv4Address
|selectionNotAvailable
},
642 __doIPv4ConfigMethod(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
648 method
= CFDictionaryGetValue(newConfiguration
, key
);
649 methodIndex
= _find_selection(method
, (selections
*)ipv4ConfigMethods
, &flags
);
650 if (methodIndex
!= kCFNotFound
) {
651 if (!(flags
& allowIPv4Address
)) {
652 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv4Addresses
);
654 if (!(flags
& allowIPv4Netmask
)) {
655 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv4SubnetMasks
);
657 if (!(flags
& allowIPv4Router
)) {
658 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv4Router
);
660 if (!(flags
& allowIPv4DHCPClientID
)) {
661 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv4DHCPClientID
);
664 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
673 __doIPv4Addresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
675 Boolean useArray
= (info
== (void *)FALSE
) ? FALSE
: TRUE
;
677 if (strlen(argv
[0]) > 0) {
680 address
= __copyIPv4Address(argv
[0]);
681 if (address
!= NULL
) {
683 CFArrayRef addresses
;
685 addresses
= CFArrayCreate(NULL
, (const void **)&address
, 1, &kCFTypeArrayCallBacks
);
686 CFDictionarySetValue(newConfiguration
, key
, addresses
);
687 CFRelease(addresses
);
689 CFDictionarySetValue(newConfiguration
, key
, address
);
696 CFDictionaryRemoveValue(newConfiguration
, key
);
703 static options ipv4Options
[] = {
704 { "ConfigMethod", "configuration method"
705 , isChooseOne
, &kSCPropNetIPv4ConfigMethod
, __doIPv4ConfigMethod
, (void *)ipv4ConfigMethods
},
706 { "config" , "configuration method"
707 , isChooseOne
, &kSCPropNetIPv4ConfigMethod
, __doIPv4ConfigMethod
, (void *)ipv4ConfigMethods
},
708 { "Addresses" , "address" , isOther
, &kSCPropNetIPv4Addresses
, __doIPv4Addresses
, (void *)TRUE
},
709 { "address" , "address" , isOther
, &kSCPropNetIPv4Addresses
, __doIPv4Addresses
, (void *)TRUE
},
710 { "SubnetMasks" , "netmask" , isOther
, &kSCPropNetIPv4SubnetMasks
, __doIPv4Addresses
, (void *)TRUE
},
711 { "netmask" , "netmask" , isOther
, &kSCPropNetIPv4SubnetMasks
, __doIPv4Addresses
, (void *)TRUE
},
712 { "Router" , "address" , isOther
, &kSCPropNetIPv4Router
, __doIPv4Addresses
, (void *)FALSE
},
713 { "DHCPClientID", "client ID", isString
, &kSCPropNetIPv4DHCPClientID
, NULL
, NULL
},
715 { "?" , NULL
, isHelp
, NULL
, NULL
,
716 "\nIPv4 configuration commands\n\n"
717 " set protocol config {BOOTP|DHCP|INFORM|MANUAL}\n"
718 "\n w/config=BOOTP\n"
721 " set protocol dhcpclientid identifier\n"
722 "\n w/config=INFORM\n"
723 " set protocol address x.x.x.x\n"
724 "\n w/config=MANUAL\n"
725 " set protocol address x.x.x.x\n"
726 " set protocol netmask x.x.x.x\n"
727 " set protocol router x.x.x.x\n"
730 #define N_IPV4_OPTIONS (sizeof(ipv4Options) / sizeof(ipv4Options[0]))
734 set_protocol_ipv4(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
738 ok
= _process_options(ipv4Options
, N_IPV4_OPTIONS
, argc
, argv
, newConfiguration
);
744 // validate configuration
745 method
= CFDictionaryGetValue(newConfiguration
, kSCPropNetIPv4ConfigMethod
);
746 methodIndex
= _find_selection(method
, (selections
*)ipv4ConfigMethods
, &flags
);
747 if (methodIndex
== kCFNotFound
) {
748 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
752 if (!(flags
& allowIPv4Address
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv4Addresses
)) {
753 SCPrint(TRUE
, stdout
,
754 CFSTR("IP address not allowed with %@ configuration\n"),
755 ipv4ConfigMethods
[methodIndex
].selection
);
759 if (!(flags
& allowIPv4Netmask
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv4SubnetMasks
)) {
760 SCPrint(TRUE
, stdout
,
761 CFSTR("Subnet mask not allowed with %@ configuration\n"),
762 ipv4ConfigMethods
[methodIndex
].selection
);
766 if (!(flags
& allowIPv4Router
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv4Router
)) {
767 SCPrint(TRUE
, stdout
,
768 CFSTR("Default route not allowed with %@ configuration\n"),
769 ipv4ConfigMethods
[methodIndex
].selection
);
773 if (!(flags
& allowIPv4DHCPClientID
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv4DHCPClientID
)) {
774 SCPrint(TRUE
, stdout
,
775 CFSTR("DHCP client ID not allowed with %@ configuration\n"),
776 ipv4ConfigMethods
[methodIndex
].selection
);
789 #define allowIPv6Address 1<<1 // allow address
790 #define allowIPv6PrefixLength 1<<2 // allow prefix length
791 #define allowIPv6Router 1<<3 // allow router
793 static selections ipv6ConfigMethods
[] = {
794 { CFSTR("Automatic") , & kSCValNetIPv6ConfigMethodAutomatic
, 0 },
795 { CFSTR("Manual") , & kSCValNetIPv6ConfigMethodManual
, allowIPv6Address
|allowIPv6PrefixLength
|allowIPv6Router
},
796 { CFSTR("RouterAdvertisement"), & kSCValNetIPv6ConfigMethodRouterAdvertisement
, allowIPv6Address
},
797 { CFSTR("6to4") , & kSCValNetIPv6ConfigMethod6to4
, 0 },
803 __doIPv6ConfigMethod(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
809 method
= CFDictionaryGetValue(newConfiguration
, key
);
810 methodIndex
= _find_selection(method
, (selections
*)ipv6ConfigMethods
, &flags
);
811 if (methodIndex
!= kCFNotFound
) {
812 if (!(flags
& allowIPv6Address
)) {
813 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv6Addresses
);
815 if (!(flags
& allowIPv6PrefixLength
)) {
816 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv6PrefixLength
);
818 if (!(flags
& allowIPv6Router
)) {
819 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv6Router
);
822 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
831 __doIPv6Addresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
833 Boolean useArray
= (info
== (void *)FALSE
) ? FALSE
: TRUE
;
835 if (strlen(argv
[0]) > 0) {
838 address
= __copyIPv6Address(argv
[0]);
839 if (address
!= NULL
) {
841 CFArrayRef addresses
;
843 addresses
= CFArrayCreate(NULL
, (const void **)&address
, 1, &kCFTypeArrayCallBacks
);
844 CFDictionarySetValue(newConfiguration
, key
, addresses
);
845 CFRelease(addresses
);
847 CFDictionarySetValue(newConfiguration
, key
, address
);
854 CFDictionaryRemoveValue(newConfiguration
, key
);
861 static options ipv6Options
[] = {
862 { "ConfigMethod", "configuration method"
863 , isChooseOne
, &kSCPropNetIPv6ConfigMethod
, __doIPv6ConfigMethod
, (void *)ipv6ConfigMethods
},
864 { "config" , "configuration method"
865 , isChooseOne
, &kSCPropNetIPv6ConfigMethod
, __doIPv6ConfigMethod
, (void *)ipv6ConfigMethods
},
866 { "Addresses" , "address" , isOther
, &kSCPropNetIPv6Addresses
, __doIPv6Addresses
, (void *)TRUE
},
867 { "address" , "address" , isOther
, &kSCPropNetIPv6Addresses
, __doIPv6Addresses
, (void *)TRUE
},
868 { "PrefixLength", "prefix length", isNumber
, &kSCPropNetIPv6PrefixLength
, NULL
, NULL
},
869 { "Router" , "address" , isOther
, &kSCPropNetIPv6Router
, __doIPv6Addresses
, (void *)FALSE
},
871 { "?" , NULL
, isHelp
, NULL
, NULL
,
872 "\nIPv6 configuration commands\n\n"
873 " set protocol config {Automatic|MANUAL}\n"
874 "\n w/config=Automatic\n"
876 "\n w/config=MANUAL\n"
877 " set protocol address x:x:x:x:x:x\n"
878 " set protocol router x:x:x:x:x:x\n"
879 " set protocol prefixlength n\n"
882 #define N_IPV6_OPTIONS (sizeof(ipv6Options) / sizeof(ipv6Options[0]))
886 set_protocol_ipv6(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
890 ok
= _process_options(ipv6Options
, N_IPV6_OPTIONS
, argc
, argv
, newConfiguration
);
896 // validate configuration
897 method
= CFDictionaryGetValue(newConfiguration
, kSCPropNetIPv6ConfigMethod
);
898 methodIndex
= _find_selection(method
, (selections
*)ipv6ConfigMethods
, &flags
);
899 if (methodIndex
== kCFNotFound
) {
900 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
904 if (!(flags
& allowIPv6Address
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6Addresses
)) {
905 SCPrint(TRUE
, stdout
,
906 CFSTR("IP address not allowed with %@ configuration\n"),
907 ipv6ConfigMethods
[methodIndex
].selection
);
911 if (!(flags
& allowIPv6PrefixLength
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6PrefixLength
)) {
912 SCPrint(TRUE
, stdout
,
913 CFSTR("Prefix length not allowed with %@ configuration\n"),
914 ipv6ConfigMethods
[methodIndex
].selection
);
918 if (!(flags
& allowIPv6Router
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6Router
)) {
919 SCPrint(TRUE
, stdout
,
920 CFSTR("Router not allowed with %@ configuration\n"),
921 ipv6ConfigMethods
[methodIndex
].selection
);
934 typedef const struct {
936 const CFStringRef
*keyEnable
;
937 const CFStringRef
*keyProxy
;
938 const CFStringRef
*keyPort
;
939 const CFStringRef
*keyURL
;
942 static proxyKeys proxyKeys_FTP
= { "FTP" , &kSCPropNetProxiesFTPEnable
, &kSCPropNetProxiesFTPProxy
, &kSCPropNetProxiesFTPPort
, NULL
};
943 static proxyKeys proxyKeys_Gopher
= { "Gopher", &kSCPropNetProxiesGopherEnable
, &kSCPropNetProxiesGopherProxy
, &kSCPropNetProxiesGopherPort
, NULL
};
944 static proxyKeys proxyKeys_HTTP
= { "HTTP" , &kSCPropNetProxiesHTTPEnable
, &kSCPropNetProxiesHTTPProxy
, &kSCPropNetProxiesHTTPPort
, NULL
};
945 static proxyKeys proxyKeys_HTTPS
= { "HTTPS" , &kSCPropNetProxiesHTTPSEnable
, &kSCPropNetProxiesHTTPSProxy
, &kSCPropNetProxiesHTTPSPort
, NULL
};
946 static proxyKeys proxyKeys_RTSP
= { "RTSP" , &kSCPropNetProxiesRTSPEnable
, &kSCPropNetProxiesRTSPProxy
, &kSCPropNetProxiesRTSPPort
, NULL
};
947 static proxyKeys proxyKeys_SOCKS
= { "SOCKS" , &kSCPropNetProxiesSOCKSEnable
, &kSCPropNetProxiesSOCKSProxy
, &kSCPropNetProxiesSOCKSPort
, NULL
};
948 static proxyKeys proxyKeys_PAC
= { ".pac" , &kSCPropNetProxiesProxyAutoConfigEnable
, NULL
, NULL
, &kSCPropNetProxiesProxyAutoConfigURLString
};
949 static proxyKeys proxyKeys_WPAD
= { "WPAD" , &kSCPropNetProxiesProxyAutoDiscoveryEnable
, NULL
, NULL
, NULL
};
951 static proxyKeys
*currentProxy
= NULL
;
954 static int __doProxySelect (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
955 static int __doProxyEnable (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
956 static int __doProxyHost (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
957 static int __doProxyPort (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
958 static int __doProxyURL (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
959 static int __doProxyFTPPassive(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
962 static options proxyOptions
[] = {
964 { "ExceptionsList" , "exceptions", isStringArray
, &kSCPropNetProxiesExceptionsList
, NULL
, NULL
},
965 { "ExcludeSimpleHostnames", NULL
, isBoolean
, &kSCPropNetProxiesExcludeSimpleHostnames
, NULL
, NULL
},
967 { "FTP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_FTP
},
968 { "Gopher" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_Gopher
},
969 { "HTTP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_HTTP
},
970 { "HTTPS" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_HTTPS
},
971 { "RTSP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_RTSP
},
972 { "SOCKS" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_SOCKS
},
973 { "ProxyAutoConfig" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_PAC
},
974 { ".pac" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_PAC
},
975 { "ProxyAutoDiscovery" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_WPAD
},
976 { "WPAD" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_WPAD
},
978 { "disable" , NULL
, isOther
, NULL
, __doProxyEnable
, (void *)FALSE
},
979 { "enable" , NULL
, isOther
, NULL
, __doProxyEnable
, (void *)TRUE
},
980 { "proxy" , NULL
, isOther
, NULL
, __doProxyHost
, NULL
},
981 { "host" , NULL
, isOther
, NULL
, __doProxyHost
, NULL
},
982 { "port" , NULL
, isOther
, NULL
, __doProxyPort
, NULL
},
983 { "url" , NULL
, isOther
, NULL
, __doProxyURL
, NULL
},
984 // (ftp) proxy modifiers
985 { "FTPPassive" , NULL
, isBoolean
, &kSCPropNetProxiesFTPPassive
, __doProxyFTPPassive
, NULL
},
986 { "passive" , NULL
, isBoolean
, &kSCPropNetProxiesFTPPassive
, __doProxyFTPPassive
, NULL
},
988 { "?" , NULL
, isHelp
, NULL
, NULL
,
989 "\nProxy configuration commands\n\n"
990 " set protocol ExceptionsList exception[,exception-2]\n"
991 " set protocol ExcludeSimpleHostnames {enable|disable}\n"
993 " set protocol ftp {enable|disable}\n"
994 " set protocol ftp host proxy-host\n"
995 " set protocol ftp port proxy-port\n"
996 " set protocol ftp passive {enable|disable}\n"
998 " set protocol http {enable|disable}\n"
999 " set protocol http host proxy-host\n"
1000 " set protocol http port proxy-port\n"
1002 " set protocol https {enable|disable}\n"
1003 " set protocol https host proxy-host\n"
1004 " set protocol https port proxy-port\n"
1006 " set protocol rtsp {enable|disable}\n"
1007 " set protocol rtsp host proxy-host\n"
1008 " set protocol rtsp port proxy-port\n"
1010 " set protocol socks {enable|disable}\n"
1011 " set protocol socks host proxy-host\n"
1012 " set protocol socks port proxy-port\n"
1014 " set protocol .pac {enable|disable}\n"
1015 " set protocol .pac url .pac-url\n"
1017 " set protocol wpad {enable|disable}\n"
1020 #define N_PROXY_OPTIONS (sizeof(proxyOptions) / sizeof(proxyOptions[0]))
1024 __doProxySelect(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1029 SCPrint(TRUE
, stdout
, CFSTR("proxy option[s] not specified\n"));
1033 currentProxy
= (proxyKeys
*)info
;
1035 nextOption
= _find_option(argv
[0], proxyOptions
, N_PROXY_OPTIONS
);
1036 if ((nextOption
== kCFNotFound
) ||
1037 (proxyOptions
[nextOption
].handler
== __doProxySelect
)) {
1038 SCPrint(TRUE
, stdout
, CFSTR("%s proxy option[s] not specified\n"), currentProxy
->proxy
);
1047 __doProxyEnable(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1049 Boolean enabled
= (info
== (void *)FALSE
) ? FALSE
: TRUE
;
1051 if (currentProxy
== NULL
) {
1052 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1056 if (currentProxy
->keyEnable
== NULL
) {
1057 SCPrint(TRUE
, stdout
, CFSTR("%s proxy cannot be %s\n"),
1058 currentProxy
->proxy
,
1059 enabled
? "enabled" : "disabled");
1065 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyEnable
), CFNumberRef_1
);
1067 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyEnable
));
1069 if (currentProxy
->keyProxy
!= NULL
) {
1070 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyProxy
));
1073 if (currentProxy
->keyPort
!= NULL
) {
1074 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyPort
));
1077 if (currentProxy
->keyURL
!= NULL
) {
1078 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyURL
));
1087 __proxy_enabled(CFDictionaryRef configuration
, const CFStringRef
*enableKey
)
1092 if (enableKey
== NULL
) {
1093 return TRUE
; // if proxy does not need to be enabled
1096 num
= CFDictionaryGetValue(configuration
, *enableKey
);
1097 if (!isA_CFNumber(num
) ||
1098 !CFNumberGetValue(num
, kCFNumberIntType
, &val
) ||
1100 return FALSE
; // if not enabled
1108 __doProxyHost(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1110 if (currentProxy
== NULL
) {
1111 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1115 if (currentProxy
->keyProxy
== NULL
) {
1116 SCPrint(TRUE
, stdout
, CFSTR("%s proxy host cannot be specified\n"), currentProxy
->proxy
);
1120 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1121 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1126 SCPrint(TRUE
, stdout
, CFSTR("%s proxy host not specified\n"), currentProxy
->proxy
);
1130 if (strlen(argv
[0]) > 0) {
1133 host
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1134 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyProxy
), host
);
1137 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyProxy
));
1145 __doProxyPort(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1147 if (currentProxy
== NULL
) {
1148 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1152 if (currentProxy
->keyPort
== NULL
) {
1153 SCPrint(TRUE
, stdout
, CFSTR("%s proxy port cannot be specified\n"), currentProxy
->proxy
);
1157 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1158 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1163 SCPrint(TRUE
, stdout
, CFSTR("%s proxy port not specified\n"), currentProxy
->proxy
);
1167 if (strlen(argv
[0]) > 0) {
1171 num
= _copy_number(argv
[0]);
1172 if (!isA_CFNumber(num
) ||
1173 !CFNumberGetValue(num
, kCFNumberIntType
, &port
) ||
1174 (port
< 0) || (port
> 65535)) {
1175 SCPrint(TRUE
, stdout
, CFSTR("invalid %s proxy port number\n"), currentProxy
->proxy
);
1179 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyPort
), num
);
1182 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyPort
));
1190 __doProxyURL(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1192 if (currentProxy
== NULL
) {
1193 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1197 if (currentProxy
->keyURL
== NULL
) {
1198 SCPrint(TRUE
, stdout
, CFSTR("%s proxy URL cannot be specified\n"), currentProxy
->proxy
);
1202 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1203 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1208 SCPrint(TRUE
, stdout
, CFSTR("%s proxy URL not specified\n"), currentProxy
->proxy
);
1212 if (strlen(argv
[0]) > 0) {
1215 url
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1216 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyURL
), url
);
1219 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyURL
));
1227 __doProxyFTPPassive(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1229 if (currentProxy
== NULL
) {
1230 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1234 if (currentProxy
!= &proxyKeys_FTP
) {
1235 SCPrint(TRUE
, stdout
, CFSTR("passive can only be enable for FTP proxy\n"));
1244 set_protocol_proxies(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1248 ok
= _process_options(proxyOptions
, N_PROXY_OPTIONS
, argc
, argv
, newConfiguration
);
1257 #if !TARGET_OS_IPHONE
1261 __cleanupName(CFStringRef name
)
1263 CFMutableStringRef newName
;
1265 newName
= CFStringCreateMutableCopy(NULL
, 0, name
);
1266 CFStringTrimWhitespace(newName
);
1267 if (CFStringGetLength(newName
) == 0) {
1277 __doSMBName(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1280 SCPrint(TRUE
, stdout
, CFSTR("NetBIOS name not specified\n"));
1284 if (strlen(argv
[0]) > 0) {
1288 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1289 name
= __cleanupName(str
);
1293 CFDictionarySetValue(newConfiguration
, key
, name
);
1296 SCPrint(TRUE
, stdout
, CFSTR("invalid NetBIOS name\n"));
1300 CFDictionaryRemoveValue(newConfiguration
, key
);
1308 __doSMBWorkgroup(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1311 SCPrint(TRUE
, stdout
, CFSTR("Workgroup not specified\n"));
1315 if (strlen(argv
[0]) > 0) {
1319 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1320 name
= __cleanupName(str
);
1324 CFDictionarySetValue(newConfiguration
, key
, name
);
1327 SCPrint(TRUE
, stdout
, CFSTR("invalid Workgroup\n"));
1331 CFDictionaryRemoveValue(newConfiguration
, key
);
1339 __doSMBWINSAddresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1341 CFMutableArrayRef servers
;
1344 SCPrint(TRUE
, stdout
, CFSTR("WINS address(es) not specified\n"));
1348 servers
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
1350 if (strlen(argv
[0]) > 0) {
1356 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1357 array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
1360 n
= (array
!= NULL
) ? CFArrayGetCount(array
) : 0;
1361 for (i
= 0; i
< n
; i
++) {
1364 if (_SC_cfstring_to_cstring(CFArrayGetValueAtIndex(array
, i
),
1367 kCFStringEncodingUTF8
) != NULL
) {
1370 server
= __copyIPv4Address(str
);
1371 //if (server == NULL) {
1372 // server = __copyIPv6Address(str);
1374 if (server
!= NULL
) {
1375 CFArrayAppendValue(servers
, server
);
1381 SCPrint(TRUE
, stdout
, CFSTR("invalid WINS address\n"));
1386 if (array
!= NULL
) CFRelease(array
);
1389 if (CFArrayGetCount(servers
) > 0) {
1390 CFDictionarySetValue(newConfiguration
, key
, servers
);
1392 CFDictionaryRemoveValue(newConfiguration
, key
);
1400 static selections smbNodeTypes
[] = {
1401 { CFSTR("Broadcast"), &kSCValNetSMBNetBIOSNodeTypeBroadcast
, 0 },
1402 { CFSTR("Peer") , &kSCValNetSMBNetBIOSNodeTypePeer
, 0 },
1403 { CFSTR("Mixed") , &kSCValNetSMBNetBIOSNodeTypeMixed
, 0 },
1404 { CFSTR("Hybrid") , &kSCValNetSMBNetBIOSNodeTypeHybrid
, 0 },
1409 static options smbOptions
[] = {
1410 { "NetBIOSName" , "name" , isOther
, &kSCPropNetSMBNetBIOSName
, __doSMBName
, NULL
},
1411 { "name" , "name" , isOther
, &kSCPropNetSMBNetBIOSName
, __doSMBName
, NULL
},
1412 { "NetBIOSNodeType", "type" , isChooseOne
, &kSCPropNetSMBNetBIOSNodeType
, NULL
, (void *)smbNodeTypes
},
1413 { "type", "type" , isChooseOne
, &kSCPropNetSMBNetBIOSNodeType
, NULL
, (void *)smbNodeTypes
},
1414 { "Workgroup" , "workgroup", isOther
, &kSCPropNetSMBWorkgroup
, __doSMBWorkgroup
, NULL
},
1415 { "WINSAddresses" , "wins" , isOther
, &kSCPropNetSMBWINSAddresses
, __doSMBWINSAddresses
, NULL
},
1416 { "wins" , "wins" , isOther
, &kSCPropNetSMBWINSAddresses
, __doSMBWINSAddresses
, NULL
},
1418 { "?" , NULL
, isHelp
, NULL
, NULL
,
1419 "\nSMB configuration commands\n\n"
1420 " set protocol name NetBIOS-name\n"
1421 " set protocol type (Broadcast|Peer|Mixed|Hybrid)\n"
1422 " set protocol workgroup SMB-workgroup\n"
1423 " set protocol wins x1.x1.x1.x1[,x2.x2.x2.x2]\n"
1426 #define N_SMB_OPTIONS (sizeof(smbOptions) / sizeof(smbOptions[0]))
1430 set_protocol_smb(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1434 ok
= _process_options(smbOptions
, N_SMB_OPTIONS
, argc
, argv
, newConfiguration
);
1439 #endif // !TARGET_OS_IPHONE
1443 #pragma mark *Protocol*
1448 set_protocol(int argc
, char **argv
)
1450 CFDictionaryRef configuration
;
1451 CFMutableDictionaryRef newConfiguration
= NULL
;
1453 CFStringRef protocolType
;
1455 if (net_protocol
== NULL
) {
1456 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
1461 SCPrint(TRUE
, stdout
, CFSTR("set what?\n"));
1465 configuration
= SCNetworkProtocolGetConfiguration(net_protocol
);
1466 if (configuration
== NULL
) {
1467 newConfiguration
= CFDictionaryCreateMutable(NULL
,
1469 &kCFTypeDictionaryKeyCallBacks
,
1470 &kCFTypeDictionaryValueCallBacks
);
1472 newConfiguration
= CFDictionaryCreateMutableCopy(NULL
, 0, configuration
);
1473 CFDictionaryRemoveValue(newConfiguration
, kSCResvInactive
);
1476 protocolType
= SCNetworkProtocolGetProtocolType(net_protocol
);
1477 if (CFEqual(protocolType
, kSCNetworkProtocolTypeDNS
)) {
1478 ok
= set_protocol_dns(argc
, argv
, newConfiguration
);
1479 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv4
)) {
1480 ok
= set_protocol_ipv4(argc
, argv
, newConfiguration
);
1481 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv6
)) {
1482 ok
= set_protocol_ipv6(argc
, argv
, newConfiguration
);
1483 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeProxies
)) {
1484 ok
= set_protocol_proxies(argc
, argv
, newConfiguration
);
1485 #if !TARGET_OS_IPHONE
1486 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeSMB
)) {
1487 ok
= set_protocol_smb(argc
, argv
, newConfiguration
);
1488 #endif // !TARGET_OS_IPHONE
1490 SCPrint(TRUE
, stdout
, CFSTR("this protocols configuration cannot be changed\n"));
1497 if (((configuration
== NULL
) && (CFDictionaryGetCount(newConfiguration
) > 0)) ||
1498 ((configuration
!= NULL
) && !CFEqual(configuration
, newConfiguration
))) {
1499 if (!SCNetworkProtocolSetConfiguration(net_protocol
, newConfiguration
)) {
1500 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
1504 _prefs_changed
= TRUE
;
1509 if (newConfiguration
!= NULL
) CFRelease(newConfiguration
);
1514 /* -------------------- */
1519 show_protocol(int argc
, char **argv
)
1521 CFDictionaryRef configuration
;
1522 SCNetworkProtocolRef protocol
= NULL
;
1523 CFStringRef protocolType
;
1526 protocol
= _find_protocol(argv
[0]);
1528 if (net_protocol
!= NULL
) {
1529 protocol
= net_protocol
;
1531 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
1536 if (protocol
== NULL
) {
1540 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1541 SCPrint(TRUE
, stdout
, CFSTR("protocol type = %@\n"), protocolType
);
1543 configuration
= SCNetworkProtocolGetConfiguration(protocol
);
1544 if (configuration
!= NULL
) {
1545 SCPrint(TRUE
, stdout
, CFSTR("\n protocol configuration\n"));
1546 _show_entity(configuration
, CFSTR(""));
1550 SCPrint(TRUE
, stdout
, CFSTR("\n%@\n"), protocol
);
1557 /* -------------------- */
1562 show_protocols(int argc
, char **argv
)
1567 if (prefs
== NULL
) {
1568 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
1572 if (net_service
== NULL
) {
1573 SCPrint(TRUE
, stdout
, CFSTR("service not selected\n"));
1577 if (protocols
!= NULL
) CFRelease(protocols
);
1578 protocols
= SCNetworkServiceCopyProtocols(net_service
);
1579 if (protocols
== NULL
) {
1580 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
1584 n
= CFArrayGetCount(protocols
);
1586 CFMutableArrayRef sorted
;
1588 sorted
= CFArrayCreateMutableCopy(NULL
, 0, protocols
);
1589 CFArraySortValues(sorted
,
1593 CFRelease(protocols
);
1597 for (i
= 0; i
< n
; i
++) {
1598 SCNetworkProtocolRef protocol
;
1599 CFStringRef protocolType
;
1601 protocol
= CFArrayGetValueAtIndex(protocols
, i
);
1602 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1604 SCPrint(TRUE
, stdout
, CFSTR("%c%2d: %@%*s :"),
1605 ((net_protocol
!= NULL
) && CFEqual(protocol
, net_protocol
)) ? '>' : ' ',
1608 sizeof("AppleTalk") - CFStringGetLength(protocolType
) - 1,
1611 if (SCNetworkProtocolGetEnabled(protocol
)) {
1612 CFStringRef description
;
1614 description
= _protocol_description(protocol
, FALSE
);
1615 SCPrint(TRUE
, stdout
, CFSTR(" %@"), description
);
1616 CFRelease(description
);
1618 SCPrint(TRUE
, stdout
, CFSTR(" *DISABLED*"));
1620 SCPrint(TRUE
, stdout
, CFSTR("\n"));
1627 /* -------------------- */
1632 _protocol_description(SCNetworkProtocolRef protocol
, Boolean skipEmpty
)
1634 CFDictionaryRef configuration
;
1635 CFMutableStringRef description
= NULL
;
1636 CFStringRef protocolType
;
1638 description
= CFStringCreateMutable(NULL
, 0);
1640 if (!SCNetworkProtocolGetEnabled(protocol
)) {
1644 configuration
= SCNetworkProtocolGetConfiguration(protocol
);
1645 if (configuration
== NULL
) {
1649 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1650 if (CFEqual(protocolType
, kSCNetworkProtocolTypeDNS
)) {
1655 domain
= CFDictionaryGetValue(configuration
, kSCPropNetDNSDomainName
);
1656 if (isA_CFString(domain
)) {
1657 CFStringAppendFormat(description
,
1663 search
= CFDictionaryGetValue(configuration
, kSCPropNetDNSSearchDomains
);
1664 if (isA_CFArray(search
)) {
1667 str
= CFStringCreateByCombiningStrings(NULL
, search
, CFSTR(","));
1668 CFStringAppendFormat(description
,
1670 CFSTR("%ssearch=%@"),
1671 CFStringGetLength(description
) > 0 ? ", " : "",
1676 servers
= CFDictionaryGetValue(configuration
, kSCPropNetDNSServerAddresses
);
1677 if (isA_CFArray(servers
)) {
1680 str
= CFStringCreateByCombiningStrings(NULL
, servers
, CFSTR(","));
1681 CFStringAppendFormat(description
,
1683 CFSTR("%sservers=%@"),
1684 CFStringGetLength(description
) > 0 ? ", " : "",
1688 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv4
)) {
1691 method
= CFDictionaryGetValue(configuration
, kSCPropNetIPv4ConfigMethod
);
1692 if (isA_CFString(method
)) {
1693 CFArrayRef addresses
;
1695 addresses
= CFDictionaryGetValue(configuration
, kSCPropNetIPv4Addresses
);
1696 if (CFEqual(method
, kSCValNetIPv4ConfigMethodINFORM
) &&
1697 isA_CFArray(addresses
)) {
1698 CFStringAppendFormat(description
,
1700 CFSTR("%@, address=%@"),
1702 CFArrayGetValueAtIndex(addresses
, 0));
1703 } else if (CFEqual(method
, kSCValNetIPv4ConfigMethodManual
) &&
1704 isA_CFArray(addresses
)) {
1705 CFStringAppendFormat(description
,
1707 CFSTR("%@, address=%@"),
1709 CFArrayGetValueAtIndex(addresses
, 0));
1711 CFStringAppendFormat(description
,
1717 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv6
)) {
1720 method
= CFDictionaryGetValue(configuration
, kSCPropNetIPv6ConfigMethod
);
1721 if (isA_CFString(method
)) {
1722 CFStringAppendFormat(description
,
1727 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeProxies
)) {
1729 static proxyKeys
*keys
[] = { &proxyKeys_FTP
, &proxyKeys_Gopher
, &proxyKeys_HTTP
, &proxyKeys_HTTPS
,
1730 &proxyKeys_RTSP
, &proxyKeys_SOCKS
, &proxyKeys_PAC
, &proxyKeys_WPAD
};
1732 for (i
= 0; i
< sizeof(keys
)/sizeof(keys
[0]); i
++) {
1733 proxyKeys
*currentProxy
= keys
[i
];
1735 if (!__proxy_enabled(configuration
, currentProxy
->keyEnable
)) {
1739 if (((currentProxy
->keyProxy
!= NULL
) &&
1740 !CFDictionaryContainsKey(configuration
, *(currentProxy
->keyProxy
))) ||
1741 ((currentProxy
->keyURL
!= NULL
) &&
1742 !CFDictionaryContainsKey(configuration
, *(currentProxy
->keyURL
)))) {
1746 CFStringAppendFormat(description
,
1749 CFStringGetLength(description
) > 0 ? ", " : "",
1750 currentProxy
->proxy
);
1752 #if !TARGET_OS_IPHONE
1753 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeSMB
)) {
1756 CFStringRef workgroup
;
1758 name
= CFDictionaryGetValue(configuration
, kSCPropNetSMBNetBIOSName
);
1759 if (isA_CFString(name
)) {
1760 CFStringAppendFormat(description
,
1762 CFSTR("NetBIOS name=%@"),
1766 workgroup
= CFDictionaryGetValue(configuration
, kSCPropNetSMBWorkgroup
);
1767 if (isA_CFString(workgroup
)) {
1768 CFStringAppendFormat(description
,
1770 CFSTR("Workgroup=%@"),
1774 servers
= CFDictionaryGetValue(configuration
, kSCPropNetSMBWINSAddresses
);
1775 if (isA_CFArray(servers
)) {
1778 str
= CFStringCreateByCombiningStrings(NULL
, servers
, CFSTR(","));
1779 CFStringAppendFormat(description
,
1781 CFSTR("%sWINS servers=%@"),
1782 CFStringGetLength(description
) > 0 ? ", " : "",
1786 #endif // !TARGET_OS_IPHONE
1791 if (skipEmpty
&& CFStringGetLength(description
) == 0) {
1792 CFRelease(description
);