Adjust included headers to be compatible with the musl C library. Patch from Matias...
[sysvinit.git] / src / ifdown.c
1 /*
2 * ifdown.c Find all network interfaces on the system and
3 * shut them down.
4 *
5 * Copyright (C) 1998 Miquel van Smoorenburg.
6 *
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.
11 *
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.
16 *
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
20 *
21 */
22 char *v_ifdown = "@(#)ifdown.c 1.11 02-Jun-1998 miquels@cistron.nl";
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <time.h>
28 #include <string.h>
29 #include <errno.h>
30
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <sys/time.h>
34
35 #include <net/if.h>
36 #include <netinet/in.h>
37
38 #define MAX_IFS 64
39
40 /*
41 * First, we find all shaper devices and down them. Then we
42 * down all real interfaces. This is because the comment in the
43 * shaper driver says "if you down the shaper device before the
44 * attached inerface your computer will follow".
45 */
46 int ifdown(void)
47 {
48 struct ifreq ifr[MAX_IFS];
49 struct ifconf ifc;
50 int i, fd;
51 int numif;
52 int shaper;
53
54 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
55 fprintf(stderr, "ifdown: ");
56 perror("socket");
57 return -1;
58 }
59 ifc.ifc_len = sizeof(ifr);
60 ifc.ifc_req = ifr;
61
62 if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
63 fprintf(stderr, "ifdown: ");
64 perror("SIOCGIFCONF");
65 close(fd);
66 return -1;
67 }
68 numif = ifc.ifc_len / sizeof(struct ifreq);
69
70 for (shaper = 1; shaper >= 0; shaper--) {
71 for (i = 0; i < numif; i++) {
72
73 if ((strncmp(ifr[i].ifr_name, "shaper", 6) == 0)
74 != shaper) continue;
75
76 if (strcmp(ifr[i].ifr_name, "lo") == 0)
77 continue;
78 if (strchr(ifr[i].ifr_name, ':') != NULL)
79 continue;
80
81 /* Read interface flags */
82 if (ioctl(fd, SIOCGIFFLAGS, &ifr[i]) < 0) {
83 fprintf(stderr, "ifdown: shutdown ");
84 perror(ifr[i].ifr_name);
85 continue;
86 }
87 /*
88 * Expected in <net/if.h> according to
89 * "UNIX Network Programming".
90 */
91 #ifdef ifr_flags
92 # define IRFFLAGS ifr_flags
93 #else /* Present on kFreeBSD */
94 # define IRFFLAGS ifr_flagshigh
95 #endif
96 if (ifr[i].IRFFLAGS & IFF_UP) {
97 ifr[i].IRFFLAGS &= ~(IFF_UP);
98 if (ioctl(fd, SIOCSIFFLAGS, &ifr[i]) < 0) {
99 fprintf(stderr, "ifdown: shutdown ");
100 perror(ifr[i].ifr_name);
101 }
102 }
103 #undef IRFFLAGS
104 }
105 }
106 close(fd);
107
108 return 0;
109 }