1 From: Gerrit Renker <gerrit@erg.abdn.ac.uk>
2 Date: Sun, 13 Mar 2011 17:39:40 +0000 (+0100)
3 Subject: Configuration: decouple configuration items
4 X-Git-Url: http://eden-feed.erg.abdn.ac.uk/cgi-bin/gitweb.cgi?p=wavemon.git;a=commitdiff_plain;h=74610068abe331927f72aa617689566a2b2a7771
6 Configuration: decouple configuration items
8 This puts access to WiFi interface list and currently selected interface
9 into wrapper functions, to better separate the code blocks.
12 diff --git a/conf.c b/conf.c
13 index 4004c73..45fa42e 100644
17 #include <sys/types.h>
20 +static char **if_list; /* array of WiFi interface names */
21 int conf_items; /* index into array storing menu items */
23 static char *on_off_names[] = { [false] = "Off", [true] = "On", NULL };
24 @@ -116,6 +117,39 @@ static void getargs(int argc, char *argv[])
28 +/** Populate interface list */
29 +void conf_get_interface_list(void)
31 + char *old_if = NULL;
35 + for (idx = 0; if_list[idx]; idx++)
36 + if (idx == conf.if_idx)
37 + old_if = if_list[idx];
42 + if_list = iw_get_interface_list();
43 + if (if_list == NULL)
44 + err_quit("no wireless interfaces found!");
48 + idx = argv_find(if_list, old_if);
55 +/** Return currently selected interface name */
56 +const char *conf_ifname(void)
58 + return if_list ? if_list[conf.if_idx] : "(none)";
61 /* Return full path of rcfile. Allocates string which must bee free()-d. */
62 static char *get_confname(void)
64 @@ -520,7 +554,7 @@ static void init_conf_items(void)
66 void getconf(int argc, char *argv[])
68 - iw_get_interface_list();
69 + conf_get_interface_list();
73 diff --git a/conf_scr.c b/conf_scr.c
74 index a338771..674137c 100644
77 @@ -159,7 +159,7 @@ static int m_pref(WINDOW *w_conf, int list_offset, int active_item, int num_item
79 void scr_conf_init(void)
81 - iw_get_interface_list(); /* may have changed in the meantime */
82 + conf_get_interface_list(); /* may have changed in the meantime */
84 num_items = ll_size(conf_items);
85 w_conf = newwin_title(0, WAV_HEIGHT, "Preferences", false);
86 diff --git a/info_scr.c b/info_scr.c
87 index 8171373..d17fdfa 100644
90 @@ -30,7 +30,7 @@ void sampling_init(void (*sampling_handler)(int))
91 div_t d = div(conf.stat_iv, 1000); /* conf.stat_iv in msec */
93 xsignal(SIGALRM, SIG_IGN);
94 - iw_getinf_range(if_list[conf.if_idx], &cur.range);
95 + iw_getinf_range(conf_ifname(), &cur.range);
96 i.it_interval.tv_sec = i.it_value.tv_sec = d.quot;
97 i.it_interval.tv_usec = i.it_value.tv_usec = d.rem * 1000;
98 xsignal(SIGALRM, sampling_handler);
99 @@ -139,7 +139,7 @@ static void display_stats(void)
100 struct if_stat nstat;
103 - if_getstat(if_list[conf.if_idx], &nstat);
104 + if_getstat(conf_ifname(), &nstat);
108 @@ -197,10 +197,10 @@ static void display_info(WINDOW *w_if, WINDOW *w_info)
112 - dyn_info_get(&info, if_list[conf.if_idx], &cur.range);
113 + dyn_info_get(&info, conf_ifname(), &cur.range);
116 - waddstr_b(w_if, if_list[conf.if_idx]);
117 + waddstr_b(w_if, conf_ifname());
118 if (cur.range.enc_capa & IW_WPA_MASK)
119 sprintf(tmp, " (%s, %s)", info.name, format_wpa(&cur.range));
121 @@ -416,11 +416,11 @@ static void display_netinfo(WINDOW *w_net)
125 - if_getinf(if_list[conf.if_idx], &info);
126 + if_getinf(conf_ifname(), &info);
129 if (getmaxy(w_net) == WH_NET_MAX) {
130 - waddstr(w_net, if_list[conf.if_idx]);
131 + waddstr(w_net, conf_ifname());
133 waddstr_b(w_net, " (");
134 waddstr(w_net, info.flags & IFF_UP ? "UP" : "DOWN");
135 diff --git a/iw_if.c b/iw_if.c
136 index 2008c14..708ff85 100644
140 /* Fallback maximum quality level when using random samples. */
141 #define WAVE_RAND_QUAL_MAX 100
144 -char **if_list = NULL; /* array of WiFi interface names */
147 * Obtain network device information
149 @@ -65,7 +62,7 @@ int if_set_up(int skfd, const char *ifname)
152 /* Interface information */
153 -void if_getinf(char *ifname, struct if_info *info)
154 +void if_getinf(const char *ifname, struct if_info *info)
157 int skfd = socket(AF_INET, SOCK_DGRAM, 0);
158 @@ -114,49 +111,27 @@ static FILE *open_proc_net(const char *filename)
162 - * iw_get_interface_list - Populate NULL-terminated array of WiFi interfaces.
163 - * Rebuild if already set, exit if no interfaces present.
164 + * iw_get_interface_list - Return NULL-terminated array of WiFi interfaces.
166 -void iw_get_interface_list(void)
167 +char **iw_get_interface_list(void)
169 - char *p, tmp[IFNAMSIZ], *old_if = NULL;
171 + char **if_list = NULL, *p, tmp[BUFSIZ];
172 + int nifs = 1; /* if_list[nifs-1] = NULL */
173 FILE *fp = open_proc_net("wireless");
176 - for (idx = 0; if_list[idx]; idx++)
177 - if (idx == conf.if_idx)
178 - old_if = if_list[idx];
180 - free(if_list[idx]);
184 - for (nifs = 0; fgets(tmp, sizeof(tmp), fp); )
185 - nifs += strchr(tmp, ':') != NULL;
187 - err_quit("no wireless interfaces found!");
190 - if_list = calloc(nifs + 1, sizeof(*if_list));
191 - if (if_list == NULL)
192 - err_sys("unable to memorize %d interfaces", nifs);
194 - for (conf.if_idx = idx = 0; fgets(tmp, sizeof(tmp), fp); ) {
195 + while (fgets(tmp, sizeof(tmp), fp))
196 if ((p = strchr(tmp, ':'))) {
197 + if_list = realloc(if_list, sizeof(char *) * (nifs + 1));
198 for (*p = '\0', p = tmp; isspace(*p); )
200 - if (old_if && strcmp(old_if, p) == 0)
202 - if_list[idx++] = strdup(p);
203 + if_list[nifs-1] = strdup(p);
204 + if_list[nifs++] = NULL;
207 - assert(idx == nifs);
213 -void if_getstat(char *ifname, struct if_stat *stat)
214 +void if_getstat(const char *ifname, struct if_stat *stat)
218 @@ -187,7 +162,8 @@ void if_getstat(char *ifname, struct if_stat *stat)
219 * @ifname: interface name
220 * @if: range information to use (number of encryption keys)
222 -void dyn_info_get(struct iw_dyn_info *info, char *ifname, struct iw_range *ir)
223 +void dyn_info_get(struct iw_dyn_info *info,
224 + const char *ifname, struct iw_range *ir)
227 int i, skfd = socket(AF_INET, SOCK_DGRAM, 0);
228 @@ -321,7 +297,7 @@ void dyn_info_cleanup(struct iw_dyn_info *info)
230 * get range information
232 -void iw_getinf_range(char *ifname, struct iw_range *range)
233 +void iw_getinf_range(const char *ifname, struct iw_range *range)
236 int skfd = socket(AF_INET, SOCK_DGRAM, 0);
237 @@ -395,7 +371,7 @@ static void iw_getstat_real(struct iw_statistics *stat)
238 wrq.u.data.pointer = (caddr_t) stat;
239 wrq.u.data.length = sizeof(*stat);
240 wrq.u.data.flags = 0;
241 - strncpy(wrq.ifr_name, if_list[conf.if_idx], IFNAMSIZ);
242 + strncpy(wrq.ifr_name, conf_ifname(), IFNAMSIZ);
244 if (ioctl(skfd, SIOCGIWSTATS, &wrq) < 0) {
246 @@ -490,13 +466,13 @@ void dump_parameters(void)
247 struct if_stat nstat;
250 - iw_getinf_range(if_list[conf.if_idx], &iw.range);
251 - dyn_info_get(&info, if_list[conf.if_idx], &iw.range);
252 + iw_getinf_range(conf_ifname(), &iw.range);
253 + dyn_info_get(&info, conf_ifname(), &iw.range);
255 - if_getstat(if_list[conf.if_idx], &nstat);
256 + if_getstat(conf_ifname(), &nstat);
259 - printf("Configured device: %s (%s)\n", if_list[conf.if_idx], info.name);
260 + printf("Configured device: %s (%s)\n", conf_ifname(), info.name);
261 printf(" Security: %s\n", iw.range.enc_capa ?
262 format_enc_capab(iw.range.enc_capa, ", ") : "WEP");
263 if (iw.range.num_encoding_sizes &&
264 diff --git a/iw_if.h b/iw_if.h
265 index 80e6595..636a63a 100644
268 @@ -71,7 +71,7 @@ struct if_info {
270 extern bool if_is_up(int skfd, const char *ifname);
271 extern int if_set_up(int skfd, const char *ifname);
272 -extern void if_getinf(char *ifname, struct if_info *info);
273 +extern void if_getinf(const char *ifname, struct if_info *info);
276 * struct iw_key - Encoding information
277 @@ -174,7 +174,8 @@ static inline uint8_t dyn_info_wep_keys(struct iw_dyn_info *info)
278 info->keys[i].size == 13;
281 -extern void dyn_info_get(struct iw_dyn_info *, char *ifname, struct iw_range *);
282 +extern void dyn_info_get(struct iw_dyn_info *info,
283 + const char *ifname, struct iw_range *ir);
284 extern void dyn_info_cleanup(struct iw_dyn_info *info);
287 @@ -188,7 +189,7 @@ struct if_stat {
291 -extern void if_getstat(char *ifname, struct if_stat *stat);
292 +extern void if_getstat(const char *ifname, struct if_stat *stat);
295 * Structs to communicate WiFi statistics
296 @@ -200,7 +201,7 @@ struct iw_levelstat {
298 #define IW_LSTAT_INIT { 0, 0, IW_QUAL_LEVEL_INVALID | IW_QUAL_NOISE_INVALID }
300 -extern void iw_getinf_range(char *ifname, struct iw_range *range);
301 +extern void iw_getinf_range(const char *ifname, struct iw_range *range);
302 extern void iw_sanitize(struct iw_range *range,
303 struct iw_quality *qual,
304 struct iw_levelstat *dbm);
305 @@ -250,7 +251,7 @@ struct scan_result {
307 struct scan_result *next;
309 -extern struct scan_result *get_scan_list(int skfd, char *ifname, int weversion);
310 +extern struct scan_result *get_scan_list(int skfd, const char *ifname, int ver);
311 extern void free_scan_result(struct scan_result *head);
314 diff --git a/iw_scan.c b/iw_scan.c
315 index d75ab6b..8423e6a 100644
318 @@ -553,7 +553,7 @@ static int cmp_scan_sig(struct scan_result *a, struct scan_result *b)
319 return a->qual.level - b->qual.level;
322 -struct scan_result *get_scan_list(int skfd, char *ifname, int we_version)
323 +struct scan_result *get_scan_list(int skfd, const char *ifname, int we_version)
325 struct scan_result *head = NULL;
327 diff --git a/scan_scr.c b/scan_scr.c
328 index 879c2f6..b9a4eee 100644
331 @@ -80,9 +80,9 @@ static void display_aplist(WINDOW *w_aplst)
333 err_sys("%s: can not open socket", __func__);
335 - iw_getinf_range(if_list[conf.if_idx], &range);
336 + iw_getinf_range(conf_ifname(), &range);
338 - head = get_scan_list(skfd, if_list[conf.if_idx], range.we_version_compiled);
339 + head = get_scan_list(skfd, conf_ifname(), range.we_version_compiled);
342 } else if (errno == EPERM || !has_net_admin_capability()) {
343 @@ -93,13 +93,13 @@ static void display_aplist(WINDOW *w_aplst)
344 } else if (errno == EINTR || errno == EAGAIN || errno == EBUSY) {
345 /* Ignore temporary errors */
347 - } else if (!if_is_up(skfd, if_list[conf.if_idx])) {
348 - sprintf(s, "Interface '%s' is down ", if_list[conf.if_idx]);
349 + } else if (!if_is_up(skfd, conf_ifname())) {
350 + sprintf(s, "Interface '%s' is down ", conf_ifname());
351 if (!has_net_admin_capability())
352 strcat(s, "- can not scan");
353 - else if (if_set_up(skfd, if_list[conf.if_idx]) < 0)
354 + else if (if_set_up(skfd, conf_ifname()) < 0)
355 sprintf(s, "Can not bring up '%s' for scanning: %s",
356 - if_list[conf.if_idx], strerror(errno));
357 + conf_ifname(), strerror(errno));
359 strcat(s, "- setting it up ...");
360 } else if (errno == EFAULT) {
361 @@ -110,9 +110,9 @@ static void display_aplist(WINDOW *w_aplst)
365 - sprintf(s, "No scan on %s: %s", if_list[conf.if_idx], strerror(errno));
366 + sprintf(s, "No scan on %s: %s", conf_ifname(), strerror(errno));
368 - sprintf(s, "No scan results on %s", if_list[conf.if_idx]);
369 + sprintf(s, "No scan results on %s", conf_ifname());
372 for (i = 1; i <= MAXYLEN; i++)
373 diff --git a/wavemon.h b/wavemon.h
374 index 22a783f..ad0b488 100644
377 @@ -103,7 +103,7 @@ static inline void threshold_action(enum threshold_action action)
378 * Global in-memory representation of current wavemon configuration state
380 extern struct wavemon_conf {
381 - int if_idx; /* Index into if_list */
382 + int if_idx; /* Index into interface list */
386 @@ -256,8 +256,9 @@ static inline int cp_from_scale(float value, const char *cscale, bool reverse)
388 * Wireless interfaces
390 -extern char **if_list;
391 -extern void iw_get_interface_list(void);
392 +extern const char *conf_ifname(void);
393 +extern void conf_get_interface_list(void);
394 +extern char **iw_get_interface_list(void);
395 extern void dump_parameters(void);