-
Notifications
You must be signed in to change notification settings - Fork 2
/
wireless_tools.c
335 lines (257 loc) · 8.06 KB
/
wireless_tools.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
#include "wireless_tools.h"
#include "runcommand.h"
static TNet *WirelessToolsParseCell(const char *Config)
{
char *Token=NULL;
const char *ptr;
TNet *Net;
Net=(TNet *) NetCreate();
ptr=GetToken(Config, "\\S", &Token, 0);
while (ptr)
{
if (strcmp(Token, "Address:")==0)
{
ptr=GetToken(ptr, "\\S", &Net->AccessPoint, 0);
}
ptr=GetToken(ptr, "\\S", &Token, 0);
}
Destroy(Token);
return(Net);
}
// Quality=19/70 Signal level=-91 dBm
static const char *WifiToolsParseQuality(const char *Data, TNet *Net)
{
char *Token=NULL;
const char *ptr;
float Quality, Max;
Quality=(float) strtol(Data, (char **) &ptr, 10);
if (ptr)
{
ptr++;
Max=(float) strtol(ptr, (char **) &ptr, 10);
}
Net->Quality=Quality / Max;
while (isspace(*ptr)) ptr++;
if (strncasecmp(ptr, "Signal level=", 13)==0)
{
ptr=GetToken(ptr+13, "\\S", &Token,0);
Net->dBm=atof(Token);
}
Destroy(Token);
return(ptr);
}
static void WirelessToolsParseBitRates(TNet *Net, const char *Rates)
{
char *Token=NULL, *Tempstr=NULL;
const char *ptr;
ptr=GetToken(Rates, ";", &Token, 0);
while (ptr)
{
Tempstr=FormatStr(Tempstr, "%0.1f ", atof(Token));
Net->BitRates=CopyStr(Net->BitRates, Tempstr);
ptr=GetToken(ptr, ";", &Token, 0);
}
Destroy(Tempstr);
Destroy(Token);
}
static void WirelessToolsGetNetworksParseLine(TNet *Net, const char *Line)
{
char *Key=NULL, *Tempstr=NULL;
const char *ptr;
ptr=GetToken(Line, ":|=", &Key, GETTOKEN_MULTI_SEP);
if (strcmp(Key, "ESSID")==0) NetSetESSID(Net, ptr);
else if (strcmp(Key, "Channel")==0) Net->Channel=atoi(ptr);
else if (strcmp(Key, "Quality")==0) WifiToolsParseQuality(ptr, Net);
else if (strcmp(Key, "Mode")==0)
{
if (strcasecmp(ptr, "ad-hoc")==0) Net->Flags |= NET_ADHOC;
}
else if (strcmp(Key, "IE")==0)
{
if (strstr(ptr, "WPA2")) Net->Flags |=NET_WPA2;
else if (strstr(ptr, "WPA")) Net->Flags |=NET_WPA1;
}
else if (strcmp(Key, "Encryption key")==0)
{
if (strcmp(ptr, "on")==0) Net->Flags |=NET_WEP;
}
else if (strcmp(Key, "Bit Rates")==0) WirelessToolsParseBitRates(Net, ptr);
Destroy(Key);
Destroy(Tempstr);
}
int WirelessToolsGetNetworks(TNetDev *Dev, ListNode *Networks)
{
char *Tempstr=NULL, *Output=NULL;
const char *ptr;
TNet *Net=NULL;
int result=FALSE;
NetSetupInterface(Dev, "", "", "", "");
Tempstr=MCopyStr(Tempstr, "iwlist ", Dev->Name, " scan", NULL);
Output=RunCommand(Output, Tempstr, RUNCMD_ROOT);
if (StrValid(Output))
{
ptr=GetToken(Output, "\n", &Tempstr, 0);
while (ptr)
{
StripLeadingWhitespace(Tempstr);
StripTrailingWhitespace(Tempstr);
if (strncmp(Tempstr, "Cell ", 5)==0)
{
//add previously configured net
if (Net) ListAddNamedItem(Networks, Net->ESSID, Net);
//load next network
Net=WirelessToolsParseCell(Tempstr+5);
result=TRUE;
}
if (Net) WirelessToolsGetNetworksParseLine(Net, Tempstr);
ptr=GetToken(ptr, "\n", &Tempstr, 0);
}
if (Net) ListAddNamedItem(Networks, Net->ESSID, Net);
}
Destroy(Tempstr);
Destroy(Output);
return(result);
}
int WirelessToolsSetupInterface(TNetDev *Dev, TNet *Conf)
{
char *Tempstr=NULL, *Cmd=NULL;
int RetVal=FALSE;
Cmd=MCopyStr(Cmd, "iwconfig ", Dev->Name, " essid \"", Conf->ESSID, "\" ", NULL);
if (Conf->Channel > 0)
{
Tempstr=FormatStr(Tempstr, " chan '%d' ", Conf->Channel);
Cmd=CatStr(Cmd, Tempstr);
}
if (StrValid(Conf->Key))
{
Cmd=CatStr(Cmd, " restricted ");
if (! (Conf->Flags & (NET_WPA1 | NET_WPA2))) Cmd=MCatStr(Cmd, " key ", Conf->Key, NULL);
}
else Cmd=CatStr(Cmd, " key off ");
Tempstr=RunCommand(Tempstr, Cmd, RUNCMD_ROOT);
if (Tempstr != NULL) RetVal=TRUE;
Destroy(Tempstr);
Destroy(Cmd);
return(RetVal);
}
/*
void WifiSetRate(TNetDev *Dev, int Rate)
{
char *Tempstr=NULL;
if (! Dev) return;
if (Rate < 1) return;
Tempstr=FormatStr(Tempstr,"%s %s rate %dM ",IWCONFIG_PATH,Dev->Name,Rate);
RunCommand(Tempstr, RUNCMD_SU);
Destroy(Tempstr);
}
*/
const char *IwConfigToks[]= {"ESSID","Mode","Frequency","Access Point","Cell","Bit Rate","Link Quality","Sensitivity","Retry limit","RTS thr","Fragment thr","Encryption key","Security mode","Signal level","Noise level",NULL};
typedef enum {IWCT_ESSID,IWCT_MODE,IWCT_FREQ,IWCT_AP,IWCT_CELL,IWCT_RATE,IWCT_QUALITY,IWCT_SENS,IWCT_RETRYLIMIT,IWCT_RETRYTHR,IWCT_RTSTHR,IWCT_FRAGTHR,IWCT_ENCKEY,IWCT_SECUREMODE,IWCT_SIGNAL,IWCT_NOISE} EIWConfigToks;
static const char *WirelessToolsGetStatusParseDifficultItems(TNet *Net, const char *Data, const char *Remainder)
{
char *Token=NULL;
const char *ptr;
int val;
ptr=GetToken(Data,"\\S",&Token,0);
while (ptr)
{
while (isspace(*ptr)) ptr++;
StripTrailingWhitespace(Token);
StripLeadingWhitespace(Token);
val=MatchTokenFromList(Token,IwConfigToks,0);
ptr=GetToken(ptr,"\\S",&Token,0);
}
if (val==IWCT_ESSID)
{
ptr=Remainder;
while (isspace(*ptr)) ptr++;
ptr=GetToken(ptr,"\\S",&Token,GETTOKEN_QUOTES);
NetSetESSID(Net, Token);
}
Destroy(Token);
return(ptr);
}
static void WirelessToolsGetStatusParseLine(TNet *Net, char *Line)
{
char *Token=NULL;
const char *ptr;
ptr=Line;
while (ptr)
{
ptr=GetToken(ptr,":|=",&Token,GETTOKEN_MULTI_SEPARATORS|GETTOKEN_QUOTES);
if (! ptr) break;
while (isspace(*ptr)) ptr++;
StripTrailingWhitespace(Token);
StripLeadingWhitespace(Token);
switch (MatchTokenFromList(Token, IwConfigToks, 0))
{
case IWCT_ESSID:
ptr=GetToken(ptr,"\\S",&Token,0);
NetSetESSID(Net, Token);
if (
(! StrValid(Net->ESSID)) ||
(strcasecmp(Net->ESSID,"not-associated")==0)
) Net->Flags &= ~NET_ASSOCIATED;
else Net->Flags |= NET_ASSOCIATED;
break;
case IWCT_MODE:
ptr=GetToken(ptr,"\\S",&Token,0);
if (strcasecmp(Token,"ad-hoc")==0) Net->Flags |= NET_ADHOC;
break;
/*
case IWCT_RATE:
ptr=GetToken(ptr,"\\S",&Token,0);
Net->Rate=CopyStr(Net->Rate,Token);
ptr=GetToken(ptr,"\\S",&Token,0);
Net->Rate=CatStr(Net->Rate,Token);
break;
*/
case IWCT_FREQ:
ptr=GetToken(ptr,"\\S",&Token,0);
// Net->Frequency=CopyStr(Net->Frequency, Token);
ptr=GetToken(ptr,"\\S",&Token,0);
// Net->Frequency=CatStr(Net->Frequency, Token);
break;
case IWCT_AP:
case IWCT_CELL:
ptr=GetToken(ptr,"\\S",&Net->AccessPoint,0);
break;
case IWCT_QUALITY:
ptr=WifiToolsParseQuality(ptr, Net);
break;
case -1:
ptr=WirelessToolsGetStatusParseDifficultItems(Net, Token, ptr);
break;
}
}
Destroy(Token);
}
int WirelessToolsGetStatus(TNetDev *Device, TNet *Net)
{
char *Tempstr=NULL, *Output=NULL, *Line=NULL;
const char *ptr;
int result=FALSE;
if (! Device) return(FALSE);
Tempstr=MCopyStr(Tempstr, "iwconfig ", Device->Name, NULL);
Output=RunCommand(Output, Tempstr, 0);
if (StrValid(Output))
{
ptr=GetToken(Output, "\n", &Line, 0);
while (ptr)
{
StripTrailingWhitespace(Line);
WirelessToolsGetStatusParseLine(Net, Line);
ptr=GetToken(ptr, "\n", &Line, 0);
}
if (
(! StrValid(Net->AccessPoint)) ||
(strcasecmp(Net->AccessPoint,"not-associated")==0)
) Net->Flags &= ~NET_ASSOCIATED;
else Net->Flags |= NET_ASSOCIATED;
result=TRUE;
}
Destroy(Line);
Destroy(Output);
Destroy(Tempstr);
return(result);
}