5 #include <sys/socket.h>
6 #include <linux/if_arp.h>
17 int __device_ifindex(const char *ifname
)
22 if (!strncmp("any", ifname
, strlen("any")))
25 sock
= af_socket(AF_INET
);
27 memset(&ifr
, 0, sizeof(ifr
));
28 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
30 ret
= ioctl(sock
, SIOCGIFINDEX
, &ifr
);
34 index
= ifr
.ifr_ifindex
;
41 int device_ifindex(const char *ifname
)
43 int index
= __device_ifindex(ifname
);
45 if (unlikely(index
< 0))
46 panic("Cannot get ifindex from device!\n");
51 int device_type(const char *ifname
)
56 if (!strncmp("any", ifname
, strlen("any")))
59 sock
= af_socket(AF_INET
);
61 memset(&ifr
, 0, sizeof(ifr
));
62 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
64 ret
= ioctl(sock
, SIOCGIFHWADDR
, &ifr
);
66 panic("Cannot get iftype from device!\n");
69 type
= ifr
.ifr_hwaddr
.sa_family
;
75 static int __device_address6(const char *ifname
, struct sockaddr_storage
*ss
)
77 int ret
, family
, found
= -EINVAL
;
78 struct ifaddrs
*ifaddr
, *ifa
;
80 ret
= getifaddrs(&ifaddr
);
81 if (unlikely(ret
< 0))
82 panic("Cannot get device addresses for IPv6!\n");
84 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
85 family
= ifa
->ifa_addr
->sa_family
;
86 if (family
!= AF_INET6
)
88 if (strcmp(ifa
->ifa_name
, ifname
))
91 memcpy(ss
, ifa
->ifa_addr
, sizeof(*ss
));
100 int device_address(const char *ifname
, int af
, struct sockaddr_storage
*ss
)
107 if (!strncmp("any", ifname
, strlen("any")))
110 return __device_address6(ifname
, ss
);
112 sock
= af_socket(af
);
114 memset(&ifr
, 0, sizeof(ifr
));
115 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
117 ifr
.ifr_addr
.sa_family
= af
;
119 ret
= ioctl(sock
, SIOCGIFADDR
, &ifr
);
121 memcpy(ss
, &ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
127 size_t device_mtu(const char *ifname
)
133 sock
= af_socket(AF_INET
);
135 memset(&ifr
, 0, sizeof(ifr
));
136 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
138 ret
= ioctl(sock
, SIOCGIFMTU
, &ifr
);
146 short device_get_flags(const char *ifname
)
152 sock
= af_socket(AF_INET
);
154 memset(&ifr
, 0, sizeof(ifr
));
155 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
157 ret
= ioctl(sock
, SIOCGIFFLAGS
, &ifr
);
159 flags
= ifr
.ifr_flags
;
165 void device_set_flags(const char *ifname
, const short flags
)
170 sock
= af_socket(AF_INET
);
172 memset(&ifr
, 0, sizeof(ifr
));
173 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
174 ifr
.ifr_flags
= flags
;
176 ret
= ioctl(sock
, SIOCSIFFLAGS
, &ifr
);
177 if (unlikely(ret
< 0))
178 panic("Cannot set NIC flags (%s)!\n", strerror(errno
));
183 int device_up_and_running(const char *ifname
)
187 if (!strncmp("any", ifname
, strlen("any")))
190 return (device_get_flags(ifname
) &
191 (IFF_UP
| IFF_RUNNING
)) ==
192 (IFF_UP
| IFF_RUNNING
);
195 u32
device_bitrate(const char *ifname
)
197 u32 scopper
, swireless
;
199 scopper
= ethtool_bitrate(ifname
);
200 swireless
= wireless_bitrate(ifname
);
202 return scopper
? : swireless
;
205 short device_enter_promiscuous_mode(const char *ifname
)
209 if (!strncmp("any", ifname
, strlen("any")))
212 ifflags
= device_get_flags(ifname
);
213 device_set_flags(ifname
, ifflags
| IFF_PROMISC
);
218 void device_leave_promiscuous_mode(const char *ifname
, short oldflags
)
220 if (!strncmp("any", ifname
, strlen("any")))
223 device_set_flags(ifname
, oldflags
);
226 const char *device_type2str(uint16_t type
)
243 case ARPHRD_APPLETLK
:
249 case ARPHRD_METRICOM
:
251 case ARPHRD_IEEE1394
:
253 case ARPHRD_INFINIBAND
:
293 case ARPHRD_LOOPBACK
:
295 case ARPHRD_LOCALTLK
:
323 case ARPHRD_FCFABRIC
:
325 case ARPHRD_FCFABRIC
+ 1:
327 case ARPHRD_FCFABRIC
+ 2:
329 case ARPHRD_FCFABRIC
+ 3:
331 case ARPHRD_FCFABRIC
+ 4:
333 case ARPHRD_FCFABRIC
+ 5:
335 case ARPHRD_FCFABRIC
+ 6:
337 case ARPHRD_FCFABRIC
+ 7:
339 case ARPHRD_FCFABRIC
+ 8:
341 case ARPHRD_FCFABRIC
+ 9:
343 case ARPHRD_FCFABRIC
+ 10:
345 case ARPHRD_FCFABRIC
+ 11:
347 case ARPHRD_FCFABRIC
+ 12:
349 case ARPHRD_IEEE802_TR
:
351 case ARPHRD_IEEE80211
:
353 case ARPHRD_IEEE80211_PRISM
:
354 return "ieee80211_prism";
355 case ARPHRD_IEEE80211_RADIOTAP
:
356 return "ieee80211_radiotap";
357 case ARPHRD_IEEE802154
:
361 case ARPHRD_PHONET_PIPE
:
362 return "phonet_pipe";
378 /* Taken from iproute2 ll_addr_n2a func */
379 const char *device_addr2str(const unsigned char *addr
, int alen
, int type
,
385 (type
== ARPHRD_TUNNEL
|| type
== ARPHRD_SIT
||
386 type
== ARPHRD_IPGRE
))
387 return inet_ntop(AF_INET
, addr
, buf
, blen
);
389 if (alen
== 16 && type
== ARPHRD_TUNNEL6
)
390 return inet_ntop(AF_INET6
, addr
, buf
, blen
);
392 for (l
= 0, i
= 0; i
< alen
; i
++) {
394 snprintf(buf
+ l
, blen
, "%02x", addr
[i
]);
398 snprintf(buf
+ l
, blen
, ":%02x", addr
[i
]);