-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
196 lines (151 loc) · 4.36 KB
/
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
#include "net.h"
#include "netdev.h"
#include "wifi.h"
#include "runcommand.h"
#include "settings.h"
#include "command_line.h"
#include "interactive.h"
#include "qrcode.h"
#include "help.h"
#define ACT_INTERACTIVE 0
#define ACT_ADD 1
#define ACT_JOIN 2
#define ACT_LEAVE 3
#define ACT_LIST 4
#define ACT_SCAN 5
#define ACT_FORGET 6
#define ACT_IFACE_LIST 7
#define ACT_QRCODE 8
#define ACT_HELP 99
void ListInterfaces()
{
ListNode *Curr, *Networks;
TNetDev *Iface;
printf("Interfaces: \n");
Curr=ListGetNext(Interfaces);
while (Curr)
{
Iface=(TNetDev *) Curr->Item;
if (Iface->Flags & DEV_WIFI) printf("% 15s %s\n", Iface->Name, Iface->Driver);
Curr=ListGetNext(Curr);
}
}
void ListNetworks()
{
ListNode *Curr, *Networks;
TNet *Net;
printf("Configured networks: \n");
Networks=SettingsLoadNets(NULL);
Curr=ListGetNext(Networks);
while (Curr)
{
Net=(TNet *) Curr->Item;
printf("% 15s ip:%s netmask:%s gateway:%s dns:%s\n", Net->ESSID, Net->Address, Net->Netmask, Net->Gateway, Net->DNSServer);
Curr=ListGetNext(Curr);
}
}
void ScanForNetworks(TNetDev *Dev)
{
ListNode *Networks, *Curr;
char *Tempstr=NULL, *Output=NULL;
TNet *Net;
//globally visible, used by OutputFormatNet
ConfiguredNets=SettingsLoadNets(NULL);
Networks=WifiGetNetworks(Dev);
Curr=ListGetNext(Networks);
while (Curr)
{
Net=(TNet *) Curr->Item;
Output=OutputFormatNet(Output, Net);
Output=CatStr(Output, "\n");
TerminalPutStr(Output, StdIO);
Curr=ListGetNext(Curr);
}
ListDestroy(ConfiguredNets, NetDestroy);
Destroy(Tempstr);
Destroy(Output);
}
void PerformAction(int Action, TNetDev *Dev, TNet *Conf)
{
char *Tempstr=NULL;
TNet *Net;
switch (Action)
{
case ACT_ADD:
if (! StrValid(Conf->Key)) Conf->Key=TerminalReadPrompt(Conf->Key, "Key/password: ", 0, StdIO);
SettingsConfigureNet(Conf);
SettingsSaveNet(Conf);
break;
case ACT_JOIN:
SettingsConfigureNet(Conf);
if (Dev->Flags & DEV_WIFI)
{
if (! StrValid(Conf->Key)) Conf->Key=TerminalReadPrompt(Conf->Key, "Key/password: ", 0, StdIO);
printf("Configure wifi: dev:%s essid:%s\n", Conf->Interface, Conf->ESSID);
WifiSetup(Dev, Conf);
}
Net=(TNet *) calloc(1, sizeof(TNet));
while (1)
{
WifiGetStatus(Dev, Net);
if (Net->Flags & NET_ASSOCIATED) break;
}
NetDestroy(Net);
printf("Configure IPv4: ip:%s netmask:%s gw:%s dns:%s\n", Conf->Address, Conf->Netmask, Conf->Gateway, Conf->DNSServer);
NetSetupInterface(Dev, Conf->Address, Conf->Netmask, Conf->Gateway, Conf->DNSServer);
if (Conf->Flags & NET_STORE) SettingsSaveNet(Conf);
PerformAction(ACT_STATUS, Dev, Conf);
break;
case ACT_STATUS:
Net=(TNet *) calloc(1, sizeof(TNet));
InteractiveHeaders(Dev, Net, StdIO);
NetDestroy(Net);
break;
case ACT_QRCODE:
SettingsConfigureNet(Conf);
DisplayQRCode(Conf);
break;
case ACT_FORGET:
SettingsForgetNet(Conf->ESSID);
break;
case ACT_LIST:
ListNetworks();
break;
case ACT_SCAN:
ScanForNetworks(Dev);
break;
case ACT_LEAVE:
NetDown(Dev);
break;
case ACT_INTERACTIVE:
Interactive(Dev);
break;
case ACT_IFACE_LIST:
ListInterfaces();
break;
case ACT_VERSION:
printf("term_wifi %s\n", VERSION);
break;
case ACT_HELP:
DisplayHelp();
break;
}
Destroy(Tempstr);
}
int main(int argc, char *argv[])
{
ListNode *Networks;
TNet *Conf;
int Action;
TNetDev *Dev;
StdIO=STREAMFromDualFD(0, 1);
SettingsInit();
CommandsInit();
Interfaces=ListCreate();
NetDevLoadInterfaces(Interfaces);
Conf=NetCreate();
Action=ParseCommandLine(argc, argv, Conf);
Dev=NetDevSelectInterface(Interfaces, Conf->Interface);
if (Dev) PerformAction(Action, Dev, Conf);
return(0);
}