]> git.decadent.org.uk Git - ap-utils.git/blob - ap-gl/stations.c
7e596982a5dd7e8ef3119d1231e1ffe53fe07a87
[ap-utils.git] / ap-gl / 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  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License Version 2 from
8  * June 1991 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 #include <sys/wait.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <string.h>
26 #include "ap-utils.h"
27 #include "ap-curses.h"
28
29 #define MAX_LINES LINES-4
30
31 #define STAS _("Associated Stations")
32
33 extern int LINES;
34 extern WINDOW *main_sub;
35 extern short ap_type;
36
37 void stations()
38 {
39     struct AssociatedSTAsInfo {
40         unsigned short Num;
41         unsigned char MacAddress[6];
42         unsigned char Status;
43         unsigned char Port;
44         unsigned char ParentMacAddress[6];
45         unsigned char RSSI;
46         unsigned char LinkQuality;
47         unsigned char IP[4];
48         unsigned char Reserved[2];
49     } *mac = NULL, get;
50     char StasNum[] =
51         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x05, 0x01,
52         0x00
53     };
54
55     char StasMac[] =
56         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x05, 0x02,
57         0x00
58     };
59
60     char bridgeOperationalMode[] = {
61          0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x04, 0x01, 0x00
62     };
63     
64     struct MacListStat *first = NULL, *curr = NULL;
65     char message[1024];
66     int mac_num, begin, end, total_mac;
67     varbind varbinds[1];
68
69     print_title(STAS);
70
71     varbinds[0].oid = bridgeOperationalMode;
72     varbinds[0].len_oid = sizeof(bridgeOperationalMode);
73     varbinds[0].value = bridgeOperationalMode;
74     varbinds[0].len_val = 0;
75     varbinds[0].type = NULL_VALUE;
76     print_help(WAIT_RET);
77     if (snmp(varbinds, 1, GET) <= 0) {
78          print_helperr(ERR_RET);
79          goto exit;
80     }
81
82     if (*(varbinds[0].value) == 3) {
83          mvwaddstr(main_sub, 3, 1, _("AP now in AP Client Mode and don't has any associated stations"));
84          print_help(ANY_KEY);
85          wrefresh(main_sub);
86          getch();
87          goto exit;
88      }
89
90                                     
91     
92     varbinds[0].oid = StasNum;
93     varbinds[0].len_oid = sizeof(StasNum);
94     varbinds[0].value = StasNum;
95     varbinds[0].type = NULL_VALUE;
96     varbinds[0].len_val = 0;
97     print_help(WAIT_RET);
98     if (snmp(varbinds, 1, GET) <= 0) {
99         print_helperr(ERR_RET);
100         getch();
101         goto exit;
102     }
103
104     total_mac = *(varbinds[0].value);
105     mac_num = 1;
106
107     sprintf(message, "%s: %d", STAS, total_mac);
108     print_title(message);
109     mvwaddstr(main_sub, 0, 1, _("#  MAC          LQ    RSSI   Status Port IP"));
110     noecho();
111
112     while (mac_num <= total_mac) {
113
114         varbinds[0].oid = StasMac;
115         varbinds[0].len_oid = sizeof(StasMac);
116         varbinds[0].type = INT_VALUE;
117         get.Num = swap2(mac_num);
118         varbinds[0].value = (char *) &get;
119         varbinds[0].len_val = sizeof(get);
120
121         if (snmp(varbinds, 1, SET) <= 0) {
122             print_helperr(ERR_RET);
123             getch();
124             goto exit;
125         }
126
127         if (varbinds[0].len_val == 24) {
128             if (mac)
129                 free(mac);
130             mac =
131                 (struct AssociatedSTAsInfo *) malloc(varbinds[0].len_val);
132             memcpy(mac, varbinds[0].value, varbinds[0].len_val);
133 /*          mac = (struct AssociatedSTAsInfo *) varbinds[0].value;*/
134         } else {
135             print_helperr(_("AssociatedSTAsInfo packet error"));
136             goto exit;
137         }
138
139
140         if (first == NULL) {
141             first =
142                 (struct MacListStat *) malloc(sizeof(struct MacListStat));
143             curr = first;
144         } else {
145             curr->next =
146                 (struct MacListStat *) malloc(sizeof(struct MacListStat));
147             curr = curr->next;
148         }
149
150         memcpy(curr->addr, mac->MacAddress, 6);
151 //      memcpy(curr->ParentMacAddress, mac->ParentMacAddress, 6);
152         memcpy(&(curr->IP.s_addr), mac->IP, 4);
153         curr->quality = 100 - (minimum(mac->LinkQuality, 40)*2.5);
154         curr->rssi = - 96 + mac->RSSI;
155         
156         curr->Port = mac->Port;
157         curr->Status = mac->Status;
158         curr->next = NULL;
159         mac_num++;
160     }
161     begin = 1;
162     end = (MAX_LINES < mac_num) ? MAX_LINES : mac_num;
163     scroll_rows(first, begin, end, 1, 2);
164     print_help(_("Arrows - scroll; S - save to file; Q - quit to menu."));
165     while (1) {
166         switch (getch()) {
167         case 'S':
168         case 's':
169             save_Stations(first);
170             continue;
171         case KEY_RIGHT:
172         case KEY_DOWN:
173             if (end < mac_num) {
174                 begin++;
175                 end++;
176                 scroll_rows(first, begin, end, 1, 2);
177             }
178             continue;
179         case KEY_UP:
180         case KEY_LEFT:
181             if (begin > 1) {
182                 begin--;
183                 end--;
184                 scroll_rows(first, begin, end, 1, 2);
185             }
186             continue;
187         case 'Q':
188         case 'q':
189             goto exit;
190         }
191     }
192
193   exit:
194     while ((curr = first) != 0) {
195         first = curr->next;
196         free(curr);
197     }
198     print_title("");
199     clear_main(0);
200 }
201