capt_get_packet(): check for key press only every 20ms
[iptraf-ng.git] / src / ifaces.c
blobc6d3da2382a53b2ef2eae9886837ab4a342f8270
1 /* For terms of usage/redistribution/modification see the LICENSE file */
2 /* For authors and contributors see the AUTHORS file */
4 /***
6 ifaces.c - routine that determines whether a given interface is supported
7 by IPTraf
9 ***/
11 #include "iptraf-ng-compat.h"
13 #include "error.h"
16 * Open /proc/net/dev and move file pointer past the two table header lines
17 * at the top of the file.
20 FILE *open_procnetdev(void)
22 FILE *fd;
23 char buf[161];
25 fd = fopen("/proc/net/dev", "r");
28 * Read and discard the table header lines in the file
31 if (fd != NULL) {
32 fgets(buf, 160, fd);
33 fgets(buf, 160, fd);
36 return fd;
40 * Get the next interface from /proc/net/dev.
42 int get_next_iface(FILE * fd, char *ifname, int n)
44 char buf[161];
46 strcpy(ifname, "");
48 if (!feof(fd)) {
49 strcpy(buf, "");
50 fgets(buf, 160, fd);
51 if (strcmp(buf, "") != 0) {
52 memset(ifname, 0, n);
53 strncpy(ifname, skip_whitespace(strtok(buf, ":")), n);
54 if (ifname[n - 1] != '\0')
55 strcpy(ifname, "");
56 return 1;
59 return 0;
62 int dev_up(char *iface)
64 int fd;
65 int ir;
66 struct ifreq ifr;
68 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
70 strcpy(ifr.ifr_name, iface);
71 ir = ioctl(fd, SIOCGIFFLAGS, &ifr);
73 close(fd);
75 if ((ir != 0) || (!(ifr.ifr_flags & IFF_UP)))
76 return 0;
78 return 1;
81 void err_iface_down(void)
83 write_error("Specified interface not active");
86 int dev_get_ifindex(const char *iface)
88 int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
89 if (fd == -1)
90 return fd;
92 struct ifreq ifr;
93 strcpy(ifr.ifr_name, iface);
94 int ir = ioctl(fd, SIOCGIFINDEX, &ifr);
96 /* need to preserve errno across call to close() */
97 int saved_errno = errno;
99 close(fd);
101 /* bug out if ioctl() failed */
102 if (ir != 0) {
103 errno = saved_errno;
104 return ir;
107 return ifr.ifr_ifindex;
110 int dev_get_mtu(const char *iface)
112 int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
113 if (fd == -1)
114 return fd;
116 struct ifreq ifr;
117 strcpy(ifr.ifr_name, iface);
118 int ir = ioctl(fd, SIOCGIFMTU, &ifr);
120 /* need to preserve errno across call to close() */
121 int saved_errno = errno;
123 close(fd);
125 /* bug out if ioctl() failed */
126 if (ir != 0) {
127 errno = saved_errno;
128 return ir;
131 return ifr.ifr_mtu;
134 int dev_get_flags(const char *iface)
136 int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
137 if (fd == -1)
138 return fd;
140 struct ifreq ifr;
141 strcpy(ifr.ifr_name, iface);
142 int ir = ioctl(fd, SIOCGIFFLAGS, &ifr);
144 /* need to preserve errno across call to close() */
145 int saved_errno = errno;
147 close(fd);
149 /* bug out if ioctl() failed */
150 if (ir != 0) {
151 errno = saved_errno;
152 return ir;
155 return ifr.ifr_flags;
158 int dev_set_flags(const char *iface, int flags)
160 int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
161 if (fd == -1)
162 return fd;
164 struct ifreq ifr;
165 strcpy(ifr.ifr_name, iface);
166 int ir = ioctl(fd, SIOCGIFFLAGS, &ifr);
167 if (ir == -1)
168 goto err;
170 ifr.ifr_flags |= flags;
171 ir = ioctl(fd, SIOCSIFFLAGS, &ifr);
173 int saved_errno;
174 err: /* need to preserve errno across call to close() */
175 saved_errno = errno;
177 close(fd);
179 /* bug out if ioctl() failed */
180 if (ir != 0)
181 errno = saved_errno;
183 return ir;
186 int dev_clear_flags(const char *iface, int flags)
188 int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
189 if (fd == -1)
190 return fd;
192 struct ifreq ifr;
193 strcpy(ifr.ifr_name, iface);
194 int ir = ioctl(fd, SIOCGIFFLAGS, &ifr);
195 if (ir == -1)
196 goto err;
198 ifr.ifr_flags &= ~flags;
199 ir = ioctl(fd, SIOCSIFFLAGS, &ifr);
201 int saved_errno;
202 err: /* need to preserve errno across call to close() */
203 saved_errno = errno;
205 close(fd);
207 /* bug out if ioctl() failed */
208 if (ir != 0)
209 errno = saved_errno;
211 return ir;
214 int dev_get_ifname(int ifindex, char *ifname)
216 int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
217 if (fd == -1)
218 return fd;
220 struct ifreq ifr = {
221 .ifr_ifindex = ifindex
223 int ir = ioctl(fd, SIOCGIFNAME, &ifr);
225 /* need to preserve errno across call to close() */
226 int saved_errno = errno;
228 close(fd);
230 /* bug out if ioctl() failed */
231 if (ir != 0) {
232 errno = saved_errno;
233 return ir;
236 strncpy(ifname, ifr.ifr_name, IFNAMSIZ);
237 return ir;
240 static int dev_bind_ifindex(int fd, const int ifindex)
242 struct sockaddr_ll fromaddr;
243 socklen_t addrlen = sizeof(fromaddr);
245 fromaddr.sll_family = AF_PACKET;
246 fromaddr.sll_protocol = htons(ETH_P_ALL);
247 fromaddr.sll_ifindex = ifindex;
248 return bind(fd, (struct sockaddr *) &fromaddr, addrlen);
251 int dev_bind_ifname(int fd, const char * const ifname)
253 int ifindex = 0;
255 if (ifname) {
256 int ir;
257 struct ifreq ifr;
259 strcpy(ifr.ifr_name, ifname);
260 ir = ioctl(fd, SIOCGIFINDEX, &ifr);
261 if (ir)
262 return ir;
263 ifindex = ifr.ifr_ifindex;
266 return dev_bind_ifindex(fd, ifindex);
269 int dev_promisc_flag(const char *dev_name)
271 int flags = dev_get_flags(dev_name);
272 if (flags < 0) {
273 write_error("Unable to obtain interface parameters for %s",
274 dev_name);
275 return -1;
278 if (flags & IFF_PROMISC)
279 return -1;
281 return flags;