]> git.decadent.org.uk Git - ap-utils.git/blob - ap-gl/stations.c
63fe84a995f276b93fcf470b168575cfe0723c5a
[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     char StasNum[] = {
40         0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x05, 0x01, 0x00
41     };
42     char StasMac[] = {
43         0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x05, 0x02, 0x00
44     };
45     char bridgeOperationalMode[] = {
46          0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x04, 0x01, 0x00
47     };
48     
49     struct AssociatedSTAsInfo {
50         unsigned short Num;
51         unsigned char MacAddress[6];
52         unsigned char Status;
53         unsigned char Port;
54         unsigned char ParentMacAddress[6];
55         unsigned char RSSI;
56         unsigned char LinkQuality;
57         unsigned char IP[4];
58         unsigned char Reserved[2];
59     } *mac = NULL, get;
60
61     struct MacListStat *first = NULL, *curr = NULL;
62     char message[1024];
63     int mac_num, begin, end, total_mac;
64     varbind varbinds[1];
65
66     print_title(STAS);
67
68     varbinds[0].oid = bridgeOperationalMode;
69     varbinds[0].len_oid = sizeof(bridgeOperationalMode);
70     varbinds[0].value = bridgeOperationalMode;
71     varbinds[0].len_val = 0;
72     varbinds[0].type = NULL_VALUE;
73     print_help(WAIT_RET);
74     if (snmp(varbinds, 1, GET) <= 0) {
75          print_helperr(ERR_RET);
76          goto exit;
77     }
78
79     if (*(varbinds[0].value) == 3) {
80         mvwaddstr(main_sub, 3, 1, _("AP is currently in AP Client Mode => "
81             "no associated STAtions."));
82         print_help(ANY_KEY);
83         wrefresh(main_sub);
84         getch();
85         goto exit;
86     }
87
88     varbinds[0].oid = StasNum;
89     varbinds[0].len_oid = sizeof(StasNum);
90     varbinds[0].value = StasNum;
91     varbinds[0].type = NULL_VALUE;
92     varbinds[0].len_val = 0;
93     print_help(WAIT_RET);
94     if (snmp(varbinds, 1, GET) <= 0) {
95         print_helperr(ERR_RET);
96         getch();
97         goto exit;
98     }
99
100     total_mac = *(varbinds[0].value);
101     mac_num = 1;
102
103     sprintf(message, "%s: %d", STAS, total_mac);
104     print_title(message);
105     mvwaddstr(main_sub, 0, 1, _("#     MAC       LQ    RSSI   Status Port IP"));
106     noecho();
107
108     while (mac_num <= total_mac) {
109
110         varbinds[0].oid = StasMac;
111         varbinds[0].len_oid = sizeof(StasMac);
112         varbinds[0].type = INT_VALUE;
113         get.Num = swap2(mac_num);
114         varbinds[0].value = (char *) &get;
115         varbinds[0].len_val = sizeof(get);
116
117         if (snmp(varbinds, 1, SET) <= 0) {
118             print_helperr(ERR_RET);
119             getch();
120             goto exit;
121         }
122
123         if (varbinds[0].len_val == 24) {
124             if (mac)
125                 free(mac);
126             mac =
127                 (struct AssociatedSTAsInfo *) malloc(varbinds[0].len_val);
128             memcpy(mac, varbinds[0].value, varbinds[0].len_val);
129 /*          mac = (struct AssociatedSTAsInfo *) varbinds[0].value;*/
130         } else {
131             print_helperr(_("AssociatedSTAsInfo packet error"));
132             goto exit;
133         }
134
135
136         if (first == NULL) {
137             first =
138                 (struct MacListStat *) malloc(sizeof(struct MacListStat));
139             curr = first;
140         } else {
141             curr->next =
142                 (struct MacListStat *) malloc(sizeof(struct MacListStat));
143             curr = curr->next;
144         }
145
146         memcpy(curr->addr, mac->MacAddress, 6);
147 //      memcpy(curr->ParentMacAddress, mac->ParentMacAddress, 6);
148         memcpy(&(curr->IP.s_addr), mac->IP, 4);
149         curr->quality = 100 - (minimum(mac->LinkQuality, 40)*2.5);
150         curr->rssi = - 96 + mac->RSSI;
151         
152         curr->Port = mac->Port;
153         curr->Status = mac->Status;
154         curr->next = NULL;
155         mac_num++;
156     }
157     begin = 1;
158     end = (MAX_LINES < mac_num) ? MAX_LINES : mac_num;
159     scroll_rows(first, begin, end, 1, 2);
160     print_help(_("Arrows - scroll; S - save to file; Q - quit to menu."));
161     while (1) {
162         switch (getch()) {
163         case 'S':
164         case 's':
165             save_Stations(first);
166             continue;
167         case KEY_RIGHT:
168         case KEY_DOWN:
169             if (end < mac_num) {
170                 begin++;
171                 end++;
172                 scroll_rows(first, begin, end, 1, 2);
173             }
174             continue;
175         case KEY_UP:
176         case KEY_LEFT:
177             if (begin > 1) {
178                 begin--;
179                 end--;
180                 scroll_rows(first, begin, end, 1, 2);
181             }
182             continue;
183         case 'Q':
184         case 'q':
185             goto exit;
186         }
187     }
188
189   exit:
190     while ((curr = first) != 0) {
191         first = curr->next;
192         free(curr);
193     }
194     print_title("");
195     clear_main(0);
196 }
197