1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
| /* Fetch the interface configuration from the kernel. */
static int if_fetch(struct interface *ife)
{
struct ifreq ifr;
char *ifname = ife->name;
int skfd;
skfd = xsocket(AF_INET, SOCK_DGRAM, 0);
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
close(skfd);
return -1;
}
ife->flags = ifr.ifr_flags;
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
memset(ife->hwaddr, 0, 32);
if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0)
memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
ife->type = ifr.ifr_hwaddr.sa_family;
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
ife->metric = 0;
if (ioctl(skfd, SIOCGIFMETRIC, &ifr) >= 0)
ife->metric = ifr.ifr_metric;
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
ife->mtu = 0;
if (ioctl(skfd, SIOCGIFMTU, &ifr) >= 0)
ife->mtu = ifr.ifr_mtu;
memset(&ife->map, 0, sizeof(struct ifmap));
#ifdef SIOCGIFMAP
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0)
ife->map = ifr.ifr_map;
#endif
#ifdef HAVE_TXQUEUELEN
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
ife->tx_queue_len = -1; /* unknown value */
if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) >= 0)
ife->tx_queue_len = ifr.ifr_qlen;
#else
ife->tx_queue_len = -1; /* unknown value */
#endif
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
ifr.ifr_addr.sa_family = AF_INET;
memset(&ife->addr, 0, sizeof(struct sockaddr));
if (ioctl(skfd, SIOCGIFADDR, &ifr) == 0) {
ife->has_ip = 1;
ife->addr = ifr.ifr_addr;
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
if (ioctl(skfd, SIOCGIFDSTADDR, &ifr) >= 0)
ife->dstaddr = ifr.ifr_dstaddr;
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
if (ioctl(skfd, SIOCGIFBRDADDR, &ifr) >= 0)
ife->broadaddr = ifr.ifr_broadaddr;
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
memset(&ife->netmask, 0, sizeof(struct sockaddr));
if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0)
ife->netmask = ifr.ifr_netmask;
}
close(skfd);
return 0;
}
|