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>
29 #include "ap-curses.h"
31 extern WINDOW *main_sub, *win_for_help, *main_win;
32 extern char *ap_types[];
34 extern int atmel410_filter;
35 #define MAX_LINES LINES-6
47 _scroll_rows(struct APList *first, int begin, int end)
50 struct APList *curr = first;
59 while (end-- > begin) {
60 sprintf(message, "%5u %15s %s %s", begin + i, curr->ip,
61 ap_types[curr->type], curr->name);
62 mvwaddstr(main_sub, 2 + i, 1, message);
69 struct APList *parse_db_str(char *str)
71 struct APList *curr=NULL;
72 char *ip=NULL, *passwd=NULL, *name=NULL, mess[1024];
75 while(str[i] != '\0') {
79 ip = (char *) malloc(j+1);
80 memcpy(ip, mess, j+1);
84 passwd = (char *) malloc(j+1);
85 memcpy(passwd, mess, j+1);
89 name = (char *) malloc(j+1);
90 memcpy(name, mess, j+1);
103 if (pos==3 && ip && passwd && ((atmel410_filter && atoi(mess) == ATMEL410)
104 || !atmel410_filter)) {
105 curr = (struct APList *) malloc(sizeof(struct APList));
106 curr->type = atoi(mess);
108 curr->ip = (char *) malloc(strlen(ip) + 1);
109 strcpy(curr->ip, ip);
110 curr->passwd = (char *) malloc(strlen(passwd) + 1);
111 strcpy(curr->passwd, passwd);
112 curr->name = (char *) malloc(strlen(name) + 1);
113 strcpy(curr->name, name);
130 extern char *community;
131 extern struct in_addr ap_ip;
134 char *home_dir, buf[1024], mess[64];
136 int c, fd, rval=0, pos;
137 signed int j, i, begin, end, record_num=0;
138 struct APList *first=NULL, *curr=NULL, *pmac;
139 struct sockaddr_in client;
142 memset(&client, 0, sizeof client);
143 client.sin_family = AF_INET;
144 client.sin_port = INADDR_ANY;
145 client.sin_addr.s_addr = INADDR_ANY;
147 if ((home_dir = getenv("HOME")) == NULL)
150 sprintf(buf, "%s/.ap-config", home_dir);
151 if ((fd = open(buf, O_RDONLY)) == -1)
155 while((j = read(fd, buf, sizeof(buf))) > 0)
156 for(i=0; i < j; i++) {
157 if (buf[i] == 0x0a) {
160 if ((first = parse_db_str(mess)) != NULL) {
165 if ((curr->next = parse_db_str(mess)) != NULL) {
172 mess[pos++] = buf[i];
178 if ((first = parse_db_str(mess)) != NULL) {
183 if ((curr->next = parse_db_str(mess)) != NULL)
193 mvwaddstr(main_sub, 0, 3,
194 _("NUM IP ADDRESS TYPE NAME (in ~/.ap-config)"));
195 print_title(_("Choose an AP to connect to"));
197 end = (MAX_LINES < record_num) ? MAX_LINES : record_num;
198 _scroll_rows(first, begin, end);
200 print_help(_("1-9,C: connect; N: new; D: delete; W: save; Q: quit; arrows: scroll"));
202 switch (c = getch()) {
222 if (record_num <= i || i <= 0)
229 inet_aton(curr->ip, &ap_ip);
233 i = strlen(curr->passwd) + 1;
234 community = (char *) malloc(i);
235 strncpy(community, curr->passwd, i);
236 ap_type = curr->type;
241 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
244 if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1)
247 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;
277 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
280 if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1)
283 print_bottom(inet_ntoa(ap_ip));
289 /* Nothing to delete */
293 mvwaddstr(main_sub, 1, 0, _("Delete num:"));
294 get_value(message, 1, 15, 6, INT_STRING,
295 1, (record_num == 1 ? 1 : record_num - 1), NULL);
307 curr->next = pmac->next;
311 /* Clear incl. line with last AP record */
312 if (record_num == 1) {
313 clear_main_new(1, 3);
318 end = (MAX_LINES < record_num) ? MAX_LINES : record_num;
319 _scroll_rows(first, begin, end);
321 clear_main_new(1, 2);
326 if (end < record_num) {
329 _scroll_rows(first, begin, end);
338 _scroll_rows(first, begin, end);
344 sprintf(buf, "%s/.ap-config", home_dir);
345 if ((fd = creat(buf, 0600)) != -1) {
348 sprintf(buf, "%s:%s:%d\n", curr->ip, curr->passwd,
350 write(fd, buf, strlen(buf));
363 while ((curr = first)) {
376 void save_Stations(struct MacListStat *curr)
381 if ((home_dir = getenv("HOME"))) {
382 sprintf(message, "%s/ap-%s.stations", home_dir, ap_types[ap_type]);
383 if ((fd = creat(message, 0600)) != -1) {
385 sprintf(message, "%02X%02X%02X%02X%02X%02X\n",
386 curr->addr[0] & 0xFF, curr->addr[1] & 0xFF,
387 curr->addr[2] & 0xFF, curr->addr[3] & 0xFF,
388 curr->addr[4] & 0xFF, curr->addr[5] & 0xFF);
389 write(fd, message, 13);