bpf: add missing decode for poff
[netsniff-ng.git] / link.c
blob95f081620d4f417ad8328bc848281e3dd76f5ee0
1 #include <sys/ioctl.h>
2 #include <linux/sockios.h>
3 #include <unistd.h>
4 #include <errno.h>
6 #include "link.h"
7 #include "sock.h"
8 #include "str.h"
10 u32 wireless_bitrate(const char *ifname)
12 int sock, ret, rate_in_mbit;
13 struct iwreq iwr;
15 sock = af_socket(AF_INET);
17 memset(&iwr, 0, sizeof(iwr));
18 strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
20 ret = ioctl(sock, SIOCGIWRATE, &iwr);
21 if (!ret)
22 rate_in_mbit = iwr.u.bitrate.value / 1000000;
23 else
24 rate_in_mbit = 0;
26 close(sock);
28 return rate_in_mbit;
31 int wireless_sigqual(const char *ifname, struct iw_statistics *stats)
33 int ret, sock;
34 struct iwreq iwr;
36 sock = af_socket(AF_INET);
38 memset(&iwr, 0, sizeof(iwr));
39 strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
41 iwr.u.data.pointer = (caddr_t) stats;
42 iwr.u.data.length = sizeof(*stats);
43 iwr.u.data.flags = 1;
45 ret = ioctl(sock, SIOCGIWSTATS, &iwr);
47 close(sock);
49 return ret;
52 int wireless_rangemax_sigqual(const char *ifname)
54 int ret, sock, sigqual;
55 struct iwreq iwr;
56 struct iw_range iwrange;
58 sock = af_socket(AF_INET);
60 memset(&iwrange, 0, sizeof(iwrange));
62 memset(&iwr, 0, sizeof(iwr));
63 strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
65 iwr.u.data.pointer = (caddr_t) &iwrange;
66 iwr.u.data.length = sizeof(iwrange);
67 iwr.u.data.flags = 0;
69 ret = ioctl(sock, SIOCGIWRANGE, &iwr);
70 if (!ret)
71 sigqual = iwrange.max_qual.qual;
72 else
73 sigqual = 0;
75 close(sock);
77 return sigqual;
80 u32 ethtool_bitrate(const char *ifname)
82 int ret, sock, bitrate;
83 struct ifreq ifr;
84 struct ethtool_cmd ecmd;
86 sock = af_socket(AF_INET);
88 memset(&ecmd, 0, sizeof(ecmd));
90 memset(&ifr, 0, sizeof(ifr));
91 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
93 ecmd.cmd = ETHTOOL_GSET;
94 ifr.ifr_data = (char *) &ecmd;
96 ret = ioctl(sock, SIOCETHTOOL, &ifr);
97 if (ret) {
98 bitrate = 0;
99 goto out;
102 switch (ecmd.speed) {
103 case SPEED_10:
104 case SPEED_100:
105 case SPEED_1000:
106 case SPEED_2500:
107 case SPEED_10000:
108 bitrate = ecmd.speed;
109 break;
110 default:
111 bitrate = 0;
112 break;
114 out:
115 close(sock);
117 return bitrate;
120 int ethtool_link(const char *ifname)
122 int ret, sock;
123 struct ifreq ifr;
124 struct ethtool_value ecmd;
126 sock = af_socket(AF_INET);
128 memset(&ecmd, 0, sizeof(ecmd));
130 memset(&ifr, 0, sizeof(ifr));
131 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
133 ecmd.cmd = ETHTOOL_GLINK;
134 ifr.ifr_data = (char *) &ecmd;
136 ret = ioctl(sock, SIOCETHTOOL, &ifr);
137 if (ret)
138 ret = -EINVAL;
139 else
140 ret = !!ecmd.data;
142 close(sock);
143 return ret;
146 int ethtool_drvinf(const char *ifname, struct ethtool_drvinfo *drvinf)
148 int ret, sock;
149 struct ifreq ifr;
151 sock = af_socket(AF_INET);
153 memset(drvinf, 0, sizeof(*drvinf));
155 memset(&ifr, 0, sizeof(ifr));
156 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
158 drvinf->cmd = ETHTOOL_GDRVINFO;
159 ifr.ifr_data = (char *) drvinf;
161 ret = ioctl(sock, SIOCETHTOOL, &ifr);
163 close(sock);
165 return ret;