13 int device_ifindex(const char *ifname
)
18 if (!strncmp("any", ifname
, strlen("any")))
21 sock
= af_socket(AF_INET
);
23 memset(&ifr
, 0, sizeof(ifr
));
24 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
26 ret
= ioctl(sock
, SIOCGIFINDEX
, &ifr
);
28 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
);
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
));
90 int device_mtu(const char *ifname
)
95 sock
= af_socket(AF_INET
);
97 memset(&ifr
, 0, sizeof(ifr
));
98 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
100 ret
= ioctl(sock
, SIOCGIFMTU
, &ifr
);
111 short device_get_flags(const char *ifname
)
117 sock
= af_socket(AF_INET
);
119 memset(&ifr
, 0, sizeof(ifr
));
120 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
122 ret
= ioctl(sock
, SIOCGIFFLAGS
, &ifr
);
124 flags
= ifr
.ifr_flags
;
133 void device_set_flags(const char *ifname
, const short flags
)
138 sock
= af_socket(AF_INET
);
140 memset(&ifr
, 0, sizeof(ifr
));
141 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
143 ifr
.ifr_flags
= flags
;
145 ret
= ioctl(sock
, SIOCSIFFLAGS
, &ifr
);
147 panic("Cannot set NIC flags!\n");
152 int device_up_and_running(char *ifname
)
156 if (!strncmp("any", ifname
, strlen("any")))
159 return (device_get_flags(ifname
) & (IFF_UP
| IFF_RUNNING
)) == (IFF_UP
| IFF_RUNNING
);
162 u32
device_bitrate(const char *ifname
)
164 u32 speed_c
, speed_w
;
166 speed_c
= ethtool_bitrate(ifname
);
167 speed_w
= wireless_bitrate(ifname
);
169 return (speed_c
== 0 ? speed_w
: speed_c
);