X-Git-Url: https://git.decadent.org.uk/gitweb/?p=ap-utils.git;a=blobdiff_plain;f=lib%2Ffile.c;h=e39d7d3431b3ca645dc812f53d2cd1614db15e95;hp=7c835a4b32c4d99a1ade40dc2859a0108fc9dd0a;hb=67fc54773e1504698e80c9cb83977bde32665350;hpb=6a9e980b8799891e0a5467a0126a60b169c8f4eb diff --git a/lib/file.c b/lib/file.c index 7c835a4..e39d7d3 100644 --- a/lib/file.c +++ b/lib/file.c @@ -35,10 +35,12 @@ extern int atmel410_filter; #define MAX_LINES LINES-6 struct APList { - char *ip; - char *passwd; - int type; - struct APList * next; + char *ip; + char *passwd; + int type; + char *name; + + struct APList * next; }; void @@ -46,278 +48,329 @@ _scroll_rows(struct APList *first, int begin, int end) { int i = 1; struct APList *curr = first; - char message[50]; + char message[56]; clear_main(3); while (i++ < begin) curr = curr->next; - i = 0; + + i = 0; while (end-- > begin) { - sprintf(message, "%5u %15s %s", begin + i, curr->ip, ap_types[curr->type]); + sprintf(message, "%5u %15s %s %s", begin + i, curr->ip, + ap_types[curr->type], curr->name); mvwaddstr(main_sub, 2 + i, 1, message); - i++; - curr = curr->next; + i++; + curr = curr->next; } wrefresh(main_sub); } struct APList *parse_db_str(char *str) { - struct APList *curr=NULL; - char *ip=NULL, *passwd=NULL, mess[1024]; - int i=0, pos=0, j=0; - - while(str[i] != '\0') { - if(str[i] == ':') { - switch (pos) { - case 0: - ip = (char *) malloc(j+1); - memcpy(ip, mess, j+1); - ip[j] = '\0'; - break; - case 1: - passwd = (char *) malloc(j+1); - memcpy(passwd, mess, j+1); - passwd[j] = '\0'; - break; - } - i++; - j=0; - pos++; - - } - else - mess[j++] = str[i++]; - } - - mess[j]='\0'; - if (pos==2 && ip && passwd && ((atmel410_filter && atoi(mess)== ATMEL410) || !atmel410_filter)) { - curr = (struct APList *) malloc(sizeof(struct APList)); - curr->type = atoi(mess); - curr->next = NULL; - curr->ip = (char *) malloc(strlen(ip)+1); - strcpy(curr->ip, ip); - curr->passwd = (char *) malloc(strlen(passwd)+1); - strcpy(curr->passwd, passwd); - } + struct APList *curr=NULL; + char *ip=NULL, *passwd=NULL, *name=NULL, mess[1024]; + int i=0, pos=0, j=0; + + while(str[i] != '\0') { + if(str[i] == ':') { + switch (pos) { + case 0: + ip = (char *) malloc(j+1); + memcpy(ip, mess, j+1); + ip[j] = '\0'; + break; + case 1: + passwd = (char *) malloc(j+1); + memcpy(passwd, mess, j+1); + passwd[j] = '\0'; + break; + case 2: + name = (char *) malloc(j+1); + memcpy(name, mess, j+1); + name[j] = '\0'; + break; + } + i++; + j=0; + pos++; + } else + mess[j++] = str[i++]; + + } + + mess[j]='\0'; + if (pos==3 && ip && passwd && ((atmel410_filter && atoi(mess) == ATMEL410) + || !atmel410_filter)) { + curr = (struct APList *) malloc(sizeof(struct APList)); + curr->type = atoi(mess); + curr->next = NULL; + curr->ip = (char *) malloc(strlen(ip) + 1); + strcpy(curr->ip, ip); + curr->passwd = (char *) malloc(strlen(passwd) + 1); + strcpy(curr->passwd, passwd); + curr->name = (char *) malloc(strlen(name) + 1); + strcpy(curr->name, name); + } - if(passwd) - free(passwd); - if(ip) - free(ip); - - return curr; -} + if (ip) + free(ip); + if (passwd) + free(passwd); + + if (name) + free(name); + + return curr; +} int get_opts() { - extern char *community; - extern struct in_addr ap_ip; - extern int sockfd; + extern char *community; + extern struct in_addr ap_ip; + extern int sockfd; - char *home_dir, buf[1024], mess[64]; - int c; - int fd, rval=0, pos; - signed int j, i, begin, end, record_num=0; - char message[50]; - struct APList *first=NULL, *curr=NULL, *pmac; + char *home_dir, buf[1024], mess[64]; + char message[50]; + int c, fd, rval=0, pos; + signed int j, i, begin, end, record_num=0; + struct APList *first=NULL, *curr=NULL, *pmac; struct sockaddr_in client; + memset(&client, 0, sizeof client); client.sin_family = AF_INET; client.sin_port = INADDR_ANY; client.sin_addr.s_addr = INADDR_ANY; - - if ((home_dir = getenv("HOME")) == NULL) + if ((home_dir = getenv("HOME")) == NULL) return 0; + sprintf(buf, "%s/.ap-config", home_dir); if ((fd = open(buf, O_RDONLY)) == -1) return 0; + pos=0; - while((j = read(fd, buf, sizeof(buf)))>0) - for(i=0; i < j; i++) { - if (buf[i] == 0x0a){ + while((j = read(fd, buf, sizeof(buf))) > 0) + for(i=0; i < j; i++) { + if (buf[i] == 0x0a) { mess[pos]='\0'; - if (first == NULL) { - if ((first = parse_db_str(mess))!=NULL) { - curr=first; - record_num=1; - } - } else { - if ((curr->next = parse_db_str(mess)) != NULL) { - curr = curr->next; - record_num++; + if (first == NULL) { + if ((first = parse_db_str(mess)) != NULL) { + curr=first; + record_num=1; + } + } else { + if ((curr->next = parse_db_str(mess)) != NULL) { + curr = curr->next; + record_num++; + } } - } pos=0; - } else + } else mess[pos++] = buf[i]; - } - mess[pos]='\0'; - if (first == NULL) { - if ((first = parse_db_str(mess))!=NULL) { - curr=first; - record_num=1; - } - } else { - if ((curr->next = parse_db_str(mess)) != NULL) - curr = curr->next; - record_num++; - } + } + + mess[pos]='\0'; + if (first == NULL) { + if ((first = parse_db_str(mess)) != NULL) { + curr=first; + record_num=1; + } + } else { + if ((curr->next = parse_db_str(mess)) != NULL) + curr = curr->next; + + record_num++; + } close(fd); - if(!record_num) - return 0; - - mvwaddstr(main_sub, 0, 3, _("NUM IP ADDRESS TYPE")); - print_title(_("Choose an AP to connect to")); + if (!record_num) + return 0; + + mvwaddstr(main_sub, 0, 3, + _("NUM IP ADDRESS TYPE NAME (in ~/.ap-config)")); + print_title(_("Choose an AP to connect to")); begin = 1; end = (MAX_LINES < record_num) ? MAX_LINES : record_num; _scroll_rows(first, begin, end); - noecho(); - print_help(_("1-9,C: connect; N: new; D: delete; W: save; Q: quit; arrows: scroll")); - while (1) { - switch (c = getch()) { - case 'q': - case 'Q': - exit_program(); - case 'n': - case 'N': - goto quit; - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - i = c - '0'; - if (record_num <= i || i <= 0) - goto wrong_num; - curr = first; - while (--i > 0) - curr = curr->next; - inet_aton(curr->ip, &ap_ip); - if (community) - free(community); - i = strlen(curr->passwd) + 1; - community = (char *) malloc(i); - strncpy(community, curr->passwd, i); - ap_type = curr->type; - rval=1; - if (sockfd) - close(sockfd); - if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) - rval=0; - if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1) - rval=0; - print_bottom(inet_ntoa(ap_ip)); - goto quit; - case 'c': - case 'C': - mvwaddstr(main_sub, 1, 1, _("Connect to AP num:")); - get_value(message, 1, 20, 6, INT_STRING, - 1, record_num - 1, NULL); - i = atoi(message); - curr = first; - while (--i > 0) - curr = curr->next; - inet_aton(curr->ip, &ap_ip); - if (community) - free(community); - i = strlen(curr->passwd) + 1; - community = (char *) malloc(i); - strncpy(community, curr->passwd, i); - ap_type = curr->type; - rval=1; - if (sockfd) - close(sockfd); - if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) - rval=0; - if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1) - rval=0; - print_bottom(inet_ntoa(ap_ip)); - goto quit; - case 'd': - case 'D': - mvwaddstr(main_sub, 1, 0, _("Delete num:")); - get_value(message, 1, 15, 6, INT_STRING, - 1, record_num - 1, NULL); - i = atoi(message); - if (i == 1) { - curr = first; - first = first->next; - free(curr); - } else { - curr = first; - while (--i > 1) - curr = curr->next; - pmac = curr->next; - curr->next = pmac->next; - free(pmac); - } - record_num--; - begin = 1; - end = (MAX_LINES < record_num) ? MAX_LINES : record_num; - _scroll_rows(first, begin, end); - wrong_num: - clear_main_new(1, 2); - continue; - case KEY_DOWN: - case KEY_RIGHT: - if (end < record_num) { - begin++; - end++; - _scroll_rows(first, begin, end); - } - - continue; - case KEY_UP: - case KEY_LEFT: - if (begin > 1) { - begin--; - end--; - _scroll_rows(first, begin, end); - } - continue; - - case 'w': - case 'W': + noecho(); + print_help(_("1-9,C: connect; N: new; D: delete; W: save; Q: quit; arrows: scroll")); + while (1) { + switch (c = getch()) { + case 'q': + case 'Q': + exit_program(); + + case 'n': + case 'N': + goto quit; + + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + i = c - '0'; + if (record_num <= i || i <= 0) + goto wrong_num; + + curr = first; + while (--i > 0) + curr = curr->next; + + inet_aton(curr->ip, &ap_ip); + if (community) + free(community); + + i = strlen(curr->passwd) + 1; + community = (char *) malloc(i); + strncpy(community, curr->passwd, i); + ap_type = curr->type; + rval=1; + if (sockfd) + close(sockfd); + + if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) + rval=0; + + if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1) + rval=0; + + print_bottom(inet_ntoa(ap_ip)); + get_mib_details(); + goto quit; + + case 'c': + case 'C': + /* Nothing to connect */ + if (record_num == 1) + continue; + + mvwaddstr(main_sub, 1, 1, _("Connect to AP num:")); + get_value(message, 1, 20, 6, INT_STRING, 1, record_num - 1, + NULL); + i = atoi(message); + curr = first; + while (--i > 0) + curr = curr->next; + + inet_aton(curr->ip, &ap_ip); + if (community) + free(community); + + i = strlen(curr->passwd) + 1; + community = (char *) malloc(i); + strncpy(community, curr->passwd, i); + ap_type = curr->type; + rval=1; + if (sockfd) + close(sockfd); + + if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) + rval=0; + + if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1) + rval=0; + + print_bottom(inet_ntoa(ap_ip)); + get_mib_details(); + goto quit; + + case 'd': + case 'D': + /* Nothing to delete */ + if (record_num == 1) + continue; + + mvwaddstr(main_sub, 1, 0, _("Delete num:")); + get_value(message, 1, 15, 6, INT_STRING, + 1, (record_num == 1 ? 1 : record_num - 1), NULL); + i = atoi(message); + if (i == 1) { + curr = first; + first = first->next; + free(curr); + } else { + curr = first; + while (--i > 1) + curr = curr->next; + + pmac = curr->next; + curr->next = pmac->next; + free(pmac); + } + record_num--; + /* Clear incl. line with last AP record */ + if (record_num == 1) { + clear_main_new(1, 3); + continue; + } + + begin = 1; + end = (MAX_LINES < record_num) ? MAX_LINES : record_num; + _scroll_rows(first, begin, end); +wrong_num: + clear_main_new(1, 2); + continue; + + case KEY_DOWN: + case KEY_RIGHT: + if (end < record_num) { + begin++; + end++; + _scroll_rows(first, begin, end); + } + continue; + + case KEY_UP: + case KEY_LEFT: + if (begin > 1) { + begin--; + end--; + _scroll_rows(first, begin, end); + } + continue; + + case 'w': + case 'W': sprintf(buf, "%s/.ap-config", home_dir); - if ((fd = creat(buf, 0600)) != -1) { - curr=first; - while (curr) { - sprintf(buf, "%s:%s:%d\n", curr->ip, curr->passwd, curr->type); - write(fd, buf, strlen(buf)); - curr = curr->next; - } - close(fd); - } - continue; + if ((fd = creat(buf, 0600)) != -1) { + curr=first; + while (curr) { + sprintf(buf, "%s:%s:%d\n", curr->ip, curr->passwd, + curr->type); + write(fd, buf, strlen(buf)); + curr = curr->next; + } + close(fd); + } + continue; + } -} + } /* print_help(ANY_KEY); getch(); */ quit: while ((curr = first)) { - first = curr->next; - free(curr->ip); - free(curr->passwd); - free(curr); - } - print_help(""); - print_title(""); - wclear(main_sub); - wrefresh(main_sub); - return rval; + first = curr->next; + free(curr->ip); + free(curr->passwd); + free(curr); + } + print_help(""); + print_title(""); + wclear(main_sub); + wrefresh(main_sub); + return rval; } void save_Stations(struct MacListStat *curr) @@ -326,14 +379,13 @@ void save_Stations(struct MacListStat *curr) char *home_dir; char message[1024]; if ((home_dir = getenv("HOME"))) { - sprintf(message, "%s/ap-%s.stations", home_dir, - ap_types[ap_type]); + sprintf(message, "%s/ap-%s.stations", home_dir, ap_types[ap_type]); if ((fd = creat(message, 0600)) != -1) { while (curr) { sprintf(message, "%02X%02X%02X%02X%02X%02X\n", - curr->addr[0] & 0xFF, curr->addr[1] & 0xFF, - curr->addr[2] & 0xFF, curr->addr[3] & 0xFF, - curr->addr[4] & 0xFF, curr->addr[5] & 0xFF); + curr->addr[0] & 0xFF, curr->addr[1] & 0xFF, + curr->addr[2] & 0xFF, curr->addr[3] & 0xFF, + curr->addr[4] & 0xFF, curr->addr[5] & 0xFF); write(fd, message, 13); curr = curr->next; }