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
|
/*
* 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 <stdio.h>
#include <pwd.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include "conf.h"
#include "error.h"
#include "errno.h"
#include "iw_if.h"
#include "llist.h"
#define CFNAME ".wavemonrc"
#define DEB(VALUE) fprintf(stderr, "checkpoint: %d\n", VALUE);
int conf_items;
char *version_str = "wavemon wireless monitor 0.4.0b by Jan Morgenstern <jan@jm-music.de>",
*license_str = "Distributed under the terms of the GNU General Public License >=v2.";
void help_exit()
{
printf("%s\n%s\n\n", version_str, license_str);
printf("Usage: wavemon [ -dhlrv ] [ -i ifname ]\n\n");
printf(" -d Dump the current device status to stdout and exit\n");
printf(" -h This help screen\n");
printf(" -i <ifname> Use specified network interface (default eth0)\n");
printf(" -l Use linear scales in favour of algorithmic ones\n");
printf(" -r Generate random levels (for testing purposes)\n");
printf(" -v Print version number and exit\n\n");
exit(0);
}
void version_exit()
{
printf("%s\n%s\n\n", version_str, license_str);
exit(0);
}
void init_conf_items(struct wavemon_conf *conf)
{
struct conf_item *item;
int if_list = iw_getif();
int s = sizeof(struct conf_item);
if (!ll_size(if_list)) fatal_error("no wireless interfaces found!");
conf_items = ll_create();
item = calloc(1, s);
item->name = strdup("Interface");
item->type = t_sep;
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->v = &conf->ifname;
item->name = strdup("Interface");
item->type = t_listval;
item->list = if_list;
item->max = 10;
item->cfname = strdup("interface");
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->v = &conf->stat_iv;
item->name = strdup("Statistics updates");
item->type = t_int;
item->min = 10; item->max = 4000;
item->inc = 10;
item->unit = strdup("ms");
item->cfname = strdup("stat_updates");
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->v = &conf->slotsize;
item->name = strdup("Histogram update cycles");
item->type = t_int;
item->min = 1; item->max = 64;
item->inc = 1;
item->cfname = strdup("lhist_slot_size");
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->v = &conf->info_iv;
item->name = strdup("Dynamic info updates");
item->type = t_int;
item->min = 1; item->max = 60;
item->inc = 1;
item->unit = strdup("s");
item->cfname = strdup("info_updates");
ll_push(conf_items, "*", item);
/* level scale items */
item = calloc(1, s);
item->type = t_sep;
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->name = strdup("Level scales");
item->type = t_sep;
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->v = &conf->override_bounds;
item->name = strdup("Override scale autodetect");
item->type = t_switch;
item->cfname = strdup("override_auto_scale");
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->v = &conf->sig_min;
item->name = strdup("Minimum signal level");
item->type = t_int;
item->min = -120; item->max = -60;
item->inc = 1;
item->unit = strdup("dBm");
item->dep = &conf->override_bounds;
item->cfname = strdup("min_signal_level");
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->v = &conf->sig_max;
item->name = strdup("Maximum signal level");
item->type = t_int;
item->min = -59; item->max = 120;
item->inc = 1;
item->unit = strdup("dBm");
item->dep = &conf->override_bounds;
item->cfname = strdup("max_signal_level");
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->v = &conf->noise_min;
item->name = strdup("Minimum noise level");
item->type = t_int;
item->min = -120; item->max = -60;
item->inc = 1;
item->unit = strdup("dBm");
item->dep = &conf->override_bounds;
item->cfname = strdup("min_noise_level");
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->v = &conf->noise_max;
item->name = strdup("Maximum noise level");
item->type = t_int;
item->min = -59; item->max = 120;
item->inc = 1;
item->unit = strdup("dBm");
item->dep = &conf->override_bounds;
item->cfname = strdup("max_noise_level");
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->v = &conf->linear;
item->name = strdup("Linear level scales");
item->type = t_switch;
item->cfname = strdup("linear_scale");
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->v = &conf->random;
item->name = strdup("Random signals");
item->type = t_switch;
item->cfname = strdup("random");
ll_push(conf_items, "*", item);
/* thresholds */
item = calloc(1, s);
item->v = &conf->lthreshold_action;
item->name = strdup("Low treshold action");
item->type = t_list;
item->list = ll_create();
ll_push(item->list, "s", "Disabled");
ll_push(item->list, "s", "Beep");
ll_push(item->list, "s", "Flash");
ll_push(item->list, "s", "Beep+Flash");
item->cfname = strdup("lo_threshold_action");
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->v = &conf->lthreshold;
item->name = strdup("Low threshold");
item->type = t_int;
item->min = -120; item->max = -60;
item->inc = 1;
item->unit = strdup("dBm");
item->dep = (char *)&conf->lthreshold_action;
item->cfname = strdup("lo_threshold");
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->v = &conf->hthreshold_action;
item->name = strdup("High treshold action");
item->type = t_list;
item->list = ll_create();
ll_push(item->list, "s", "Disabled");
ll_push(item->list, "s", "Beep");
ll_push(item->list, "s", "Flash");
ll_push(item->list, "s", "Beep+Flash");
item->cfname = strdup("hi_threshold_action");
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->v = &conf->hthreshold;
item->name = strdup("High threshold");
item->type = t_int;
item->min = -59; item->max = 120;
item->inc = 1;
item->unit = strdup("dBm");
item->dep = (char *)&conf->hthreshold_action;
item->cfname = strdup("hi_threshold");
ll_push(conf_items, "*", item);
/* start-up items */
item = calloc(1, s);
item->type = t_sep;
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->name = strdup("Startup");
item->type = t_sep;
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->v = &conf->startup_scr;
item->name = strdup("Startup screen");
item->type = t_list;
item->list = ll_create();
ll_push(item->list, "s", "Info");
ll_push(item->list, "s", "Histogram");
ll_push(item->list, "s", "Access points");
item->cfname = strdup("startup_screen");
ll_push(conf_items, "*", item);
/* functions */
item = calloc(1, s);
item->type = t_sep;
ll_push(conf_items, "*", item);
item = calloc(1, s);
item->v = write_cf;
item->name = strdup("Save configuration");
item->type = t_func;
ll_push(conf_items, "*", item);
}
void getargs(struct wavemon_conf *conf, int argc, char *argv[])
{
int arg, tmp;
while ((arg = getopt(argc, argv, "dhi:lrv")) >= 0)
switch (arg) {
case 'd': conf->dump = 1;
break;
case 'h': help_exit();
break;
case 'i': if ((tmp = ll_scan(iw_getif(), "S", optarg)) >= 0)
strncpy(conf->ifname, (char *)ll_get(iw_getif(), tmp), sizeof(conf->ifname)); else fatal_error("no wireless extensions found on device %s", optarg);
break;
case 'l': conf->linear = 1;
break;
case 'r': conf->random = 1;
break;
case 'v': version_exit();
break;
default: /* bad argument. bad bad */
exit(-1);
}
}
void read_cf(struct wavemon_conf *conf)
{
char tmp[0x100], lv[0x20], rv[0x20], *cfname;
char *lp;
FILE *fd;
int lnum = 0;
char found;
struct conf_item *ci = NULL;
struct passwd *pw = getpwuid(getuid());
int v_int;
char *conv_err;
float v_float;
cfname = malloc(strlen(pw->pw_dir) + strlen(CFNAME) + 3);
strcpy(cfname, pw->pw_dir);
strcat(cfname, "/"); strcat(cfname, CFNAME);
if ((fd = fopen(cfname, "r"))) {
while (fgets(tmp, 0x100, fd)) {
found = 0;
++lnum;
lp = tmp + strspn(tmp, " ");
if (*lp != '#' && *lp != '\n') {
if (strcspn(lp, " =") > sizeof(lv)) {
fclose(fd);
fatal_error("parse error in %s, line %d: identifier too long", cfname, lnum);
}
strncpy(lv, lp, strcspn(lp, " ="));
lv[strcspn(lp, " =")] = '\0';
ll_reset(conf_items);
while (!found && (ci = ll_getall(conf_items)))
if (ci->type != t_sep && ci->type != t_func && !strcasecmp(ci->cfname, lv)) found = 1;
if (!found) {
fclose(fd);
fatal_error("parse error in %s, line %d: unknown identifier '%s'", cfname, lnum, lv);
}
lp += strlen(lv);
lp += strspn(lp, " ");
if (*lp++ != '=') {
fclose(fd);
fatal_error("parse error in %s, line %d: missing '=' operator in assignment", cfname, lnum);
}
lp += strspn(lp, " ");
if (strcspn(lp, " \n") > sizeof(rv)) {
fclose(fd);
fatal_error("parse error in %s, line %d: argument too long", cfname, lnum);
}
if (*lp == '\n') {
fclose(fd);
fatal_error("parse error in %s, line %d: argument expected", cfname, lnum);
}
strncpy(rv, lp, strcspn(lp, " \n"));
rv[strcspn(lp, " \n")] = '\0';
switch (ci->type) {
case t_int: v_int = strtol(rv, &conv_err, 10);
if (*conv_err == '\0')
if (v_int > ci->max) {
fclose(fd);
fatal_error("parse error in %s, line %d: value exceeds maximum of %d", cfname, lnum, (int)ci->max);
} else if (v_int < ci->min) {
fclose(fd);
fatal_error("parse error in %s, line %d: value is below minimum of %d", cfname, lnum, (int)ci->min);
} else *(int *)ci->v = v_int;
else {
fclose(fd);
fatal_error("parse error in %s, line %d: integer value expected, '%s' found instead", cfname, lnum, rv);
}
break;
case t_float: v_float = strtod(rv, &conv_err);
if (*conv_err == '\0')
if (v_float > ci->max) {
fclose(fd);
fatal_error("parse error in %s, line %d: value exceeds maximum of %g", cfname, lnum, ci->max);
} else if (v_float < ci->min) {
fclose(fd);
fatal_error("parse error in %s, line %d: value is below minimum of %g", cfname, lnum, ci->min);
} else *(float *)ci->v = v_float;
else {
fclose(fd);
fatal_error("parse error in %s, line %d: float value expected, '%s' found instead", cfname, lnum, rv);
}
break;
case t_string: if (strlen(rv) <= ci->max)
ci->v = strdup(rv);
else {
fclose(fd);
fatal_error("parse error in %s, line %d: argument too long (max %d chars)", cfname, lnum, ci->max);
}
break;
case t_switch: if (!strcasecmp(rv, "on") || !strcasecmp(rv, "enabled")
|| !strcasecmp(rv, "yes") || !strcasecmp(rv, "1"))
*(char *)ci->v = 1;
else if (!strcasecmp(rv, "off") || !strcasecmp(rv, "disabled")
|| !strcasecmp(rv, "no") || !strcasecmp(rv, "0"))
*(char *)ci->v = 0;
else {
fclose(fd);
fatal_error("parse error in %s, line %d: boolean expected, '%s' found instead", cfname, lnum, rv);
}
break;
case t_list: if ((v_int = ll_scan(ci->list, "S", rv)) >= 0)
*(int *)ci->v = v_int;
else {
fclose(fd);
fatal_error("parse error in %s, line %d: '%s' is not a valid argument here", cfname, lnum, rv);
}
break;
case t_listval: if ((v_int = ll_scan(ci->list, "S", rv)) >= 0) {
strncpy((char *)ci->v, (char *)ll_get(ci->list, v_int), 32);
} else {
fclose(fd);
fatal_error("parse error in %s, line %d: '%s' is not a valid argument here", cfname, lnum, rv);
}
break;
case t_sep: /* gcc -Wall sucks */
case t_func:
}
}
}
fclose(fd);
} else if (errno != ENOENT) {
switch (errno) {
case EACCES: fatal_error("cannot open %s: permission denied", cfname);
break;
case EAGAIN: fatal_error("cannot open %s: resource temporarily unavailable", cfname);
break;
case EBUSY: fatal_error("cannot open %s: resource busy", cfname);
break;
case EINTR: fatal_error("cannot open %s: interrupted function call", cfname);
break;
case EINVAL: fatal_error("cannot open %s: invalid argument", cfname);
break;
case EIO: fatal_error("cannot open %s: i/o error", cfname);
break;
case EISDIR: fatal_error("cannot open %s: is a directory", cfname);
break;
case EMFILE: fatal_error("cannot open %s: too many open files", cfname);
break;
case EPERM: fatal_error("cannot open %s: operation not permitted", cfname);
break;
case EPIPE: fatal_error("cannot open %s: broken pipe", cfname);
break;
}
}
}
void write_cf()
{
char tmp[0x100], rv[0x40], *cfname;
char *lp, *cp;
FILE *fd;
int cfld;
int add;
struct conf_item *ci = NULL;
struct passwd *pw = getpwuid(getuid());
int i;
cfname = malloc(strlen(pw->pw_dir) + strlen(CFNAME) + 3);
strcpy(cfname, pw->pw_dir);
strcat(cfname, "/"); strcat(cfname, CFNAME);
cfld = ll_create();
if ((fd = fopen(cfname, "r"))) {
while (fgets(tmp, 0x100, fd)) ll_push(cfld, "s", tmp);
fclose(fd);
}
ll_reset(conf_items);
while ((ci = ll_getall(conf_items))) {
if (ci->type != t_sep && ci->type != t_func
&& (!ci->dep || (ci->dep && *ci->dep))) {
switch (ci->type) {
case t_int: sprintf(rv, "%d", *(int *)ci->v);
break;
case t_float: sprintf(rv, "%g", *(float *)ci->v);
break;
case t_string: strcpy(rv, (char *)ci->v);
break;
case t_switch: sprintf(rv, "%s", (*(char *)ci->v ? "on" : "off"));
break;
case t_list: sprintf(rv, "%s", (char *)ll_get(ci->list, *(int *)ci->v));
for (i = 0; i < strlen(rv); i++)
if (*(rv + i) >= 65 && *(rv + i) <= 90) *(rv + i) += 32;
break;
case t_listval: sprintf(rv, "%s", (char *)ci->v);
case t_sep: break;
case t_func:
}
add = 1;
for (i = 0; i < ll_size(cfld); i++) {
lp = ll_get(cfld, i);
cp = lp += strspn(lp, " ");
if (!strncasecmp(cp, ci->cfname, strcspn(cp, " ="))
&& strlen(ci->cfname) == strcspn(cp, " =")) {
add = 0;
cp += strcspn(cp, "=") + 1;
cp += strspn(cp, " ");
strncpy(tmp, cp, strcspn(cp, " #\n"));
if (strcasecmp(tmp, rv)) {
strncpy(tmp, lp, strcspn(lp, " ="));
tmp[strcspn(lp, " =")] = '\0';
strcat(tmp, " = ");
strcat(tmp, rv);
strcat(tmp, "\n");
ll_replace(cfld, i, "s", tmp);
}
}
}
if (add) {
strcpy(tmp, ci->cfname);
strcat(tmp, " = ");
strcat(tmp, rv);
strcat(tmp, "\n");
ll_push(cfld, "s", tmp);
}
}
}
if ((fd = fopen(cfname, "w")) < 0) fatal_error("cannot open %s", cfname);
ll_reset(cfld);
while ((lp = ll_getall(cfld))) fputs(lp, fd);
fclose(fd);
ll_destroy(cfld);
free(cfname);
}
void set_defaults(struct wavemon_conf *conf)
{
strncpy(conf->ifname, (char *)ll_get(iw_getif(), 0), sizeof(conf->ifname));
conf->stat_iv = 100;
conf->info_iv = 10;
conf->slotsize = 4;
conf->override_bounds = 0;
conf->sig_min = -102;
conf->sig_max = 10;
conf->noise_min = -102;
conf->noise_max = 10;
conf->random = 0;
conf->linear = 0;
conf->lthreshold_action = 0;
conf->lthreshold = -80;
conf->hthreshold_action = 0;
conf->hthreshold = -10;
conf->startup_scr = 0;
}
void dealloc_on_exit()
{
struct conf_item *ci;
if (conf_items) {
while ((ci = ll_getall(conf_items)))
if ((ci->type == t_list) || (ci->type == t_listval)) ll_destroy(ci->list);
ll_destroy(conf_items);
}
}
void getconf(struct wavemon_conf *conf, int argc, char *argv[])
{
set_defaults(conf);
init_conf_items(conf);
read_cf(conf);
getargs(conf, argc, argv);
}
|