]>
Commit | Line | Data |
---|---|---|
a74aeac6 PR |
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 | ||
30 | #include <sys/ioctl.h> | |
31 | #include <sys/socket.h> | |
32 | #include <sys/time.h> | |
33 | #include <sys/errno.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 | ifr[i].ifr_flags &= ~(IFF_UP); | |
81 | if (ioctl(fd, SIOCSIFFLAGS, &ifr[i]) < 0) { | |
82 | fprintf(stderr, "ifdown: shutdown "); | |
83 | perror(ifr[i].ifr_name); | |
84 | } | |
85 | } | |
86 | } | |
87 | close(fd); | |
88 | ||
89 | return 0; | |
90 | } | |
91 |