]> git.decadent.org.uk Git - ap-utils.git/blob - lib/sysinfo.c
a190762d51a08af40fc68386b2ea07cf1d229545
[ap-utils.git] / lib / sysinfo.c
1 /*
2  *      sysinfo.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
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 #include "ap-utils.h"
28
29 #define SYS_DESCR _("System Description: ")
30 #define SYSINFO _("System Info")
31
32 #define OID_NUM 11
33
34 extern WINDOW *main_sub;
35 extern char* bridge_modes[6];
36 extern int poll_delay;
37
38 void atmel_sysinfo()
39 {
40     char sysDescr[] = {
41         0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x01, 0x01, 0x00
42     };
43     char sysDeviceInfo[] = {
44         0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x01, 0x05, 0x00
45     };
46     char bridgeOperationalMode[] = {
47         0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x04, 0x01, 0x00
48     };
49     /* This one is ATMEL12350 EZYNET MIB specific. */
50     char UpTime[] = {
51         0x2B, 0x06, 0x01, 0x04, 0x01, 0xE0, 0x3E, 0x01, 0x02, 0x09, 0x01, 0x00
52     };
53
54 char *bridge_modes[6] = {
55             _("Wireless Bridge Point to MultiPoint"),
56                 _("Access Point"),
57                     _("Access Point client"),
58                         _("Wireless Bridge Point to Point"),
59                             _("Repeater"),
60                                 _("unknown")
61 };
62
63     
64     char message[200];
65     int i;
66     varbind varbinds[4];
67     struct sysDeviceInfo_128 str128;
68     struct sysDeviceInfo_160 str160;
69     extern short ap_type, ap_vendorext;
70     extern int LINES, wait_mode;
71     extern rdprops regdom_types[];
72
73     /*
74      * These have to represent used sysDeviceInfo_{128,160} members
75      */
76     uint32_t ap_sversion;
77     char *ap_macaddr;
78     uint32_t ap_regdomain;
79     uint32_t ap_prodtype;
80     char *ap_oemname;
81     uint32_t ap_oemid;
82     char *ap_prodname;
83     uint32_t ap_hwrev;
84
85     if (ap_type == ATMEL12350) {
86         sysDescr[5] = 0xE0;
87         sysDescr[6] = 0x3E;
88         sysDeviceInfo[5] = 0xE0;
89         sysDeviceInfo[6] = 0x3E;
90         bridgeOperationalMode[5] = 0xE0;
91         bridgeOperationalMode[6] = 0x3E;
92     }
93
94     print_top(wait_mode == WAIT_TIMEOUT ? POLL_ON : POLL_OFF, SYSINFO);
95
96     if (wait_mode == WAIT_TIMEOUT)
97         print_help(QT_HELP);
98
99     while (1) {
100         i = 0;
101
102         varbinds[i].oid = sysDescr;
103         varbinds[i].len_oid = sizeof(sysDescr);
104         varbinds[i].value = sysDescr;
105         varbinds[i].type = NULL_VALUE;
106         varbinds[i].len_val = 0;
107         i++;
108
109         varbinds[i].oid = sysDeviceInfo;
110         varbinds[i].len_oid = sizeof(sysDeviceInfo);
111         varbinds[i].value = sysDeviceInfo;
112         varbinds[i].len_val = 0;
113         varbinds[i].type = NULL_VALUE;
114         i++;
115
116         varbinds[i].oid = bridgeOperationalMode;
117         varbinds[i].len_oid = sizeof(bridgeOperationalMode);
118         varbinds[i].value = bridgeOperationalMode;
119         varbinds[i].len_val = 0;
120         varbinds[i].type = NULL_VALUE;
121         i++;
122
123         if (ap_type == ATMEL12350 && ap_vendorext == EZYNET) {
124             varbinds[i].oid = UpTime;
125             varbinds[i].len_oid = sizeof(UpTime);
126             varbinds[i].value = UpTime;
127             varbinds[i].len_val = 0;
128             varbinds[i].type = NULL_VALUE;
129             i++;
130         }
131
132         if (wait_mode == WAIT_FOREVER)
133             print_help(WAIT_RET);
134
135         if (snmp(varbinds, i, GET) < i) {
136             print_helperr(ERR_RET);
137             getch();
138             goto quit;
139         }
140
141         if (wait_mode == WAIT_FOREVER)
142             print_help(QT_HELP);
143
144         mvwaddstr(main_sub, 0, 0, _("Device hardware/software/name info:"));
145         for (i = 0; i < varbinds[0].len_val && *(varbinds[0].value + i); i++)
146             mvwaddch(main_sub, 1, i + 1, *(varbinds[0].value + i));
147
148         if (varbinds[1].len_val == 92 || varbinds[1].len_val == 128) {
149             memcpy(&str128, varbinds[1].value,
150                 sizeof(struct sysDeviceInfo_128));
151             ap_sversion = str128.StructVersion;
152             ap_macaddr = str128.MacAddress;
153             ap_regdomain = swap4(str128.RegulatoryDomain);
154             ap_prodtype = str128.ProductType;
155             ap_oemname = str128.OEMName;
156             ap_oemid = str128.OEMID;
157             ap_prodname = str128.ProductName;
158             ap_hwrev = str128.HardwareRevision;
159         } else { /* varbinds[1].len_val == 160 */
160             memcpy(&str160, varbinds[1].value,
161                 sizeof(struct sysDeviceInfo_160));
162             ap_regdomain = str160.RegulatoryDomain;
163             ap_sversion = str160.StructVersion;
164             ap_macaddr = str160.MacAddress;
165             ap_prodtype = str160.ProductType;
166             ap_oemname = str160.OEMName;
167             ap_oemid = str160.OEMID;
168             ap_prodname = str160.ProductName;
169             ap_hwrev = str160.HardwareRevision;
170         }
171
172         sprintf(message, _("Operational mode: %s"),
173             bridge_modes[*(varbinds[2].value) - 1]);
174         mvwaddstr(main_sub, 2, 0, message);
175
176         sprintf(message, "%s%02X%02X%02X%02X%02X%02X", MAC,
177             ap_macaddr[0] & 0xFF, ap_macaddr[1] & 0xFF,
178             ap_macaddr[2] & 0xFF, ap_macaddr[3] & 0xFF,
179             ap_macaddr[4] & 0xFF, ap_macaddr[5] & 0xFF);
180         mvwaddstr(main_sub, 3, 0, message);
181
182         mvwaddstr(main_sub, 4, 0, _("Product name: "));
183         for (i = 0; i < 32 && ap_prodname[i]; i++)
184             waddch(main_sub, ap_prodname[i]);
185
186         sprintf(message, _("Product type: %u"), swap4(ap_prodtype));
187         mvwaddstr(main_sub, 5, 0, message);
188
189         mvwaddstr(main_sub, 6, 0, _("OEM name: "));
190         for (i = 0; i < 32 && ap_oemname[i]; i++)
191             waddch(main_sub, ap_oemname[i]);
192
193         sprintf(message, "OEM ID: %u", swap4(ap_oemid));
194         mvwaddstr(main_sub, 7, 0, message);
195
196         sprintf(message, _("Hardware revision: %u"), swap4(ap_hwrev));
197         mvwaddstr(main_sub, 8, 0, message);
198
199         i = regdom_idx(ap_regdomain);
200         sprintf(message, "Regulatory domain: %s [%d]",
201             regdom_types[i].desc, ap_regdomain);
202         mvwaddstr(main_sub, 9, 0, message);
203
204         sprintf(message, _("Info structure version: %u"), swap4(ap_sversion));
205         mvwaddstr(main_sub, 10, 0, message);
206
207         sprintf(message, _("Manufacturer OUI: %02X %02X %02X (%s)"),
208             ap_macaddr[0] & 0xFF, ap_macaddr[1] & 0xFF, ap_macaddr[2] & 0xFF,
209             oui2manufacturer(ap_macaddr));
210         mvwaddstr(main_sub, 11, 0, message);
211
212         if (ap_type == ATMEL12350 && ap_vendorext == EZYNET) {
213             unsigned int j = 0;
214
215             if (varbinds[3].len_val > 1) {
216                 for (i = 0; i < varbinds[3].len_val; i++)
217                     j |= (varbinds[3].value[i]
218                         << (8 * (varbinds[3].len_val - i - 1)));
219             } else {
220                 j = varbinds[3].value[0] & 0x80 ?
221                     0xff00 | varbinds[3].value[0] : varbinds[3].value[0];
222             }
223             sprintf(message,
224                 _("Uptime: %u days, %02u:%02u:%02u hours:mins:secs"),
225                 j / 60 / 60 / 24, j / 60 / 60 % 24, j / 60 % 60, j % 60);
226             mvwaddstr(main_sub, 12, 0, message);
227         }
228
229         wrefresh(main_sub);
230
231         i = wait_key(poll_delay);
232         if (i == -1)
233             goto quit;
234
235         switch((char) i) {
236             case 'q':
237             case 'Q':
238                 goto quit;
239             case 't':
240             case 'T':
241                 wait_mode = (wait_mode == WAIT_FOREVER ?
242                     WAIT_TIMEOUT : WAIT_FOREVER);
243                 print_top(wait_mode == WAIT_TIMEOUT ? POLL_ON : POLL_OFF,
244                     SYSINFO);
245         }
246
247         /* either timeout for user input (i == 0) or invalid key => continue */
248     }
249   quit:
250     print_top(NULL, NULL);
251     clear_main(0);
252 }
253
254 int get_RegDomain()
255 {
256     char oid_dot11CurrentRegDomain[] =
257         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x04, 0x01, 0x01, 0x02, 0x01 };
258     varbind varbinds[1];
259
260     varbinds[0].oid = oid_dot11CurrentRegDomain;
261     varbinds[0].len_oid = sizeof(oid_dot11CurrentRegDomain);
262     varbinds[0].value = oid_dot11CurrentRegDomain;
263     varbinds[0].type = NULL_VALUE;
264     varbinds[0].len_val = 0;
265
266     if (snmp(varbinds, 1, GET) <= 0)
267         return 0;
268     return *(varbinds[0].value);
269 }
270
271 void nwn_sysinfo()
272 {
273     char oid_mac[] =
274         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x02, 0x01, 0x01, 0x01, 0x01 };
275     char oid_manufacturerID[] =
276         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x02, 0x01, 0x01, 0x08, 0x01 };
277     char oid_productID[] =
278         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x02, 0x01, 0x01, 0x09, 0x01 };
279     char oid_dot11manufacturerOUI[] =
280         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x03, 0x01, 0x02, 0x01, 0x01,
281         0x01
282     };
283     char oid_dot11manufacturerName[] =
284         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x03, 0x01, 0x02, 0x01, 0x02,
285         0x01
286     };
287     char oid_dot11manufacturerProductName[] =
288         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x03, 0x01, 0x02, 0x01, 0x03,
289         0x01
290     };
291     char oid_dot11manufacturerProductVersion[] =
292         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x03, 0x01, 0x02, 0x01, 0x04,
293         0x01
294     };
295     char oid_dot11PHYType[] =
296         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x04, 0x01, 0x01, 0x01, 0x01 };
297     char oid_dot11TempType[] =
298         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x04, 0x01, 0x01, 0x03, 0x01 };
299     char oid_TxPowerLevel1[] =
300         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x04, 0x03, 0x01, 0x02, 0x01 };
301     char oid_dot11PrivacyOptionImplemented[] =
302         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x01, 0x01, 0x01, 0x07, 0x01 };
303     char oid_dot11DiversitySupport[] =
304         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x04, 0x02, 0x01, 0x03, 0x01 };
305     char oid_sysDescr[] =
306         { 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00 };
307     char oid_sysUptime[] =
308         { 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x03, 0x00 };
309     char oid_ip[] =
310         { 0x2b, 0x06, 0x01, 0x02, 0x01, 0x04, 0x14, 0x01, 0x01 };
311     char *types[3] =
312         { _("FHSS 2.4 GHz"), _("DSSS 2.4 GHz"), _("IR Baseband") };
313     char *temp[3] = { _("Commercial range 0..40 C"),
314         _("Industrial range -30..70 C"),
315         _("unknown")
316     }, *diversity[3] = {
317     _("manual"), _("notsupported"), _("dynamic")};
318
319     char message[300];
320     int i, hand, c, min, sec;
321     struct in_addr ip;
322     varbind varbinds[OID_NUM];
323     size_t time_stamp;
324     extern int wait_mode;
325
326     print_top(wait_mode == WAIT_TIMEOUT ? POLL_ON : POLL_OFF, SYSINFO);
327
328     if (wait_mode == WAIT_TIMEOUT)
329         print_help(QT_HELP);
330
331     while (1) {
332         for (i = 0; i < OID_NUM; i++) {
333             varbinds[i].value = oid_mac;
334             varbinds[i].type = NULL_VALUE;
335             varbinds[i].len_val = 0;
336         }
337         varbinds[0].oid = oid_mac;
338         varbinds[0].len_oid = sizeof(oid_mac);
339         varbinds[1].oid = oid_manufacturerID;
340         varbinds[1].len_oid = sizeof(oid_manufacturerID);
341         varbinds[2].oid = oid_productID;
342         varbinds[2].len_oid = sizeof(oid_productID);
343         varbinds[3].oid = oid_dot11manufacturerOUI;
344         varbinds[3].len_oid = sizeof(oid_dot11manufacturerOUI);
345         varbinds[4].oid = oid_dot11manufacturerName;
346         varbinds[4].len_oid = sizeof(oid_dot11manufacturerName);
347         varbinds[5].oid = oid_dot11manufacturerProductName;
348         varbinds[5].len_oid = sizeof(oid_dot11manufacturerProductName);
349         varbinds[6].oid = oid_dot11manufacturerProductVersion;
350         varbinds[6].len_oid = sizeof(oid_dot11manufacturerProductVersion);
351         varbinds[7].oid = oid_dot11PHYType;
352         varbinds[7].len_oid = sizeof(oid_dot11PHYType);
353         varbinds[8].oid = oid_dot11TempType;
354         varbinds[8].len_oid = sizeof(oid_dot11TempType);
355         varbinds[9].oid = oid_TxPowerLevel1;
356         varbinds[9].len_oid = sizeof(oid_TxPowerLevel1);
357
358         if (wait_mode == WAIT_FOREVER)
359             print_help(WAIT_RET);
360
361         if (snmp(varbinds, OID_NUM - 1, GET) < OID_NUM - 1) {
362             print_helperr(ERR_RET);
363             getch();
364             goto quit;
365         }
366
367         sprintf(message, "%s%02X%02X%02X%02X%02X%02X", MAC,
368             varbinds[0].value[0] & 0xFF, varbinds[0].value[1] & 0xFF,
369             varbinds[0].value[2] & 0xFF, varbinds[0].value[3] & 0xFF,
370             varbinds[0].value[4] & 0xFF, varbinds[0].value[5] & 0xFF);
371         mvwaddstr(main_sub, 0, 0, message);
372
373         mvwaddstr(main_sub, 3, 0, _("Manufacturer:"));
374         i = 0;
375         while (varbinds[4].len_val > i)
376             mvwaddch(main_sub, 3, i + 20, *(varbinds[4].value + i++));
377
378         mvwaddstr(main_sub, 4, 0, _("Manufacturer ID:"));
379         i = 0;
380         while (varbinds[1].len_val > i)
381             mvwaddch(main_sub, 4, i + 20, *(varbinds[1].value + i++));
382
383         sprintf(message, _("Manufacturer OUI: %02X %02X %02X (%s)"),
384             varbinds[3].value[0] & 0xFF, varbinds[3].value[1] & 0xFF,
385             varbinds[3].value[2] & 0xFF,
386             oui2manufacturer(varbinds[3].value));
387         mvwaddstr(main_sub, 5, 0, message);
388
389         sprintf(message, _("Product Name:"));
390         mvwaddstr(main_sub, 6, 0, message);
391         i = 0;
392         while (varbinds[5].len_val > i)
393             mvwaddch(main_sub, 6, i + 20, *(varbinds[5].value + i++));
394
395         sprintf(message, _("Product ID:"));
396         mvwaddstr(main_sub, 7, 0, message);
397         i = 0;
398         while (varbinds[2].len_val > i)
399             mvwaddch(main_sub, 7, i + 20, *(varbinds[2].value + i++));
400
401         sprintf(message, _("Product Version:"));
402         mvwaddstr(main_sub, 8, 0, message);
403         i = 0;
404         while (varbinds[6].len_val > i)
405             mvwaddch(main_sub, 8, i + 20, *(varbinds[6].value + i++));
406
407         sprintf(message, _("PHYType: %s"), types[*(varbinds[7].value) - 1]);
408         mvwaddstr(main_sub, 9, 0, message);
409
410         sprintf(message, _("Temperature: %s"), temp[*(varbinds[8].value) - 1]);
411         mvwaddstr(main_sub, 10, 0, message);
412
413         if ((i = get_RegDomain()) == 0) {
414             print_helperr(ERR_RET);
415             getch();
416             goto quit;
417         }
418         sprintf(message, _("Regulatory Domain: %s"),
419             (i == 0x10) ? _("FCC (USA)") :
420             (i == 0x20) ? _("DOC (Canada)") :
421             (i == 0x30) ? _("ETSI (Europe)") :
422             (i == 0x31) ? _("Spain") :
423             (i == 0x32) ? _("France") :
424             (i == 0x40) ? _("MKK (Japan)") : _("unknown"));
425         mvwaddstr(main_sub, 11, 0, message);
426
427         sprintf(message, _("Transmit Power: %u mW"), *(varbinds[9].value));
428         mvwaddstr(main_sub, 12, 0, message);
429
430         varbinds[0].oid = oid_dot11PrivacyOptionImplemented;
431         varbinds[0].len_oid = sizeof(oid_dot11PrivacyOptionImplemented);
432         varbinds[0].value = oid_mac;
433         varbinds[0].type = NULL_VALUE;
434         varbinds[0].len_val = 0;
435         varbinds[1].oid = oid_dot11DiversitySupport;
436         varbinds[1].len_oid = sizeof(oid_dot11DiversitySupport);
437         varbinds[1].value = oid_mac;
438         varbinds[1].type = NULL_VALUE;
439         varbinds[1].len_val = 0;
440         varbinds[2].oid = oid_sysDescr;
441         varbinds[2].len_oid = sizeof(oid_sysDescr);
442         varbinds[2].value = oid_mac;
443         varbinds[2].type = NULL_VALUE;
444         varbinds[2].len_val = 0;
445         varbinds[3].oid = oid_sysUptime;
446         varbinds[3].len_oid = sizeof(oid_sysUptime);
447         varbinds[3].value = oid_mac;
448         varbinds[3].type = NULL_VALUE;
449         varbinds[3].len_val = 0;
450
451         if (snmp(varbinds, 4, GET) < 4) {
452             print_helperr(ERR_RET);
453             getch();
454             goto quit;
455         }
456
457         sprintf(message, _("WEP implemented: %s"),
458             (*(varbinds[0].value) == 1) ? ON : OFF);
459         mvwaddstr(main_sub, 13, 0, message);
460         sprintf(message, _("Diversity: %s"),
461             diversity[*(varbinds[1].value) - 1]);
462         mvwaddstr(main_sub, 14, 0, message);
463         mvwaddstr(main_sub, LINES - 8, 0, SYS_DESCR);
464         i = 0;
465         while (varbinds[2].len_val > i && varbinds[2].value[i]) {
466             message[i] = varbinds[2].value[i];
467             i++;
468         }
469         message[i] = '\0';
470         mvwaddstr(main_sub, LINES - 8, strlen(SYS_DESCR) + 1, message);
471
472         time_stamp = 0;
473         i = 1;
474
475         for (c = 1; c <= varbinds[3].len_val; c++) {
476             time_stamp +=
477                 (unsigned char) varbinds[3].value[varbinds[3].len_val - c] * i;
478             i *= 256;
479         }
480
481         hand = time_stamp % 100;
482         time_stamp = time_stamp / 100;
483         sec = time_stamp % 60;
484         time_stamp = time_stamp / 60;
485         min = time_stamp % 60;
486         time_stamp = time_stamp / 60;
487
488         sprintf(message, _("Uptime: %u:%02u:%02u.%02u"), time_stamp, min, sec,
489             hand);
490         mvwaddstr(main_sub, 15, 0, message);
491
492         varbinds[0].oid = oid_ip;
493         varbinds[0].len_oid = sizeof(oid_ip);
494         varbinds[0].value = oid_mac;
495         varbinds[0].type = NULL_VALUE;
496         varbinds[0].len_val = 0;
497
498         if (snmp(varbinds, 1, GET_NEXT) <= 0) {
499             print_helperr(ERR_RET);
500             getch();
501             goto quit;
502         }
503
504         if (wait_mode == WAIT_FOREVER)
505             print_help(QT_HELP);
506
507         memcpy(&ip.s_addr, varbinds[0].value, 4);
508         sprintf(message, _("IP  Address: %s"), inet_ntoa(ip));
509         mvwaddstr(main_sub, 1, 0, message);
510         wrefresh(main_sub);
511
512         i = wait_key(poll_delay);
513         if (i == -1)
514             goto quit;
515
516         switch((char) i) {
517             case 'q':
518             case 'Q':
519                 goto quit;
520             case 't':
521             case 'T':
522                 wait_mode = (wait_mode == WAIT_FOREVER ?
523                     WAIT_TIMEOUT : WAIT_FOREVER);
524                 print_top(wait_mode == WAIT_TIMEOUT ? POLL_ON : POLL_OFF,
525                     SYSINFO);
526         }
527
528         /* either timeout for user input (i == 0) or invalid key => continue */
529     }
530   quit:
531     print_top(NULL, NULL);
532     clear_main(0);
533 }
534