14 int device_ifindex(const char *ifname
)
19 if (!strncmp("any", ifname
, strlen("any")))
22 sock
= af_socket(AF_INET
);
24 memset(&ifr
, 0, sizeof(ifr
));
25 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
27 ret
= ioctl(sock
, SIOCGIFINDEX
, &ifr
);
29 panic("Cannot get ifindex from device!\n");
31 index
= ifr
.ifr_ifindex
;
37 static int __device_address6(const char *ifname
, struct sockaddr_storage
*ss
)
39 int ret
, family
, found
= -EINVAL
;
40 struct ifaddrs
*ifaddr
, *ifa
;
42 ret
= getifaddrs(&ifaddr
);
43 if (unlikely(ret
< 0))
44 panic("Cannot get device addresses for IPv6!\n");
46 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
47 family
= ifa
->ifa_addr
->sa_family
;
48 if (family
!= AF_INET6
)
50 if (strcmp(ifa
->ifa_name
, ifname
))
53 memcpy(ss
, ifa
->ifa_addr
, sizeof(*ss
));
62 int device_address(const char *ifname
, int af
, struct sockaddr_storage
*ss
)
69 if (!strncmp("any", ifname
, strlen("any")))
72 return __device_address6(ifname
, ss
);
76 memset(&ifr
, 0, sizeof(ifr
));
77 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
79 ifr
.ifr_addr
.sa_family
= af
;
81 ret
= ioctl(sock
, SIOCGIFADDR
, &ifr
);
83 memcpy(ss
, &ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
89 int device_mtu(const char *ifname
)
91 int ret
, sock
, mtu
= 0;
94 sock
= af_socket(AF_INET
);
96 memset(&ifr
, 0, sizeof(ifr
));
97 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
99 ret
= ioctl(sock
, SIOCGIFMTU
, &ifr
);
107 short device_get_flags(const char *ifname
)
113 sock
= af_socket(AF_INET
);
115 memset(&ifr
, 0, sizeof(ifr
));
116 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
118 ret
= ioctl(sock
, SIOCGIFFLAGS
, &ifr
);
120 flags
= ifr
.ifr_flags
;
126 void device_set_flags(const char *ifname
, const short flags
)
131 sock
= af_socket(AF_INET
);
133 memset(&ifr
, 0, sizeof(ifr
));
134 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
135 ifr
.ifr_flags
= flags
;
137 ret
= ioctl(sock
, SIOCSIFFLAGS
, &ifr
);
139 panic("Cannot set NIC flags!\n");
144 int device_up_and_running(char *ifname
)
148 if (!strncmp("any", ifname
, strlen("any")))
151 return (device_get_flags(ifname
) &
152 (IFF_UP
| IFF_RUNNING
)) ==
153 (IFF_UP
| IFF_RUNNING
);
156 u32
device_bitrate(const char *ifname
)
158 u32 scopper
, swireless
;
160 scopper
= ethtool_bitrate(ifname
);
161 swireless
= wireless_bitrate(ifname
);
163 return scopper
? : swireless
;