-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathdisplay-main.c
571 lines (476 loc) · 15.5 KB
/
display-main.c
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
/* horst - Highly Optimized Radio Scanning Tool
*
* Copyright (C) 2005-2016 Bruno Randolf (br1@einfach.org)
*
* This program 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
* of the License, or (at your option) any later version.
*
* This program 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/******************* MAIN / OVERVIEW *******************/
#include <stdlib.h>
#include <string.h>
#include <uwifi/wlan80211.h>
#include <uwifi/wlan_util.h>
#include <uwifi/channel.h>
#include <uwifi/node.h>
#include <uwifi/essid.h>
#include "display.h"
#include "main.h"
#include "hutil.h"
#include "olsr_header.h"
#include "batman_adv_header-14.h"
#include "listsort.h"
static WINDOW *sort_win = NULL;
static WINDOW *dump_win = NULL;
static WINDOW *list_win = NULL;
static WINDOW *stat_win = NULL;
static int do_sort = 'n';
/* pointer to the sort function */
static int(*sortfunc)(const struct cc_list_node*, const struct cc_list_node*) = NULL;
/* sizes of split window (list_win & status_win) */
static int win_split;
static int stat_height;
static struct ewma usen_avg;
static struct ewma bpsn_avg;
/******************* UTIL *******************/
void print_dump_win(const char *str, int color, bool refresh)
{
wattron(dump_win, color);
wprintw(dump_win, "%s", str);
wattroff(dump_win, color);
if (refresh)
wrefresh(dump_win);
else
wnoutrefresh(dump_win);
}
/******************* SORTING *******************/
static int compare_nodes_signal(const struct cc_list_node *p1, const struct cc_list_node *p2)
{
struct uwifi_node* n1 = cc_list_entry(p1, struct uwifi_node, list);
struct uwifi_node* n2 = cc_list_entry(p2, struct uwifi_node, list);
if (n1->phy_sig_last > n2->phy_sig_last)
return -1;
else if (n1->phy_sig_last == n2->phy_sig_last)
return 0;
else
return 1;
}
static int compare_nodes_time(const struct cc_list_node *p1, const struct cc_list_node *p2)
{
struct uwifi_node* n1 = cc_list_entry(p1, struct uwifi_node, list);
struct uwifi_node* n2 = cc_list_entry(p2, struct uwifi_node, list);
if (n1->last_seen > n2->last_seen)
return -1;
else if (n1->last_seen == n2->last_seen)
return 0;
else
return 1;
}
static int compare_nodes_channel(const struct cc_list_node *p1, const struct cc_list_node *p2)
{
struct uwifi_node* n1 = cc_list_entry(p1, struct uwifi_node, list);
struct uwifi_node* n2 = cc_list_entry(p2, struct uwifi_node, list);
if (n1->wlan_channel < n2->wlan_channel)
return 1;
else if (n1->wlan_channel == n2->wlan_channel)
return 0;
else
return -1;
}
static int compare_nodes_bssid(const struct cc_list_node *p1, const struct cc_list_node *p2)
{
struct uwifi_node* n1 = cc_list_entry(p1, struct uwifi_node, list);
struct uwifi_node* n2 = cc_list_entry(p2, struct uwifi_node, list);
return -memcmp(n1->wlan_bssid, n2->wlan_bssid, WLAN_MAC_LEN);
}
static bool sort_input(int c)
{
switch (c) {
case 'n': case 'N': sortfunc = NULL; break;
case 's': case 'S': sortfunc = compare_nodes_signal; break;
case 't': case 'T': sortfunc = compare_nodes_time; break;
case 'c': case 'C': sortfunc = compare_nodes_channel; break;
case 'b': case 'B': sortfunc = compare_nodes_bssid; break;
}
switch (c) {
case 'n': case 'N':
case 's': case 'S':
case 't': case 'T':
case 'c': case 'C':
case 'b': case 'B':
do_sort = c;
/* fallthru */
case '\r': case KEY_ENTER:
delwin(sort_win);
sort_win = NULL;
update_display(NULL);
return true;
}
return false;
}
static void show_sort_win(void)
{
if (sort_win == NULL) {
sort_win = newwin(1, COLS-2, win_split - 2, 1);
wattron(sort_win, BLACKONWHITE);
mvwhline(sort_win, 0, 0, ' ', COLS);
mvwprintw(sort_win, 0, 0, " -> Sort by s:Signal t:Time b:BSSID c:Channel n:Don't sort [current: %c]", do_sort);
wrefresh(sort_win);
}
}
/******************* WINDOWS *******************/
#define STAT_WIDTH 11
#define STAT_START 4
static void update_status_win(struct uwifi_packet* p)
{
int sig, siga, bps, dps, pps, rps, bpsn, usen;
float use, rpsp = 0.0;
int max_stat_bar = stat_height - STAT_START;
struct channel_info* chan = NULL;
if (p != NULL)
werase(stat_win);
wattron(stat_win, WHITE);
mvwvline(stat_win, 0, 0, ACS_VLINE, stat_height);
get_per_second(stats.bytes, stats.duration, stats.packets, stats.retries,
&bps, &dps, &pps, &rps);
bps *= 8;
bpsn = normalize(bps, conf.intf.max_phy_rate * 100000 / 3 * 2, max_stat_bar);
use = dps * 1.0 / 10000; /* usec, in percent */
usen = normalize(use, 100, max_stat_bar);
if (pps)
rpsp = rps * 100.0 / pps;
ewma_add(&usen_avg, usen);
ewma_add(&bpsn_avg, bpsn);
if (p != NULL) {
sig = normalize_db(-p->phy_signal, max_stat_bar);
if (p->pkt_chan_idx > 0)
chan = &spectrum[p->pkt_chan_idx];
if (chan != NULL && chan->packets >= 8)
siga = normalize_db(ewma_read(&chan->signal_avg),
max_stat_bar);
else
siga = sig;
wattron(stat_win, GREEN);
mvwprintw(stat_win, 0, 1, "Sig: %5d", p->phy_signal);
signal_average_bar(stat_win, sig, siga, STAT_START, 2, stat_height, 2);
}
wattron(stat_win, CYAN);
mvwprintw(stat_win, 1, 1, "bps:%6s", kilo_mega_ize(bps));
general_average_bar(stat_win, bpsn, ewma_read(&bpsn_avg),
stat_height, 5, 2,
CYAN, ALLCYAN);
wattron(stat_win, YELLOW);
mvwprintw(stat_win, 2, 1, "Use:%5.1f%%", use);
general_average_bar(stat_win, usen, ewma_read(&usen_avg),
stat_height, 8, 2,
YELLOW, ALLYELLOW);
mvwprintw(stat_win, 3, 1, "Retry: %2.0f%%", rpsp);
wnoutrefresh(stat_win);
}
#define COL_PKT 3
#define COL_CHAN COL_PKT + 7
#define COL_SIG COL_CHAN + 4
#define COL_MODE COL_SIG + 4
#define COL_SOURCE COL_MODE + 3
#define COL_WIDTH COL_SOURCE + 18
#define COL_ENC COL_WIDTH + 11
#define COL_INFO COL_ENC + 6
static char spin[4] = {'/', '-', '\\', '|'};
static bool print_node_list_line(int line, struct uwifi_node* n)
{
if (conf.filter_mode != 0 && (n->wlan_mode & conf.filter_mode) == 0)
return false;
if (n->pkt_types & PKT_TYPE_OLSR)
wattron(list_win, GREEN);
if (n->last_seen > (time_mono.tv_sec - conf.node_timeout / 2))
wattron(list_win, A_BOLD);
else
wattron(list_win, A_NORMAL);
if (n->essid != NULL && n->essid->split > 0)
wattron(list_win, RED);
mvwprintw(list_win, line, 1, "%c", spin[n->pkt_count % 4]);
mvwprintw(list_win, line, COL_PKT, "%.0f/%.0f%%",
n->pkt_count * 100.0 / stats.packets,
n->wlan_retries_all * 100.0 / n->pkt_count);
if (n->wlan_channel)
mvwprintw(list_win, line, COL_CHAN, "%3d", n->wlan_channel );
mvwprintw(list_win, line, COL_SIG, "%3d", -ewma_read(&n->phy_sig_avg));
wmove(list_win, line, COL_MODE);
if (n->wlan_mode & WLAN_MODE_AP)
wprintw(list_win, "AP");
if (n->wlan_mode & WLAN_MODE_IBSS)
wprintw(list_win, "AD");
if (n->wlan_mode & WLAN_MODE_STA)
wprintw(list_win, "ST");
if (n->wlan_mode & WLAN_MODE_4ADDR)
wprintw(list_win, "4A");
mvwprintw(list_win, line, COL_SOURCE, "%-17s", mac_name_lookup(n->wlan_src, 0));
mvwprintw(list_win, line, COL_WIDTH, "%-2s %-3s",
wlan_80211std_str(n->wlan_std),
(n->wlan_chan_width == CHAN_WIDTH_UNSPEC ||
n->wlan_chan_width == CHAN_WIDTH_20_NOHT) ? "20" :
uwifi_channel_width_string_short(n->wlan_chan_width, n->wlan_ht40plus));
if (n->wlan_rx_streams)
wprintw(list_win, " %dx%d", n->wlan_tx_streams, n->wlan_rx_streams);
if (n->wlan_rsn && n->wlan_wpa)
mvwprintw(list_win, line, COL_ENC, "WPA12");
else if (n->wlan_rsn)
mvwprintw(list_win, line, COL_ENC, "WPA2");
else if (n->wlan_wpa)
mvwprintw(list_win, line, COL_ENC, "WPA1");
else if (n->wlan_wep)
mvwprintw(list_win, line, COL_ENC, "WEP?");
wmove(list_win, line, COL_INFO);
if (n->wlan_mode & (WLAN_MODE_IBSS | WLAN_MODE_AP)) {
wprintw(list_win, "TSF %016llx", n->wlan_tsf);
wprintw(list_win, " (%d) ", n->wlan_bintval);
}
if (n->pkt_types & PKT_TYPE_OLSR)
wprintw(list_win, "OLSR N:%d ", n->olsr_neigh);
if (n->pkt_types & PKT_TYPE_BATMAN)
wprintw(list_win, "BATMAN %s ", n->bat_gw ? "GW " : "");
if (n->pkt_types & (PKT_TYPE_MESHZ))
wprintw(list_win, "MC ");
if (!(n->wlan_mode & WLAN_MODE_AP) && n->pkt_types & PKT_TYPE_IP)
wprintw(list_win, "%s", ip_sprintf(n->ip_src));
wattroff(list_win, A_BOLD);
wattroff(list_win, GREEN);
wattroff(list_win, RED);
return true;
}
#define LINE_INC(_l) if (++line > win_split - 1) goto out;
static void update_node_list_win(void)
{
struct essid_info* e;
struct uwifi_node* n, *m;
int line = 1;
werase(list_win);
wattron(list_win, WHITE);
box(list_win, 0 , 0);
mvwprintw(list_win, 0, COL_PKT, "Pk/Re%%");
mvwprintw(list_win, 0, COL_CHAN, "Cha");
mvwprintw(list_win, 0, COL_SIG, "Sig");
mvwprintw(list_win, 0, COL_MODE, "AP");
mvwprintw(list_win, 0, COL_SOURCE, "TRANSMITTER");
mvwprintw(list_win, 0, COL_WIDTH, "ST-MHz-TxR");
mvwprintw(list_win, 0, COL_ENC, "ENCR");
mvwprintw(list_win, 0, COL_INFO, "INFO");
/* reuse bottom line for information on other win */
mvwprintw(list_win, win_split - 1, 0, "Cha-Sig");
wprintw(list_win, "-RAT-TRANSMITTER");
mvwprintw(list_win, win_split - 1, 30, "(BSSID)");
mvwprintw(list_win, win_split - 1, 50, "TYPE");
mvwprintw(list_win, win_split - 1, 57, "INFO");
mvwprintw(list_win, win_split - 1, COLS-10, "LiveStatus");
if (sortfunc)
listsort(&conf.intf.wlan_nodes.n, sortfunc);
/* All ESSIDs */
cc_list_for_each(&essids, e, list) {
wattron(list_win, GREEN | A_BOLD);
mvwprintw(list_win, line, 1, "ESSID: '%s'", e->essid);
if (e->split > 0) {
wattron(list_win, RED);
wprintw(list_win, " *** SPLIT ***");
}
wattroff(list_win, GREEN | A_BOLD);
LINE_INC(line);
/* All APs/IBSS of ESSID */
cc_list_for_each(&e->nodes, n, essid_nodes) {
if (print_node_list_line(line, n))
LINE_INC(line);
/* All STAs associated to AP */
cc_list_for_each(&n->ap_nodes, m, ap_list) {
if (print_node_list_line(line, m))
LINE_INC(line);
}
}
}
/* finally print all nodes which can not be associated to an AP or ESSID */
int lline = line;
int count = 0;
LINE_INC(line);
cc_list_for_each(&conf.intf.wlan_nodes, n, list) {
if (n->ap_node != NULL || n->essid != NULL)
continue;
count++;
if (print_node_list_line(line, n))
LINE_INC(line);
}
if (count > 0) {
wattron(list_win, GREEN | A_BOLD);
mvwprintw(list_win, lline, 1, "NO ESSID:");
wattroff(list_win, GREEN | A_BOLD);
}
out:
wnoutrefresh(list_win);
}
void update_dump_win(struct uwifi_packet* p)
{
if (!p) {
redrawwin(dump_win);
wnoutrefresh(dump_win);
return;
}
wattron(dump_win, get_packet_type_color(p->wlan_type));
if (p->pkt_types & PKT_TYPE_IP)
wattron(dump_win, A_BOLD);
if (p->phy_flags & PHY_FLAG_BADFCS)
wattron(dump_win, RED);
wprintw(dump_win, "\n%3d ", p->wlan_channel);
wprintw(dump_win, "%03d ", p->phy_signal);
wprintw(dump_win, "%3d ", p->phy_rate/10);
wprintw(dump_win, "%-17s ", mac_name_lookup(p->wlan_ta, 0));
wprintw(dump_win, "(" MAC_FMT ") ", MAC_PAR(p->wlan_bssid));
if (p->phy_flags & PHY_FLAG_BADFCS) {
wprintw(dump_win, "*BADFCS* ");
return;
}
if ((p->pkt_types & PKT_TYPE_BATMAN) && p->bat_packet_type == BAT_UNICAST) {
/* unicast traffic can carry IP/ICMP which we show below */
wprintw(dump_win, "BATMAN ");
}
if (p->pkt_types & PKT_TYPE_OLSR) {
wprintw(dump_win, "%-7s%s ", "OLSR", ip_sprintf(p->ip_src));
switch (p->olsr_type) {
case HELLO_MESSAGE: wprintw(dump_win, "HELLO"); break;
case TC_MESSAGE: wprintw(dump_win, "TC"); break;
case MID_MESSAGE: wprintw(dump_win, "MID");break;
case HNA_MESSAGE: wprintw(dump_win, "HNA"); break;
case LQ_HELLO_MESSAGE: wprintw(dump_win, "LQ_HELLO"); break;
case LQ_TC_MESSAGE: wprintw(dump_win, "LQ_TC"); break;
default: wprintw(dump_win, "(%d)", p->olsr_type);
}
}
else if ((p->pkt_types & PKT_TYPE_BATMAN) && p->bat_packet_type != BAT_UNICAST) {
wprintw(dump_win, "BATMAN ");
switch (p->bat_packet_type) {
case BAT_OGM: wprintw(dump_win, "OGM"); break;
case BAT_ICMP: wprintw(dump_win, "BAT_ICMP"); break;
case BAT_BCAST: wprintw(dump_win, "BCAST"); break;
case BAT_VIS: wprintw(dump_win, "VIS"); break;
case BAT_UNICAST_FRAG: wprintw(dump_win, "FRAG"); break;
case BAT_TT_QUERY: wprintw(dump_win, "TT_QUERY"); break;
case BAT_ROAM_ADV: wprintw(dump_win, "ROAM_ADV"); break;
default: wprintw(dump_win, "UNKNOWN %d", p->bat_packet_type);
}
}
else if (p->pkt_types & PKT_TYPE_MESHZ) {
wprintw(dump_win, "%-7s%s",
p->tcpudp_port == 9256 ? "MC_NBR" : "MC_RT",
ip_sprintf(p->ip_src));
wprintw(dump_win, " -> %s", ip_sprintf(p->ip_dst));
}
else if (p->pkt_types & PKT_TYPE_UDP) {
wprintw(dump_win, "%-7s%s", "UDP", ip_sprintf(p->ip_src));
wprintw(dump_win, " -> %s", ip_sprintf(p->ip_dst));
}
else if (p->pkt_types & PKT_TYPE_TCP) {
wprintw(dump_win, "%-7s%s", "TCP", ip_sprintf(p->ip_src));
wprintw(dump_win, " -> %s", ip_sprintf(p->ip_dst));
}
else if (p->pkt_types & PKT_TYPE_ICMP) {
wprintw(dump_win, "%-7s%s", "PING", ip_sprintf(p->ip_src));
wprintw(dump_win, " -> %s", ip_sprintf(p->ip_dst));
}
else if (p->pkt_types & PKT_TYPE_IP) {
wprintw(dump_win, "%-7s%s", "IP", ip_sprintf(p->ip_src));
wprintw(dump_win, " -> %s", ip_sprintf(p->ip_dst));
}
else if (p->pkt_types & PKT_TYPE_ARP) {
wprintw(dump_win, "%-7s", "ARP", ip_sprintf(p->ip_src));
}
else {
wprintw(dump_win, "%-7s", wlan_get_packet_type_name(p->wlan_type));
switch (p->wlan_type) {
case WLAN_FRAME_DATA:
case WLAN_FRAME_DATA_CF_ACK:
case WLAN_FRAME_DATA_CF_POLL:
case WLAN_FRAME_DATA_CF_ACKPOLL:
case WLAN_FRAME_QDATA:
case WLAN_FRAME_QDATA_CF_ACK:
case WLAN_FRAME_QDATA_CF_POLL:
case WLAN_FRAME_QDATA_CF_ACKPOLL:
if ( p->wlan_wep == 1)
wprintw(dump_win, "ENCRYPTED");
break;
case WLAN_FRAME_CTS:
case WLAN_FRAME_RTS:
case WLAN_FRAME_ACK:
case WLAN_FRAME_BLKACK:
case WLAN_FRAME_BLKACK_REQ:
wprintw(dump_win, "%-17s", mac_name_lookup(p->wlan_ra, 0));
break;
case WLAN_FRAME_BEACON:
case WLAN_FRAME_PROBE_RESP:
wprintw(dump_win, "'%s' %llx", p->wlan_essid,
p->wlan_tsf);
break;
case WLAN_FRAME_PROBE_REQ:
wprintw(dump_win, "'%s'", p->wlan_essid);
break;
}
}
if (p->wlan_retry)
wprintw(dump_win, " [r]");
wattroff(dump_win, A_BOLD);
}
void update_main_win(struct uwifi_packet *p)
{
update_node_list_win();
update_status_win(p);
update_dump_win(p);
wnoutrefresh(dump_win);
if (sort_win != NULL) {
redrawwin(sort_win);
wnoutrefresh(sort_win);
}
}
bool main_input(int key)
{
if (sort_win != NULL)
return sort_input(key);
switch(key) {
case 'o': case 'O':
show_sort_win();
return true;
}
return false;
}
void init_display_main(void)
{
win_split = LINES / 2 + 1;
stat_height = LINES - win_split - 1;
list_win = newwin(win_split, COLS, 0, 0);
scrollok(list_win, FALSE);
stat_win = newwin(stat_height, STAT_WIDTH, win_split, COLS - STAT_WIDTH);
scrollok(stat_win, FALSE);
dump_win = newwin(stat_height, COLS - STAT_WIDTH, win_split, 0);
scrollok(dump_win, TRUE);
ewma_init(&usen_avg, 1024, 8);
ewma_init(&bpsn_avg, 1024, 8);
}
void resize_display_main(void)
{
win_split = LINES / 2 + 1;
stat_height = LINES - win_split - 1;
wresize(list_win, win_split, COLS);
wresize(dump_win, stat_height, COLS - STAT_WIDTH);
mvwin(dump_win, win_split, 0);
wresize(stat_win, stat_height, STAT_WIDTH);
mvwin(stat_win, win_split, COLS - STAT_WIDTH);
}
void clear_display_main(void)
{
werase(dump_win);
werase(stat_win);
}