-
Notifications
You must be signed in to change notification settings - Fork 2
/
interactive.c
479 lines (365 loc) · 12.3 KB
/
interactive.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
#include "interactive.h"
#include "wifi.h"
#include "net.h"
#include "netdev.h"
#include "settings.h"
#define DISPLAY_ALL_NETS 0
#define DISPLAY_KNOWN_NETS 1
#define DISPLAY_OPEN_NETS 2
#define DISPLAY_OTHER_NETS 4
ListNode *InteractiveNetList=NULL;
int InteractiveDisplayMode=0;
void InteractiveHeaders(TNetDev *Dev, TNet *Net, STREAM *Out)
{
char *Line=NULL, *Tempstr=NULL;
WifiGetStatus(Dev, Net);
NetGetStatus(Dev, Net);
Line=MCopyStr(Line, " Iface: ~e", Dev->Name, "~0 ", Net->MacAddress, "\n", NULL);
if (Net->Flags & NET_ASSOCIATED)
{
Tempstr=FormatStr(Tempstr, " Wifi: ~gASSOCIATED~0 ~e%s~0 AP:%s chan:%03d qual:%s%0.1f%%~0\n", Net->ESSID, Net->AccessPoint, Net->Channel, OutputNetQualityColor(Net), Net->Quality * 100.0);
Line=CatStr(Line, Tempstr);
}
else Line=MCatStr(Line, " Wifi: ~rnot associated~0~>\n", NULL);
if (StrValid(Net->Address)) Line=MCatStr(Line, " IP4: ~e", Net->Address, "~0 mask ", Net->Netmask, NULL);
Line=CatStr(Line, "\n\n");
TerminalPutStr(Line, Out);
Destroy(Tempstr);
Destroy(Line);
}
static void InteractiveWifiMenuRefresh(TERMMENU *Menu)
{
ListNode *Curr;
Curr=ListGetNext(Menu->Options);
while (Curr)
{
Curr->Tag=OutputFormatNet(Curr->Tag, (TNet *) Curr->Item);
Curr=ListGetNext(Curr);
}
}
static void InteractiveWifiMenuUpdate(TERMMENU *Menu)
{
//header for menu
TerminalCursorMove(StdIO, 0, 6);
switch (InteractiveDisplayMode)
{
case DISPLAY_OPEN_NETS:
TerminalPutStr(" Open networks that are currently visible:~>", StdIO);
break;
case DISPLAY_KNOWN_NETS:
TerminalPutStr(" Known networks that are currently visible:~>", StdIO);
break;
case DISPLAY_OTHER_NETS:
TerminalPutStr(" Known networks:~>", StdIO);
break;
default:
TerminalPutStr(" Available networks: Those marked with a leading '*' have saved configs~>", StdIO);
break;
}
InteractiveWifiMenuRefresh(Menu);
}
static void InteractiveTitleBar(TNetDev *Dev, const char *Str)
{
TNet *Net;
char *Tempstr=NULL;
TerminalCursorMove(StdIO, 0, 0);
Tempstr=MCopyStr(Tempstr, Str, "~>~0", NULL);
TerminalPutStr(Tempstr, StdIO);
Net=NetCreate();
TerminalCursorMove(StdIO, 0, 2);
InteractiveHeaders(Dev, Net, StdIO);
Destroy(Tempstr);
NetDestroy(Net);
}
static void InteractiveBottomBar(STREAM *StdIO, int wid, int len)
{
TerminalCursorMove(StdIO, 0, len-2);
TerminalPutStr("~B~wKeys: up/down-arrow:select network Enter:join network f:forget network k:known nets o:open nets~>~0", StdIO);
TerminalCursorMove(StdIO, 0, len-1);
TerminalPutStr("~B~w s:Scan again d:disconnect escape-escape:exit i:change interface~>~0", StdIO);
}
static void InteractiveWifiNetworksReload(TERMMENU *Menu, ListNode *Networks)
{
ListNode *Curr;
TNet *Net;
if (ConfiguredNets) ListDestroy(ConfiguredNets, NetDestroy);
ConfiguredNets=SettingsLoadNets(NULL);
ListClear(Menu->Options, NULL);
if (InteractiveDisplayMode == DISPLAY_OTHER_NETS) Curr=ListGetNext(ConfiguredNets);
else Curr=ListGetNext(Networks);
while (Curr)
{
Net=(TNet *) Curr->Item;
if (InteractiveDisplayMode == DISPLAY_KNOWN_NETS)
{
if (ListFindNamedItem(ConfiguredNets, Net->ESSID)) ListAddItem(Menu->Options, Net);
}
else if (InteractiveDisplayMode == DISPLAY_OPEN_NETS)
{
if (! (Net->Flags & NET_ENCRYPTED)) ListAddItem(Menu->Options, Net);
}
else ListAddItem(Menu->Options, Net);
Curr=ListGetNext(Curr);
}
InteractiveWifiMenuUpdate(Menu);
}
static void InteractiveWifiScan(TNetDev *Dev, TERMMENU *Menu)
{
InteractiveTitleBar(Dev, "~R~w Please wait, scanning for wireless networks");
// move cursor in case 'InteractiveWifiScan' asks for a root password,
//otherwise we will overwrite the Title bar with the request
TerminalCursorMove(StdIO, 0, 6);
if (! InteractiveNetList) InteractiveNetList=ListCreate();
InteractiveNetList=WifiGetNetworks(Dev);
//clear any leftover text from password query
TerminalCursorMove(StdIO, 0, 6);
TerminalPutStr("~>\n~>\n~>\n~>\n", StdIO);
InteractiveWifiNetworksReload(Menu, InteractiveNetList);
}
void InteractiveWifiUpdate(TNetDev *Dev, STREAM *StdIO, TERMMENU *Menu, int wid, int len)
{
DisplayStatus=InteractiveTitleBar;
InteractiveBottomBar(StdIO, wid, len);
InteractiveWifiScan(Dev, Menu);
}
int InteractiveQueryNetConfig(TNetDev *Dev, TNet *Net)
{
char *Tempstr=NULL;
TerminalClear(StdIO);
TerminalCursorMove(StdIO, 0, 0);
TerminalPutStr("~M~w Please enter config for wireless network~>~0\n", StdIO);
TerminalPutStr("~e Double-escape to cancel and return to network select~0\n\n", StdIO);
TerminalCursorMove(StdIO, 0, 2);
if (Net->Flags & NET_ENCRYPTED)
{
Net->CountryCode=TerminalReadPrompt(Net->CountryCode, "2-letter country code: ", 0, StdIO);
if (Net->CountryCode==NULL)
{
TerminalClear(StdIO);
Destroy(Tempstr);
return(FALSE);
}
StripCRLF(Net->CountryCode);
TerminalPutStr("\n", StdIO);
Net->UserID=TerminalReadPrompt(Net->UserID, "Username (blank for none): ", 0, StdIO);
if (Net->UserID==NULL)
{
TerminalClear(StdIO);
Destroy(Tempstr);
return(FALSE);
}
StripCRLF(Net->UserID);
TerminalPutStr("\n", StdIO);
Net->Key=TerminalReadPrompt(Net->Key, "Key/password: ", 0, StdIO);
if (Net->Key==NULL)
{
TerminalClear(StdIO);
Destroy(Tempstr);
return(FALSE);
}
StripCRLF(Net->Key);
TerminalPutStr("\n", StdIO);
}
Net->Address=TerminalReadPrompt(Net->Address, "Address (blank for DHCP): ", 0, StdIO);
if (Net->Address==NULL)
{
TerminalClear(StdIO);
Destroy(Tempstr);
return(FALSE);
}
StripCRLF(Net->Address);
TerminalPutStr("\n", StdIO);
if (StrValid(Net->Address))
{
Net->Netmask=TerminalReadPrompt(Net->Netmask, "Netmask: ", 0, StdIO);
if (Net->Netmask==NULL)
{
TerminalClear(StdIO);
Destroy(Tempstr);
return(FALSE);
}
StripCRLF(Net->Netmask);
TerminalPutStr("\n", StdIO);
Net->Gateway=TerminalReadPrompt(Net->Gateway, "Gateway: ", 0, StdIO);
if (Net->Gateway==NULL)
{
TerminalClear(StdIO);
Destroy(Tempstr);
return(FALSE);
}
StripCRLF(Net->Gateway);
TerminalPutStr("\n", StdIO);
Net->DNSServer=TerminalReadPrompt(Net->DNSServer, "DNS Server: ", 0, StdIO);
if (Net->DNSServer==NULL)
{
TerminalClear(StdIO);
Destroy(Tempstr);
return(FALSE);
}
StripCRLF(Net->DNSServer);
}
else Net->Address=CopyStr(Net->Address, "dhcp");
TerminalPutStr("\n", StdIO);
TerminalPutStr("\n", StdIO);
Tempstr=TerminalReadPrompt(Tempstr, "Save for future use? Y/n: ", 0, StdIO);
if (Tempstr==NULL)
{
TerminalClear(StdIO);
return(FALSE);
}
StripLeadingWhitespace(Tempstr);
StripTrailingWhitespace(Tempstr);
if (StrValid(Tempstr) && (tolower(Tempstr[0])=='y') ) SettingsSaveNet(Net);
TerminalClear(StdIO);
Destroy(Tempstr);
return(TRUE);
}
void InteractiveJoinNetwork(TNetDev *Dev, TNet *Conf, STREAM *Out)
{
TNet *Net;
if (! StrValid(Conf->Address))
{
if (! InteractiveQueryNetConfig(Dev, Conf)) return;
}
Net=NetCreate();
InteractiveTitleBar(Dev, "~M~w joining network");
while (1)
{
WifiSetup(Dev, Conf);
usleep(250000);
TerminalCursorMove(Out, 0, 2);
InteractiveHeaders(Dev, Net, Out);
if (Net->Flags & NET_ASSOCIATED)
{
usleep(10000); //if we've just associated then allow some time for the process to complete
NetSetupInterface(Dev, Conf->Address, Conf->Netmask, Conf->Gateway, Conf->DNSServer);
Net->Flags &= ~NET_JOINING;
break;
}
}
InteractiveTitleBar(Dev, "~B~w done");
NetDestroy(Net);
}
void InteractiveChangeInterface(TNetDev *Dev)
{
ListNode *Options, *Curr;
TNetDev *Iface;
char *Tempstr=NULL;
int wid, len, ch;
TerminalClear(StdIO);
InteractiveTitleBar(Dev, "~B~w Change Network Interface");
TerminalGeometry(StdIO, &wid, &len);
TerminalCursorMove(StdIO, 0, 6);
Options=ListCreate();
Curr=ListGetNext(Interfaces);
while (Curr)
{
Iface=(TNetDev *) Curr->Item;
if (Iface->Flags & DEV_WIFI)
{
Tempstr=FormatStr(Tempstr, "% 15s % 10s", Iface->Name, Iface->Driver);
ListAddNamedItem(Options, Tempstr, Iface);
}
Curr=ListGetNext(Curr);
}
Curr=TerminalMenu(StdIO, Options, 2, 7, wid - 4, len - 11);
if (Curr)
{
Iface=(TNetDev *) Curr->Item;
Dev->Name=CopyStr(Dev->Name, Iface->Name);
Dev->Driver=CopyStr(Dev->Driver, Iface->Driver);
Dev->Flags=Iface->Flags;
}
ListDestroy(Options, NULL);
Destroy(Tempstr);
}
void Interactive(TNetDev *iDev)
{
TERMMENU *Menu;
char *Tempstr=NULL, *Line=NULL;
const char *p_qcolor;
ListNode *Curr;
TNetDev *Dev;
TNet *Net;
int wid, len, ch;
int NotExit=TRUE;
Dev=NetDevClone(iDev);
if (ConfiguredNets) ListDestroy(ConfiguredNets, NetDestroy);
ConfiguredNets=SettingsLoadNets(NULL);
TerminalInit(StdIO, TERM_RAWKEYS | TERM_SAVEATTRIBS);
TerminalClear(StdIO);
TerminalGeometry(StdIO, &wid, &len);
Menu=TerminalMenuCreate(StdIO, 2, 7, wid - 4, len - 11);
InteractiveWifiUpdate(Dev, StdIO, Menu, wid, len);
Menu->MenuAttribs=CopyStr(Menu->MenuAttribs, "~N~w");
STREAMSetTimeout(StdIO, 200);
while (NotExit)
{
Tempstr=FormatStr(Tempstr, "~B~w %d wireless networks found", ListSize(Menu->Options));
InteractiveTitleBar(Dev, Tempstr);
TerminalMenuDraw(Menu);
ch=TerminalReadChar(StdIO);
switch (ch)
{
case STREAM_CLOSED:
//stdin/terminal closed, exit gracefully
exit(0);
break;
case ESCAPE:
NotExit=FALSE;
break;
case 's':
InteractiveWifiScan(Dev, Menu);
break;
case 'i':
InteractiveChangeInterface(Dev);
InteractiveWifiUpdate(Dev, StdIO, Menu, wid, len);
break;
case 'd':
NetDown(Dev);
break;
case 'f':
if (Menu->Options->Side)
{
Net=(TNet *) Menu->Options->Side->Item;
SettingsForgetNet(Net->ESSID);
InteractiveWifiNetworksReload(Menu, InteractiveNetList);
}
break;
case 'o':
if (InteractiveDisplayMode != DISPLAY_OPEN_NETS) InteractiveDisplayMode=DISPLAY_OPEN_NETS;
else InteractiveDisplayMode=DISPLAY_ALL_NETS;
InteractiveWifiNetworksReload(Menu, InteractiveNetList);
break;
case 'k':
if (InteractiveDisplayMode != DISPLAY_KNOWN_NETS) InteractiveDisplayMode=DISPLAY_KNOWN_NETS;
else InteractiveDisplayMode=DISPLAY_ALL_NETS;
InteractiveWifiNetworksReload(Menu, InteractiveNetList);
break;
case 'h':
if (InteractiveDisplayMode != DISPLAY_OTHER_NETS) InteractiveDisplayMode=DISPLAY_OTHER_NETS;
else InteractiveDisplayMode=DISPLAY_ALL_NETS;
InteractiveWifiNetworksReload(Menu, InteractiveNetList);
break;
break;
default:
Curr=TerminalMenuOnKey(Menu, ch);
if (Curr)
{
Net=(TNet *) Curr->Item;
SettingsConfigureNet(Net);
InteractiveJoinNetwork(Dev, Net, StdIO);
InteractiveWifiNetworksReload(Menu, InteractiveNetList);
TerminalMenuDraw(Menu);
}
break;
}
}
TerminalClear(StdIO);
TerminalCursorMove(StdIO, 0, 0);
TerminalReset(StdIO);
NetDevDestroy(Dev);
STREAMDestroy(StdIO);
Destroy(Tempstr);
Destroy(Line);
}