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