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;
35 #define MAX_LINES LINES-6
48 _scroll_rows(struct APList *first, int begin, int end)
51 struct APList *curr = first;
60 while (end-- > begin) {
61 sprintf(message, " %3u %-15s %-10s %-8s %-16s", begin + i,
62 curr->ip, ap_types[curr->type],
63 ap_vendorexts[curr->type][curr->vendorext], curr->label);
64 mvwaddstr(main_sub, 2 + i, 0, message);
71 struct APList *parse_db_str(char *str)
73 struct APList *curr=NULL;
74 char *ip=NULL, *passwd=NULL, *label=NULL, *aptype=NULL, mess[1024];
77 while(str[i] != '\0') {
81 ip = (char *) malloc(j+1);
82 memcpy(ip, mess, j+1);
86 passwd = (char *) malloc(j+1);
87 memcpy(passwd, mess, j+1);
91 label = (char *) malloc(j+1);
92 memcpy(label, mess, j+1);
93 label[j > 16 ? 16 : j] = '\0';
96 aptype = (char *) malloc(j+1);
97 memcpy(aptype, mess, j+1);
105 mess[j++] = str[i++];
110 if (pos==4 && ip && passwd && ((atmel410_filter && atoi(aptype) == ATMEL410)
111 || !atmel410_filter)) {
112 curr = (struct APList *) malloc(sizeof(struct APList));
113 curr->type = atoi(aptype);
114 curr->vendorext = atoi(mess);
116 curr->ip = (char *) malloc(strlen(ip) + 1);
117 strcpy(curr->ip, ip);
118 curr->passwd = (char *) malloc(strlen(passwd) + 1);
119 strcpy(curr->passwd, passwd);
120 curr->label = (char *) malloc(strlen(label) + 1);
121 strcpy(curr->label, label);
141 extern char *community;
142 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;
150 struct sockaddr_in client;
153 memset(&client, 0, sizeof client);
154 client.sin_family = AF_INET;
155 client.sin_port = INADDR_ANY;
156 client.sin_addr.s_addr = INADDR_ANY;
158 if ((home_dir = getenv("HOME")) == NULL)
161 sprintf(buf, "%s/.ap-config", home_dir);
162 if ((fd = open(buf, O_RDONLY)) == -1)
166 while((j = read(fd, buf, sizeof(buf))) > 0)
167 for(i=0; i < j; i++) {
168 if (buf[i] == 0x0a) {
171 if ((first = parse_db_str(mess)) != NULL) {
176 if ((curr->next = parse_db_str(mess)) != NULL) {
183 mess[pos++] = buf[i];
189 if ((first = parse_db_str(mess)) != NULL) {
194 if ((curr->next = parse_db_str(mess)) != NULL)
204 mvwaddstr(main_sub, 0, 2,
205 _("NUM IP ADDRESS MIB TYPE MIB EXT. LABEL"));
206 print_top(NULL, _("Choose an AP to connect to"));
208 end = (MAX_LINES < record_num) ? MAX_LINES : record_num;
209 _scroll_rows(first, begin, end);
212 print_help(_("1-9,C: connect; N: new; D: delete; W: save; Q: quit; arrows: scroll"));
213 switch (c = getch()) {
233 if (record_num <= i || i <= 0)
240 inet_aton(curr->ip, &ap_ip);
244 i = strlen(curr->passwd) + 1;
245 community = (char *) malloc(i);
246 strncpy(community, curr->passwd, i);
247 ap_type = curr->type;
248 ap_vendorext = curr->vendorext;
253 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
256 if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1)
259 print_bottom(inet_ntoa(ap_ip));
264 /* Nothing to connect */
268 mvwaddstr(main_sub, 1, 1, _("Connect to AP num:"));
269 get_value(message, 1, 20, 6, INT_STRING, 1, record_num - 1,
276 inet_aton(curr->ip, &ap_ip);
280 i = strlen(curr->passwd) + 1;
281 community = (char *) malloc(i);
282 strncpy(community, curr->passwd, i);
283 ap_type = curr->type;
284 ap_vendorext = curr->vendorext;
289 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
292 if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1)
295 print_bottom(inet_ntoa(ap_ip));
300 /* Nothing to delete */
304 mvwaddstr(main_sub, 1, 0, _("Delete num:"));
305 get_value(message, 1, 15, 6, INT_STRING,
306 1, (record_num == 1 ? 1 : record_num - 1), NULL);
321 curr->next = pmac->next;
328 /* Clear incl. line with last AP record */
329 if (record_num == 1) {
330 clear_main_new(1, 3);
335 end = (MAX_LINES < record_num) ? MAX_LINES : record_num;
336 _scroll_rows(first, begin, end);
338 clear_main_new(1, 2);
343 if (end < record_num) {
346 _scroll_rows(first, begin, end);
355 _scroll_rows(first, begin, end);
361 sprintf(buf, "%s/.ap-config", home_dir);
362 if ((fd = creat(buf, 0600)) != -1) {
365 sprintf(buf, "%s:%s:%s:%d:%d\n", curr->ip, curr->passwd,
366 curr->label,curr->type, curr->vendorext);
367 write(fd, buf, strlen(buf));
372 _("AP list file ~/.ap-config successfully written. "
376 _("Unable to write AP list file ~/.ap-config. "
387 while ((curr = first)) {
395 print_top(NULL, NULL);
401 void save_Stations(struct MacListStat *curr)
406 if ((home_dir = getenv("HOME"))) {
407 sprintf(message, "%s/ap-%s-%s-%s.stations", home_dir, inet_ntoa(ap_ip),
408 ap_types[ap_type],ap_vendorexts[ap_type][ap_vendorext]);
409 if ((fd = creat(message, 0600)) != -1) {
411 sprintf(message, "%02X%02X%02X%02X%02X%02X\n",
412 curr->addr[0] & 0xFF, curr->addr[1] & 0xFF,
413 curr->addr[2] & 0xFF, curr->addr[3] & 0xFF,
414 curr->addr[4] & 0xFF, curr->addr[5] & 0xFF);
415 write(fd, message, 13);
426 print_helperr(_("Unable to write stations file. Press any key."));
428 print_help(_("Stations file succesfully written. Press any key."));