-
Notifications
You must be signed in to change notification settings - Fork 2
/
gkrellm-wifi-linux.c
417 lines (343 loc) · 10.6 KB
/
gkrellm-wifi-linux.c
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* gkrellm-wifi - A wireless link monitor plug-in for GKrellM2
*
* Copyright (C) 2003 Henrik Brix Andersen <[email protected]>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "gkrellm-wifi.h"
#include <errno.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/wireless.h>
#include "gkrellm-wifi-monitor.h"
#include "gkrellm-wifi-linux.h"
#define MAX_LINE_LENGTH 128
#define PROC_NET_WIRELESS "/proc/net/wireless"
#define DEFAULT_QUALITY_MAX 96
#define DEFAULT_BITRATE 0
/* the following is needed for backwards compatibility with older
wireless extension versions - compat code inspired by the code in
iwlib.c from wireless_tools.27 copyright (C) Jean Tourrilhes
<[email protected]> */
#define MAGIC_10_LENGTH 300
#define IW_MAX_FREQUENCIES_15 16
#define IW_MAX_BITRATES_15 8
#define IW_MAX_ENCODING_SIZES_15 8
#define IW_MAX_TXPOWER_15 8
#define IW_MAX_BITRATES_16 32
#define IW_MAX_ENCODING_SIZES_16 8
#define IW_MAX_TXPOWER_16 8
#define IW_MAX_FREQUENCIES_16 32
struct iw_freq_15
{
__s32 m;
__s16 e;
__u8 i;
__u8 pad;
};
struct iw_quality_15
{
__u8 qual;
__u8 level;
__u8 noise;
__u8 updated;
};
struct iw_range_15
{
__u32 throughput;
__u32 min_nwid;
__u32 max_nwid;
__u16 num_channels;
__u8 num_frequency;
struct iw_freq_15 freq[IW_MAX_FREQUENCIES_15];
__s32 sensitivity;
struct iw_quality_15 max_qual;
__u8 num_bitrates;
__s32 bitrate[IW_MAX_BITRATES_15];
__s32 min_rts;
__s32 max_rts;
__s32 min_frag;
__s32 max_frag;
__s32 min_pmp;
__s32 max_pmp;
__s32 min_pmt;
__s32 max_pmt;
__u16 pmp_flags;
__u16 pmt_flags;
__u16 pm_capa;
__u16 encoding_size[IW_MAX_ENCODING_SIZES_15];
__u8 num_encoding_sizes;
__u8 max_encoding_tokens;
__u16 txpower_capa;
__u8 num_txpower;
__s32 txpower[IW_MAX_TXPOWER_15];
__u8 we_version_compiled;
__u8 we_version_source;
__u16 retry_capa;
__u16 retry_flags;
__u16 r_time_flags;
__s32 min_retry;
__s32 max_retry;
__s32 min_r_time;
__s32 max_r_time;
struct iw_quality_15 avg_qual;
};
struct iw_quality_16
{
__u8 qual;
__u8 level;
__u8 noise;
__u8 updated;
};
struct iw_freq_16
{
__s32 m;
__s16 e;
__u8 i;
__u8 pad;
};
struct iw_range_16
{
__u32 throughput;
__u32 min_nwid;
__u32 max_nwid;
__u16 old_num_channels;
__u8 old_num_frequency;
__s32 old_freq[6];
__s32 sensitivity;
struct iw_quality_16 max_qual;
struct iw_quality_16 avg_qual;
__u8 num_bitrates;
__s32 bitrate[IW_MAX_BITRATES_16];
__s32 min_rts;
__s32 max_rts;
__s32 min_frag;
__s32 max_frag;
__s32 min_pmp;
__s32 max_pmp;
__s32 min_pmt;
__s32 max_pmt;
__u16 pmp_flags;
__u16 pmt_flags;
__u16 pm_capa;
__u16 encoding_size[IW_MAX_ENCODING_SIZES_16];
__u8 num_encoding_sizes;
__u8 max_encoding_tokens;
__u8 encoding_login_index;
__u16 txpower_capa;
__u8 num_txpower;
__s32 txpower[IW_MAX_TXPOWER_16];
__u8 we_version_compiled;
__u8 we_version_source;
__u16 retry_capa;
__u16 retry_flags;
__u16 r_time_flags;
__s32 min_retry;
__s32 max_retry;
__s32 min_r_time;
__s32 max_r_time;
__u16 num_channels;
__u8 num_frequency;
struct iw_freq_16 freq[IW_MAX_FREQUENCIES_16];
};
/* end of compatibility cruft */
/* prototypes */
static guint8 get_quality_max (const gchar *interface);
static gint32 get_bitrate (const gchar *interface);
static gchar * get_essid (const gchar *interface);
/* public functions */
void
gkrellm_wifi_wireless_info_read (void)
{
GkrellmWifiMonitor *wifimon;
FILE *file;
gchar line[MAX_LINE_LENGTH];
gint lineno = 0;
gint items;
gchar interface[MAX_LINE_LENGTH];
gint quality;
gint signal;
gint noise;
static gboolean warn = TRUE;
file = fopen (PROC_NET_WIRELESS, "r");
if (file != NULL)
{
while (fgets (line, sizeof (line), file))
{
if (++lineno < 3)
continue;
items = sscanf (line, " %[^: ] : %*x %d%*[. ] %d%*[. ] %d%*[. ] %*d %*d %*d %*d %*d %*d\n",
interface, &quality, &signal, &noise);
if (items == 4)
{
wifimon = gkrellm_wifi_monitor_find (interface);
if (! wifimon)
{
wifimon = gkrellm_wifi_monitor_create (interface);
wifimon->enabled = TRUE;
}
wifimon->quality = quality;
wifimon->quality_max = get_quality_max (wifimon->interface);
wifimon->signal = signal - 0x100;
wifimon->noise = noise - 0x100;
wifimon->bitrate = get_bitrate (wifimon->interface);
if (wifimon->essid)
g_free (wifimon->essid);
wifimon->essid = get_essid (wifimon->interface);
if (wifimon->quality > wifimon->quality_max)
{
/* quality is in dBm */
wifimon->percent = rint (100 * (wifimon->quality / wifimon->quality_max));
}
else
{
/* quality is relative */
wifimon->percent = rint (100 * (log (wifimon->quality) / log (wifimon->quality_max)));
}
wifimon->percent = CLAMP (wifimon->percent, 0, 100);
wifimon->updated = TRUE;
}
else
{
g_message (_("Parse error in %s line %d, skipping line..."),
PROC_NET_WIRELESS, lineno);
continue;
}
}
fclose (file);
}
else if (warn)
{
g_warning (_("Could not open %s for reading, no wireless extensions found..."),
PROC_NET_WIRELESS);
warn = FALSE;
}
}
/* private functions */
static guint8
get_quality_max (const gchar *interface)
{
struct iw_range range;
struct iwreq request;
gchar buffer[sizeof (range) * 2] = { 0 };
gint fd;
guint8 ret;
gint offset;
g_assert (interface != NULL);
fd = socket (AF_INET, SOCK_DGRAM, 0);
if (fd >= 0)
{
request.u.data.pointer = (caddr_t) &buffer;
request.u.data.length = sizeof (buffer);
request.u.data.flags = 0;
strncpy (request.ifr_name, interface, IFNAMSIZ);
if (ioctl (fd, SIOCGIWRANGE, &request) >= 0)
{
memcpy (&range, buffer, sizeof (range));
if (request.u.data.length < MAGIC_10_LENGTH)
range.we_version_compiled = 10;
if (range.we_version_compiled <= 15)
{
offset = G_STRUCT_OFFSET (struct iw_range_15, max_qual) +
G_STRUCT_OFFSET (struct iw_quality_15, qual);
}
else
{
offset = G_STRUCT_OFFSET (struct iw_range_16, max_qual) +
G_STRUCT_OFFSET (struct iw_quality_16, qual);
}
memcpy (&ret, buffer + offset, sizeof (ret));
}
else
{
g_message (_("Could not get range for %s: %s"),
interface, g_strerror (errno));
ret = DEFAULT_QUALITY_MAX;
}
close (fd);
}
else
{
g_warning (_("Could not open socket: %s"),
g_strerror (errno));
ret = DEFAULT_QUALITY_MAX;
}
return ret;
}
static gint32
get_bitrate (const gchar *interface)
{
struct iwreq request;
gint fd;
gint ret;
g_assert (interface != NULL);
fd = socket (AF_INET, SOCK_DGRAM, 0);
if (fd >= 0)
{
strncpy (request.ifr_name, interface, IFNAMSIZ);
if (ioctl (fd, SIOCGIWRATE, &request) >= 0)
ret = request.u.bitrate.value;
else
ret = DEFAULT_BITRATE;
close (fd);
}
else
{
g_warning (_("Could not open socket: %s"),
g_strerror (errno));
ret = DEFAULT_BITRATE;
}
return ret;
}
static gchar *
get_essid (const gchar *interface)
{
struct iwreq request;
gchar buffer[IW_ESSID_MAX_SIZE + 1] = { 0 };
gint fd;
gchar *ret;
g_assert (interface != NULL);
fd = socket (AF_INET, SOCK_DGRAM, 0);
if (fd >= 0)
{
request.u.essid.pointer = (caddr_t) &buffer;
request.u.essid.length = sizeof (buffer);
request.u.essid.flags = 0;
strncpy (request.ifr_name, interface, IFNAMSIZ);
if (ioctl (fd, SIOCGIWESSID, &request) >= 0)
{
if (request.u.data.flags)
ret = g_strdup (buffer);
else
ret = g_strdup (_("off/any"));
}
else
{
ret = g_strdup (_("n/a"));
}
close (fd);
}
else
{
g_warning (_("Could not open socket: %s"),
g_strerror (errno));
ret = g_strdup (_("n/a"));
}
return ret;
}