+++ /dev/null
-From: Gerrit Renker <gerrit@erg.abdn.ac.uk>
-Date: Sun, 13 Mar 2011 17:39:40 +0000 (+0100)
-Subject: Configuration: decouple configuration items
-X-Git-Url: http://eden-feed.erg.abdn.ac.uk/cgi-bin/gitweb.cgi?p=wavemon.git;a=commitdiff_plain;h=74610068abe331927f72aa617689566a2b2a7771
-
-Configuration: decouple configuration items
-
-This puts access to WiFi interface list and currently selected interface
-into wrapper functions, to better separate the code blocks.
----
-
-diff --git a/conf.c b/conf.c
-index 4004c73..45fa42e 100644
---- a/conf.c
-+++ b/conf.c
-@@ -22,6 +22,7 @@
- #include <sys/types.h>
-
- /* GLOBALS */
-+static char **if_list; /* array of WiFi interface names */
- int conf_items; /* index into array storing menu items */
-
- static char *on_off_names[] = { [false] = "Off", [true] = "On", NULL };
-@@ -116,6 +117,39 @@ static void getargs(int argc, char *argv[])
- }
- }
-
-+/** Populate interface list */
-+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);
-+ }
-+ if_list = iw_get_interface_list();
-+ if (if_list == NULL)
-+ err_quit("no wireless interfaces found!");
-+
-+ conf.if_idx = 0;
-+ if (old_if) {
-+ idx = argv_find(if_list, old_if);
-+ if (idx > 0)
-+ conf.if_idx = idx;
-+ free(old_if);
-+ }
-+}
-+
-+/** Return currently selected interface name */
-+const char *conf_ifname(void)
-+{
-+ return if_list ? if_list[conf.if_idx] : "(none)";
-+}
-+
- /* Return full path of rcfile. Allocates string which must bee free()-d. */
- static char *get_confname(void)
- {
-@@ -520,7 +554,7 @@ static void init_conf_items(void)
-
- void getconf(int argc, char *argv[])
- {
-- iw_get_interface_list();
-+ conf_get_interface_list();
- init_conf_items();
- read_cf();
- getargs(argc, argv);
-diff --git a/conf_scr.c b/conf_scr.c
-index a338771..674137c 100644
---- a/conf_scr.c
-+++ b/conf_scr.c
-@@ -159,7 +159,7 @@ static int m_pref(WINDOW *w_conf, int list_offset, int active_item, int num_item
-
- void scr_conf_init(void)
- {
-- iw_get_interface_list(); /* may have changed in the meantime */
-+ conf_get_interface_list(); /* may have changed in the meantime */
-
- num_items = ll_size(conf_items);
- w_conf = newwin_title(0, WAV_HEIGHT, "Preferences", false);
-diff --git a/info_scr.c b/info_scr.c
-index 8171373..d17fdfa 100644
---- a/info_scr.c
-+++ b/info_scr.c
-@@ -30,7 +30,7 @@ void sampling_init(void (*sampling_handler)(int))
- div_t d = div(conf.stat_iv, 1000); /* conf.stat_iv in msec */
-
- xsignal(SIGALRM, SIG_IGN);
-- iw_getinf_range(if_list[conf.if_idx], &cur.range);
-+ iw_getinf_range(conf_ifname(), &cur.range);
- i.it_interval.tv_sec = i.it_value.tv_sec = d.quot;
- i.it_interval.tv_usec = i.it_value.tv_usec = d.rem * 1000;
- xsignal(SIGALRM, sampling_handler);
-@@ -139,7 +139,7 @@ static void display_stats(void)
- struct if_stat nstat;
- char tmp[0x100];
-
-- if_getstat(if_list[conf.if_idx], &nstat);
-+ if_getstat(conf_ifname(), &nstat);
-
- /*
- * Interface RX stats
-@@ -197,10 +197,10 @@ static void display_info(WINDOW *w_if, WINDOW *w_info)
- char tmp[0x100];
- int i;
-
-- dyn_info_get(&info, if_list[conf.if_idx], &cur.range);
-+ dyn_info_get(&info, conf_ifname(), &cur.range);
-
- wmove(w_if, 1, 1);
-- waddstr_b(w_if, if_list[conf.if_idx]);
-+ waddstr_b(w_if, conf_ifname());
- if (cur.range.enc_capa & IW_WPA_MASK)
- sprintf(tmp, " (%s, %s)", info.name, format_wpa(&cur.range));
- else
-@@ -416,11 +416,11 @@ static void display_netinfo(WINDOW *w_net)
- struct if_info info;
- char tmp[0x40];
-
-- if_getinf(if_list[conf.if_idx], &info);
-+ if_getinf(conf_ifname(), &info);
-
- wmove(w_net, 1, 1);
- if (getmaxy(w_net) == WH_NET_MAX) {
-- waddstr(w_net, if_list[conf.if_idx]);
-+ waddstr(w_net, conf_ifname());
-
- waddstr_b(w_net, " (");
- waddstr(w_net, info.flags & IFF_UP ? "UP" : "DOWN");
-diff --git a/iw_if.c b/iw_if.c
-index 2008c14..708ff85 100644
---- a/iw_if.c
-+++ b/iw_if.c
-@@ -24,9 +24,6 @@
- /* Fallback maximum quality level when using random samples. */
- #define WAVE_RAND_QUAL_MAX 100
-
--/* GLOBALS */
--char **if_list = NULL; /* array of WiFi interface names */
--
- /*
- * Obtain network device information
- */
-@@ -65,7 +62,7 @@ int if_set_up(int skfd, const char *ifname)
- }
-
- /* Interface information */
--void if_getinf(char *ifname, struct if_info *info)
-+void if_getinf(const char *ifname, struct if_info *info)
- {
- struct ifreq ifr;
- int skfd = socket(AF_INET, SOCK_DGRAM, 0);
-@@ -114,49 +111,27 @@ static FILE *open_proc_net(const char *filename)
- }
-
- /**
-- * iw_get_interface_list - Populate NULL-terminated array of WiFi interfaces.
-- * Rebuild if already set, exit if no interfaces present.
-+ * iw_get_interface_list - Return NULL-terminated array of WiFi interfaces.
- */
--void iw_get_interface_list(void)
-+char **iw_get_interface_list(void)
- {
-- char *p, tmp[IFNAMSIZ], *old_if = NULL;
-- int idx, nifs;
-+ char **if_list = NULL, *p, tmp[BUFSIZ];
-+ int nifs = 1; /* if_list[nifs-1] = NULL */
- FILE *fp = open_proc_net("wireless");
-
-- 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 (nifs = 0; fgets(tmp, sizeof(tmp), fp); )
-- nifs += strchr(tmp, ':') != NULL;
-- if (!nifs)
-- err_quit("no wireless interfaces found!");
-- rewind(fp);
--
-- if_list = calloc(nifs + 1, sizeof(*if_list));
-- if (if_list == NULL)
-- err_sys("unable to memorize %d interfaces", nifs);
--
-- for (conf.if_idx = idx = 0; fgets(tmp, sizeof(tmp), fp); ) {
-+ while (fgets(tmp, sizeof(tmp), fp))
- if ((p = strchr(tmp, ':'))) {
-+ if_list = realloc(if_list, sizeof(char *) * (nifs + 1));
- for (*p = '\0', p = tmp; isspace(*p); )
- p++;
-- if (old_if && strcmp(old_if, p) == 0)
-- conf.if_idx = idx;
-- if_list[idx++] = strdup(p);
-+ if_list[nifs-1] = strdup(p);
-+ if_list[nifs++] = NULL;
- }
-- }
-- assert(idx == nifs);
- fclose(fp);
-- free(old_if);
-+ return if_list;
- }
-
--void if_getstat(char *ifname, struct if_stat *stat)
-+void if_getstat(const char *ifname, struct if_stat *stat)
- {
- char line[0x100];
- unsigned long d;
-@@ -187,7 +162,8 @@ void if_getstat(char *ifname, struct if_stat *stat)
- * @ifname: interface name
- * @if: range information to use (number of encryption keys)
- */
--void dyn_info_get(struct iw_dyn_info *info, char *ifname, struct iw_range *ir)
-+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);
-@@ -321,7 +297,7 @@ void dyn_info_cleanup(struct iw_dyn_info *info)
- /*
- * get range information
- */
--void iw_getinf_range(char *ifname, struct iw_range *range)
-+void iw_getinf_range(const char *ifname, struct iw_range *range)
- {
- struct iwreq iwr;
- int skfd = socket(AF_INET, SOCK_DGRAM, 0);
-@@ -395,7 +371,7 @@ static void iw_getstat_real(struct iw_statistics *stat)
- wrq.u.data.pointer = (caddr_t) stat;
- wrq.u.data.length = sizeof(*stat);
- wrq.u.data.flags = 0;
-- strncpy(wrq.ifr_name, if_list[conf.if_idx], IFNAMSIZ);
-+ strncpy(wrq.ifr_name, conf_ifname(), IFNAMSIZ);
-
- if (ioctl(skfd, SIOCGIWSTATS, &wrq) < 0) {
- /*
-@@ -490,13 +466,13 @@ void dump_parameters(void)
- struct if_stat nstat;
- int i;
-
-- iw_getinf_range(if_list[conf.if_idx], &iw.range);
-- dyn_info_get(&info, if_list[conf.if_idx], &iw.range);
-+ iw_getinf_range(conf_ifname(), &iw.range);
-+ dyn_info_get(&info, conf_ifname(), &iw.range);
- iw_getstat(&iw);
-- if_getstat(if_list[conf.if_idx], &nstat);
-+ if_getstat(conf_ifname(), &nstat);
-
- printf("\n");
-- printf("Configured device: %s (%s)\n", if_list[conf.if_idx], info.name);
-+ printf("Configured device: %s (%s)\n", conf_ifname(), info.name);
- printf(" Security: %s\n", iw.range.enc_capa ?
- format_enc_capab(iw.range.enc_capa, ", ") : "WEP");
- if (iw.range.num_encoding_sizes &&
-diff --git a/iw_if.h b/iw_if.h
-index 80e6595..636a63a 100644
---- a/iw_if.h
-+++ b/iw_if.h
-@@ -71,7 +71,7 @@ struct if_info {
- };
- extern bool if_is_up(int skfd, const char *ifname);
- extern int if_set_up(int skfd, const char *ifname);
--extern void if_getinf(char *ifname, struct if_info *info);
-+extern void if_getinf(const char *ifname, struct if_info *info);
-
- /**
- * struct iw_key - Encoding information
-@@ -174,7 +174,8 @@ static inline uint8_t dyn_info_wep_keys(struct iw_dyn_info *info)
- info->keys[i].size == 13;
- return num_wep;
- }
--extern void dyn_info_get(struct iw_dyn_info *, char *ifname, struct iw_range *);
-+extern void dyn_info_get(struct iw_dyn_info *info,
-+ const char *ifname, struct iw_range *ir);
- extern void dyn_info_cleanup(struct iw_dyn_info *info);
-
-
-@@ -188,7 +189,7 @@ struct if_stat {
- tx_bytes;
- };
-
--extern void if_getstat(char *ifname, struct if_stat *stat);
-+extern void if_getstat(const char *ifname, struct if_stat *stat);
-
- /*
- * Structs to communicate WiFi statistics
-@@ -200,7 +201,7 @@ struct iw_levelstat {
- };
- #define IW_LSTAT_INIT { 0, 0, IW_QUAL_LEVEL_INVALID | IW_QUAL_NOISE_INVALID }
-
--extern void iw_getinf_range(char *ifname, struct iw_range *range);
-+extern void iw_getinf_range(const char *ifname, struct iw_range *range);
- extern void iw_sanitize(struct iw_range *range,
- struct iw_quality *qual,
- struct iw_levelstat *dbm);
-@@ -250,7 +251,7 @@ struct scan_result {
-
- struct scan_result *next;
- };
--extern struct scan_result *get_scan_list(int skfd, char *ifname, int weversion);
-+extern struct scan_result *get_scan_list(int skfd, const char *ifname, int ver);
- extern void free_scan_result(struct scan_result *head);
-
-
-diff --git a/iw_scan.c b/iw_scan.c
-index d75ab6b..8423e6a 100644
---- a/iw_scan.c
-+++ b/iw_scan.c
-@@ -553,7 +553,7 @@ static int cmp_scan_sig(struct scan_result *a, struct scan_result *b)
- return a->qual.level - b->qual.level;
- }
-
--struct scan_result *get_scan_list(int skfd, char *ifname, int we_version)
-+struct scan_result *get_scan_list(int skfd, const char *ifname, int we_version)
- {
- struct scan_result *head = NULL;
- struct iwreq wrq;
-diff --git a/scan_scr.c b/scan_scr.c
-index 879c2f6..b9a4eee 100644
---- a/scan_scr.c
-+++ b/scan_scr.c
-@@ -80,9 +80,9 @@ static void display_aplist(WINDOW *w_aplst)
- if (skfd < 0)
- err_sys("%s: can not open socket", __func__);
-
-- iw_getinf_range(if_list[conf.if_idx], &range);
-+ iw_getinf_range(conf_ifname(), &range);
-
-- head = get_scan_list(skfd, if_list[conf.if_idx], range.we_version_compiled);
-+ head = get_scan_list(skfd, conf_ifname(), range.we_version_compiled);
- if (head) {
- ;
- } else if (errno == EPERM || !has_net_admin_capability()) {
-@@ -93,13 +93,13 @@ static void display_aplist(WINDOW *w_aplst)
- } else if (errno == EINTR || errno == EAGAIN || errno == EBUSY) {
- /* Ignore temporary errors */
- goto done;
-- } else if (!if_is_up(skfd, if_list[conf.if_idx])) {
-- sprintf(s, "Interface '%s' is down ", if_list[conf.if_idx]);
-+ } else if (!if_is_up(skfd, conf_ifname())) {
-+ sprintf(s, "Interface '%s' is down ", conf_ifname());
- if (!has_net_admin_capability())
- strcat(s, "- can not scan");
-- else if (if_set_up(skfd, if_list[conf.if_idx]) < 0)
-+ else if (if_set_up(skfd, conf_ifname()) < 0)
- sprintf(s, "Can not bring up '%s' for scanning: %s",
-- if_list[conf.if_idx], strerror(errno));
-+ conf_ifname(), strerror(errno));
- else
- strcat(s, "- setting it up ...");
- } else if (errno == EFAULT) {
-@@ -110,9 +110,9 @@ static void display_aplist(WINDOW *w_aplst)
- */
- goto done;
- } else if (errno) {
-- sprintf(s, "No scan on %s: %s", if_list[conf.if_idx], strerror(errno));
-+ sprintf(s, "No scan on %s: %s", conf_ifname(), strerror(errno));
- } else {
-- sprintf(s, "No scan results on %s", if_list[conf.if_idx]);
-+ sprintf(s, "No scan results on %s", conf_ifname());
- }
-
- for (i = 1; i <= MAXYLEN; i++)
-diff --git a/wavemon.h b/wavemon.h
-index 22a783f..ad0b488 100644
---- a/wavemon.h
-+++ b/wavemon.h
-@@ -103,7 +103,7 @@ static inline void threshold_action(enum threshold_action action)
- * Global in-memory representation of current wavemon configuration state
- */
- extern struct wavemon_conf {
-- int if_idx; /* Index into if_list */
-+ int if_idx; /* Index into interface list */
-
- int stat_iv,
- info_iv;
-@@ -256,8 +256,9 @@ static inline int cp_from_scale(float value, const char *cscale, bool reverse)
- /*
- * Wireless interfaces
- */
--extern char **if_list;
--extern void iw_get_interface_list(void);
-+extern const char *conf_ifname(void);
-+extern void conf_get_interface_list(void);
-+extern char **iw_get_interface_list(void);
- extern void dump_parameters(void);
-
- /*