AUTHORS: Add Hisao Tanabe for his contribution
[netsniff-ng-new.git] / link.c
blob56b839b456538db9e9ca209231d55a67d1fe84d5
1 #include <sys/types.h>
2 #include <sys/ioctl.h>
3 #include <linux/sockios.h>
4 #include <unistd.h>
5 #include <errno.h>
7 #include "link.h"
8 #include "sock.h"
9 #include "str.h"
11 u32 wireless_bitrate(const char *ifname)
13 int sock, ret, rate_in_mbit;
14 struct iwreq iwr;
16 sock = af_socket(AF_INET);
18 memset(&iwr, 0, sizeof(iwr));
19 strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
21 ret = ioctl(sock, SIOCGIWRATE, &iwr);
22 if (!ret)
23 rate_in_mbit = iwr.u.bitrate.value / 1000000;
24 else
25 rate_in_mbit = 0;
27 close(sock);
29 return rate_in_mbit;
32 int wireless_sigqual(const char *ifname, struct iw_statistics *stats)
34 int ret, sock;
35 struct iwreq iwr;
37 sock = af_socket(AF_INET);
39 memset(&iwr, 0, sizeof(iwr));
40 strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
42 iwr.u.data.pointer = (caddr_t) stats;
43 iwr.u.data.length = sizeof(*stats);
44 iwr.u.data.flags = 1;
46 ret = ioctl(sock, SIOCGIWSTATS, &iwr);
48 close(sock);
50 return ret;
53 int wireless_rangemax_sigqual(const char *ifname)
55 int ret, sock, sigqual;
56 struct iwreq iwr;
57 struct iw_range iwrange;
59 sock = af_socket(AF_INET);
61 memset(&iwrange, 0, sizeof(iwrange));
63 memset(&iwr, 0, sizeof(iwr));
64 strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
66 iwr.u.data.pointer = (caddr_t) &iwrange;
67 iwr.u.data.length = sizeof(iwrange);
68 iwr.u.data.flags = 0;
70 ret = ioctl(sock, SIOCGIWRANGE, &iwr);
71 if (!ret)
72 sigqual = iwrange.max_qual.qual;
73 else
74 sigqual = 0;
76 close(sock);
78 return sigqual;
81 u32 ethtool_bitrate(const char *ifname)
83 int ret, sock, bitrate;
84 struct ifreq ifr;
85 struct ethtool_cmd ecmd;
87 sock = af_socket(AF_INET);
89 memset(&ecmd, 0, sizeof(ecmd));
91 memset(&ifr, 0, sizeof(ifr));
92 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
94 ecmd.cmd = ETHTOOL_GSET;
95 ifr.ifr_data = (char *) &ecmd;
97 ret = ioctl(sock, SIOCETHTOOL, &ifr);
98 if (ret) {
99 bitrate = 0;
100 goto out;
103 bitrate = ethtool_cmd_speed(&ecmd);
104 if (bitrate == SPEED_UNKNOWN)
105 bitrate = 0;
106 out:
107 close(sock);
109 return bitrate;
112 int ethtool_link(const char *ifname)
114 int ret, sock;
115 struct ifreq ifr;
116 struct ethtool_value ecmd;
118 sock = af_socket(AF_INET);
120 memset(&ecmd, 0, sizeof(ecmd));
122 memset(&ifr, 0, sizeof(ifr));
123 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
125 ecmd.cmd = ETHTOOL_GLINK;
126 ifr.ifr_data = (char *) &ecmd;
128 ret = ioctl(sock, SIOCETHTOOL, &ifr);
129 if (ret)
130 ret = -EINVAL;
131 else
132 ret = !!ecmd.data;
134 close(sock);
135 return ret;
138 int ethtool_drvinf(const char *ifname, struct ethtool_drvinfo *drvinf)
140 int ret, sock;
141 struct ifreq ifr;
143 sock = af_socket(AF_INET);
145 memset(drvinf, 0, sizeof(*drvinf));
147 memset(&ifr, 0, sizeof(ifr));
148 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
150 drvinf->cmd = ETHTOOL_GDRVINFO;
151 ifr.ifr_data = (char *) drvinf;
153 ret = ioctl(sock, SIOCETHTOOL, &ifr);
155 close(sock);
157 return ret;