]>
git.saurik.com Git - apple/network_cmds.git/blob - ip6conf.tproj/ip6tool.c
1 /* Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
3 * @APPLE_LICENSE_HEADER_START@
5 * "Portions Copyright (c) 2002 Apple Computer, Inc. All Rights
6 * Reserved. This file contains Original Code and/or Modifications of
7 * Original Code as defined in and that are subject to the Apple Public
8 * Source License Version 1.0 (the 'License'). You may not use this file
9 * except in compliance with the License. Please obtain a copy of the
10 * License at http://www.apple.com/publicsource and read it before using
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 OR NON-INFRINGEMENT. Please see the
18 * License for the specific language governing rights and limitations
21 * @APPLE_LICENSE_HEADER_END@
28 #include <sys/socket.h>
29 /* this is needed to get SIOCPROTOATTACH/SIOCPROTODETACH */
30 #define KERNEL_PRIVATE
31 #include <sys/ioctl.h>
34 #include <sys/types.h>
39 #define IPv6_STARTUP 1
40 #define IPv6_SHUTDOWN 2
41 #define IPv6_STARTUP_ALL 3
42 #define IPv6_SHUTDOWN_ALL 4
44 const char *if_exceptions
[] = {"lo0", "gif0", "faith0", "stf0"};
49 int do_protoattach(int s
, char *name
);
50 int do_protodetach(int s
, char *name
);
51 int do_protoattach_all(int s
);
52 int do_protodetach_all(int s
);
56 main(int argc
, char **argv
)
62 char *interface
= NULL
;
64 if ((ch
= getopt(argc
, argv
, "u:d:ax")) != -1) {
67 /* option -u: start up proto */
68 option
= IPv6_STARTUP
;
72 /* option -d: shut down proto */
73 option
= IPv6_SHUTDOWN
;
77 /* option -a: start up proto on all interfaces */
78 option
= IPv6_STARTUP_ALL
;
81 /* option -x: shut down proto on all interfaces */
82 option
= IPv6_SHUTDOWN_ALL
;
94 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
96 printf("%s: Error %d creating socket.\n", argv
[0], err
);
102 err
= do_protoattach(s
, interface
);
104 printf("%s: Error %d encountered attaching to interface %s.\n", argv
[0], err
, interface
);
108 err
= do_protodetach(s
, interface
);
110 printf("%s: Error %d encountered detaching to interface %s.\n", argv
[0], err
, interface
);
113 case IPv6_STARTUP_ALL
:
114 err
= do_protoattach_all(s
);
116 printf("%s: Error %d encountered attaching to interfaces.\n", argv
[0], err
);
119 case IPv6_SHUTDOWN_ALL
:
120 err
= do_protodetach_all(s
);
122 printf("%s: Error %d encountered detaching to interfaces.\n", argv
[0], err
);
138 Start up IPv6 on ALL interfaces: -a\n\
139 Shut down IPv6 on ALL interfaces: -x\n\
140 Start up IPv6 on given interface: -u [interface]\n\
141 Shut down IPv6 on given interface: -d [interface].\n");
145 do_protoattach(int s
, char *name
)
149 bzero(&ifr
, sizeof(ifr
));
150 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
151 return (ioctl(s
, SIOCPROTOATTACH
, &ifr
));
155 do_protodetach(int s
, char *name
)
159 bzero(&ifr
, sizeof(ifr
));
160 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
161 return (ioctl(s
, SIOCPROTODETACH
, &ifr
));
165 do_protoattach_all(int s
)
167 struct ifaddrs
*ifaddrs
, *ifa
;
170 if ((err
= getifaddrs(&ifaddrs
)) < 0)
171 return err
; /* getifaddrs properly sets errno */
173 for (ifa
= ifaddrs
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
174 /* skip over invalid interfaces */
175 if ((strcmp(ifa
->ifa_name
, if_exceptions
[0])))
176 if (strcmp(ifa
->ifa_name
, if_exceptions
[1]))
177 if (strcmp(ifa
->ifa_name
, if_exceptions
[2]))
178 if (strcmp(ifa
->ifa_name
, if_exceptions
[3])) {
179 /* this is a valid interface */
180 err
= do_protoattach(s
, ifa
->ifa_name
);
184 while (ifa
->ifa_next
!= NULL
&&
185 !(strcmp(ifa
->ifa_name
, ifa
->ifa_next
->ifa_name
))) {
186 /* skip multiple entries for same interface */
192 freeifaddrs(ifaddrs
);
198 do_protodetach_all(int s
)
200 struct ifaddrs
*ifaddrs
, *ifa
;
203 if ((err
= getifaddrs(&ifaddrs
)) < 0)
204 return err
; /* getifaddrs properly sets errno */
206 for (ifa
= ifaddrs
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
207 /* skip over invalid interfaces */
208 if ((strcmp(ifa
->ifa_name
, if_exceptions
[0])))
209 if (strcmp(ifa
->ifa_name
, if_exceptions
[1]))
210 if (strcmp(ifa
->ifa_name
, if_exceptions
[2]))
211 if (strcmp(ifa
->ifa_name
, if_exceptions
[3])) {
212 /* this is a valid interface */
213 err
= do_protodetach(s
, ifa
->ifa_name
);
217 while (ifa
->ifa_next
!= NULL
&&
218 !(strcmp(ifa
->ifa_name
, ifa
->ifa_next
->ifa_name
))) {
219 /* skip multiple entries for same interface */
225 freeifaddrs(ifaddrs
);