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
45 _scroll_rows(struct APList *first, int begin, int end)
48 struct APList *curr = first;
56 while (end-- > begin) {
57 sprintf(message, "%5u %15s %s", begin + i, curr->ip, ap_types[curr->type]);
58 mvwaddstr(main_sub, 2 + i, 1, message);
65 struct APList *parse_db_str(char *str)
67 struct APList *curr=NULL;
68 char *ip=NULL, *passwd=NULL, mess[1024];
71 while(str[i] != '\0') {
75 ip = (char *) malloc(j+1);
76 memcpy(ip, mess, j+1);
80 passwd = (char *) malloc(j+1);
81 memcpy(passwd, mess, j+1);
95 if (pos==2 && ip && passwd && ((atmel410_filter && atoi(mess)== ATMEL410) || !atmel410_filter)) {
96 curr = (struct APList *) malloc(sizeof(struct APList));
97 curr->type = atoi(mess);
99 curr->ip = (char *) malloc(strlen(ip)+1);
100 strcpy(curr->ip, ip);
101 curr->passwd = (char *) malloc(strlen(passwd)+1);
102 strcpy(curr->passwd, passwd);
116 extern char *community;
117 extern struct in_addr ap_ip;
120 char *home_dir, buf[1024], mess[64];
123 signed int j, i, begin, end, record_num=0;
125 struct APList *first=NULL, *curr=NULL, *pmac;
126 struct sockaddr_in client;
128 memset(&client, 0, sizeof client);
129 client.sin_family = AF_INET;
130 client.sin_port = INADDR_ANY;
131 client.sin_addr.s_addr = INADDR_ANY;
134 if ((home_dir = getenv("HOME")) == NULL)
136 sprintf(buf, "%s/.ap-config", home_dir);
137 if ((fd = open(buf, O_RDONLY)) == -1)
140 while((j = read(fd, buf, sizeof(buf)))>0)
141 for(i=0; i < j; i++) {
145 if ((first = parse_db_str(mess))!=NULL) {
150 if ((curr->next = parse_db_str(mess)) != NULL) {
157 mess[pos++] = buf[i];
162 if ((first = parse_db_str(mess))!=NULL) {
167 if ((curr->next = parse_db_str(mess)) != NULL)
176 mvwaddstr(main_sub, 0, 3, _("NUM IP ADDRESS TYPE"));
177 print_title(_("Choose an AP to connect to"));
179 end = (MAX_LINES < record_num) ? MAX_LINES : record_num;
180 _scroll_rows(first, begin, end);
182 print_help(_("1-9,C: connect; N: new; D: delete; W: save; Q: quit; arrows: scroll"));
184 switch (c = getch()) {
202 if (record_num <= i || i <= 0)
207 inet_aton(curr->ip, &ap_ip);
210 i = strlen(curr->passwd) + 1;
211 community = (char *) malloc(i);
212 strncpy(community, curr->passwd, i);
213 ap_type = curr->type;
217 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
219 if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1)
221 print_bottom(inet_ntoa(ap_ip));
225 mvwaddstr(main_sub, 1, 1, _("Connect to AP num:"));
226 get_value(message, 1, 20, 6, INT_STRING,
227 1, record_num - 1, NULL);
232 inet_aton(curr->ip, &ap_ip);
235 i = strlen(curr->passwd) + 1;
236 community = (char *) malloc(i);
237 strncpy(community, curr->passwd, i);
238 ap_type = curr->type;
242 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
244 if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1)
246 print_bottom(inet_ntoa(ap_ip));
250 mvwaddstr(main_sub, 1, 0, _("Delete num:"));
251 get_value(message, 1, 15, 6, INT_STRING,
252 1, record_num - 1, NULL);
263 curr->next = pmac->next;
268 end = (MAX_LINES < record_num) ? MAX_LINES : record_num;
269 _scroll_rows(first, begin, end);
271 clear_main_new(1, 2);
275 if (end < record_num) {
278 _scroll_rows(first, begin, end);
287 _scroll_rows(first, begin, end);
293 sprintf(buf, "%s/.ap-config", home_dir);
294 if ((fd = creat(buf, 0600)) != -1) {
297 sprintf(buf, "%s:%s:%d\n", curr->ip, curr->passwd, curr->type);
298 write(fd, buf, strlen(buf));
310 while ((curr = first)) {
323 void save_Stations(struct MacListStat *curr)
328 if ((home_dir = getenv("HOME"))) {
329 sprintf(message, "%s/ap-%s.stations", home_dir,
331 if ((fd = creat(message, 0600)) != -1) {
333 sprintf(message, "%02X%02X%02X%02X%02X%02X\n",
334 curr->addr[0] & 0xFF, curr->addr[1] & 0xFF,
335 curr->addr[2] & 0xFF, curr->addr[3] & 0xFF,
336 curr->addr[4] & 0xFF, curr->addr[5] & 0xFF);
337 write(fd, message, 13);