]> git.decadent.org.uk Git - ap-utils.git/blob - lib/common.c
16f342dc00ce196228d3eb64eaee2165efa4355f
[ap-utils.git] / lib / common.c
1 /*
2  *      common.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 <stdlib.h>
22 #include <string.h>
23 #include <menu.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/ioctl.h>
28 #include "ap-utils.h"
29
30 #define AUTODETECT _("Autodetect AP type? ")
31 #define APIP _("Access Point IP-address: ")
32 #define APPASS _("Password (community): ")
33 #define APTYPE _("AP type: ")
34 #define SAVESETTINGS _("Save connect-settings: ")
35
36 extern WINDOW *main_sub;
37 extern char *community, *prog_title;
38 extern short ap_type;
39 extern int sockfd;
40 extern struct in_addr ap_ip;
41 extern char *ap_types[];
42
43 void about()
44 {
45     int nrow = 12, ncol = 47, brow = (LINES - 4 - nrow) / 2, bcol =
46         (COLS - MCOLS - 2 - ncol) / 2, i;
47     char message[100];
48
49     noecho();
50     curs_set(0);
51
52     print_title(_("About"));
53
54     /* drawing the box */
55     mvwaddch(main_sub, brow, bcol, ACS_ULCORNER);
56     mvwaddch(main_sub, brow, bcol + ncol, ACS_URCORNER);
57     mvwaddch(main_sub, brow + nrow, bcol, ACS_LLCORNER);
58     mvwaddch(main_sub, brow + nrow, bcol + ncol, ACS_LRCORNER);
59     for (i = 1; i < ncol; i++) {
60         mvwaddch(main_sub, brow, bcol + i, ACS_HLINE);
61         mvwaddch(main_sub, brow + nrow, bcol + i, ACS_HLINE);
62     }
63     for (i = 1; i < nrow; i++) {
64         mvwaddch(main_sub, brow + i, bcol, ACS_VLINE);
65         mvwaddch(main_sub, brow + i, bcol + ncol, ACS_VLINE);
66     }
67
68     mvwaddstr(main_sub, brow + 1, bcol + 2, prog_title);
69     sprintf(message, _("From %s"), TITLE);
70     mvwaddstr(main_sub, brow + 2, bcol + 2, message);
71     sprintf(message, _("Version %s"), VERSION);
72     mvwaddstr(main_sub, brow + 3, bcol + 2, message);
73     mvwaddstr(main_sub, brow + 5, bcol + 2,
74               _("Written by Roman Festchook roma@polesye.net"));
75     mvwaddstr(main_sub, brow + 6, bcol + 2,
76               _("Copyright (c) Roman Festchook 2001-2004"));
77     mvwaddstr(main_sub, brow + 7, bcol + 2, "http://ap-utils.polesye.net/");
78     mvwaddstr(main_sub, brow + 9, bcol + 2,
79               _("This program is distributed under the terms"));
80     mvwaddstr(main_sub, brow +10, bcol + 2,
81               _("of the GNU General Public License version 2."));
82     mvwaddstr(main_sub, brow + 11, bcol + 2,
83               _("See the included COPYING file for details."));
84
85
86     wrefresh(main_sub);
87
88     print_help(ANY_KEY);
89     getch();
90     print_help("");
91     print_title("");
92     clear_main(0);
93     return;
94 }
95
96 void connect_options(unsigned long int ip, int type)
97 {
98     int i;
99     struct sockaddr_in client;
100     unsigned char message[16];
101     int fd;
102     char *home_dir;
103     char save_settings=1;
104     char sysDescr_NWN[] = { 0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00 };
105     char sysDescr_ATMEL410[] = { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A,
106         0x01, 0x01, 0x01, 0x01, 0x00 };
107     char sysDescr_ATMEL12350[] = { 0x2B, 0x06, 0x01, 0x04, 0x01, 0xE0, 0x3E,
108         0x01, 0x01, 0x01, 0x01, 0x00 };
109     varbind varbinds[1];
110
111
112     memset(&client, 0, sizeof client);
113     client.sin_family = AF_INET;
114     client.sin_port = INADDR_ANY;
115     client.sin_addr.s_addr = INADDR_ANY;
116
117     if (sockfd)
118         close(sockfd);
119
120     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
121         print_helperr(CREATE_SOCKET_ERROR);
122         getch();
123         goto exit;
124     }
125
126     if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1) {
127         print_helperr(BIND_SOCKET_ERROR);
128         getch();
129         goto exit;
130     }
131
132     noecho();
133     curs_set(0);
134
135     print_title(_("Connect options"));
136
137 get_all_again:
138     mvwaddstr(main_sub, 1, 1, APIP);
139     if (ip) {
140         ap_ip.s_addr = ip;
141         mvwaddstr(main_sub, 1, 1 + strlen(APIP), inet_ntoa(ap_ip));
142     } else {
143         print_help(_("Enter IP address of your Access Point."));
144         get_ip(&ap_ip, 1, 1 + strlen(APIP), NULL);
145     }
146
147     mvwaddstr(main_sub, 2, 1, APPASS);
148     print_help(_("Entered characters will not be displayed "
149         "for security reason."));
150     get_pass(message, 2, 1 + strlen(APPASS), 16);
151     if (community)
152         free(community);
153     i = strlen(message) + 1;
154     community = (char *) malloc(i);
155     strncpy(community, message, i);
156
157     print_help("");
158     if (type) {
159         ap_type = --type;
160         mvwaddstr(main_sub, 3, 1, APTYPE);
161     } else {
162         mvwaddstr(main_sub, 3, 1, AUTODETECT);
163         wrefresh(main_sub);
164         i = yes_no(3, 1 + strlen(AUTODETECT));
165         clear_main_new(3, 4);
166         mvwaddstr(main_sub, 3, 1, APTYPE);
167         if (i == 2) { /* NO */
168             wrefresh(main_sub);
169             ap_type = menu_choose(3, 1 + strlen(APTYPE), ap_types, 3);
170         } else { /* YES */
171             print_help(_("Determining AP type. Please wait..."));
172             varbinds[0].oid = sysDescr_NWN;
173             varbinds[0].len_oid = sizeof(sysDescr_NWN);
174             varbinds[0].value = NULL;
175             varbinds[0].len_val = 0;
176             varbinds[0].type = NULL_VALUE;
177             if (snmp(varbinds, 1, GET) > 0) {
178                 ap_type = NWN;
179             } else {
180                 varbinds[0].oid = sysDescr_ATMEL410;
181                 varbinds[0].len_oid = sizeof(sysDescr_ATMEL410);
182                 varbinds[0].value = NULL;
183                 varbinds[0].len_val = 0;
184                 varbinds[0].type = NULL_VALUE;
185                 if (snmp(varbinds, 1, GET) > 0) {
186                     ap_type = ATMEL410;
187                 } else {
188                     varbinds[0].oid = sysDescr_ATMEL12350;
189                     varbinds[0].len_oid = sizeof(sysDescr_ATMEL12350);
190                     varbinds[0].value = NULL;
191                     varbinds[0].len_val = 0;
192                     varbinds[0].type = NULL_VALUE;
193                     if (snmp(varbinds, 1, GET) > 0) {
194                         ap_type = ATMEL12350;
195                     } else {
196                         print_helperr(_("Unable to determine AP type "
197                             "(no response). Press any key."));
198                         getch();
199                         clear_main_new(1, 4);
200                         goto get_all_again;
201                     }
202                 }
203             }
204         }                           
205         wattrset(main_sub, A_BOLD);
206     }
207     waddstr(main_sub, ap_types[ap_type]);
208     wattrset(main_sub, A_NORMAL);
209
210     mvwaddstr(main_sub, 4, 1, SAVESETTINGS);
211     wrefresh(main_sub);
212     save_settings = on_off(4, 1 + strlen(SAVESETTINGS));
213
214     print_bottom(inet_ntoa(ap_ip));
215
216     if(save_settings == 1) {
217         if ((home_dir = getenv("HOME"))) {
218             sprintf(message, "%s/.ap-config", home_dir);
219             if ((fd = open(message, O_CREAT | O_WRONLY | O_APPEND, 0600)) != -1) {
220                 sprintf(message, "%s:%s:%d\n", inet_ntoa(ap_ip),
221                     community, ap_type);
222                 write(fd, message, strlen(message));
223                 close(fd);
224             }
225         }
226     }
227   exit:
228     print_help("");
229     print_title("");
230     clear_main(0);
231     return;
232 }
233
234 void exit_program()
235 {
236     endwin();
237     exit(0);
238 }
239
240 void exit_shell()
241 {
242     def_prog_mode();
243     endwin();
244     system("sh");
245     curs_set(0);
246     refresh();
247 }
248
249 /* 
250  * Fill channels list string array using regulation domain
251  * restrictions and return number of the channels
252  */
253 int ch_list(int regulation_domain, char **list)
254 {
255     int FIRST_CHANNEL, LAST_CHANNEL, i;
256
257 #ifndef NO_REG_DOMAIN
258     switch (regulation_domain) {
259     case 0x10:
260     case 0x20:
261         FIRST_CHANNEL = 1;
262         LAST_CHANNEL = 11;
263         break;
264     case 0x30:
265         FIRST_CHANNEL = 1;
266         LAST_CHANNEL = 13;
267         break;
268     case 0x31:
269         FIRST_CHANNEL = 10;
270         LAST_CHANNEL = 11;
271         break;
272     case 0x32:
273         FIRST_CHANNEL = 10;
274         LAST_CHANNEL = 13;
275         break;
276     default: /* also case 0x40 */
277 #endif    
278         FIRST_CHANNEL = 1;
279         LAST_CHANNEL = 14;
280 #ifndef NO_REG_DOMAIN
281     }
282 #endif    
283
284     for (i = 0; i < LAST_CHANNEL - FIRST_CHANNEL + 1; i++) {
285         list[i] = (char *) malloc(3);
286         sprintf(list[i], "%02u", FIRST_CHANNEL + i);
287     }
288     return i;
289 }
290