2 * file.c from Access Point SNMP Utils for Linux
3 * file accessing functions
5 * Copyright (c) 2002 Roman Festchook <roma at polesye dot net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License Version 2 from
9 * June 1991 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <sys/types.h>
30 extern WINDOW *main_sub, *win_for_help, *main_win;
31 extern char *ap_types[], *ap_vendorexts[][3];
32 extern short ap_type, ap_vendorext;
33 extern struct in_addr ap_ip;
34 extern int atmel410_filter;
36 #define MAX_LINES LINES-6
49 _scroll_rows(struct APList *first, int begin, int end)
52 struct APList *curr = first;
61 while (end-- > begin) {
62 sprintf(message, " %3u %-15s %-10s %-8s %-16s", begin + i,
63 curr->ip, ap_types[curr->type],
64 ap_vendorexts[curr->type][curr->vendorext], curr->label);
65 mvwaddstr(main_sub, 2 + i, 0, message);
72 struct APList *parse_db_str(char *str)
74 struct APList *curr=NULL;
75 char *ip=NULL, *passwd=NULL, *label=NULL, *aptype=NULL, mess[1024];
78 while(str[i] != '\0') {
82 ip = (char *) malloc(j+1);
83 memcpy(ip, mess, j+1);
87 passwd = (char *) malloc(j+1);
88 memcpy(passwd, mess, j+1);
92 label = (char *) malloc(j+1);
93 memcpy(label, mess, j+1);
94 label[j > 16 ? 16 : j] = '\0';
97 aptype = (char *) malloc(j+1);
98 memcpy(aptype, mess, j+1);
106 mess[j++] = str[i++];
111 if (pos==4 && ip && passwd && ((atmel410_filter && atoi(aptype) == ATMEL410)
112 || !atmel410_filter)) {
113 curr = (struct APList *) malloc(sizeof(struct APList));
114 curr->type = atoi(aptype);
115 curr->vendorext = atoi(mess);
117 curr->ip = (char *) malloc(strlen(ip) + 1);
118 strcpy(curr->ip, ip);
119 curr->passwd = (char *) malloc(strlen(passwd) + 1);
120 strcpy(curr->passwd, passwd);
121 curr->label = (char *) malloc(strlen(label) + 1);
122 strcpy(curr->label, label);
142 extern char *community;
143 extern struct in_addr ap_ip;
145 char *home_dir, buf[1024], mess[64];
147 int c, fd, rval = 0, pos;
148 signed int j, i, begin, end, record_num = 0;
149 struct APList *first = NULL, *curr = NULL, *pmac;
152 if ((home_dir = getenv("HOME")) == NULL)
155 sprintf(buf, "%s/.ap-config", home_dir);
156 if ((fd = open(buf, O_RDONLY)) == -1)
160 while((j = read(fd, buf, sizeof(buf))) > 0)
161 for(i=0; i < j; i++) {
162 if (buf[i] == 0x0a) {
165 if ((first = parse_db_str(mess)) != NULL) {
170 if ((curr->next = parse_db_str(mess)) != NULL) {
177 mess[pos++] = buf[i];
183 if ((first = parse_db_str(mess)) != NULL) {
188 if ((curr->next = parse_db_str(mess)) != NULL)
198 mvwaddstr(main_sub, 0, 2,
199 _("NUM IP ADDRESS MIB TYPE MIB EXT. LABEL"));
200 print_top(NULL, _("Choose an AP to connect to"));
202 end = (MAX_LINES < record_num) ? MAX_LINES : record_num;
203 _scroll_rows(first, begin, end);
206 print_help(_("1-9,C: connect; N: new; D: delete; W: save; Q: quit; arrows: scroll"));
207 switch (c = getch()) {
227 if (record_num <= i || i <= 0)
234 inet_aton(curr->ip, &ap_ip);
238 i = strlen(curr->passwd) + 1;
239 community = (char *) malloc(i);
240 strncpy(community, curr->passwd, i);
241 ap_type = curr->type;
242 ap_vendorext = curr->vendorext;
245 if (reopen_sockfd() == -1)
248 print_bottom(inet_ntoa(ap_ip));
253 /* Nothing to connect */
257 mvwaddstr(main_sub, 1, 1, _("Connect to AP num:"));
258 get_value(message, 1, 20, 6, INT_STRING, 1, record_num - 1,
265 inet_aton(curr->ip, &ap_ip);
269 i = strlen(curr->passwd) + 1;
270 community = (char *) malloc(i);
271 strncpy(community, curr->passwd, i);
272 ap_type = curr->type;
273 ap_vendorext = curr->vendorext;
276 if (reopen_sockfd() == -1)
279 print_bottom(inet_ntoa(ap_ip));
284 /* Nothing to delete */
288 mvwaddstr(main_sub, 1, 0, _("Delete num:"));
289 get_value(message, 1, 15, 6, INT_STRING,
290 1, (record_num == 1 ? 1 : record_num - 1), NULL);
305 curr->next = pmac->next;
312 /* Clear incl. line with last AP record */
313 if (record_num == 1) {
314 clear_main_new(1, 3);
319 end = (MAX_LINES < record_num) ? MAX_LINES : record_num;
320 _scroll_rows(first, begin, end);
322 clear_main_new(1, 2);
327 if (end < record_num) {
330 _scroll_rows(first, begin, end);
339 _scroll_rows(first, begin, end);
344 if (end < record_num) {
345 end = (end + (MAX_LINES) - 1 < record_num) ?
346 end + (MAX_LINES) - 1 : record_num;
347 begin = (end - (MAX_LINES) + 1 > 0) ?
348 end - (MAX_LINES) + 1 : 1;
349 _scroll_rows(first, begin, end);
355 begin = (begin - (MAX_LINES) + 1 > 0) ?
356 begin - (MAX_LINES) + 1 : 1;
357 end = (begin + (MAX_LINES) - 1 < record_num) ?
358 begin + (MAX_LINES) - 1 : record_num;
359 _scroll_rows(first, begin, end);
364 if (end < record_num) {
365 begin = (record_num > MAX_LINES) ? record_num + 1 - (MAX_LINES) : 1;
367 _scroll_rows(first, begin, end);
374 end = (record_num > MAX_LINES) ? MAX_LINES : record_num;
375 _scroll_rows(first, begin, end);
381 sprintf(buf, "%s/.ap-config", home_dir);
382 if ((fd = creat(buf, 0600)) != -1) {
385 sprintf(buf, "%s:%s:%s:%d:%d\n", curr->ip, curr->passwd,
386 curr->label,curr->type, curr->vendorext);
387 write(fd, buf, strlen(buf));
391 print_help(DONE_WRITING_APCONF);
393 print_helperr(ERR_WRITING_APCONF);
403 while ((curr = first)) {
411 print_top(NULL, NULL);
417 void save_Stations(struct MacListStat *curr)
422 if ((home_dir = getenv("HOME"))) {
423 sprintf(message, "%s/ap-%s-%s-%s.stations", home_dir, inet_ntoa(ap_ip),
424 ap_types[ap_type],ap_vendorexts[ap_type][ap_vendorext]);
425 if ((fd = creat(message, 0600)) != -1) {
427 sprintf(message, "%02X%02X%02X%02X%02X%02X\n",
428 curr->addr[0] & 0xFF, curr->addr[1] & 0xFF,
429 curr->addr[2] & 0xFF, curr->addr[3] & 0xFF,
430 curr->addr[4] & 0xFF, curr->addr[5] & 0xFF);
431 write(fd, message, 13);
442 print_helperr(_("Unable to write stations file. Press any key."));
444 print_help(_("Stations file succesfully written. Press any key."));