netsniff-ng: fix --bind-cpu option in example command line
[netsniff-ng-new.git] / link.c
blob72f513f7e883590bedae64a8ca1b6e3d84d03f02
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 u32 wireless_bitrate(const char *ifname)
14 int sock, ret, rate_in_mbit;
15 struct iwreq iwr;
17 sock = af_socket(AF_INET);
19 memset(&iwr, 0, sizeof(iwr));
20 strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
22 ret = ioctl(sock, SIOCGIWRATE, &iwr);
23 if (!ret)
24 rate_in_mbit = iwr.u.bitrate.value / 1000000;
25 else
26 rate_in_mbit = 0;
28 close(sock);
30 return rate_in_mbit;
33 int wireless_sigqual(const char *ifname, struct iw_statistics *stats)
35 int ret, sock;
36 struct iwreq iwr;
38 sock = af_socket(AF_INET);
40 memset(&iwr, 0, sizeof(iwr));
41 strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
43 iwr.u.data.pointer = (caddr_t) stats;
44 iwr.u.data.length = sizeof(*stats);
45 iwr.u.data.flags = 1;
47 ret = ioctl(sock, SIOCGIWSTATS, &iwr);
49 close(sock);
51 return ret;
54 int wireless_rangemax_sigqual(const char *ifname)
56 int ret, sock, sigqual;
57 struct iwreq iwr;
58 struct iw_range iwrange;
60 sock = af_socket(AF_INET);
62 memset(&iwrange, 0, sizeof(iwrange));
64 memset(&iwr, 0, sizeof(iwr));
65 strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
67 iwr.u.data.pointer = (caddr_t) &iwrange;
68 iwr.u.data.length = sizeof(iwrange);
69 iwr.u.data.flags = 0;
71 ret = ioctl(sock, SIOCGIWRANGE, &iwr);
72 if (!ret)
73 sigqual = iwrange.max_qual.qual;
74 else
75 sigqual = 0;
77 close(sock);
79 return sigqual;
82 u32 ethtool_bitrate(const char *ifname)
84 int ret, sock, bitrate;
85 struct ifreq ifr;
86 struct ethtool_cmd ecmd;
88 sock = af_socket(AF_INET);
90 memset(&ecmd, 0, sizeof(ecmd));
92 memset(&ifr, 0, sizeof(ifr));
93 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
95 ecmd.cmd = ETHTOOL_GSET;
96 ifr.ifr_data = (char *) &ecmd;
98 ret = ioctl(sock, SIOCETHTOOL, &ifr);
99 if (ret) {
100 bitrate = 0;
101 goto out;
104 bitrate = ethtool_cmd_speed(&ecmd);
105 if (bitrate == SPEED_UNKNOWN)
106 bitrate = 0;
107 out:
108 close(sock);
110 return bitrate;
113 int ethtool_link(const char *ifname)
115 int ret, sock;
116 struct ifreq ifr;
117 struct ethtool_value ecmd;
119 sock = af_socket(AF_INET);
121 memset(&ecmd, 0, sizeof(ecmd));
123 memset(&ifr, 0, sizeof(ifr));
124 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
126 ecmd.cmd = ETHTOOL_GLINK;
127 ifr.ifr_data = (char *) &ecmd;
129 ret = ioctl(sock, SIOCETHTOOL, &ifr);
130 if (ret)
131 ret = -EINVAL;
132 else
133 ret = !!ecmd.data;
135 close(sock);
136 return ret;
139 int ethtool_drvinf(const char *ifname, struct ethtool_drvinfo *drvinf)
141 int ret, sock;
142 struct ifreq ifr;
144 sock = af_socket(AF_INET);
146 memset(drvinf, 0, sizeof(*drvinf));
148 memset(&ifr, 0, sizeof(ifr));
149 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
151 drvinf->cmd = ETHTOOL_GDRVINFO;
152 ifr.ifr_data = (char *) drvinf;
154 ret = ioctl(sock, SIOCETHTOOL, &ifr);
156 close(sock);
158 return ret;