]>
git.wh0rd.org - sysvinit.git/blob - src/ifdown.c
2 * ifdown.c Find all network interfaces on the system and
5 * Copyright (C) 1998 Miquel van Smoorenburg.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 char *v_ifdown
= "@(#)ifdown.c 1.11 02-Jun-1998 miquels@cistron.nl";
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
36 #include <netinet/in.h>
40 /* XXX: Ideally this would get detected at configure time... */
41 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
42 defined(__NetBSD__) || defined(__OpenBSD__)
43 #define HAVE_SOCKADDR_SA_LEN 1
46 #ifndef _SIZEOF_ADDR_IFREQ
47 #ifdef HAVE_SOCKADDR_SA_LEN
48 #define _SIZEOF_ADDR_IFREQ(ifr) \
49 ((ifr).ifr_addr.sa_len > sizeof(struct sockaddr) ? \
50 (sizeof((ifr).ifr_name) + (ifr).ifr_addr.sa_len) : \
53 #define _SIZEOF_ADDR_IFREQ(ifr) sizeof(struct ifreq)
58 * First, we find all shaper devices and down them. Then we
59 * down all real interfaces. This is because the comment in the
60 * shaper driver says "if you down the shaper device before the
61 * attached inerface your computer will follow".
65 char ifr_buf
[sizeof(struct ifreq
) * MAX_IFS
];
71 if ((fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
72 fprintf(stderr
, "ifdown: ");
76 ifc
.ifc_len
= sizeof(ifr_buf
);
77 ifc
.ifc_buf
= ifr_buf
;
79 if (ioctl(fd
, SIOCGIFCONF
, &ifc
) < 0) {
80 fprintf(stderr
, "ifdown: ");
81 perror("SIOCGIFCONF");
85 ifr_end
= ifr_buf
+ ifc
.ifc_len
;
87 for (shaper
= 1; shaper
>= 0; shaper
--) {
88 char *ifr_next
= ifr_buf
;
90 while (ifr_next
< ifr_end
) {
94 ifr
= (struct ifreq
*)ifr_next
;
95 ifr_next
+= _SIZEOF_ADDR_IFREQ(*ifr
);
97 if ((strncmp(ifr
->ifr_name
, "shaper", 6) == 0)
100 if (strncmp(ifr
->ifr_name
, "lo", 2) == 0)
102 if (strchr(ifr
->ifr_name
, ':') != NULL
)
105 /* Read interface flags */
106 if (ioctl(fd
, SIOCGIFFLAGS
, ifr
) < 0) {
107 fprintf(stderr
, "ifdown: shutdown ");
108 perror(ifr
->ifr_name
);
112 * Expected in <net/if.h> according to
113 * "UNIX Network Programming".
116 flags
= (ifr
->ifr_flags
& 0xffff) |
117 (ifr
->ifr_flagshigh
<< 16);
119 flags
= ifr
->ifr_flags
;
121 if (flags
& IFF_UP
) {
124 ifr
->ifr_flags
= flags
& 0xffff;
125 ifr
->ifr_flagshigh
= flags
>> 16;
127 ifr
->ifr_flags
= flags
;
129 if (ioctl(fd
, SIOCSIFFLAGS
, ifr
) < 0) {
130 fprintf(stderr
, "ifdown: shutdown ");
131 perror(ifr
->ifr_name
);