aboutsummaryrefslogtreecommitdiffstats
path: root/plugin_wireless.c
blob: f5d340dc82a8096af85daaaf2f075e2a25eb1c48 (plain)
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/* $Id: plugin_wireless.c,v 1.1 2004/04/07 08:29:05 hejl Exp $
 *
 * Wireless Extension plugin
 *
 * Copyright 2004 Xavier Vello <xavier66@free.fr>
 * Copyright 2004 Martin Hejl <martin@hejl.de> 
 * Copyright 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net> 
 *
 * Losts of code borrowed from Wireless Tools, which is
 *         Copyright (c) 1997-2002 Jean Tourrilhes <jt@hpl.hp.com>
 * (avaible at http://web.hpl.hp.com/personal/Jean_Tourrilhes/Linux/)
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux 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.
 *
 * LCD4Linux 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 * $Log: plugin_wireless.c,v $
 * Revision 1.1  2004/04/07 08:29:05  hejl
 * New plugin for wireless info
 *
 */
 
 /*
  * Exported functions:
  
  wifi_level
  wifi_noise
  wifi_quality
  wifi_protocol
  wifi_frequency
  wifi_bitrate
  wifi_essid
  wifi_op_mode
  wifi_sensitivity
  wifi_sec_mode
  
  All Functions take one parameter 
  (the name of the device, like "wlan0", "ath0" and so on)  
  
  */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

#include <sys/ioctl.h>
#include <net/if_arp.h>
#include <linux/wireless.h>
#include <math.h>

#include "debug.h"
#include "plugin.h"
#include "cfg.h"
#include "hash.h"
#include "qprintf.h"


#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif

#define HASH_TTL 100

//#define KILO  1e3
//#define MEGA  1e6
//#define GIGA  1e9


#define KEY_LEVEL "level"
#define KEY_QUALITY "quality"
#define KEY_NOISE "noise"
#define KEY_PROTO "proto"
#define KEY_FREQUENCY "frequency"
#define KEY_BIT_RATE "bit_rate"
#define KEY_ESSID "essid"
#define KEY_OP_MODE "op_mode"
#define KEY_SENS "sens"
#define KEY_SEC_MODE "sec_mode"

#define FREQ2FLOAT(m,e) (((double) m) * pow(10,e))
#define MWATT2DBM(in) ((int) (ceil(10.0 * log10((double) in))))


static HASH wireless = { 0, };
static int sock=-2;

static char *operation_mode[] = { 
          "Auto",
          "Ad-Hoc",
          "Managed",
          "Master",
          "Repeater",
          "Secondary",
          "Monitor",
          "n/a"
        };


static void ioctl_error(int line) {
	error("IOCTL call to wireless extensions in line %d returned error", line);	
}

int do_ioctl(int  sock,    /* Socket to the kernel */
     char *    ifname,     /* Device name */
     int      request,     /* WE ID */
     struct iwreq *  pwrq) /* Fixed part of the request */
{
  /* Set device name */
  strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);

  /* Do the request */
  return(ioctl(sock, request, pwrq));
}

int get_range_info(  int    sock,
          char *  ifname,
          struct iw_range *range)
{
  struct iwreq    req;
  char        buffer[sizeof(struct iw_range) * 2];  /* Large enough */

  if (sock<=0) {
    return(-1);
  }

  /* Cleanup */
  memset(buffer, 0, sizeof(buffer));

  req.u.data.pointer = (caddr_t) buffer;
  req.u.data.length = sizeof(buffer);
  req.u.data.flags = 0;
  if(do_ioctl(sock, ifname, SIOCGIWRANGE, &req) < 0)
  return(-1);

  /* Copy stuff at the right place, ignore extra */
  memcpy((char *) range, buffer, sizeof(struct iw_range));

  return(0);
}



static int get_ifname(struct iwreq *preq, char *dev) {
  /* do not cache this call !!! */
  struct iwreq req;
  char key_buffer[32];
  
  if (sock<=0) {
    return(-1);
  }
  
  qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev,KEY_PROTO);
  
  if (!preq) preq=&req;

  if(do_ioctl(sock, dev, SIOCGIWNAME, preq)) {
    debug("%s isn't a wirelesss interface !", dev);
    return -1;
  }

  hash_set(&wireless,key_buffer, preq->u.name);
  return(0);

}

static int get_frequency(char* dev,char* key) {    
  /* Get frequency / channel */
  struct iwreq  req; 
  char qprintf_buffer[1024];
  char key_buffer[32];
  double freq;
  int age;

  qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev,key);
  age=hash_age(&wireless, key, NULL);
  
  /* reread every HASH_TTL msec only */
  if (age>0 && age<=HASH_TTL) {
    return(0);
  }
  
  if (get_ifname(&req,dev)!=0) {
    return(-1);
  }
  
  req.u.data.length = 0;
  req.u.data.flags = 1;    /* Clear updated flag */

  if(do_ioctl(sock, dev, SIOCGIWFREQ, &req) < 0) {
    ioctl_error(__LINE__);
    return -1;
  }
  
  freq = FREQ2FLOAT(req.u.freq.m, req.u.freq.e);

  /*if( freq>= GIGA)
    snprintf(qprintf_buffer,sizeof(qprintf_buffer), "%g GHz", freq / GIGA);
  else
    if(freq>= MEGA)
      snprintf(qprintf_buffer,sizeof(qprintf_buffer), "%g MHz", freq / MEGA);
    else
      snprintf(qprintf_buffer,sizeof(qprintf_buffer), "%g kHz", freq / KILO);
*/
  snprintf(qprintf_buffer,sizeof(qprintf_buffer), "%g", freq);

  hash_set(&wireless, key_buffer, qprintf_buffer);
  return(0);
  
}

static int get_essid(char* dev,char* key) {    
  /* Get ESSID */
  struct iwreq  req; 
  char key_buffer[32];
  char essid_buffer[IW_ESSID_MAX_SIZE+1];
  int age;

  
  qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev,key);
  age=hash_age(&wireless, key, NULL);
  
  /* reread every HASH_TTL msec only */
  if (age>0 && age<=HASH_TTL) {
    return(0);
  }
  
  if (get_ifname(&req,dev)!=0) {
    return(-1);
  }
  
  memset(essid_buffer, 0, sizeof(essid_buffer));
  req.u.essid.pointer = (caddr_t) essid_buffer;
  req.u.essid.length = IW_ESSID_MAX_SIZE + 1;
  req.u.essid.flags = 0;

  if(do_ioctl(sock, dev,SIOCGIWESSID, &req) < 0) {
    ioctl_error(__LINE__);
    return -1;
  }

  hash_set(&wireless,key_buffer, essid_buffer);  
  return(0);
  
}

static int get_op_mode(char* dev,char* key) {    
  /* Get operation mode */
  struct iwreq  req; 
  char key_buffer[32];
  int age;

  qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev,key);
  age=hash_age(&wireless, key, NULL);
  
  /* reread every HASH_TTL msec only */
  if (age>0 && age<=HASH_TTL) {
    return(0);
  }
  
  if (get_ifname(&req,dev)!=0) {
    return(-1);
  }
  
  if(do_ioctl(sock, dev,SIOCGIWMODE, &req) < 0) {
    ioctl_error(__LINE__);
    return -1;
  }
      
  if((req.u.mode > 6) || (req.u.mode < 0)) {
    req.u.mode=7; /* mode not available */
  }      

  hash_set(&wireless,key_buffer, operation_mode[req.u.mode]);  
  return(0);
      
}

static int get_bitrate(char* dev,char* key) {    
  /* Get bit rate */
  struct iwreq  req; 
  char key_buffer[32];
  char bitrate_buffer[64];
  int age;
  double bitrate=0;

  qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev,key);
  age=hash_age(&wireless, key, NULL);
  
  /* reread every HASH_TTL msec only */
  if (age>0 && age<=HASH_TTL) {
    return(0);
  }
  
  if (get_ifname(&req,dev)!=0) {
    return(-1);
  }
  
  if(do_ioctl(sock, dev,SIOCGIWRATE, &req) < 0) {
    ioctl_error(__LINE__);
    return -1;
  }

    bitrate_buffer[0]='\0';
    bitrate = (double)(req.u.bitrate.value);
/*  if( bitrate>= GIGA)
    snprintf(bitrate_buffer,sizeof(bitrate_buffer), "%gGb/s", bitrate / GIGA);
  else
    if(bitrate >= MEGA)
      snprintf(bitrate_buffer,sizeof(bitrate_buffer), "%gMb/s", bitrate / MEGA);
    else
      snprintf(bitrate_buffer,sizeof(bitrate_buffer), "%gkb/s", bitrate / KILO);
*/  
  snprintf(bitrate_buffer,sizeof(bitrate_buffer), "%g", bitrate);
  
  hash_set(&wireless,key_buffer, bitrate_buffer);   
  
  return(0);
}
    
static int get_sens(char* dev,char* key) {
  /* Get sensitivity */
  struct iwreq  req; 
  struct iw_range range;
  char key_buffer[32];
  char buffer[64];
  int age;
  int has_sens =0;
  int has_range =0;

  qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev,key);
  age=hash_age(&wireless, key, NULL);
  
  /* reread every HASH_TTL msec only */
  if (age>0 && age<=HASH_TTL) {
    return(0);
  }
  
  if (get_ifname(&req,dev)!=0) {
    return(-1);
  }
  
  if(do_ioctl(sock, dev, SIOCGIWSENS, &req) >= 0)
  {
    has_sens = 1;
  }

  if(get_range_info(sock, dev, &range) < 0) {
    memset(&range, 0, sizeof(range));
  } else {
    has_range=1;
  }

  if(has_range) {
    if(req.u.sens.value < 0) {
      qprintf(buffer, sizeof(buffer), "%d dBm", req.u.sens.value);
    }
    else
    {
      qprintf(buffer, sizeof(buffer), "%d/%d", req.u.sens.value, range.sensitivity);
    }
  } else {
    qprintf(buffer, sizeof(buffer), "%d", req.u.sens.value);
  }

  hash_set(&wireless,key_buffer, buffer);
    return(0);
}
    

static int get_sec_mode(char* dev,char* key) {
  // Get encryption information 
  struct iwreq  req; 
  char key_buffer[32];
  char encrypt_key[IW_ENCODING_TOKEN_MAX+1];
  int age;
  int has_key=0;
  int key_flags=0;
  int key_size=0;

  qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev,key);
  age=hash_age(&wireless, key, NULL);
  
  /* reread every HASH_TTL msec only */
  if (age>0 && age<=HASH_TTL) {
    return(0);
  }
  
  if (get_ifname(&req,dev)!=0) {
    return(-1);
  }

  req.u.data.pointer = (caddr_t) &encrypt_key;
  req.u.data.length = IW_ENCODING_TOKEN_MAX;
  req.u.data.flags = 0;  

  if(do_ioctl(sock, dev, SIOCGIWENCODE, &req) >= 0)
  {
    has_key = 1;
    key_flags = req.u.data.flags;
    key_size = req.u.data.length;
  } else {
    return(-1);
  }
  
  // Display encryption information 
  //if(has_key && (key_flags & IW_ENCODE_INDEX) > 1)
  //  printf(" [%d]", info->key_flags & IW_ENCODE_INDEX);

  if(has_key && (key_flags & IW_ENCODE_RESTRICTED))
    hash_set(&wireless,key_buffer, "restricted");
  else if(has_key && (key_flags & IW_ENCODE_OPEN))
    hash_set(&wireless,key_buffer, "open");  

    return(0);
}

static int get_stats(char *dev, char *key)
{
  struct iw_statistics stats;
  struct iwreq  req; 
  char qprintf_buffer[1024];
  char key_buffer[32];
  int age;
  struct iw_range range;

  qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev,key);
  age=hash_age(&wireless, key, NULL);
  
  /* reread every HASH_TTL msec only */
  if (age>0 && age<=HASH_TTL) {
    return(0);
  }
  
  if (get_ifname(&req,dev)!=0) {
    return(-1);
  }
  
  req.u.data.pointer = (caddr_t) &stats;
  req.u.data.length = 0;
  req.u.data.flags = 1;    /* Clear updated flag */

  if(do_ioctl(sock, dev,SIOCGIWSTATS, &req) < 0) {
    ioctl_error(__LINE__);
    return -1;
  }
  
  if(get_range_info(sock, dev, &range) < 0)
    memset(&range, 0, sizeof(range));
    
  if(stats.qual.level > range.max_qual.level)  {
    qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d", stats.qual.level-0x100);
    qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev, KEY_LEVEL);
    hash_set(&wireless,key_buffer, qprintf_buffer);

    qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d", stats.qual.noise-0x100);
    qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev, KEY_NOISE);
    hash_set(&wireless,key_buffer, qprintf_buffer);

    qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d/%d", stats.qual.qual,range.max_qual.qual);
    qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev, KEY_QUALITY);
    hash_set(&wireless,key_buffer, qprintf_buffer);
  } else {

    qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d/%d", stats.qual.level, range.max_qual.level);
    qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev, KEY_LEVEL);;
    hash_set(&wireless,key_buffer, qprintf_buffer);

    qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d/%d", stats.qual.noise,range.max_qual.noise);
    qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev, KEY_NOISE);
    hash_set(&wireless,key_buffer, qprintf_buffer);

    qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d/%d", stats.qual.qual, range.max_qual.qual);
    qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev, KEY_QUALITY);
    hash_set(&wireless,key_buffer, qprintf_buffer);    
  }

  return 0; 
}

static int check_socket() {
  
  // already handled in a previous run
  if (sock==-3) return(-1);
  
  // socket not initialized
  if (sock==-2) sock = socket(AF_INET, SOCK_DGRAM, 0);  
  
  // error initilalizing socket
  if (sock<=0) {
    error("Error opening socket for reading wireless stats");
    sock=-3;
    return(-1);    
  }
  
  return(0);
  
}

static void save_result(RESULT *result, char* dev, char* key, int res) {
  char key_buffer[64];
  qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev,key);
  char* val=NULL;
  
  if (res<0) {
    SetResult(&result, R_STRING, ""); 
    return;
  }
  
  val=hash_get(&wireless,key_buffer);

  if (val) {
    SetResult(&result, R_STRING, val);     
  } else {
    SetResult(&result, R_STRING, ""); 
  }
  
  
}

/* 
  functions exported to the evaluator 
*/

static void wireless_quality(RESULT *result, RESULT *arg1) {
  char *dev=R2S(arg1);
  if (check_socket() != 0) return;
    
  save_result(result,dev,KEY_QUALITY,get_stats(dev, KEY_QUALITY));
}

static void wireless_level(RESULT *result, RESULT *arg1) {
  char *dev=R2S(arg1);
  if (check_socket() != 0) return;
    
  save_result(result,dev,KEY_LEVEL,get_stats(dev, KEY_LEVEL));
}


static void wireless_noise(RESULT *result, RESULT *arg1) {
  char *dev=R2S(arg1);
  if (check_socket() != 0) return;
    
  save_result(result,dev,KEY_NOISE,get_stats(dev, KEY_NOISE));
}


static void wireless_protocol(RESULT *result, RESULT *arg1) {
  char *dev=R2S(arg1);
  if (check_socket() != 0) return;
    
  save_result(result,dev,KEY_PROTO,get_ifname(NULL, dev));
}

static void wireless_frequency(RESULT *result, RESULT *arg1) {
  char *dev=R2S(arg1);
  if (check_socket() != 0) return;
    
  save_result(result,dev,KEY_FREQUENCY,get_frequency(dev, KEY_FREQUENCY));
}

static void wireless_bitrate(RESULT *result, RESULT *arg1) {
  char *dev=R2S(arg1);
  if (check_socket() != 0) return;
    
  save_result(result,dev,KEY_BIT_RATE,get_bitrate(dev, KEY_BIT_RATE));
}

static void wireless_essid(RESULT *result, RESULT *arg1) {
  char *dev=R2S(arg1);
  if (check_socket() != 0) return;
    
  save_result(result,dev,KEY_ESSID,get_essid(dev, KEY_ESSID));
}

static void wireless_op_mode(RESULT *result, RESULT *arg1) {
  char *dev=R2S(arg1);
  if (check_socket() != 0) return;
    
  save_result(result,dev,KEY_OP_MODE,get_op_mode(dev, KEY_OP_MODE));
}

static void wireless_sensitivity(RESULT *result, RESULT *arg1) {
  char *dev=R2S(arg1);
  if (check_socket() != 0) return;
    
  save_result(result,dev,KEY_SENS,get_sens(dev, KEY_SENS));
}

static void wireless_sec_mode(RESULT *result, RESULT *arg1) {
  char *dev=R2S(arg1);
  if (check_socket() != 0) return;  
    
  save_result(result,dev,KEY_SEC_MODE,get_sec_mode(dev, KEY_SEC_MODE));
}

/*
init and cleanup
*/

int plugin_init_wireless(void)
{
  AddFunction ("wifi_level", 1, wireless_level);
  AddFunction ("wifi_noise", 1, wireless_noise);
  AddFunction ("wifi_quality", 1, wireless_quality);
  AddFunction ("wifi_protocol", 1, wireless_protocol);
  AddFunction ("wifi_frequency", 1, wireless_frequency);
  AddFunction ("wifi_bitrate", 1, wireless_bitrate);
  AddFunction ("wifi_essid", 1, wireless_essid);
  AddFunction ("wifi_op_mode", 1, wireless_op_mode);
  AddFunction ("wifi_sensitivity", 1, wireless_sensitivity);
  AddFunction ("wifi_sec_mode", 1, wireless_sec_mode);
  
  return 0;
}

void plugin_exit_wireless(void) 
{
  if (sock>0) close(sock);
  hash_destroy(&wireless);
}