2 * Copyright (c) 2004 Apple Computer, 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()));
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()));
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()));
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()));
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
));
420 /* -------------------- */
423 static selections appletalkConfigMethods
[] = {
424 { CFSTR("node") , &kSCValNetAppleTalkConfigMethodNode
, 0 },
425 { CFSTR("router") , &kSCValNetAppleTalkConfigMethodRouter
, 0 },
426 { CFSTR("seedrouter"), &kSCValNetAppleTalkConfigMethodSeedRouter
, 0 },
432 __doAppleTalkConfigMethod(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
434 CFStringRef configMethod
;
436 configMethod
= CFDictionaryGetValue(newConfiguration
, key
);
437 if (!CFEqual(key
, kSCValNetAppleTalkConfigMethodSeedRouter
)) {
438 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetAppleTalkSeedZones
);
439 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetAppleTalkSeedNetworkRange
);
447 __doAppleTalkNetworkRange(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
450 SCPrint(TRUE
, stdout
, CFSTR("network range not specified\n"));
454 if (strlen(argv
[0]) > 0) {
457 CFNumberRef range
[2];
459 range
[0] = _copy_number(argv
[0]);
460 if (range
[0] == NULL
) {
461 SCPrint(TRUE
, stdout
, CFSTR("invalid start of range\n"));
465 cp
= strchr(argv
[0], '-');
467 range
[1] = _copy_number(cp
);
468 if (range
[1] == NULL
) {
470 SCPrint(TRUE
, stdout
, CFSTR("invalid end of range\n"));
474 range
[1] = CFRetain(range
[0]);
477 array
= CFArrayCreate(NULL
,
478 (const void **)range
,
479 sizeof(range
)/sizeof(range
[0]),
480 &kCFTypeArrayCallBacks
);
484 CFDictionarySetValue(newConfiguration
, key
, array
);
487 CFDictionaryRemoveValue(newConfiguration
, key
);
494 static options appletalkOptions
[] = {
495 { "ConfigMethod" , "configuration method"
496 , isChooseOne
, &kSCPropNetAppleTalkConfigMethod
, __doAppleTalkConfigMethod
, (void *)appletalkConfigMethods
},
497 { "config" , "configuration method"
498 , isChooseOne
, &kSCPropNetAppleTalkConfigMethod
, __doAppleTalkConfigMethod
, (void *)appletalkConfigMethods
},
499 { "DefaultZone" , "zone" , isString
, &kSCPropNetAppleTalkDefaultZone
, NULL
, NULL
},
500 { "NodeID" , "node" , isNumber
, &kSCPropNetAppleTalkNodeID
, NULL
, NULL
},
501 { "NetworkID" , "network", isNumber
, &kSCPropNetAppleTalkNetworkID
, NULL
, NULL
},
502 { "SeedNetworkRange", "range" , isOther
, &kSCPropNetAppleTalkSeedNetworkRange
, __doAppleTalkNetworkRange
, NULL
},
503 { "SeedZones" , "zone" , isStringArray
, &kSCPropNetAppleTalkSeedZones
, NULL
, NULL
},
505 { "?" , NULL
, isHelp
, NULL
, NULL
,
506 "\nAppleTalk configuration commands\n\n"
507 " set protocol config {Node|Router|SeedRouter}\n"
508 " set protocol defaultzone zone\n"
509 " set protocol node id\n"
510 " set protocol network id\n"
513 "\n w/config=Router\n"
515 "\n w/config=SeedRouter\n"
516 " set protocol seednetworkrange low[-high]\n"
517 " set protocol seedzones zone[,zone-2]\n"
520 #define N_APPLETALK_OPTIONS (sizeof(appletalkOptions) / sizeof(appletalkOptions[0]))
524 set_protocol_appletalk(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
528 ok
= _process_options(appletalkOptions
, N_APPLETALK_OPTIONS
, argc
, argv
, newConfiguration
);
533 /* -------------------- */
537 __cleanupDomainName(CFStringRef domain
)
539 CFMutableStringRef newDomain
;
541 newDomain
= CFStringCreateMutableCopy(NULL
, 0, domain
);
542 CFStringTrimWhitespace(newDomain
);
543 CFStringTrim(newDomain
, CFSTR("."));
544 if (CFStringGetLength(newDomain
) == 0) {
545 CFRelease(newDomain
);
554 __doDNSDomain(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
557 SCPrint(TRUE
, stdout
, CFSTR("DNS domain name not specified\n"));
561 if (strlen(argv
[0]) > 0) {
565 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
566 domain
= __cleanupDomainName(str
);
569 if (domain
!= NULL
) {
570 CFDictionarySetValue(newConfiguration
, key
, domain
);
573 SCPrint(TRUE
, stdout
, CFSTR("invalid DNS domain name\n"));
577 CFDictionaryRemoveValue(newConfiguration
, key
);
585 __doDNSDomainArray(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
587 CFMutableArrayRef domains
;
590 SCPrint(TRUE
, stdout
, CFSTR("DNS search domain name(s) not specified\n"));
594 domains
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
596 if (strlen(argv
[0]) > 0) {
600 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
601 array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
606 CFIndex n
= CFArrayGetCount(array
);
608 for (i
= 0; i
< n
; i
++) {
611 domain
= __cleanupDomainName(CFArrayGetValueAtIndex(array
, i
));
612 if (domain
!= NULL
) {
613 CFArrayAppendValue(domains
, domain
);
618 SCPrint(TRUE
, stdout
, CFSTR("invalid DNS search domain name\n"));
626 if (CFArrayGetCount(domains
) > 0) {
627 CFDictionarySetValue(newConfiguration
, key
, domains
);
629 CFDictionaryRemoveValue(newConfiguration
, key
);
638 __doDNSServerAddresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
640 CFMutableArrayRef servers
;
643 SCPrint(TRUE
, stdout
, CFSTR("DNS search domain name(s) not specified\n"));
647 servers
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
649 if (strlen(argv
[0]) > 0) {
655 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
656 array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
659 n
= (array
!= NULL
) ? CFArrayGetCount(array
) : 0;
660 for (i
= 0; i
< n
; i
++) {
663 if (_SC_cfstring_to_cstring(CFArrayGetValueAtIndex(array
, i
),
666 kCFStringEncodingUTF8
) != NULL
) {
669 server
= __copyIPv4Address(str
);
670 if (server
== NULL
) {
671 server
= __copyIPv6Address(str
);
673 if (server
!= NULL
) {
674 CFArrayAppendValue(servers
, server
);
680 SCPrint(TRUE
, stdout
, CFSTR("invalid DNS name server address\n"));
685 if (array
!= NULL
) CFRelease(array
);
688 if (CFArrayGetCount(servers
) > 0) {
689 CFDictionarySetValue(newConfiguration
, key
, servers
);
691 CFDictionaryRemoveValue(newConfiguration
, key
);
699 static options dnsOptions
[] = {
700 { "DomainName" , "domain" , isOther
, &kSCPropNetDNSDomainName
, __doDNSDomain
, NULL
},
701 { "domain" , "domain" , isOther
, &kSCPropNetDNSDomainName
, __doDNSDomain
, NULL
},
702 { "SearchDomains" , "search" , isOther
, &kSCPropNetDNSSearchDomains
, __doDNSDomainArray
, NULL
},
703 { "search" , "search" , isOther
, &kSCPropNetDNSSearchDomains
, __doDNSDomainArray
, NULL
},
704 { "ServerAddresses", "address", isOther
, &kSCPropNetDNSServerAddresses
, __doDNSServerAddresses
, NULL
},
705 { "nameserver" , "address", isOther
, &kSCPropNetDNSServerAddresses
, __doDNSServerAddresses
, NULL
},
706 { "nameservers" , "address", isOther
, &kSCPropNetDNSServerAddresses
, __doDNSServerAddresses
, NULL
},
708 { "?" , NULL
, isHelp
, NULL
, NULL
,
709 "\nDNS configuration commands\n\n"
710 " set protocol search domain-name[,domain-name-2]\n"
711 " set protocol nameserver x1.x1.x1.x1[,x2.x2.x2.x2]"
714 #define N_DNS_OPTIONS (sizeof(dnsOptions) / sizeof(dnsOptions[0]))
718 set_protocol_dns(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
722 ok
= _process_options(dnsOptions
, N_DNS_OPTIONS
, argc
, argv
, newConfiguration
);
727 /* -------------------- */
730 #define allowIPv4Address 1<<1 // allow address
731 #define allowIPv4Netmask 1<<2 // allow subnet mask
732 #define allowIPv4Router 1<<3 // allow router
733 #define allowIPv4DHCPClientID 1<<4 // allow DCHP Client ID
735 static selections ipv4ConfigMethods
[] = {
736 { CFSTR("BOOTP") , &kSCValNetIPv4ConfigMethodBOOTP
, 0 },
737 { CFSTR("DHCP") , &kSCValNetIPv4ConfigMethodDHCP
, allowIPv4DHCPClientID
},
738 { CFSTR("INFORM") , &kSCValNetIPv4ConfigMethodINFORM
, allowIPv4Address
},
739 { CFSTR("LinkLocal"), &kSCValNetIPv4ConfigMethodLinkLocal
, 0 },
740 { CFSTR("Manual") , &kSCValNetIPv4ConfigMethodManual
, allowIPv4Address
|allowIPv4Netmask
|allowIPv4Router
},
741 { CFSTR("PPP") , &kSCValNetIPv4ConfigMethodPPP
, allowIPv4Address
|selectionNotAvailable
},
747 __doIPv4ConfigMethod(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
753 method
= CFDictionaryGetValue(newConfiguration
, key
);
754 methodIndex
= _find_selection(method
, (selections
*)ipv4ConfigMethods
, &flags
);
755 if (methodIndex
!= kCFNotFound
) {
756 if (!(flags
& allowIPv4Address
)) {
757 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv4Addresses
);
759 if (!(flags
& allowIPv4Netmask
)) {
760 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv4SubnetMasks
);
762 if (!(flags
& allowIPv4Router
)) {
763 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv4Router
);
765 if (!(flags
& allowIPv4DHCPClientID
)) {
766 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv4DHCPClientID
);
769 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
778 __doIPv4Addresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
780 Boolean useArray
= (info
== (void *)FALSE
) ? FALSE
: TRUE
;
782 if (strlen(argv
[0]) > 0) {
785 address
= __copyIPv4Address(argv
[0]);
786 if (address
!= NULL
) {
788 CFArrayRef addresses
;
790 addresses
= CFArrayCreate(NULL
, (const void **)&address
, 1, &kCFTypeArrayCallBacks
);
791 CFDictionarySetValue(newConfiguration
, key
, addresses
);
792 CFRelease(addresses
);
794 CFDictionarySetValue(newConfiguration
, key
, address
);
801 CFDictionaryRemoveValue(newConfiguration
, key
);
808 static options ipv4Options
[] = {
809 { "ConfigMethod", "configuration method"
810 , isChooseOne
, &kSCPropNetIPv4ConfigMethod
, __doIPv4ConfigMethod
, (void *)ipv4ConfigMethods
},
811 { "config" , "configuration method"
812 , isChooseOne
, &kSCPropNetIPv4ConfigMethod
, __doIPv4ConfigMethod
, (void *)ipv4ConfigMethods
},
813 { "Addresses" , "address" , isOther
, &kSCPropNetIPv4Addresses
, __doIPv4Addresses
, (void *)TRUE
},
814 { "address" , "address" , isOther
, &kSCPropNetIPv4Addresses
, __doIPv4Addresses
, (void *)TRUE
},
815 { "SubnetMasks" , "netmask" , isOther
, &kSCPropNetIPv4SubnetMasks
, __doIPv4Addresses
, (void *)TRUE
},
816 { "netmask" , "netmask" , isOther
, &kSCPropNetIPv4SubnetMasks
, __doIPv4Addresses
, (void *)TRUE
},
817 { "Router" , "address" , isOther
, &kSCPropNetIPv4Router
, __doIPv4Addresses
, (void *)FALSE
},
818 { "DHCPClientID", "client ID", isString
, &kSCPropNetIPv4DHCPClientID
, NULL
, NULL
},
820 { "?" , NULL
, isHelp
, NULL
, NULL
,
821 "\nIPv4 configuration commands\n\n"
822 " set protocol config {BOOTP|DHCP|INFORM|MANUAL}\n"
823 "\n w/config=BOOTP\n"
826 " set protocol dhcpclientid identifier\n"
827 "\n w/config=INFORM\n"
828 " set protocol address x.x.x.x\n"
829 "\n w/config=MANUAL\n"
830 " set protocol address x.x.x.x\n"
831 " set protocol netmask x.x.x.x\n"
832 " set protocol router x.x.x.x\n"
835 #define N_IPV4_OPTIONS (sizeof(ipv4Options) / sizeof(ipv4Options[0]))
839 set_protocol_ipv4(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
843 ok
= _process_options(ipv4Options
, N_IPV4_OPTIONS
, argc
, argv
, newConfiguration
);
849 // validate configuration
850 method
= CFDictionaryGetValue(newConfiguration
, kSCPropNetIPv4ConfigMethod
);
851 methodIndex
= _find_selection(method
, (selections
*)ipv4ConfigMethods
, &flags
);
852 if (methodIndex
== kCFNotFound
) {
853 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
857 if (!(flags
& allowIPv4Address
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv4Addresses
)) {
858 SCPrint(TRUE
, stdout
,
859 CFSTR("IP address not allowed with %@ configuration\n"),
860 ipv4ConfigMethods
[methodIndex
].selection
);
864 if (!(flags
& allowIPv4Netmask
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv4SubnetMasks
)) {
865 SCPrint(TRUE
, stdout
,
866 CFSTR("Subnet mask not allowed with %@ configuration\n"),
867 ipv4ConfigMethods
[methodIndex
].selection
);
871 if (!(flags
& allowIPv4Router
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv4Router
)) {
872 SCPrint(TRUE
, stdout
,
873 CFSTR("Default route not allowed with %@ configuration\n"),
874 ipv4ConfigMethods
[methodIndex
].selection
);
878 if (!(flags
& allowIPv4DHCPClientID
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv4DHCPClientID
)) {
879 SCPrint(TRUE
, stdout
,
880 CFSTR("DHCP client ID not allowed with %@ configuration\n"),
881 ipv4ConfigMethods
[methodIndex
].selection
);
890 /* -------------------- */
893 #define allowIPv6Address 1<<1 // allow address
894 #define allowIPv6PrefixLength 1<<2 // allow prefix length
895 #define allowIPv6Router 1<<3 // allow router
897 static selections ipv6ConfigMethods
[] = {
898 { CFSTR("Automatic") , & kSCValNetIPv6ConfigMethodAutomatic
, 0 },
899 { CFSTR("Manual") , & kSCValNetIPv6ConfigMethodManual
, allowIPv6Address
|allowIPv6PrefixLength
|allowIPv6Router
},
900 { CFSTR("RouterAdvertisement"), & kSCValNetIPv6ConfigMethodRouterAdvertisement
, allowIPv6Address
},
901 { CFSTR("6to4") , & kSCValNetIPv6ConfigMethod6to4
, 0 },
907 __doIPv6ConfigMethod(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
913 method
= CFDictionaryGetValue(newConfiguration
, key
);
914 methodIndex
= _find_selection(method
, (selections
*)ipv6ConfigMethods
, &flags
);
915 if (methodIndex
!= kCFNotFound
) {
916 if (!(flags
& allowIPv6Address
)) {
917 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv6Addresses
);
919 if (!(flags
& allowIPv6PrefixLength
)) {
920 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv6PrefixLength
);
922 if (!(flags
& allowIPv6Router
)) {
923 CFDictionaryRemoveValue(newConfiguration
, kSCPropNetIPv6Router
);
926 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
935 __doIPv6Addresses(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
937 Boolean useArray
= (info
== (void *)FALSE
) ? FALSE
: TRUE
;
939 if (strlen(argv
[0]) > 0) {
942 address
= __copyIPv6Address(argv
[0]);
943 if (address
!= NULL
) {
945 CFArrayRef addresses
;
947 addresses
= CFArrayCreate(NULL
, (const void **)&address
, 1, &kCFTypeArrayCallBacks
);
948 CFDictionarySetValue(newConfiguration
, key
, addresses
);
949 CFRelease(addresses
);
951 CFDictionarySetValue(newConfiguration
, key
, address
);
958 CFDictionaryRemoveValue(newConfiguration
, key
);
966 __doIPv6PrefixLength(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
971 num
= CFDictionaryGetValue(newConfiguration
, kSCPropNetPPPAuthPasswordEncryption
);
972 if (isA_CFNumber(num
) &&
973 CFNumberGetValue(num
, kCFNumberIntType
, &prefixLength
) &&
974 (prefixLength
>= 0) && (prefixLength
<= (sizeof(struct in6_addr
) * 8))) {
982 static options ipv6Options
[] = {
983 { "ConfigMethod", "configuration method"
984 , isChooseOne
, &kSCPropNetIPv6ConfigMethod
, __doIPv6ConfigMethod
, (void *)ipv6ConfigMethods
},
985 { "config" , "configuration method"
986 , isChooseOne
, &kSCPropNetIPv6ConfigMethod
, __doIPv6ConfigMethod
, (void *)ipv6ConfigMethods
},
987 { "Addresses" , "address" , isOther
, &kSCPropNetIPv6Addresses
, __doIPv6Addresses
, (void *)TRUE
},
988 { "address" , "address" , isOther
, &kSCPropNetIPv6Addresses
, __doIPv6Addresses
, (void *)TRUE
},
989 { "PrefixLength", "prefix length", isNumber
, &kSCPropNetIPv6PrefixLength
, __doIPv6PrefixLength
, NULL
},
990 { "Router" , "address" , isOther
, &kSCPropNetIPv6Router
, __doIPv6Addresses
, (void *)FALSE
},
992 { "?" , NULL
, isHelp
, NULL
, NULL
,
993 "\nIPv6 configuration commands\n\n"
994 " set protocol config {Automatic|MANUAL}\n"
995 "\n w/config=Automatic\n"
997 "\n w/config=MANUAL\n"
998 " set protocol address x:x:x:x:x:x\n"
999 " set protocol router x:x:x:x:x:x\n"
1000 " set protocol prefixlength n\n"
1003 #define N_IPV6_OPTIONS (sizeof(ipv6Options) / sizeof(ipv6Options[0]))
1007 set_protocol_ipv6(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1011 ok
= _process_options(ipv6Options
, N_IPV6_OPTIONS
, argc
, argv
, newConfiguration
);
1015 CFIndex methodIndex
;
1017 // validate configuration
1018 method
= CFDictionaryGetValue(newConfiguration
, kSCPropNetIPv6ConfigMethod
);
1019 methodIndex
= _find_selection(method
, (selections
*)ipv6ConfigMethods
, &flags
);
1020 if (methodIndex
== kCFNotFound
) {
1021 SCPrint(TRUE
, stdout
, CFSTR("unknown configuration method\n"));
1025 if (!(flags
& allowIPv6Address
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6Addresses
)) {
1026 SCPrint(TRUE
, stdout
,
1027 CFSTR("IP address not allowed with %@ configuration\n"),
1028 ipv6ConfigMethods
[methodIndex
].selection
);
1032 if (!(flags
& allowIPv6PrefixLength
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6PrefixLength
)) {
1033 SCPrint(TRUE
, stdout
,
1034 CFSTR("Prefix length not allowed with %@ configuration\n"),
1035 ipv6ConfigMethods
[methodIndex
].selection
);
1039 if (!(flags
& allowIPv6Router
) && CFDictionaryContainsKey(newConfiguration
, kSCPropNetIPv6Router
)) {
1040 SCPrint(TRUE
, stdout
,
1041 CFSTR("Router not allowed with %@ configuration\n"),
1042 ipv6ConfigMethods
[methodIndex
].selection
);
1051 /* -------------------- */
1054 typedef const struct {
1056 const CFStringRef
*keyEnable
;
1057 const CFStringRef
*keyProxy
;
1058 const CFStringRef
*keyPort
;
1059 const CFStringRef
*keyURL
;
1062 static proxyKeys proxyKeys_FTP
= { "FTP" , &kSCPropNetProxiesFTPEnable
, &kSCPropNetProxiesFTPProxy
, &kSCPropNetProxiesFTPPort
, NULL
};
1063 static proxyKeys proxyKeys_Gopher
= { "Gopher", &kSCPropNetProxiesGopherEnable
, &kSCPropNetProxiesGopherProxy
, &kSCPropNetProxiesGopherPort
, NULL
};
1064 static proxyKeys proxyKeys_HTTP
= { "HTTP" , &kSCPropNetProxiesHTTPEnable
, &kSCPropNetProxiesHTTPProxy
, &kSCPropNetProxiesHTTPPort
, NULL
};
1065 static proxyKeys proxyKeys_HTTPS
= { "HTTPS" , &kSCPropNetProxiesHTTPSEnable
, &kSCPropNetProxiesHTTPSProxy
, &kSCPropNetProxiesHTTPSPort
, NULL
};
1066 static proxyKeys proxyKeys_RTSP
= { "RTSP" , &kSCPropNetProxiesRTSPEnable
, &kSCPropNetProxiesRTSPProxy
, &kSCPropNetProxiesRTSPPort
, NULL
};
1067 static proxyKeys proxyKeys_SOCKS
= { "SOCKS" , &kSCPropNetProxiesSOCKSEnable
, &kSCPropNetProxiesSOCKSProxy
, &kSCPropNetProxiesSOCKSPort
, NULL
};
1068 static proxyKeys proxyKeys_PAC
= { ".pac" , &kSCPropNetProxiesProxyAutoConfigEnable
, NULL
, NULL
, &kSCPropNetProxiesProxyAutoConfigURLString
};
1069 static proxyKeys proxyKeys_WPAD
= { "WPAD" , &kSCPropNetProxiesProxyAutoDiscoveryEnable
, NULL
, NULL
, NULL
};
1071 static proxyKeys
*currentProxy
= NULL
;
1074 static int __doProxySelect (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
1075 static int __doProxyEnable (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
1076 static int __doProxyHost (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
1077 static int __doProxyPort (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
1078 static int __doProxyURL (CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
1079 static int __doProxyFTPPassive(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
);
1082 static options proxyOptions
[] = {
1084 { "ExceptionsList" , "exceptions", isStringArray
, &kSCPropNetProxiesExceptionsList
, NULL
, NULL
},
1085 { "ExcludeSimpleHostnames", NULL
, isBoolean
, &kSCPropNetProxiesExcludeSimpleHostnames
, NULL
, NULL
},
1087 { "FTP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_FTP
},
1088 { "Gopher" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_Gopher
},
1089 { "HTTP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_HTTP
},
1090 { "HTTPS" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_HTTPS
},
1091 { "RTSP" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_RTSP
},
1092 { "SOCKS" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_SOCKS
},
1093 { "ProxyAutoConfig" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_PAC
},
1094 { ".pac" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_PAC
},
1095 { "ProxyAutoDiscovery" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_WPAD
},
1096 { "WPAD" , NULL
, isOther
, NULL
, __doProxySelect
, (void *)&proxyKeys_WPAD
},
1098 { "disable" , NULL
, isOther
, NULL
, __doProxyEnable
, (void *)FALSE
},
1099 { "enable" , NULL
, isOther
, NULL
, __doProxyEnable
, (void *)TRUE
},
1100 { "proxy" , NULL
, isOther
, NULL
, __doProxyHost
, NULL
},
1101 { "host" , NULL
, isOther
, NULL
, __doProxyHost
, NULL
},
1102 { "port" , NULL
, isOther
, NULL
, __doProxyPort
, NULL
},
1103 { "url" , NULL
, isOther
, NULL
, __doProxyURL
, NULL
},
1104 // (ftp) proxy modifiers
1105 { "FTPPassive" , NULL
, isBoolean
, &kSCPropNetProxiesFTPPassive
, __doProxyFTPPassive
, NULL
},
1106 { "passive" , NULL
, isBoolean
, &kSCPropNetProxiesFTPPassive
, __doProxyFTPPassive
, NULL
},
1108 { "?" , NULL
, isHelp
, NULL
, NULL
,
1109 "\nProxy configuration commands\n\n"
1110 " set protocol ExceptionsList exception[,exception-2]\n"
1111 " set protocol ExcludeSimpleHostnames {enable|disable}\n"
1113 " set protocol ftp {enable|disable}\n"
1114 " set protocol ftp host proxy-host\n"
1115 " set protocol ftp port proxy-port\n"
1116 " set protocol ftp passive {enable|disable}\n"
1118 " set protocol http {enable|disable}\n"
1119 " set protocol http host proxy-host\n"
1120 " set protocol http port proxy-port\n"
1122 " set protocol https {enable|disable}\n"
1123 " set protocol https host proxy-host\n"
1124 " set protocol https port proxy-port\n"
1126 " set protocol rtsp {enable|disable}\n"
1127 " set protocol rtsp host proxy-host\n"
1128 " set protocol rtsp port proxy-port\n"
1130 " set protocol socks {enable|disable}\n"
1131 " set protocol socks host proxy-host\n"
1132 " set protocol socks port proxy-port\n"
1134 " set protocol .pac {enable|disable}\n"
1135 " set protocol .pac url .pac-url\n"
1137 " set protocol wpad {enable|disable}\n"
1140 #define N_PROXY_OPTIONS (sizeof(proxyOptions) / sizeof(proxyOptions[0]))
1144 __doProxySelect(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1149 SCPrint(TRUE
, stdout
, CFSTR("proxy option[s] not specified\n"));
1153 currentProxy
= (proxyKeys
*)info
;
1155 nextOption
= _find_option(argv
[0], proxyOptions
, N_PROXY_OPTIONS
);
1156 if ((nextOption
== kCFNotFound
) ||
1157 (proxyOptions
[nextOption
].handler
== __doProxySelect
)) {
1158 SCPrint(TRUE
, stdout
, CFSTR("%s proxy option[s] not specified\n"), currentProxy
->proxy
);
1167 __doProxyEnable(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1169 Boolean enabled
= (info
== (void *)FALSE
) ? FALSE
: TRUE
;
1171 if (currentProxy
== NULL
) {
1172 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1176 if (currentProxy
->keyEnable
== NULL
) {
1177 SCPrint(TRUE
, stdout
, CFSTR("%s proxy cannot be %s\n"),
1178 currentProxy
->proxy
,
1179 enabled
? "enabled" : "disabled");
1185 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyEnable
), CFNumberRef_1
);
1187 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyEnable
), CFNumberRef_0
);
1189 if (currentProxy
->keyProxy
!= NULL
) {
1190 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyProxy
));
1193 if (currentProxy
->keyPort
!= NULL
) {
1194 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyPort
));
1197 if (currentProxy
->keyURL
!= NULL
) {
1198 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyURL
));
1207 __proxy_enabled(CFDictionaryRef configuration
, const CFStringRef
*enableKey
)
1212 if (enableKey
== NULL
) {
1213 return TRUE
; // if proxy does not need to be enabled
1216 num
= CFDictionaryGetValue(configuration
, *enableKey
);
1217 if (!isA_CFNumber(num
) ||
1218 !CFNumberGetValue(num
, kCFNumberIntType
, &val
) ||
1220 return FALSE
; // if not enabled
1228 __doProxyHost(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1230 if (currentProxy
== NULL
) {
1231 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1235 if (currentProxy
->keyProxy
== NULL
) {
1236 SCPrint(TRUE
, stdout
, CFSTR("%s proxy host cannot be specified\n"), currentProxy
->proxy
);
1240 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1241 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1246 SCPrint(TRUE
, stdout
, CFSTR("%s proxy host not specified\n"), currentProxy
->proxy
);
1250 if (strlen(argv
[0]) > 0) {
1253 host
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1254 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyProxy
), host
);
1257 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyProxy
));
1265 __doProxyPort(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1267 if (currentProxy
== NULL
) {
1268 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1272 if (currentProxy
->keyPort
== NULL
) {
1273 SCPrint(TRUE
, stdout
, CFSTR("%s proxy port cannot be specified\n"), currentProxy
->proxy
);
1277 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1278 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1283 SCPrint(TRUE
, stdout
, CFSTR("%s proxy port not specified\n"), currentProxy
->proxy
);
1287 if (strlen(argv
[0]) > 0) {
1291 num
= _copy_number(argv
[0]);
1292 if (!isA_CFNumber(num
) ||
1293 !CFNumberGetValue(num
, kCFNumberIntType
, &port
) ||
1294 (port
< 0) || (port
> 65535)) {
1295 SCPrint(TRUE
, stdout
, CFSTR("invalid %s proxy port number\n"), currentProxy
->proxy
);
1299 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyPort
), num
);
1302 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyPort
));
1310 __doProxyURL(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1312 if (currentProxy
== NULL
) {
1313 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1317 if (currentProxy
->keyURL
== NULL
) {
1318 SCPrint(TRUE
, stdout
, CFSTR("%s proxy URL cannot be specified\n"), currentProxy
->proxy
);
1322 if (!__proxy_enabled(newConfiguration
, currentProxy
->keyEnable
)) {
1323 SCPrint(TRUE
, stdout
, CFSTR("%s proxy not enabled\n"), currentProxy
->proxy
);
1328 SCPrint(TRUE
, stdout
, CFSTR("%s proxy URL not specified\n"), currentProxy
->proxy
);
1332 if (strlen(argv
[0]) > 0) {
1335 url
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1336 CFDictionarySetValue(newConfiguration
, *(currentProxy
->keyURL
), url
);
1339 CFDictionaryRemoveValue(newConfiguration
, *(currentProxy
->keyURL
));
1347 __doProxyFTPPassive(CFStringRef key
, const char *description
, void *info
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1349 if (currentProxy
== NULL
) {
1350 SCPrint(TRUE
, stdout
, CFSTR("proxy not specified\n"));
1354 if (currentProxy
!= &proxyKeys_FTP
) {
1355 SCPrint(TRUE
, stdout
, CFSTR("passive can only be enable for FTP proxy\n"));
1364 set_protocol_proxies(int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
1368 ok
= _process_options(proxyOptions
, N_PROXY_OPTIONS
, argc
, argv
, newConfiguration
);
1373 /* -------------------- */
1378 set_protocol(int argc
, char **argv
)
1380 CFDictionaryRef configuration
;
1381 CFMutableDictionaryRef newConfiguration
= NULL
;
1383 CFStringRef protocolType
;
1385 if (net_protocol
== NULL
) {
1386 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
1391 SCPrint(TRUE
, stdout
, CFSTR("set what?\n"));
1395 configuration
= SCNetworkProtocolGetConfiguration(net_protocol
);
1396 if (configuration
== NULL
) {
1397 newConfiguration
= CFDictionaryCreateMutable(NULL
,
1399 &kCFTypeDictionaryKeyCallBacks
,
1400 &kCFTypeDictionaryValueCallBacks
);
1402 newConfiguration
= CFDictionaryCreateMutableCopy(NULL
, 0, configuration
);
1403 CFDictionaryRemoveValue(newConfiguration
, kSCResvInactive
);
1406 protocolType
= SCNetworkProtocolGetProtocolType(net_protocol
);
1407 if (CFEqual(protocolType
, kSCNetworkProtocolTypeAppleTalk
)) {
1408 ok
= set_protocol_appletalk(argc
, argv
, newConfiguration
);
1409 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeDNS
)) {
1410 ok
= set_protocol_dns(argc
, argv
, newConfiguration
);
1411 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv4
)) {
1412 ok
= set_protocol_ipv4(argc
, argv
, newConfiguration
);
1413 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv6
)) {
1414 ok
= set_protocol_ipv6(argc
, argv
, newConfiguration
);
1415 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeProxies
)) {
1416 ok
= set_protocol_proxies(argc
, argv
, newConfiguration
);
1418 SCPrint(TRUE
, stdout
, CFSTR("this protocols configuration cannot be changed\n"));
1425 if (((configuration
== NULL
) && (CFDictionaryGetCount(newConfiguration
) > 0)) ||
1426 ((configuration
!= NULL
) && !CFEqual(configuration
, newConfiguration
))) {
1427 if (!SCNetworkProtocolSetConfiguration(net_protocol
, newConfiguration
)) {
1428 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
1437 if (newConfiguration
!= NULL
) CFRelease(newConfiguration
);
1442 /* -------------------- */
1447 show_protocol(int argc
, char **argv
)
1449 CFDictionaryRef configuration
;
1450 SCNetworkProtocolRef protocol
= NULL
;
1451 CFStringRef protocolType
;
1454 protocol
= _find_protocol(argv
[0]);
1456 if (net_protocol
!= NULL
) {
1457 protocol
= net_protocol
;
1459 SCPrint(TRUE
, stdout
, CFSTR("protocol not selected\n"));
1464 if (protocol
== NULL
) {
1468 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1469 SCPrint(TRUE
, stdout
, CFSTR("protocol type = %@\n"), protocolType
);
1471 configuration
= SCNetworkProtocolGetConfiguration(protocol
);
1472 if (configuration
!= NULL
) {
1473 SCPrint(TRUE
, stdout
, CFSTR("\n protocol configuration\n"));
1474 _show_entity(configuration
, CFSTR(""));
1478 SCPrint(TRUE
, stdout
, CFSTR("\n%@\n"), protocol
);
1485 /* -------------------- */
1490 show_protocols(int argc
, char **argv
)
1495 if (prefs
== NULL
) {
1496 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
1500 if (net_service
== NULL
) {
1501 SCPrint(TRUE
, stdout
, CFSTR("service not selected\n"));
1505 if (protocols
!= NULL
) CFRelease(protocols
);
1506 protocols
= SCNetworkServiceCopyProtocols(net_service
);
1507 if (protocols
== NULL
) {
1508 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
1512 n
= CFArrayGetCount(protocols
);
1514 CFMutableArrayRef sorted
;
1516 sorted
= CFArrayCreateMutableCopy(NULL
, 0, protocols
);
1517 CFArraySortValues(sorted
,
1521 CFRelease(protocols
);
1525 for (i
= 0; i
< n
; i
++) {
1526 SCNetworkProtocolRef protocol
;
1527 CFStringRef protocolType
;
1529 protocol
= CFArrayGetValueAtIndex(protocols
, i
);
1530 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1532 SCPrint(TRUE
, stdout
, CFSTR("%c%2d: %@%*s :"),
1533 ((net_protocol
!= NULL
) && CFEqual(protocol
, net_protocol
)) ? '>' : ' ',
1536 sizeof("AppleTalk") - CFStringGetLength(protocolType
) - 1,
1539 if (SCNetworkProtocolGetEnabled(protocol
)) {
1540 CFStringRef description
;
1542 description
= _protocol_description(protocol
, FALSE
);
1543 SCPrint(TRUE
, stdout
, CFSTR(" %@"), description
);
1544 CFRelease(description
);
1546 SCPrint(TRUE
, stdout
, CFSTR(" *DISABLED*"));
1548 SCPrint(TRUE
, stdout
, CFSTR("\n"));
1555 /* -------------------- */
1560 _protocol_description(SCNetworkProtocolRef protocol
, Boolean skipEmpty
)
1562 CFDictionaryRef configuration
;
1563 CFMutableStringRef description
= NULL
;
1564 CFStringRef protocolType
;
1566 description
= CFStringCreateMutable(NULL
, 0);
1568 if (!SCNetworkProtocolGetEnabled(protocol
)) {
1572 configuration
= SCNetworkProtocolGetConfiguration(protocol
);
1573 if (configuration
== NULL
) {
1577 protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
1578 if (CFEqual(protocolType
, kSCNetworkProtocolTypeAppleTalk
)) {
1581 method
= CFDictionaryGetValue(configuration
, kSCPropNetAppleTalkConfigMethod
);
1582 if (isA_CFString(method
)) {
1583 CFStringAppendFormat(description
,
1588 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeDNS
)) {
1593 domain
= CFDictionaryGetValue(configuration
, kSCPropNetDNSDomainName
);
1594 if (isA_CFString(domain
)) {
1595 CFStringAppendFormat(description
,
1601 search
= CFDictionaryGetValue(configuration
, kSCPropNetDNSSearchDomains
);
1602 if (isA_CFArray(search
)) {
1605 str
= CFStringCreateByCombiningStrings(NULL
, search
, CFSTR(","));
1606 CFStringAppendFormat(description
,
1608 CFSTR("%ssearch=%@"),
1609 CFStringGetLength(description
) > 0 ? ", " : "",
1614 servers
= CFDictionaryGetValue(configuration
, kSCPropNetDNSServerAddresses
);
1615 if (isA_CFArray(servers
)) {
1618 str
= CFStringCreateByCombiningStrings(NULL
, servers
, CFSTR(","));
1619 CFStringAppendFormat(description
,
1621 CFSTR("%sservers=%@"),
1622 CFStringGetLength(description
) > 0 ? ", " : "",
1626 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv4
)) {
1629 method
= CFDictionaryGetValue(configuration
, kSCPropNetIPv4ConfigMethod
);
1630 if (isA_CFString(method
)) {
1631 CFArrayRef addresses
;
1633 addresses
= CFDictionaryGetValue(configuration
, kSCPropNetIPv4Addresses
);
1634 if (CFEqual(method
, kSCValNetIPv4ConfigMethodINFORM
) &&
1635 isA_CFArray(addresses
)) {
1636 CFStringAppendFormat(description
,
1638 CFSTR("%@, address=%@"),
1640 CFArrayGetValueAtIndex(addresses
, 0));
1641 } else if (CFEqual(method
, kSCValNetIPv4ConfigMethodManual
) &&
1642 isA_CFArray(addresses
)) {
1643 CFStringAppendFormat(description
,
1645 CFSTR("%@, address=%@"),
1647 CFArrayGetValueAtIndex(addresses
, 0));
1649 CFStringAppendFormat(description
,
1655 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeIPv6
)) {
1658 method
= CFDictionaryGetValue(configuration
, kSCPropNetIPv6ConfigMethod
);
1659 if (isA_CFString(method
)) {
1660 CFStringAppendFormat(description
,
1665 } else if (CFEqual(protocolType
, kSCNetworkProtocolTypeProxies
)) {
1667 static proxyKeys
*keys
[] = { &proxyKeys_FTP
, &proxyKeys_Gopher
, &proxyKeys_HTTP
, &proxyKeys_HTTPS
,
1668 &proxyKeys_RTSP
, &proxyKeys_SOCKS
, &proxyKeys_PAC
, &proxyKeys_WPAD
};
1670 for (i
= 0; i
< sizeof(keys
)/sizeof(keys
[0]); i
++) {
1671 proxyKeys
*currentProxy
= keys
[i
];
1673 if (!__proxy_enabled(configuration
, currentProxy
->keyEnable
)) {
1677 if (((currentProxy
->keyProxy
!= NULL
) &&
1678 !CFDictionaryContainsKey(configuration
, *(currentProxy
->keyProxy
))) ||
1679 ((currentProxy
->keyURL
!= NULL
) &&
1680 !CFDictionaryContainsKey(configuration
, *(currentProxy
->keyURL
)))) {
1684 CFStringAppendFormat(description
,
1687 CFStringGetLength(description
) > 0 ? ", " : "",
1688 currentProxy
->proxy
);
1694 if (skipEmpty
&& CFStringGetLength(description
) == 0) {
1695 CFRelease(description
);