Remove mentions of Travis CI in docs and config
[netsniff-ng-new.git] / link.c
blob74cc75997ced1998ac5f6655bc35dfb8fe968cd1
1 #include <sys/types.h>
2 #include <sys/ioctl.h>
3 #include <linux/sockios.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <errno.h>
8 #include "link.h"
9 #include "sock.h"
10 #include "str.h"
12 uint32_t wireless_bitrate(const char *ifname)
14 int sock, ret;
15 uint32_t rate_in_mbit;
16 struct iwreq iwr;
18 sock = af_socket(AF_INET);
20 memset(&iwr, 0, sizeof(iwr));
21 strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
23 ret = ioctl(sock, SIOCGIWRATE, &iwr);
24 if (!ret)
25 rate_in_mbit = iwr.u.bitrate.value / 1000000;
26 else
27 rate_in_mbit = 0;
29 close(sock);
31 return rate_in_mbit;
34 int wireless_sigqual(const char *ifname, struct iw_statistics *stats)
36 int ret, sock;
37 struct iwreq iwr;
39 sock = af_socket(AF_INET);
41 memset(&iwr, 0, sizeof(iwr));
42 strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
44 iwr.u.data.pointer = (caddr_t) stats;
45 iwr.u.data.length = sizeof(*stats);
46 iwr.u.data.flags = 1;
48 ret = ioctl(sock, SIOCGIWSTATS, &iwr);
50 close(sock);
52 return ret;
55 int wireless_rangemax_sigqual(const char *ifname)
57 int ret, sock, sigqual;
58 struct iwreq iwr;
59 struct iw_range iwrange;
61 sock = af_socket(AF_INET);
63 memset(&iwrange, 0, sizeof(iwrange));
65 memset(&iwr, 0, sizeof(iwr));
66 strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
68 iwr.u.data.pointer = (caddr_t) &iwrange;
69 iwr.u.data.length = sizeof(iwrange);
70 iwr.u.data.flags = 0;
72 ret = ioctl(sock, SIOCGIWRANGE, &iwr);
73 if (!ret)
74 sigqual = iwrange.max_qual.qual;
75 else
76 sigqual = 0;
78 close(sock);
80 return sigqual;
83 uint32_t ethtool_bitrate(const char *ifname)
85 int ret, sock;
86 uint32_t bitrate;
87 struct ifreq ifr;
88 struct ethtool_cmd ecmd;
90 sock = af_socket(AF_INET);
92 memset(&ecmd, 0, sizeof(ecmd));
94 memset(&ifr, 0, sizeof(ifr));
95 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
97 ecmd.cmd = ETHTOOL_GSET;
98 ifr.ifr_data = (char *) &ecmd;
100 ret = ioctl(sock, SIOCETHTOOL, &ifr);
101 if (ret) {
102 bitrate = 0;
103 goto out;
106 bitrate = ethtool_cmd_speed(&ecmd);
107 if (bitrate == SPEED_UNKNOWN)
108 bitrate = 0;
109 out:
110 close(sock);
112 return bitrate;
115 int ethtool_link(const char *ifname)
117 int ret, sock;
118 struct ifreq ifr;
119 struct ethtool_value ecmd;
121 sock = af_socket(AF_INET);
123 memset(&ecmd, 0, sizeof(ecmd));
125 memset(&ifr, 0, sizeof(ifr));
126 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
128 ecmd.cmd = ETHTOOL_GLINK;
129 ifr.ifr_data = (char *) &ecmd;
131 ret = ioctl(sock, SIOCETHTOOL, &ifr);
132 if (ret)
133 ret = -EINVAL;
134 else
135 ret = !!ecmd.data;
137 close(sock);
138 return ret;
141 int ethtool_drvinf(const char *ifname, struct ethtool_drvinfo *drvinf)
143 int ret, sock;
144 struct ifreq ifr;
146 sock = af_socket(AF_INET);
148 memset(drvinf, 0, sizeof(*drvinf));
150 memset(&ifr, 0, sizeof(ifr));
151 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
153 drvinf->cmd = ETHTOOL_GDRVINFO;
154 ifr.ifr_data = (char *) drvinf;
156 ret = ioctl(sock, SIOCETHTOOL, &ifr);
158 close(sock);
160 return ret;