From 0b0aac6ce21bcb38d7e03dc2b3ff419861476a24 Mon Sep 17 00:00:00 2001 From: Jonathan McCrohan Date: Sat, 30 Jan 2016 17:08:40 +0000 Subject: Imported Upstream version 0.8.0 --- conf.c | 264 ++++++++++++++++++++++++++++++----------------------------------- 1 file changed, 123 insertions(+), 141 deletions(-) (limited to 'conf.c') diff --git a/conf.c b/conf.c index 376569e..27c11dc 100644 --- a/conf.c +++ b/conf.c @@ -22,7 +22,8 @@ #include /* GLOBALS */ -static char **if_list; /* array of WiFi interface names */ +#define MAX_IFLIST_ENTRIES 64 +static char *if_list[MAX_IFLIST_ENTRIES]; /* array of WiFi interface names */ int conf_items; /* index into array storing menu items */ static char *on_off_names[] = { [false] = "Off", [true] = "On", NULL }; @@ -37,6 +38,7 @@ static char *action_items[] = { static char *sort_order[] = { [SO_CHAN] = "Channel", [SO_SIGNAL] = "Signal", + [SO_MAC] = "MAC", [SO_ESSID] = "Essid", [SO_OPEN] = "Open", [SO_CHAN_SIG] = "Chan/Sig", @@ -62,12 +64,11 @@ struct wavemon_conf conf = { .check_geometry = false, .cisco_mac = false, .override_bounds = false, - .random = false, - .sig_min = -102, - .sig_max = 10, - .noise_min = -102, - .noise_max = 10, + .sig_min = -100, + .sig_max = -10, + .noise_min = -120, + .noise_max = -40, .scan_sort_order = SO_CHAN_SIG, .scan_sort_asc = false, @@ -80,22 +81,20 @@ struct wavemon_conf conf = { }; /** Populate interface list */ -void conf_get_interface_list(bool init) +void conf_get_interface_list(void) { char *old_if = NULL; int idx; - if (if_list) { - for (idx = 0; if_list[idx]; idx++) - if (idx == conf.if_idx) - old_if = if_list[idx]; - else - free(if_list[idx]); - free(if_list); + for (idx = 0; if_list[idx]; idx++) { + if (idx == conf.if_idx) + old_if = if_list[idx]; + else + free(if_list[idx]); } - if_list = iw_get_interface_list(); - if (if_list == NULL && !init) - err_quit("no wireless interfaces found!"); + iw_get_interface_list(if_list, MAX_IFLIST_ENTRIES); + if (!if_list[0]) + err_quit("no supported wireless interfaces found!"); conf.if_idx = 0; if (old_if) { @@ -109,7 +108,7 @@ void conf_get_interface_list(bool init) /** Return currently selected interface name */ const char *conf_ifname(void) { - return if_list ? if_list[conf.if_idx] : "(none)"; + return if_list[0] && if_list[conf.if_idx] ? if_list[conf.if_idx] : "(none)"; } /* Return full path of rcfile. Allocates string which must bee free()-d. */ @@ -130,6 +129,77 @@ static char *get_confname(void) return full_path; } +static void write_cf(void) +{ + char tmp[0x100], rv[0x40]; + struct conf_item *ci = NULL; + char *lp, *cp; + int add, i; + char *cfname = get_confname(); + int cfld = ll_create(); + FILE *fd = fopen(cfname, "w"); + + if (fd == NULL) + err_sys("failed to open configuration file '%s'", cfname); + + for (ll_reset(conf_items); (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", *ci->v.i); + break; + case t_list: + if (!argv_count(ci->list)) + continue; + sprintf(rv, "%s", ci->list[*ci->v.i]); + str_tolower(rv); + break; + case t_sep: + case t_func: + break; + } + + 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); + } + } + } + + for (ll_reset(cfld); (lp = ll_getall(cfld)); ) + fputs(lp, fd); + fclose(fd); + + ll_destroy(cfld); + free(cfname); +} + static void read_cf(void) { char tmp[0x100], lv[0x20], rv[0x20]; @@ -138,6 +208,7 @@ static void read_cf(void) size_t len; int lnum, found, v_int; char *lp, *conv_err; + bool file_needs_update = false; char *cfname = get_confname(); if (access(cfname, F_OK) != 0) @@ -148,7 +219,6 @@ static void read_cf(void) err_sys("can not read configuration file '%s'", cfname); for (lnum = 1; fgets(tmp, sizeof(tmp), fd); lnum++) { - lp = tmp + strspn(tmp, " "); if (*lp == '#' || *lp == '\n') continue; @@ -168,6 +238,7 @@ static void read_cf(void) if (!found) { err_msg("%s, line %d: ignoring unknown identifier '%s'", cfname, lnum, lv); + file_needs_update = true; continue; } @@ -192,110 +263,43 @@ static void read_cf(void) err_quit("parse error in %s, line %d: integer value expected, '%s' found instead", cfname, lnum, rv); } else if (v_int > ci->max) { - err_quit("parse error in %s, line %d: value exceeds maximum of %d", + err_msg("%s, line %d: value exceeds maximum of %d - using maximum", cfname, lnum, (int)ci->max); + *ci->v.i = ci->max; + file_needs_update = true; } else if (v_int < ci->min) { - err_quit("parse error in %s, line %d: value is below minimum of %d", + err_msg("%s, line %d: value is below minimum of %d - using minimum", cfname, lnum, (int)ci->min); + *ci->v.i = ci->min; + file_needs_update = true; } else { *ci->v.i = v_int; } break; case t_list: - v_int = ci->list ? argv_find(ci->list, rv) : -1; - if (v_int < 0) + assert(ci->list != NULL); + if (!argv_count(ci->list)) + err_quit("no usable %s candidates available for '%s'", ci->name, rv); + v_int = argv_find(ci->list, rv); + if (v_int < 0) { err_msg("%s, line %d: '%s = %s' is not valid - using defaults", cfname, lnum, lv, rv); - else + file_needs_update = true; + } else { *ci->v.i = v_int; - case t_sep: /* These two cases are missing from the enum, they are not handled */ - case t_func: /* To pacify gcc -Wall, fall through here */ + } + case t_sep: + case t_func: break; } } fclose(fd); done: free(cfname); -} -static void write_cf(void) -{ - char tmp[0x100], rv[0x40]; - struct conf_item *ci = NULL; - char *lp, *cp; - int add, i; - FILE *fd; - char *cfname = get_confname(); - int cfld = ll_create(); - - if (access(cfname, F_OK) == 0) { - fd = fopen(cfname, "r"); - if (fd == NULL) - err_sys("can not read configuration file '%s'", cfname); - while (fgets(tmp, sizeof(tmp), fd)) - ll_push(cfld, "s", tmp); - fclose(fd); + if (file_needs_update) { + write_cf(); } - - for (ll_reset(conf_items); (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", *ci->v.i); - break; - case t_list: - sprintf(rv, "%s", ci->list[*ci->v.i]); - str_tolower(rv); - break; - /* Fall through, the rest are dummy statements to pacify gcc -Wall */ - case t_sep: - case t_func: - break; - } - - 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); - } - } - } - - fd = fopen(cfname, "w"); - if (fd == NULL) - err_sys("can not write to configuration file '%s'", cfname); - - for (ll_reset(cfld); (lp = ll_getall(cfld)); ) - fputs(lp, fd); - fclose(fd); - - ll_destroy(cfld); - free(cfname); } static void init_conf_items(void) @@ -407,8 +411,8 @@ static void init_conf_items(void) item->cfname = strdup("min_signal_level"); item->type = t_int; item->v.i = &conf.sig_min; - item->min = -128; - item->max = -60; + item->min = -100; + item->max = -39; item->inc = 1; item->unit = strdup("dBm"); item->dep = &conf.override_bounds; @@ -419,8 +423,8 @@ static void init_conf_items(void) item->cfname = strdup("max_signal_level"); item->type = t_int; item->v.i = &conf.sig_max; - item->min = -59; - item->max = 120; + item->min = -40; + item->max = -10; item->inc = 1; item->unit = strdup("dBm"); item->dep = &conf.override_bounds; @@ -431,8 +435,8 @@ static void init_conf_items(void) item->cfname = strdup("min_noise_level"); item->type = t_int; item->v.i = &conf.noise_min; - item->min = -128; - item->max = -60; + item->min = -120; + item->max = -70; item->inc = 1; item->unit = strdup("dBm"); item->dep = &conf.override_bounds; @@ -443,21 +447,13 @@ static void init_conf_items(void) item->cfname = strdup("max_noise_level"); item->type = t_int; item->v.i = &conf.noise_max; - item->min = -60; - item->max = 120; + item->min = -69; + item->max = -40; item->inc = 1; item->unit = strdup("dBm"); item->dep = &conf.override_bounds; ll_push(conf_items, "*", item); - item = calloc(1, sizeof(*item)); - item->name = strdup("Random signals"); - item->cfname = strdup("random"); - item->type = t_list; - item->v.i = &conf.random; - item->list = on_off_names; - ll_push(conf_items, "*", item); - /* thresholds */ item = calloc(1, sizeof(*item)); item->name = strdup("Low threshold action"); @@ -532,18 +528,14 @@ static void init_conf_items(void) void getconf(int argc, char *argv[]) { - int arg, dump = 0, help = 0, version = 0; + int arg, help = 0, version = 0; - conf_get_interface_list(true); + conf_get_interface_list(); init_conf_items(); read_cf(); - while ((arg = getopt(argc, argv, "dghi:rv")) >= 0) { + while ((arg = getopt(argc, argv, "ghi:v")) >= 0) { switch (arg) { - case 'd': - if (if_list) - dump++; - break; case 'g': conf.check_geometry = true; break; @@ -551,42 +543,32 @@ void getconf(int argc, char *argv[]) help++; break; case 'i': - conf.if_idx = if_list ? argv_find(if_list, optarg) : -1; + conf.if_idx = argv_find(if_list, optarg); if (conf.if_idx < 0) err_quit("no wireless extensions found on '%s'", optarg); break; - case 'r': - conf.random = true; - break; case 'v': version++; break; default: - /* bad argument. bad bad */ exit(EXIT_FAILURE); } } if (version) { printf("wavemon %s", PACKAGE_VERSION); - printf(" with %s and %s.\n", we_version(), curses_version()); + printf(" with %s.\n", curses_version()); printf("Distributed under the terms of the GPLv3.\n%s", help ? "\n" : ""); } if (help) { - printf("usage: wavemon [ -dhlrv ] [ -i ifname ]\n"); - printf(" -d Dump the current device status to stdout and exit\n"); + printf("usage: wavemon [ -hgv ] [ -i ifname ]\n"); printf(" -g Ensure screen is sufficiently dimensioned\n"); printf(" -h This help screen\n"); printf(" -i Use specified network interface (default: auto)\n"); - printf(" -r Generate random levels (for testing purposes)\n"); printf(" -v Print version number\n"); - } else if (dump) { - dump_parameters(); } - if (version || help || dump) + if (version || help) exit(EXIT_SUCCESS); - else if (if_list == NULL) - err_quit("no supported wireless interfaces found"); } -- cgit v1.2.3