]> git.decadent.org.uk Git - ap-utils.git/blob - src/stations.c
Pass correct platform options to configure
[ap-utils.git] / src / stations.c
1 /*
2  *      stations.c from Access Point SNMP Utils for Linux
3  *
4  * Copyright (c) 2002 Roman Festchook <roma at polesye dot net>
5  * Copyright (c) 2005 Jan Rafaj <jr-aputils at cedric dot unob dot cz>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License Version 2 from
9  * June 1991 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 #include <sys/wait.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <string.h>
27 #include "ap-utils.h"
28
29 #define MAX_LINES LINES-4
30
31 extern int LINES;
32 extern WINDOW *main_sub;
33 extern short ap_type, ap_vendorext;
34 extern int sts_viewtype;
35
36 void atmel_stations()
37 {
38     char bridgeOperationalMode[] = {
39         0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x04, 0x01, 0x00
40     };
41     char AssociatedSTAsNum[] = {
42         0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x05, 0x01, 0x00
43     };
44     char AssociatedSTAsInfo[] = {
45         0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x05, 0x02, 0x00
46     };
47
48     /* AP mode: connected APClients info */
49
50     struct AssociatedSTAsInfo_ATMEL410 ap_410;
51     struct AssociatedSTAsInfo_ATMEL12350 ap_12350;
52
53     struct MacListStat *first = NULL, *curr = NULL;
54     char message[1024];
55     int mac_idx, begin, end, total_mac;
56     varbind varbinds[1];
57
58
59     if (ap_type == ATMEL12350) {
60         bridgeOperationalMode[5] = 0xE0;
61         bridgeOperationalMode[6] = 0x3E;
62         AssociatedSTAsNum[5] = 0xE0;
63         AssociatedSTAsNum[6] = 0x3E;
64         AssociatedSTAsInfo[5] = 0xE0;
65         AssociatedSTAsInfo[6] = 0x3E;
66     }   
67
68     /* find out mode the device is currently in */
69     varbinds[0].oid = bridgeOperationalMode;
70     varbinds[0].len_oid = sizeof(bridgeOperationalMode);
71     varbinds[0].value = bridgeOperationalMode;
72     varbinds[0].len_val = 0;
73     varbinds[0].type = NULL_VALUE;
74     print_help(WAIT_RET);
75     if (snmp(varbinds, 1, GET) <= 0) {
76           print_helperr(ERR_RET);
77           goto exit;
78     }
79
80     /* Rule out all modes except AP(2). */
81     if (*(varbinds[0].value) != 2) {
82         mvwaddstr(main_sub, 1, 1, _("Not available (device not in AP mode)."));
83         print_help(ANY_KEY);
84         wrefresh(main_sub);
85         goto exit;
86     }
87
88     noecho();
89
90     wattrset(main_sub, COLOR_PAIR(13));
91     mvwaddstr(main_sub, 0, 0,
92         _("  #     MAC       Parent MAC  RSSI   LQ Sts MACn      IP        "));
93     wattrset(main_sub, A_NORMAL);
94
95 refresh:
96     /* find out how many STAtions is in the list */
97     varbinds[0].oid = AssociatedSTAsNum;
98     varbinds[0].len_oid = sizeof(AssociatedSTAsNum);
99     varbinds[0].value = AssociatedSTAsNum;
100     varbinds[0].type = NULL_VALUE;
101     varbinds[0].len_val = 0;
102
103     print_help(WAIT_RET);
104
105     if (snmp(varbinds, 1, GET) <= 0) {
106         print_helperr(ERR_RET);
107         goto exit;
108     }
109
110     total_mac = *(varbinds[0].value);
111     print_help(WAIT_SET);
112     mac_idx = 1;
113
114     while (mac_idx <= total_mac) {
115         /*
116          * Tell the AP we want mac_idx-th AssociatedSTAsInfo structure.
117          * Note: since:
118          * - position of 'Index' member in ap_* struct is always the same
119          * - only the 'Index' member is actually to be used
120          * - both structures have same size (24)
121          * we may pick *any* ap_* struct here.
122          */
123         varbinds[0].oid = AssociatedSTAsInfo;
124         varbinds[0].len_oid = sizeof(AssociatedSTAsInfo);
125         varbinds[0].type = INT_VALUE;
126         ap_410.Index = swap2(mac_idx);
127         varbinds[0].value = (char *)&ap_410;
128         varbinds[0].len_val = sizeof(ap_410);
129
130         if (snmp(varbinds, 1, SET) <= 0) {
131             print_helperr(ERR_RET);
132             goto exit;
133         }
134
135         if (varbinds[0].len_val != 24) {
136             print_helperr(_("AssociatedSTAsInfo packet error"));
137             goto exit;
138         }
139
140         if (first == NULL) {
141             first =
142                 (struct MacListStat *) calloc(1, sizeof(struct MacListStat));
143             curr = first;
144         } else {
145             curr->next =
146                 (struct MacListStat *) calloc(1, sizeof(struct MacListStat));
147             curr = curr->next;
148         }
149
150         /* lets not use black magic (casting) here, ok? */
151         if (ap_type == ATMEL410) {
152             memcpy(&ap_410, varbinds[0].value, sizeof(ap_410));
153
154             memcpy(curr->addr, ap_410.MacAddress, 6);
155             if (ap_vendorext == SBRIDGES) {
156                 memcpy(curr->ParentMacAddress, ap_410.ParentMacAddress, 6);
157                 memcpy(&(curr->IP.s_addr), ap_410.IP, 4);
158                 curr->rssi = ap_410.RSSI;
159                 curr->quality = ap_410.LinkQuality;
160                 curr->Status = ap_410.Status;
161                 curr->Port = ap_410.Port;
162             }
163         } else { /* ap_type == ATMEL12350 */
164             memcpy(&ap_12350, varbinds[0].value, sizeof(ap_12350));
165
166             memcpy(curr->addr, ap_12350.MacAddress, 6);
167             if (ap_vendorext == EZYNET) {
168                 memcpy(curr->ParentMacAddress, ap_12350.ParentMacAddress, 6);
169                 memcpy(&(curr->IP.s_addr), ap_12350.IP, 4);
170                 curr->rssi = ap_12350.RSSI;
171                 /* curr->quality stays empty */
172                 curr->Status = ap_12350.Status;
173                 curr->Port = ap_12350.Port;
174             }
175         }
176
177         curr->next = NULL;
178         mac_idx++;
179     }
180
181     begin = 1;
182     end = (MAX_LINES < mac_idx) ? MAX_LINES : mac_idx;
183
184     sprintf(message, "%s: %d", ST_TITLE, total_mac);
185     while (1) {
186         if ((ap_type == ATMEL410 && ap_vendorext == SBRIDGES) ||
187             (ap_type == ATMEL12350 && ap_vendorext == EZYNET)) {
188             print_top_rssi(message);
189             print_help(_("Arrows - scroll; S - save to file; Q - return; "
190                          "T - toggle view; Other - refresh"));
191         } else {
192             print_top(NULL, message);
193             print_help(_("Arrows - scroll; S - save to file; Q - return; "
194                          "Other key - refresh"));
195         }
196         scroll_rows(first, begin, end, 1, 2);
197
198         switch (getch()) {
199             case 'S':
200             case 's':
201                 save_Stations(first);
202                 continue;
203             case KEY_RIGHT:
204             case KEY_DOWN:
205                 if (end < mac_idx) {
206                     begin++;
207                     end++;
208                 }
209                 continue;
210             case KEY_UP:
211             case KEY_LEFT:
212                 if (begin > 1) {
213                     begin--;
214                     end--;
215                 }
216                 continue;
217             case 'Q':
218             case 'q':
219                 goto quit;
220             case 'T':
221             case 't':
222                 if ((ap_type == ATMEL410 && ap_vendorext == SBRIDGES) ||
223                     (ap_type == ATMEL12350 && ap_vendorext == EZYNET)) {
224                     sts_viewtype += 1;
225                     if (sts_viewtype == 3)
226                         sts_viewtype = 0;
227                 }
228                 continue;
229             default:
230                 while ((curr = first)) {
231                     first = curr->next;
232                     free(curr);
233                 }
234                 first = curr = NULL;
235                 goto refresh;
236         }
237     }
238
239 exit:
240     getch();
241 quit:
242     while ((curr = first)) {
243         first = curr->next;
244         free(curr);
245     }
246     print_top(NULL, NULL);
247     clear_main(0);
248 }
249
250 void nwn_stations()
251 {
252     unsigned char Mac[] = {
253         0x2b, 0x06, 0x01, 0x04, 0x01, 0x87, 0x29, 0x03, 0x01, 0x03, 0x01,
254         0x02, 0x01, 0x02, 0x80, 0x00 };
255     unsigned char Quality[] = {
256         0x2b, 0x06, 0x01, 0x04, 0x01, 0x87, 0x29, 0x03, 0x01, 0x03, 0x01,
257         0x02, 0x01, 0x03, 0x80, 0x00 };
258     unsigned char Age[] = {
259         0x2b, 0x06, 0x01, 0x04, 0x01, 0x87, 0x29, 0x03, 0x01, 0x03, 0x01,
260         0x02, 0x01, 0x04, 0x80, 0x00 };
261     unsigned char RSSI[] = {
262         0x2b, 0x06, 0x01, 0x04, 0x01, 0x87, 0x29, 0x03, 0x01, 0x03, 0x01,
263         0x02, 0x01, 0x05, 0x80, 0x00 };
264
265     struct MacListStat *first = NULL, *curr = NULL;
266     char null[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, message[1024];
267     int mac_idx, begin, end;
268     varbind varbinds[4];
269     unsigned char next_num;
270
271
272     mac_idx = 0;
273     print_top(NULL, ST_TITLE);
274     mvwaddstr(main_sub, 0, 3,
275         _("Id       MAC address     Quality  Age  RSSI"));
276     noecho();
277     print_help(WAIT_RET);
278
279     varbinds[0].oid = Mac;
280     varbinds[0].len_oid = sizeof(Mac);
281     varbinds[0].len_val = 0;
282     varbinds[0].type = NULL_VALUE;
283     if (snmp(varbinds, 1, GET_NEXT) <= 0) {
284         print_helperr(ERR_RET);
285         goto exit;
286     }
287    next_num = varbinds[0].oid[varbinds[0].len_oid - 1];
288
289    while (memcmp(varbinds[0].oid, Mac, sizeof(Mac) - 2) == 0) {
290
291         Mac[sizeof(Mac) - 1] = next_num;
292         Quality[sizeof(Mac) - 1] = next_num;
293         Age[sizeof(Mac) - 1] = next_num;
294         RSSI[sizeof(Mac) - 1] = next_num;
295
296         if(sizeof(Mac) == varbinds[0].len_oid) {
297                 Mac[sizeof(Mac) - 2] = varbinds[0].oid[varbinds[0].len_oid - 2];
298                 Quality[sizeof(Mac) - 2] = varbinds[0].oid[varbinds[0].len_oid - 2];
299                 Age[sizeof(Mac) - 2]  = varbinds[0].oid[varbinds[0].len_oid - 2];
300                 RSSI[sizeof(Mac) - 2] = varbinds[0].oid[varbinds[0].len_oid - 2];
301         }
302
303         varbinds[0].oid = Mac;
304         varbinds[0].len_oid = sizeof(Mac);
305         varbinds[0].len_val = 0;
306         varbinds[0].type = NULL_VALUE;
307         varbinds[1].oid = Quality;
308         varbinds[1].len_oid = sizeof(Quality);
309         varbinds[1].len_val = 0;
310         varbinds[1].type = NULL_VALUE;
311         varbinds[2].oid = Age;
312         varbinds[2].len_oid = sizeof(Age);
313         varbinds[2].len_val = 0;
314         varbinds[2].type = NULL_VALUE;
315         varbinds[3].oid = RSSI;
316         varbinds[3].len_oid = sizeof(RSSI);
317         varbinds[3].len_val = 0;
318         varbinds[3].type = NULL_VALUE;
319         if (snmp(varbinds, 4, GET) < 4) {
320             print_helperr(ERR_RET);
321             getch();
322             goto exit;
323         }
324
325         if (memcmp(null, varbinds[0].value, 6)) {
326             if (first == NULL) {
327                 first =
328                     (struct MacListStat *)
329                     malloc(sizeof(struct MacListStat));
330                 curr = first;
331             } else {
332                 curr->next =
333                     (struct MacListStat *)
334                     malloc(sizeof(struct MacListStat));
335                 curr = curr->next;
336             } 
337             memcpy(curr->addr, varbinds[0].value, 6);
338             curr->quality = *varbinds[1].value;
339             curr->idle = *varbinds[2].value;
340             curr->rssi = *varbinds[3].value;
341             curr->next = NULL;
342             mac_idx++;
343         }
344
345         varbinds[0].oid = Mac;
346         varbinds[0].len_oid = sizeof(Mac);
347         varbinds[0].len_val = 0;
348         varbinds[0].type = NULL_VALUE;
349         if (snmp(varbinds, 1, GET_NEXT) <= 0) {
350             print_helperr(ERR_RET);
351             goto exit;
352         }
353         next_num = varbinds[0].oid[varbinds[0].len_oid - 1];
354                                                 
355     }
356
357    if(mac_idx) {
358
359     begin = 1;
360     end = (MAX_LINES < mac_idx + 1) ? MAX_LINES : mac_idx + 1;
361
362     while (1) {
363         print_help(_("Arrows - scroll; S - save to file; Q - return; "
364             "T - toggle view; Other - refresh"));
365
366         scroll_rows(first, begin, end, 1, 1);
367
368         sprintf(message, "%s: %d", ST_TITLE, mac_idx);
369         print_top_rssi(message);
370
371         switch (getch()) {
372         case 'S':
373         case 's':
374             save_Stations(first);
375             continue;
376         case KEY_DOWN:
377         case KEY_RIGHT:
378             if (end < mac_idx+1) {
379                 begin++;
380                 end++;
381             }
382             continue;
383         case KEY_UP:
384         case KEY_LEFT:
385             if (begin > 1) {
386                 begin--;
387                 end--;
388             }
389             continue;
390         case 'Q':
391         case 'q':
392             goto exit;
393         case 'T':
394         case 't':
395             sts_viewtype += 1;
396             if (sts_viewtype == 3)
397                 sts_viewtype = 0;
398
399             continue;
400         }
401     }
402    }
403    
404    print_help(ANY_KEY);
405    getch();
406    
407   exit:
408     while ((curr = first)) {
409         first = curr->next;
410         free(curr);
411     }
412     
413     print_top(NULL, NULL);
414     clear_main(0);
415 }