1 /* For terms of usage/redistribution/modification see the LICENSE file */
2 /* For authors and contributors see the AUTHORS file */
6 ifaces.c - routine that determines whether a given interface is supported
11 #include "iptraf-ng-compat.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)
25 fd
= fopen("/proc/net/dev", "r");
28 * Read and discard the table header lines in the file
40 * Get the next interface from /proc/net/dev.
42 int get_next_iface(FILE * fd
, char *ifname
, int n
)
51 if (strcmp(buf
, "") != 0) {
53 strncpy(ifname
, skip_whitespace(strtok(buf
, ":")), n
);
54 if (ifname
[n
- 1] != '\0')
62 int dev_up(char *iface
)
68 fd
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
70 strcpy(ifr
.ifr_name
, iface
);
71 ir
= ioctl(fd
, SIOCGIFFLAGS
, &ifr
);
75 if ((ir
!= 0) || (!(ifr
.ifr_flags
& IFF_UP
)))
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
);
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
;
101 /* bug out if ioctl() failed */
107 return ifr
.ifr_ifindex
;
110 int dev_get_mtu(const char *iface
)
112 int fd
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
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
;
125 /* bug out if ioctl() failed */
134 int dev_get_flags(const char *iface
)
136 int fd
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
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
;
149 /* bug out if ioctl() failed */
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
);
165 strcpy(ifr
.ifr_name
, iface
);
166 int ir
= ioctl(fd
, SIOCGIFFLAGS
, &ifr
);
170 ifr
.ifr_flags
|= flags
;
171 ir
= ioctl(fd
, SIOCSIFFLAGS
, &ifr
);
174 err
: /* need to preserve errno across call to close() */
179 /* bug out if ioctl() failed */
186 int dev_clear_flags(const char *iface
, int flags
)
188 int fd
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
193 strcpy(ifr
.ifr_name
, iface
);
194 int ir
= ioctl(fd
, SIOCGIFFLAGS
, &ifr
);
198 ifr
.ifr_flags
&= ~flags
;
199 ir
= ioctl(fd
, SIOCSIFFLAGS
, &ifr
);
202 err
: /* need to preserve errno across call to close() */
207 /* bug out if ioctl() failed */
214 int dev_get_ifname(int ifindex
, char *ifname
)
216 int fd
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
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
;
230 /* bug out if ioctl() failed */
236 strncpy(ifname
, ifr
.ifr_name
, IFNAMSIZ
);
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
)
259 strcpy(ifr
.ifr_name
, ifname
);
260 ir
= ioctl(fd
, SIOCGIFINDEX
, &ifr
);
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
);
273 write_error("Unable to obtain interface parameters for %s",
278 if (flags
& IFF_PROMISC
)