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
|
/*
* iwinfo - Wireless Information Library - Linux Wireless Extension Backend
*
* Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
*
* The iwinfo library is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* The iwinfo library 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 the iwinfo library. If not, see http://www.gnu.org/licenses/.
*
* Parts of this code are derived from the Linux wireless tools, iwlib.c,
* iwlist.c and iwconfig.c in particular.
*/
#include "iwinfo.h"
#include "iwinfo_wext.h"
static int ioctl_socket = -1;
static double wext_freq2float(const struct iw_freq *in)
{
int i;
double res = (double) in->m;
for(i = 0; i < in->e; i++) res *= 10;
return res;
}
static int wext_freq2mhz(const struct iw_freq *in)
{
int i, mhz;
if( in->e == 6 )
{
return in->m;
}
else
{
mhz = in->m;
for(i = 0; i < in->e; i++)
mhz *= 10;
return (int)(mhz / 100000);
}
}
static int wext_ioctl(const char *ifname, int cmd, struct iwreq *wrq)
{
/* prepare socket */
if( ioctl_socket == -1 )
ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
return ioctl(ioctl_socket, cmd, wrq);
}
int wext_probe(const char *ifname)
{
struct iwreq wrq;
if(wext_ioctl(ifname, SIOCGIWNAME, &wrq) >= 0)
return 1;
return 0;
}
int wext_get_mode(const char *ifname, char *buf)
{
struct iwreq wrq;
if(wext_ioctl(ifname, SIOCGIWMODE, &wrq) >= 0)
{
switch(wrq.u.mode)
{
case 0:
sprintf(buf, "Auto");
break;
case 1:
sprintf(buf, "Ad-Hoc");
break;
case 2:
sprintf(buf, "Client");
break;
case 3:
sprintf(buf, "Master");
break;
case 4:
sprintf(buf, "Repeater");
break;
case 5:
sprintf(buf, "Secondary");
break;
case 6:
sprintf(buf, "Monitor");
break;
default:
sprintf(buf, "Unknown");
}
return 0;
}
return -1;
}
int wext_get_ssid(const char *ifname, char *buf)
{
struct iwreq wrq;
wrq.u.essid.pointer = (caddr_t) buf;
wrq.u.essid.length = IW_ESSID_MAX_SIZE + 1;
wrq.u.essid.flags = 0;
if(wext_ioctl(ifname, SIOCGIWESSID, &wrq) >= 0)
return 0;
return -1;
}
int wext_get_bssid(const char *ifname, char *buf)
{
struct iwreq wrq;
if(wext_ioctl(ifname, SIOCGIWAP, &wrq) >= 0)
{
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
(uint8_t)wrq.u.ap_addr.sa_data[0], (uint8_t)wrq.u.ap_addr.sa_data[1],
(uint8_t)wrq.u.ap_addr.sa_data[2], (uint8_t)wrq.u.ap_addr.sa_data[3],
(uint8_t)wrq.u.ap_addr.sa_data[4], (uint8_t)wrq.u.ap_addr.sa_data[5]);
return 0;
}
return -1;
}
int wext_get_bitrate(const char *ifname, int *buf)
{
struct iwreq wrq;
if(wext_ioctl(ifname, SIOCGIWRATE, &wrq) >= 0)
{
*buf = wrq.u.bitrate.value;
return 0;
}
return -1;
}
int wext_get_channel(const char *ifname, int *buf)
{
struct iwreq wrq;
if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
{
/* FIXME: iwlib has some strange workarounds here, maybe we need them as well... */
*buf = (int) wext_freq2float(&wrq.u.freq);
return 0;
}
return -1;
}
int wext_get_frequency(const char *ifname, int *buf)
{
struct iwreq wrq;
struct iw_range range;
int i, channel;
if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
{
/* We got a channel number instead ... */
if( wrq.u.freq.m < 1000 )
{
channel = wrq.u.freq.m;
wrq.u.data.pointer = (caddr_t) ⦥
wrq.u.data.length = sizeof(struct iw_range);
wrq.u.data.flags = 0;
if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
{
for(i = 0; i < range.num_frequency; i++)
{
if( range.freq[i].i == channel )
{
*buf = wext_freq2mhz(&range.freq[i]);
return 0;
}
}
}
}
else
{
*buf = wext_freq2mhz(&wrq.u.freq);
return 0;
}
}
return -1;
}
int wext_get_signal(const char *ifname, int *buf)
{
struct iwreq wrq;
struct iw_statistics stats;
wrq.u.data.pointer = (caddr_t) &stats;
wrq.u.data.length = sizeof(struct iw_statistics);
wrq.u.data.flags = 1;
if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
{
*buf = (stats.qual.level - 0x100);
return 0;
}
return -1;
}
int wext_get_noise(const char *ifname, int *buf)
{
struct iwreq wrq;
struct iw_statistics stats;
wrq.u.data.pointer = (caddr_t) &stats;
wrq.u.data.length = sizeof(struct iw_statistics);
wrq.u.data.flags = 1;
if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
{
*buf = (stats.qual.noise - 0x100);
return 0;
}
return -1;
}
int wext_get_quality(const char *ifname, int *buf)
{
struct iwreq wrq;
struct iw_statistics stats;
wrq.u.data.pointer = (caddr_t) &stats;
wrq.u.data.length = sizeof(struct iw_statistics);
wrq.u.data.flags = 1;
if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
{
*buf = stats.qual.qual;
return 0;
}
return -1;
}
int wext_get_quality_max(const char *ifname, int *buf)
{
struct iwreq wrq;
struct iw_range range;
wrq.u.data.pointer = (caddr_t) ⦥
wrq.u.data.length = sizeof(struct iw_range);
wrq.u.data.flags = 0;
if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
{
*buf = range.max_qual.qual;
return 0;
}
return -1;
}
int wext_get_enctype(const char *ifname, char *buf)
{
/* Stub */
return -1;
}
int wext_get_assoclist(const char *ifname, char *buf, int *len)
{
/* Stub */
return -1;
}
|