1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/*
* wavemon - a wireless network monitoring aplication
*
* Copyright (c) 2001-2002 Jan Morgenstern <jan@jm-music.de>
*
* wavemon is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2, or (at your option) any later
* version.
*
* wavemon is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with wavemon; see the file COPYING. If not, write to the Free Software
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "iw_if.h"
/* Determine the artificial spreading of random samples (best: 1..10) */
#define WAVE_RAND_SPREAD 1
/* Fallback maximum quality level when using random samples. */
#define WAVE_RAND_QUAL_MAX 100
/*
* Obtain network device information
*/
static int if_get_flags(int skfd, const char *ifname)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(struct ifreq));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
err_sys("can not get interface flags for %s", ifname);
return ifr.ifr_flags;
}
/* Return true if @ifname is known to be up. */
bool if_is_up(const char *ifname)
{
int ret, skfd = socket(AF_INET, SOCK_DGRAM, 0);
if (skfd < 0)
err_sys("%s: can not open socket", __func__);
ret = if_get_flags(skfd, ifname) & IFF_UP;
close(skfd);
return ret;
}
/** Bring @ifname up if not already up. Return 0 if ok, < 0 on error. */
int if_set_up(const char *ifname)
{
struct ifreq ifr;
int ret, skfd = socket(AF_INET, SOCK_DGRAM, 0);
if (skfd < 0)
err_sys("%s: can not open socket", __func__);
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
ifr.ifr_flags = if_get_flags(skfd, ifname);
if (ifr.ifr_flags & IFF_UP)
return 0;
ifr.ifr_flags |= IFF_UP;
ret = ioctl(skfd, SIOCSIFFLAGS, &ifr);
close(skfd);
return ret;
}
/* Interface information */
void if_getinf(const char *ifname, struct if_info *info)
{
struct ifreq ifr;
int skfd = socket(AF_INET, SOCK_DGRAM, 0);
if (skfd < 0)
err_sys("%s: can not open socket", __func__);
memset(&ifr, 0, sizeof(struct ifreq));
memset(info, 0, sizeof(struct if_info));
info->flags = if_get_flags(skfd, ifname);
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(skfd, SIOCGIFMTU, &ifr) == 0)
info->mtu = ifr.ifr_mtu;
if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) >= 0)
info->txqlen = ifr.ifr_qlen;
/* Copy the 6 byte Ethernet address and the 4 byte struct in_addrs */
if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0)
memcpy(&info->hwaddr, &ifr.ifr_hwaddr.sa_data, 6);
if (ioctl(skfd, SIOCGIFADDR, &ifr) >= 0)
memcpy(&info->addr, &ifr.ifr_addr.sa_data[2], 4);
if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0)
memcpy(&info->netmask, &ifr.ifr_netmask.sa_data[2], 4);
if (ioctl(skfd, SIOCGIFBRDADDR, &ifr) >= 0)
memcpy(&info->bcast, &ifr.ifr_broadaddr.sa_data[2], 4);
close(skfd);
}
/**
* iw_get_interface_list - fill if_list with NULL-terminated array of WiFi
* interfaces.
* Use the safe route of checking /proc/net/dev/ for wireless interfaces:
* - SIOCGIFCONF only returns running interfaces that have an IP address;
* - /proc/net/wireless may exist, but may not list all wireless interfaces.
*/
void iw_get_interface_list(char** if_list, size_t max_entries)
{
char *p, tmp[BUFSIZ];
int nifs = 1; /* if_list[nifs-1] = NULL */
struct iwreq wrq;
FILE *fp;
int skfd = socket(AF_INET, SOCK_DGRAM, 0);
if (skfd < 0)
err_sys("%s: can not open socket", __func__);
fp = fopen("/proc/net/dev", "r");
if (fp == NULL)
err_sys("can not open /proc/net/dev");
while (fgets(tmp, sizeof(tmp), fp)) {
if ((p = strchr(tmp, ':'))) {
for (*p = '\0', p = tmp; isspace(*p); )
p++;
/*
* Use SIOCGIWNAME as indicator: if interface does not
* support this ioctl, it has no wireless extensions.
*/
snprintf(wrq.ifr_name, IFNAMSIZ, "%s", p);
if (ioctl(skfd, SIOCGIWNAME, &wrq) < 0)
continue;
if(nifs >= max_entries) break;
if_list[nifs-1] = strdup(p);
if_list[nifs++] = NULL;
}
}
close(skfd);
fclose(fp);
}
void if_getstat(const char *ifname, struct if_stat *stat)
{
char line[0x100];
unsigned long long d;
char *lp;
size_t l = strlen(ifname);
const char path[] = "/proc/net/dev";
FILE *fp = fopen(path, "r");
if (fp == NULL)
err_sys("can not open %s", path);
/*
* Inter-| Receive | Transmit
* face |bytes packets errs drop fifo frame compressed multicast|bytes packets
*/
while (fgets(line, sizeof(line), fp)) {
lp = line + strspn(line, " ");
if (!strncmp(lp, ifname, l) && lp[l] == ':') {
lp += l + 1;
lp += strspn(lp, " ");
sscanf(lp, "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
&stat->rx_bytes, &stat->rx_packets, &d, &d, &d, &d, &d, &d,
&stat->tx_bytes, &stat->tx_packets);
}
}
fclose(fp);
}
/**
* iw_dyn_info_get - populate dynamic information
* @info: information to populate
* @ifname: interface name
* @if: range information to use (number of encryption keys)
*/
void dyn_info_get(struct iw_dyn_info *info,
const char *ifname, struct iw_range *ir)
{
struct iwreq iwr;
int i, skfd = socket(AF_INET, SOCK_DGRAM, 0);
if (skfd < 0)
err_sys("%s: can not open socket", __func__);
memset(info, 0, sizeof(struct iw_dyn_info));
strncpy(iwr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(skfd, SIOCGIWNAME, &iwr) < 0)
err_sys("can not open device '%s'", ifname);
strncpy(info->name, iwr.u.name, IFNAMSIZ);
iwr.u.essid.pointer = (caddr_t) info->essid;
iwr.u.essid.length = sizeof(info->essid);
iwr.u.essid.flags = 0;
if (ioctl(skfd, SIOCGIWESSID, &iwr) >= 0) {
info->cap_essid = 1;
/* Convert potential ESSID index to count > 0 */
info->essid_ct = iwr.u.essid.flags & IW_ENCODE_INDEX ? : 1;
info->essid[iwr.u.essid.length] = '\0';
}
iwr.u.essid.pointer = (caddr_t) info->nickname;
iwr.u.essid.length = sizeof(info->nickname);
iwr.u.essid.flags = 0;
if (ioctl(skfd, SIOCGIWNICKN, &iwr) >= 0 &&
iwr.u.data.length > 1)
info->cap_nickname = 1;
if (ioctl(skfd, SIOCGIWFREQ, &iwr) >= 0) {
info->cap_freq = 1;
info->freq = freq_to_hz(&iwr.u.freq);
}
if (ioctl(skfd, SIOCGIWSENS, &iwr) >= 0) {
info->cap_sens = 1;
info->sens = iwr.u.sens.value;
}
if (ioctl(skfd, SIOCGIWRATE, &iwr) >= 0)
info->bitrate = iwr.u.bitrate.value;
if (ioctl(skfd, SIOCGIWTXPOW, &iwr) >= 0) {
info->cap_txpower = 1;
memcpy(&info->txpower, &iwr.u.txpower, sizeof(info->txpower));
}
if (ioctl(skfd, SIOCGIWPOWER, &iwr) >= 0) {
info->cap_power = 1;
memcpy(&info->power, &iwr.u.power, sizeof(info->power));
}
if (ioctl(skfd, SIOCGIWRETRY, &iwr) >= 0) {
info->cap_retry = 1;
memcpy(&info->retry, &iwr.u.retry, sizeof(info->retry));
}
if (ioctl(skfd, SIOCGIWRTS, &iwr) >= 0) {
info->cap_rts = 1;
memcpy(&info->rts, &iwr.u.rts, sizeof(info->rts));
}
if (ioctl(skfd, SIOCGIWFRAG, &iwr) >= 0) {
info->cap_frag = 1;
memcpy(&info->frag, &iwr.u.frag, sizeof(info->frag));
}
if (ioctl(skfd, SIOCGIWMODE, &iwr) >= 0) {
info->cap_mode = 1;
info->mode = iwr.u.mode;
}
info->nkeys = ir->max_encoding_tokens;
if (info->nkeys) {
info->keys = calloc(info->nkeys, sizeof(*info->keys));
if (info->keys == NULL)
err_sys("malloc(key array)");
/* Get index of default key first */
iwr.u.data.pointer = info->keys[0].key;
iwr.u.data.length = sizeof(info->keys[0].key);
iwr.u.data.flags = 0;
if (ioctl(skfd, SIOCGIWENCODE, &iwr) < 0) {
free(info->keys);
info->keys = NULL;
info->nkeys = 0;
} else {
info->active_key = iwr.u.data.flags & IW_ENCODE_INDEX;
}
}
/* If successful, populate the key array */
for (i = 0; i < info->nkeys; i++) {
iwr.u.data.pointer = info->keys[i].key;
iwr.u.data.length = sizeof(info->keys->key);
iwr.u.data.flags = i + 1; /* counts 1..n instead of 0..n-1 */
if (ioctl(skfd, SIOCGIWENCODE, &iwr) < 0) {
free(info->keys);
info->nkeys = 0;
break;
}
info->keys[i].size = iwr.u.data.length;
info->keys[i].flags = iwr.u.data.flags;
/* Validate whether the current key is indeed active */
if (i + 1 == info->active_key && (info->keys[i].size == 0 ||
(info->keys[i].flags & IW_ENCODE_DISABLED)))
info->active_key = 0;
}
if (ioctl(skfd, SIOCGIWAP, &iwr) >= 0) {
info->cap_ap = 1;
memcpy(&info->ap_addr, &iwr.u.ap_addr, sizeof(struct sockaddr));
}
close(skfd);
}
void dyn_info_cleanup(struct iw_dyn_info *info)
{
if (info)
free(info->keys);
}
/*
* Request range information for a given wireless interface.
* @ifname: name of the wireless argument
* @range: storage location to populate with range information.
*/
void iw_getinf_range(const char *ifname, struct iw_range *range)
{
struct iwreq iwr;
int skfd = socket(AF_INET, SOCK_DGRAM, 0);
if (skfd < 0)
err_sys("%s: can not open socket", __func__);
memset(range, 0, sizeof(struct iw_range));
strncpy(iwr.ifr_name, ifname, IFNAMSIZ);
iwr.u.data.pointer = (caddr_t) range;
iwr.u.data.length = sizeof(struct iw_range);
iwr.u.data.flags = 0;
if (ioctl(skfd, SIOCGIWRANGE, &iwr) < 0)
err_sys("can not get range information");
close(skfd);
}
|