build: announcement: also add v prefix to checkout command
[netsniff-ng.git] / link.c
blobeb3ba189c7734a7576741c03d0d9b3a39cdc4662
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 bitrate = ethtool_cmd_speed(&ecmd);
103 if (bitrate == SPEED_UNKNOWN)
104 bitrate = 0;
105 out:
106 close(sock);
108 return bitrate;
111 int ethtool_link(const char *ifname)
113 int ret, sock;
114 struct ifreq ifr;
115 struct ethtool_value ecmd;
117 sock = af_socket(AF_INET);
119 memset(&ecmd, 0, sizeof(ecmd));
121 memset(&ifr, 0, sizeof(ifr));
122 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
124 ecmd.cmd = ETHTOOL_GLINK;
125 ifr.ifr_data = (char *) &ecmd;
127 ret = ioctl(sock, SIOCETHTOOL, &ifr);
128 if (ret)
129 ret = -EINVAL;
130 else
131 ret = !!ecmd.data;
133 close(sock);
134 return ret;
137 int ethtool_drvinf(const char *ifname, struct ethtool_drvinfo *drvinf)
139 int ret, sock;
140 struct ifreq ifr;
142 sock = af_socket(AF_INET);
144 memset(drvinf, 0, sizeof(*drvinf));
146 memset(&ifr, 0, sizeof(ifr));
147 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
149 drvinf->cmd = ETHTOOL_GDRVINFO;
150 ifr.ifr_data = (char *) drvinf;
152 ret = ioctl(sock, SIOCETHTOOL, &ifr);
154 close(sock);
156 return ret;