/*********** Signal function **************/ static void refresh_sig(void) { int ret; char buf[32]; NMP_DEBUG("Refresh network map!\n"); networkmap_fullscan = 1; refresh_exist_table = 0; scan_count = 0; #if 0 nvram_set("networkmap_status", "1"); nvram_set("networkmap_fullscan", "1"); eval("rm", "-rf", "/var/client*"); #else //Andy Chiu, 2014/10/21 SAFE_FREE(nmp_client_list); //nmp_client_list = strdup(""); ret = tcapi_set("ClientList_Common", "scan", "1"); if(ret) NMP_DEBUG("set scan flag failed(%d).\n", ret); ret = tcapi_set("ClientList_Common", "size", "0"); if(ret) NMP_DEBUG("set cl size failed(%d).\n", ret); unlink(cl_path); ret = tcapi_unset("ClientList_Entry"); if(ret) NMP_DEBUG("unset entry(%s) failed(%d).\n", buf, ret); #endif }
void add_custom(int unit, char *p[]) { char node[MAXLEN_NODE_NAME]; char custom[MAXLEN_TCAPI_MSG] = {0}; char *param = NULL; char *final_custom = NULL; int i = 0, size = 0; if(!p[0]) return; while(p[i]) { size += strlen(p[i]) + 1; i++; } param = (char*)calloc(size, sizeof(char)); if(!param) return; i = 0; while(p[i]) { if(*param) strcat(param, " "); strcat(param, p[i]); i++; } snprintf(node, sizeof(node), "OpenVPN_Entry%d", unit + CLIENT_IF_START); tcapi_get(node, "custom", custom); if(strlen(custom)) { if(strlen(custom) + strlen(param) + 1 > MAXLEN_TCAPI_MSG) { logmessage ("OVPN", "Too many custom configuration"); free(param); return; } final_custom = calloc(strlen(custom) + strlen(param) + 2, sizeof(char)); if(final_custom) { if(*custom) { strcat(final_custom, custom); strcat(final_custom, "\n"); } strcat(final_custom, param); tcapi_set(node, "custom", final_custom); free(final_custom); } } else tcapi_set(node, "custom", param); free(param); }
void setup_udp_timeout(int connflag) { unsigned int v[10]; const char *p; char buf[70]; if (connflag // #ifdef RTCONFIG_WIRELESSREPEATER // && nvram_get_int("sw_mode")!=SW_MODE_REPEATER // #endif ) { tcapi_get("SysInfo_Entry", "ct_udp_timeout", buf); p = buf; // p = nvram_safe_get("ct_udp_timeout"); if (sscanf(p, "%u%u", &v[0], &v[1]) == 2) { write_udp_timeout(NULL, v[0]); write_udp_timeout("stream", v[1]); } else { v[0] = read_udp_timeout(NULL); v[1] = read_udp_timeout("stream"); sprintf(buf, "%u %u", v[0], v[1]); // nvram_set("ct_udp_timeout", buf); tcapi_set("SysInfo_Entry", "ct_udp_timeout", buf); } } else { write_udp_timeout(NULL, 1); write_udp_timeout("stream", 6); } }
void setup_ct_timeout(int connflag) { unsigned int v[10]; const char *p; char buf[70]; int i; if (connflag // #ifdef RTCONFIG_WIRELESSREPEATER // && nvram_get_int("sw_mode")!=SW_MODE_REPEATER // #endif ) { tcapi_get("SysInfo_Entry", "ct_udp_timeout", buf); p = buf; // p = nvram_safe_get("ct_timeout"); if (sscanf(p, "%u%u", &v[0], &v[1]) == 2) { // write_ct_timeout("generic", NULL, v[0]); write_ct_timeout("icmp", NULL, v[1]); } else { v[0] = read_ct_timeout("generic", NULL); v[1] = read_ct_timeout("icmp", NULL); sprintf(buf, "%u %u", v[0], v[1]); // nvram_set("ct_timeout", buf); tcapi_set("SysInfo_Entry", "ct_udp_timeout", buf); } } else { for (i = 0; i < 3; i++) { if (scan_icmp_unreplied_conntrack() > 0) { // write_ct_timeout("generic", NULL, 0); write_ct_timeout("icmp", NULL, 0); sleep(2); } } } }
int main(int argc, char *argv[]) { int arp_sockfd, arp_getlen, i; int send_count=0, file_num=0; struct sockaddr_in router_addr, device_addr; char router_ipaddr[17], router_mac[17], buffer[ARP_BUFFER_SIZE]; unsigned char scan_ipaddr[4]; // scan ip FILE *fp_ip, *fp; fd_set rfds; ARP_HEADER * arp_ptr; struct timeval tv1, tv2, arp_timeout; int shm_client_detail_info_id; int ip_dup, mac_dup, real_num; int lock; int ret; int pid, flag; //Andy Chiu, 2015/06/16. Checking the networkmap exist. unsigned short msg_type; #if defined(RTCONFIG_QCA) && defined(RTCONFIG_WIRELESSREPEATER) char *mac; #endif signal(SIGTERM, nwmap_sig_term); //Andy Chiu, 2015/06/12. Add for terminate signal handling loop = 1; //Andy Chiu, 2015/06/16. check otehr networkmap exist. flag = 0; fp = fopen("/var/run/networkmap.pid", "r"); if(fp) { if(fscanf(fp, "%d", &pid) > 0) { sprintf(buffer, "/proc/%d", pid); if(!access(buffer, F_OK)) flag = 1; } fclose(fp); } if(flag) { NMP_DEBUG("[%s, %d]networkmap is already running now.\n", __FUNCTION__, __LINE__); return 0; } fp = fopen("/var/run/networkmap.pid", "w"); if(fp != NULL){ fprintf(fp, "%d", getpid()); fclose(fp); } #ifdef DEBUG eval("rm", "/var/client*"); #endif //Initial client tables lock = file_lock("networkmap"); shm_client_detail_info_id = shmget((key_t)1001, sizeof(CLIENT_DETAIL_INFO_TABLE), 0666|IPC_CREAT); if (shm_client_detail_info_id == -1){ fprintf(stderr,"shmget failed\n"); file_unlock(lock); exit(1); } CLIENT_DETAIL_INFO_TABLE *p_client_detail_info_tab = (P_CLIENT_DETAIL_INFO_TABLE)shmat(shm_client_detail_info_id,(void *) 0,0); //Reset shared memory memset(p_client_detail_info_tab, 0x00, sizeof(CLIENT_DETAIL_INFO_TABLE)); p_client_detail_info_tab->ip_mac_num = 0; p_client_detail_info_tab->detail_info_num = 0; file_unlock(lock); #ifdef NMP_DB nmp_client_list = NULL; if(!load_db(&nmp_client_list) && nmp_client_list) NMP_DEBUG_M("NMP Client:\n%s\n", nmp_client_list); signal(SIGUSR2, reset_db); #endif //Get Router's IP/Mac //Andy Chiu, 2014/10/22. tcapi_get("Info_Ether", "ip", router_ipaddr); tcapi_get("Info_Ether", "mac", router_mac); inet_aton(router_ipaddr, &router_addr.sin_addr); memcpy(my_ipaddr, &router_addr.sin_addr, 4); //Prepare scan networkmap_fullscan = 1; //Andy Chiu, 2014/10/22. if(tcapi_set("ClientList_Common", "scan", "1")) NMP_DEBUG_M("set node(ClientList_Common:scan) failed.\n"); if (argc > 1) { if (strcmp(argv[1], "--bootwait") == 0) { sleep(30); //Andy Chiu, 2014/12/09 if(!access("/var/static_arp_tbl.sh", F_OK)) system("/var/static_arp_tbl.sh"); } } if (strlen(router_mac)!=0) ether_atoe(router_mac, my_hwaddr); signal(SIGUSR1, refresh_sig); //catch UI refresh signal // create UDP socket and bind to "br0" to get ARP packet// arp_sockfd = create_socket(INTERFACE); if(arp_sockfd < 0) perror("create socket ERR:"); else { arp_timeout.tv_sec = 0; arp_timeout.tv_usec = 50000; setsockopt(arp_sockfd, SOL_SOCKET, SO_RCVTIMEO, &arp_timeout, sizeof(arp_timeout));//set receive timeout dst_sockll = src_sockll; //Copy sockaddr info to dst memset(dst_sockll.sll_addr, -1, sizeof(dst_sockll.sll_addr)); // set dmac= FF:FF:FF:FF:FF:FF } while(loop)//main while loop { while(loop) { //full scan and reflush recv buffer fullscan: if(networkmap_fullscan == 1) { //Scan all IP address in the subnetwork if(scan_count == 0) { eval("asusdiscovery"); //find asus device // (re)-start from the begining memset(scan_ipaddr, 0x00, 4); memcpy(scan_ipaddr, &router_addr.sin_addr, 3); arp_timeout.tv_sec = 0; arp_timeout.tv_usec = 50000; setsockopt(arp_sockfd, SOL_SOCKET, SO_RCVTIMEO, &arp_timeout, sizeof(arp_timeout));//set receive timeout NMP_DEBUG("Starting full scan!\n"); //Andy Chiu, 2014/10/22. remove unused flag, "refresh_networkmap". #if 0 if(nvram_match("refresh_networkmap", "1")) //reset client tables { lock = file_lock("networkmap"); memset(p_client_detail_info_tab, 0x00, sizeof(CLIENT_DETAIL_INFO_TABLE)); //p_client_detail_info_tab->detail_info_num = 0; //p_client_detail_info_tab->ip_mac_num = 0; file_unlock(lock); nvram_unset("refresh_networkmap"); } else { int x = 0; for(; x<255; x++) p_client_detail_info_tab->exist[x]=0; } #else memset(p_client_detail_info_tab, 0x00, sizeof(CLIENT_DETAIL_INFO_TABLE)); #endif } scan_count++; scan_ipaddr[3]++; if( scan_count<255 && memcmp(scan_ipaddr, my_ipaddr, 4) ) { sent_arppacket(arp_sockfd, scan_ipaddr); } else if(scan_count==255) { //Scan completed arp_timeout.tv_sec = 2; arp_timeout.tv_usec = 0; //Reset timeout at monitor state for decase cpu loading setsockopt(arp_sockfd, SOL_SOCKET, SO_RCVTIMEO, &arp_timeout, sizeof(arp_timeout));//set receive timeout networkmap_fullscan = 0; //scan_count = 0; //nvram_set("networkmap_fullscan", "0"); if(tcapi_set("ClientList_Common", "scan", "0")) NMP_DEBUG_M("set node(ClientList_Common:scan) failed.\n"); NMP_DEBUG("Finish full scan!\n"); } }// End of full scan memset(buffer, 0, ARP_BUFFER_SIZE); arp_getlen=recvfrom(arp_sockfd, buffer, ARP_BUFFER_SIZE, 0, NULL, NULL); if(arp_getlen == -1) { if( scan_count<255) goto fullscan; else break; } else { arp_ptr = (ARP_HEADER*)(buffer); NMP_DEBUG("*Receive an ARP Packet from: %d.%d.%d.%d to %d.%d.%d.%d:%02X:%02X:%02X:%02X - len:%d\n", (int *)arp_ptr->source_ipaddr[0],(int *)arp_ptr->source_ipaddr[1], (int *)arp_ptr->source_ipaddr[2],(int *)arp_ptr->source_ipaddr[3], (int *)arp_ptr->dest_ipaddr[0],(int *)arp_ptr->dest_ipaddr[1], (int *)arp_ptr->dest_ipaddr[2],(int *)arp_ptr->dest_ipaddr[3], arp_ptr->dest_hwaddr[0],arp_ptr->dest_hwaddr[1], arp_ptr->dest_hwaddr[2],arp_ptr->dest_hwaddr[3], arp_getlen); //Check ARP packet if source ip and router ip at the same network if( !memcmp(my_ipaddr, arp_ptr->source_ipaddr, 3) ) { msg_type = ntohs(arp_ptr->message_type); if( //ARP packet to router ( msg_type == 0x02 && // ARP response memcmp(arp_ptr->dest_ipaddr, my_ipaddr, 4) == 0 && // dest IP memcmp(arp_ptr->dest_hwaddr, my_hwaddr, 6) == 0) // dest MAC || (msg_type == 0x01 && // ARP request memcmp(arp_ptr->dest_ipaddr, my_ipaddr, 4) == 0) // dest IP ){ //NMP_DEBUG(" It's an ARP Response to Router!\n"); NMP_DEBUG("*RCV %d.%d.%d.%d-%02X:%02X:%02X:%02X:%02X:%02X\n", arp_ptr->source_ipaddr[0],arp_ptr->source_ipaddr[1], arp_ptr->source_ipaddr[2],arp_ptr->source_ipaddr[3], arp_ptr->source_hwaddr[0],arp_ptr->source_hwaddr[1], arp_ptr->source_hwaddr[2],arp_ptr->source_hwaddr[3], arp_ptr->source_hwaddr[4],arp_ptr->source_hwaddr[5]); for(i=0; i<p_client_detail_info_tab->ip_mac_num; i++) { ip_dup = memcmp(p_client_detail_info_tab->ip_addr[i], arp_ptr->source_ipaddr, 4); mac_dup = memcmp(p_client_detail_info_tab->mac_addr[i], arp_ptr->source_hwaddr, 6); if((ip_dup == 0) && (mac_dup == 0)) { lock = file_lock("networkmap"); p_client_detail_info_tab->exist[i] = 1; file_unlock(lock); break; } else if((ip_dup != 0) && (mac_dup != 0)) { continue; } else if( (scan_count>=255) && ((ip_dup != 0) && (mac_dup == 0)) ) { NMP_DEBUG("IP changed, update immediately\n"); NMP_DEBUG("*CMP %d.%d.%d.%d-%02X:%02X:%02X:%02X:%02X:%02X\n", p_client_detail_info_tab->ip_addr[i][0],p_client_detail_info_tab->ip_addr[i][1], p_client_detail_info_tab->ip_addr[i][2],p_client_detail_info_tab->ip_addr[i][3], p_client_detail_info_tab->mac_addr[i][0],p_client_detail_info_tab->mac_addr[i][1], p_client_detail_info_tab->mac_addr[i][2],p_client_detail_info_tab->mac_addr[i][3], p_client_detail_info_tab->mac_addr[i][4],p_client_detail_info_tab->mac_addr[i][5]); lock = file_lock("networkmap"); memcpy(p_client_detail_info_tab->ip_addr[i], arp_ptr->source_ipaddr, 4); memcpy(p_client_detail_info_tab->mac_addr[i], arp_ptr->source_hwaddr, 6); p_client_detail_info_tab->exist[i] = 1; file_unlock(lock); /* real_num = p_client_detail_info_tab->detail_info_num; p_client_detail_info_tab->detail_info_num = i; #ifdef NMP_DB check_nmp_db(p_client_detail_info_tab, i); #endif FindAllApp(my_ipaddr, p_client_detail_info_tab); FindHostname(p_client_detail_info_tab); p_client_detail_info_tab->detail_info_num = real_num; */ break; } } //NMP_DEBUG("Out check!\n"); //i=0, table is empty. //i=num, no the same ip at table. if(i==p_client_detail_info_tab->ip_mac_num){ lock = file_lock("networkmap"); memcpy(p_client_detail_info_tab->ip_addr[p_client_detail_info_tab->ip_mac_num], arp_ptr->source_ipaddr, 4); memcpy(p_client_detail_info_tab->mac_addr[p_client_detail_info_tab->ip_mac_num], arp_ptr->source_hwaddr, 6); p_client_detail_info_tab->exist[p_client_detail_info_tab->ip_mac_num] = 1; #ifdef NMP_DB check_nmp_db(p_client_detail_info_tab, i); #endif p_client_detail_info_tab->ip_mac_num++; file_unlock(lock); #ifdef DEBUG //Write client info to file fp_ip=fopen("/var/client_ip_mac.txt", "a"); if (fp_ip==NULL) { NMP_DEBUG("File Open Error!\n"); } else { NMP_DEBUG_M("Fill: %d-> %d.%d\n", i,p_client_detail_info_tab->ip_addr[i][2],p_client_detail_info_tab->ip_addr[i][3]); fprintf(fp_ip, "%d.%d.%d.%d,%02X:%02X:%02X:%02X:%02X:%02X\n", p_client_detail_info_tab->ip_addr[i][0],p_client_detail_info_tab->ip_addr[i][1], p_client_detail_info_tab->ip_addr[i][2],p_client_detail_info_tab->ip_addr[i][3], p_client_detail_info_tab->mac_addr[i][0],p_client_detail_info_tab->mac_addr[i][1], p_client_detail_info_tab->mac_addr[i][2],p_client_detail_info_tab->mac_addr[i][3], p_client_detail_info_tab->mac_addr[i][4],p_client_detail_info_tab->mac_addr[i][5]); } fclose(fp_ip); #endif } } else { //Nomo ARP Packet or ARP response to other IP //Compare IP and IP buffer if not exist for(i=0; i<p_client_detail_info_tab->ip_mac_num; i++) { if( !memcmp(p_client_detail_info_tab->ip_addr[i], arp_ptr->source_ipaddr, 4) && !memcmp(p_client_detail_info_tab->mac_addr[i], arp_ptr->source_hwaddr, 6)) { NMP_DEBUG_M("Find the same IP/MAC at the table!\n"); break; } } if( i==p_client_detail_info_tab->ip_mac_num ) //Find a new IP or table is empty! Send an ARP request. { NMP_DEBUG("New device or IP/MAC changed!!\n"); if(memcmp(my_ipaddr, arp_ptr->source_ipaddr, 4)) sent_arppacket(arp_sockfd, arp_ptr->source_ipaddr); else NMP_DEBUG("New IP is the same as Router IP! Ignore it!\n"); } }//End of Nomo ARP Packet }//Source IP in the same subnetwork }//End of arp_getlen != -1 } // End of while for flush buffer if(!loop) break; /* int y = 0; while(p_client_detail_info_tab->type[y]!=0){ NMP_DEBUG("%d: %d.%d.%d.%d,%02X:%02X:%02X:%02X:%02X:%02X,%s,%d,%d,%d,%d,%d\n", y , p_client_detail_info_tab->ip_addr[y][0],p_client_detail_info_tab->ip_addr[y][1], p_client_detail_info_tab->ip_addr[y][2],p_client_detail_info_tab->ip_addr[y][3], p_client_detail_info_tab->mac_addr[y][0],p_client_detail_info_tab->mac_addr[y][1], p_client_detail_info_tab->mac_addr[y][2],p_client_detail_info_tab->mac_addr[y][3], p_client_detail_info_tab->mac_addr[y][4],p_client_detail_info_tab->mac_addr[y][5], p_client_detail_info_tab->device_name[y], p_client_detail_info_tab->type[y], p_client_detail_info_tab->http[y], p_client_detail_info_tab->printer[y], p_client_detail_info_tab->itune[y], p_client_detail_info_tab->exist[y]); y++; } */ //Find All Application of clients //NMP_DEBUG("\ndetail ? ip : %d ? %d\n\n", p_client_detail_info_tab->detail_info_num, p_client_detail_info_tab->ip_mac_num); if(p_client_detail_info_tab->detail_info_num < p_client_detail_info_tab->ip_mac_num) { //nvram_set("networkmap_status", "1"); if(tcapi_set("ClientList_Common", "status", "1")) NMP_DEBUG_M("set node(ClientList_Common:status) failed.\n"); FindAllApp(my_ipaddr, p_client_detail_info_tab); FindHostname(p_client_detail_info_tab); #ifdef DEBUG //Fill client detail info table fp_ip=fopen("/var/client_detail_info.txt", "a"); if (fp_ip==NULL) { NMP_DEBUG("File Open Error!\n"); } else { fprintf(fp_ip, "%s,%d,%d,%d,%d\n", p_client_detail_info_tab->device_name[p_client_detail_info_tab->detail_info_num], p_client_detail_info_tab->type[p_client_detail_info_tab->detail_info_num], p_client_detail_info_tab->http[p_client_detail_info_tab->detail_info_num], p_client_detail_info_tab->printer[p_client_detail_info_tab->detail_info_num], p_client_detail_info_tab->itune[p_client_detail_info_tab->detail_info_num]); fclose(fp_ip); } #endif #ifdef NMP_DB write_to_cfg_node(p_client_detail_info_tab); #endif p_client_detail_info_tab->detail_info_num++; } #ifdef NMP_DB else { NMP_DEBUG_M("commit_no, cli_no, updated: %d, %d, %d\n", commit_no, p_client_detail_info_tab->detail_info_num, client_updated); if( (commit_no != p_client_detail_info_tab->detail_info_num) || client_updated ) { NMP_DEBUG("Commit nmp client list\n"); //nvram_commit(); commit_no = p_client_detail_info_tab->detail_info_num; client_updated = 0; } } #endif if(p_client_detail_info_tab->detail_info_num == p_client_detail_info_tab->ip_mac_num) { //nvram_set("networkmap_status", "0"); // Done scanning and resolving NMP_DEBUG_M("Scan Finish!\n"); if(tcapi_set("ClientList_Common", "status", "0")) NMP_DEBUG_M("set node(ClientList_Common:status) failed.\n"); } } //End of main while loop SAFE_FREE(p_client_detail_info_tab); close(arp_sockfd); return 0; }
void write_to_cfg_node(CLIENT_DETAIL_INFO_TABLE *p_client_tab) { char new_mac[13], *dst_list; char *nv, *nvp, *b = NULL, *search_list = NULL; char *db_mac, *db_user_def, *db_device_name, *db_type, *db_http, *db_printer, *db_itune; FILE *fp = NULL; convert_mac_to_string(p_client_tab->mac_addr[p_client_tab->detail_info_num], new_mac); //Andy Chiu, 2014/10/27. get station list int count = 0; int ret = 0; char *tmpbuf = NULL; NMP_DEBUG("*** write_to_cfg_node: %s ***\n",new_mac); if(nmp_client_list) { search_list = strdup(nmp_client_list); b = strstr(search_list, new_mac); } if(b!=NULL) { //find the client in the DB dst_list = malloc(sizeof(char)*10000); NMP_DEBUG_M("client data in DB: %s\n", new_mac); nvp = nv = b; *(b-1) = '\0'; strcpy(dst_list, search_list); NMP_DEBUG_M("dst_list= %s\n", dst_list); //b++; while (nv && (b = strsep(&nvp, "<")) != NULL) { if (b == NULL) continue; if (vstrsep(b, ">", &db_mac, &db_user_def, &db_device_name, &db_type, &db_http, &db_printer, &db_itune) != 7) continue; NMP_DEBUG_M("%s,%s,%d,%d,%d,%d\n", db_mac, db_user_def, db_device_name, atoi(db_type), atoi(db_http), atoi(db_printer), atoi(db_itune)); if (!strcmp(p_client_tab->device_name[p_client_tab->detail_info_num], db_device_name) && p_client_tab->type[p_client_tab->detail_info_num] == atoi(db_type) && p_client_tab->http[p_client_tab->detail_info_num] == atoi(db_http) && p_client_tab->printer[p_client_tab->detail_info_num] == atoi(db_printer) && p_client_tab->itune[p_client_tab->detail_info_num] == atoi(db_itune) ) { NMP_DEBUG("DATA the same!\n"); return; } sprintf(dst_list, "%s<%s<%s", dst_list, db_mac, db_user_def); if (strcmp(p_client_tab->device_name[p_client_tab->detail_info_num], "")) { NMP_DEBUG("Update device name: %s.\n", p_client_tab->device_name[p_client_tab->detail_info_num]); sprintf(dst_list, "%s>%s", dst_list, p_client_tab->device_name[p_client_tab->detail_info_num]); } else sprintf(dst_list, "%s>%s", dst_list, db_device_name); if (p_client_tab->type[p_client_tab->detail_info_num] != 6) { client_updated = 1; NMP_DEBUG("Update type: %d\n", p_client_tab->type[p_client_tab->detail_info_num]); sprintf(dst_list, "%s>%d", dst_list, p_client_tab->type[p_client_tab->detail_info_num]); } else sprintf(dst_list, "%s>%s", dst_list, db_type); if (!strcmp(db_http, "0") ) { client_updated = 1; NMP_DEBUG("Update http: %d\n", p_client_tab->http[p_client_tab->detail_info_num]); sprintf(dst_list, "%s>%d", dst_list, p_client_tab->http[p_client_tab->detail_info_num]); } else sprintf(dst_list, "%s>%s", dst_list, db_http); if (!strcmp(db_printer, "0") ) { client_updated = 1; NMP_DEBUG("Update type: %d\n", p_client_tab->printer[p_client_tab->detail_info_num]); sprintf(dst_list, "%s>%d", dst_list, p_client_tab->printer[p_client_tab->detail_info_num]); } else sprintf(dst_list, "%s>%s", dst_list, db_printer); if (!strcmp(db_itune, "0")) { client_updated = 1; NMP_DEBUG("Update type: %d\n", p_client_tab->itune[p_client_tab->detail_info_num]); sprintf(dst_list, "%s>%d", dst_list, p_client_tab->itune[p_client_tab->detail_info_num]); } else sprintf(dst_list, "%s>%s", dst_list, db_itune); if(nvp != NULL) { strcat(dst_list, "<"); strcat(dst_list, nvp); } //Andy Chiu, 2014/10/22. Fix memory leak SAFE_FREE(nmp_client_list); nmp_client_list = strdup(dst_list); SAFE_FREE(dst_list); NMP_DEBUG_M("*** Update nmp_client_list:\n%s\n", nmp_client_list); write_to_file(nmp_client_path, nmp_client_list, strlen(nmp_client_list), "w"); //nvram_set("nmp_client_list", nmp_client_list); break; } } else { //new client NMP_DEBUG_M("new client: %d-%s,%s,%d\n",p_client_tab->detail_info_num, new_mac, p_client_tab->device_name[p_client_tab->detail_info_num], p_client_tab->type[p_client_tab->detail_info_num]); tmpbuf = malloc(10000); if(!tmpbuf) { NMP_DEBUG("[%s, %d]Can't alloc memory\n", __FUNCTION__, __LINE__); return; } //sprintf(nmp_client_list,"%s<%s>>%s>%d>%d>%d>%d", nmp_client_list, sprintf(tmpbuf,"%s<%s>>%s>%d>%d>%d>%d", nmp_client_list? nmp_client_list: "", new_mac, p_client_tab->device_name[p_client_tab->detail_info_num], p_client_tab->type[p_client_tab->detail_info_num], p_client_tab->http[p_client_tab->detail_info_num], p_client_tab->printer[p_client_tab->detail_info_num], p_client_tab->itune[p_client_tab->detail_info_num] ); SAFE_FREE(nmp_client_list); nmp_client_list = strdup(tmpbuf); SAFE_FREE(tmpbuf); //nvram_set("nmp_client_list", nmp_client_list); NMP_DEBUG("[%s, %d]\n", __FUNCTION__, __LINE__); write_to_file(nmp_client_path, nmp_client_list, strlen(nmp_client_list), "w"); NMP_DEBUG("[%s, %d]\n", __FUNCTION__, __LINE__); } SAFE_FREE(search_list); //Andy Chiu, 2014/10/27. update the cfg_node int i; char buf[128]; fp = fopen(cl_path, "w"); //Andy Chiu, 2014/12/03 count = 0; for(i = 0; i < 255; ++i) { if(p_client_tab->exist[i]) //item exist { char attr[64]; char buf[256]; sprintf(buf, "<%d.%d.%d.%d>%02x:%02x:%02x:%02x:%02x:%02x>%s>%d>%d>%d>%d", p_client_tab->ip_addr[i][0], p_client_tab->ip_addr[i][1], p_client_tab->ip_addr[i][2], p_client_tab->ip_addr[i][3], p_client_tab->mac_addr[i][0], p_client_tab->mac_addr[i][1] , p_client_tab->mac_addr[i][2], p_client_tab->mac_addr[i][3], p_client_tab->mac_addr[i][4], p_client_tab->mac_addr[i][5], p_client_tab->device_name[i], p_client_tab->type[i], p_client_tab->http[i], p_client_tab->printer[i], p_client_tab->itune[i]); sprintf(attr, "dev%d", count); // ret = tcapi_set("ClientList_Entry", attr, buf); // if(ret) // NMP_DEBUG_M("set device(%d) failed!(%d)(%s)\n", count, ret, buf); if(fp) //Andy Chiu, 2014/12/03 fwrite(buf, sizeof(char), strlen(buf), fp); ++count; } if( count >= p_client_tab->detail_info_num + 1) break; } if(fp) //Andy Chiu, 2014/12/03 fclose(fp); sprintf(buf, "%d", p_client_tab->detail_info_num + 1); ret = tcapi_set("ClientList_Common", "size", buf); if(ret) NMP_DEBUG_M("set ClientList_Common:size failed!\n"); NMP_DEBUG_M("new client list size is %d\n", p_client_tab->detail_info_num + 1); }
void setup_conntrack(void) { unsigned int v[10]; char p[128] = {0}; char buf[70]; int i; char *pch; // p = nvram_safe_get("ct_tcp_timeout"); tcapi_get("SysInfo_Entry", "ct_tcp_timeout", p); if (sscanf(p, "%u%u%u%u%u%u%u%u%u%u", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7], &v[8], &v[9]) == 10) { // lightly verify write_tcp_timeout("established", v[1]); write_tcp_timeout("syn_sent", v[2]); write_tcp_timeout("syn_recv", v[3]); write_tcp_timeout("fin_wait", v[4]); write_tcp_timeout("time_wait", v[5]); write_tcp_timeout("close", v[6]); write_tcp_timeout("close_wait", v[7]); write_tcp_timeout("last_ack", v[8]); } else { v[1] = read_tcp_timeout("established"); v[2] = read_tcp_timeout("syn_sent"); v[3] = read_tcp_timeout("syn_recv"); v[4] = read_tcp_timeout("fin_wait"); v[5] = read_tcp_timeout("time_wait"); v[6] = read_tcp_timeout("close"); v[7] = read_tcp_timeout("close_wait"); v[8] = read_tcp_timeout("last_ack"); sprintf(buf, "0 %u %u %u %u %u %u %u %u 0", v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8]); // nvram_set("ct_tcp_timeout", buf); tcapi_set("SysInfo_Entry", "ct_tcp_timeout", buf); } setup_udp_timeout(FALSE); // p = nvram_safe_get("ct_timeout"); tcapi_get("SysInfo_Entry", "ct_timeout", p); if (sscanf(p, "%u%u", &v[0], &v[1]) == 2) { // write_ct_timeout("generic", NULL, v[0]); write_ct_timeout("icmp", NULL, v[1]); } else { v[0] = read_ct_timeout("generic", NULL); v[1] = read_ct_timeout("icmp", NULL); sprintf(buf, "%u %u", v[0], v[1]); // nvram_set("ct_timeout", buf); tcapi_set("SysInfo_Entry", "ct_timeout", buf); } #ifdef LINUX26 // p = nvram_safe_get("ct_hashsize"); tcapi_get("SysInfo_Entry", "ct_hashsize", p); i = atoi(p); if (i >= 127) { f_write_string("/sys/module/nf_conntrack/parameters/hashsize", p, 0, 0); } else if (f_read_string("/sys/module/nf_conntrack/parameters/hashsize", buf, sizeof(buf)) > 0) { if (atoi(buf) > 0) {//nvram_set("ct_hashsize", buf); pch = strchr(buf, '\n'); if(pch != NULL) memset(pch, 0, 1); tcapi_set("SysInfo_Entry", "ct_hashsize", buf); } } #endif // p = nvram_safe_get("ct_max"); tcapi_get("SysInfo_Entry", "ct_max", p); i = atoi(p); if (i >= 128) { f_write_string("/proc/sys/net/ipv4/netfilter/ip_conntrack_max", p, 0, 0); } else if (f_read_string("/proc/sys/net/ipv4/netfilter/ip_conntrack_max", buf, sizeof(buf)) > 0) { if (atoi(buf) > 0) {//nvram_set("ct_max", buf); pch = strchr(buf, '\n'); if(pch != NULL) memset(pch, 0, 1); tcapi_set("SysInfo_Entry", "ct_max", buf); } } //if (!nvram_match("nf_rtsp", "0")) { // ct_modprobe("rtsp"); //} //else { // ct_modprobe_r("rtsp"); //} // if (!nvram_match("nf_h323", "0")) { // ct_modprobe("h323"); // } // else { // ct_modprobe_r("h323"); // } // #ifdef LINUX26 // if (!nvram_match("nf_sip", "0")) { // ct_modprobe("sip"); // } // else { // ct_modprobe_r("sip"); // } // #endif // !!TB - FTP Server // #ifdef RTCONFIG_FTP // i = nvram_get_int("ftp_port"); // if (nvram_match("ftp_enable", "1") && (i > 0) && (i != 21)) // { // char ports[32]; // sprintf(ports, "ports=21,%d", i); // ct_modprobe("ftp", ports); // } // else // #endif // if (!nvram_match("nf_ftp", "0") // #ifdef RTCONFIG_FTP // || nvram_match("ftp_enable", "1") // !!TB - FTP Server // #endif // ) { // ct_modprobe("ftp"); // } // else { // ct_modprobe_r("ftp"); // } // if (!nvram_match("nf_pptp", "0")) { // ct_modprobe("proto_gre"); // ct_modprobe("pptp"); // } // else { // ct_modprobe_r("pptp"); // ct_modprobe_r("proto_gre"); // } }
static void dhcpExtractOption43(uint8_t *data, int len) { uint8_t *datap = data; int index = 0, paramLen = 0, ret = 0, spSaveFlag = 0, bootstrapFlag = 0; char tempCwmpString[CwmpTempStringMaxLen]; char buf[CwmpTempStringMaxLen]; if(data == NULL){ return; } while (index < len) { switch (datap[index]){ case OPT43_SUBOPT_ACS_URL: #ifdef OPT43_DBG tcdbg_printf("%s: acs url case\n", __FUNCTION__); #endif paramLen = min((CwmpURLMaxLen-1), datap[index+1]); memset(tempCwmpString, 0, CwmpTempStringMaxLen); memcpy(tempCwmpString, &datap[index+2], paramLen); memset(buf, 0, sizeof(buf)); tcapi_get("Cwmp_Entry", "acsUrl", buf); if (0 != strcmp(buf, tempCwmpString)) { tcapi_set("Cwmp_Entry", "acsUrl", (char*)tempCwmpString); spSaveFlag = 1; bootstrapFlag = 1; } break; case OPT43_SUBOPT_PRVCODE: #ifdef OPT43_DBG tcdbg_printf("%s: prv code case.\n", __FUNCTION__); #endif paramLen = min((CwmpPrvCodeMaxLen-1), datap[index+1]); memset(tempCwmpString, 0, CwmpTempStringMaxLen); memcpy(tempCwmpString, &datap[index+2], paramLen); memset(buf, 0, sizeof(buf)); tcapi_get("Cwmp_Entry", "PrvCode", buf); if (0 != strcmp(buf, tempCwmpString)) { tcapi_set("Cwmp_Entry", "PrvCode", (char*)tempCwmpString); spSaveFlag = 1; } break; default: #ifdef OPT43_DBG tcdbg_printf("%s,default case,and datap[index]=%d\n", __FUNCTION__, datap[index]); #endif break; } if (datap[index] == DHCP_PADDING) index++; else if(datap[index] == DHCP_END) break; else index += (datap[index+1] + 2); } #ifdef OPT43_DBG tcdbg_printf("%s spSaveFlag=%d bootstrapFlag=%d\n", __FUNCTION__, spSaveFlag, bootstrapFlag); #endif /* save to flash */ if (spSaveFlag) tcapi_save(); /* send msg to cwmp for signal inform */ if (bootstrapFlag) tcapi_commit("Cwmp_Entry"); return; }
void main(int argc, char *argv[]) { int sockfd , clisockfd; unsigned int clilen; int childpid; struct sockaddr_in serv_addr,cli_addr; int err_select;//JY1113 char value[32] = {0}; #ifdef LPR_with_ASUS//JY1112 int LPRflag = 0; fd_set rfds, afds; int nfds; int sockfd_ASUS; unsigned int clilen_ASUS; int childpid_ASUS; struct sockaddr_in serv_addr_ASUS,cli_addr_ASUS; #endif #ifdef Raw_Printing_with_ASUS //Lisa int netfd, fd, clientlen, one = 1; struct sockaddr_in netaddr, client; #endif //Initial the server the not busy lptstatus.busy = FALSE; sigset_t sigs_to_catch; sigemptyset(&sigs_to_catch); sigaddset(&sigs_to_catch, SIGCLD); sigaddset(&sigs_to_catch, SIGINT); sigaddset(&sigs_to_catch, SIGQUIT); sigaddset(&sigs_to_catch, SIGKILL); sigaddset(&sigs_to_catch, SIGUSR2); sigprocmask(SIG_UNBLOCK, &sigs_to_catch, NULL); //Setup the signal handler signal(SIGCLD, sig_child); signal(SIGINT, sig_cleanup); signal(SIGQUIT, sig_cleanup); signal(SIGKILL, sig_cleanup); signal(SIGUSR2, sig_remove);//JY1110 if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0 ) { //perror("can't open stream socket:"); exit(0); } bzero((char *)&serv_addr , sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(PNT_SVR_PORT_LPR); if(bind(sockfd,(struct sockaddr *)&serv_addr , sizeof(serv_addr)) < 0 ) { //perror("can't bind:"); exit(0); } /*JY1111*/ int windowsize=2920; setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (char *)&windowsize, sizeof(windowsize)); int no_delay=1; setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &no_delay, sizeof(no_delay)); // by Jiahao. 20080808. #if 1 int currentpid=getpid(); FILE *pidfileread; if((pidfileread=fopen("/var/run/lpdparent.pid", "r")) == NULL) { pidfileread=fopen("/var/run/lpdparent.pid", "w"); fprintf(pidfileread, "%d", currentpid); fclose(pidfileread); } else{ //printf("another lpd daemon exists!!\n"); fclose(pidfileread); exit(0); } #endif listen(sockfd , 15); #ifdef Raw_Printing_with_ASUS //Lisa if ((netfd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0) { // syslog(LOGOPTS, "socket: %m\n"); exit(1); } if (setsockopt(netfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) { // syslog(LOGOPTS, "setsocketopt: %m\n"); exit(1); } netaddr.sin_port = htons(BASEPORT); netaddr.sin_addr.s_addr = htonl(INADDR_ANY); memset(netaddr.sin_zero, 0, sizeof(netaddr.sin_zero)); if (bind(netfd, (struct sockaddr*) &netaddr, sizeof(netaddr)) < 0) { //syslog(LOGOPTS, "bind: %m\n"); exit(1); } if (listen(netfd, 5) < 0) { //syslog(LOGOPTS, "listen: %m\n"); exit(1); } //clientlen = sizeof(client); //memset(&client, 0, sizeof(client)); #endif #ifdef LPR_with_ASUS//JY1112 if((sockfd_ASUS = socket(AF_INET,SOCK_STREAM,0)) < 0 ) { //perror("can't open stream socket:"); exit(0); } bzero((char *)&serv_addr_ASUS , sizeof(serv_addr_ASUS)); serv_addr_ASUS.sin_family = AF_INET; serv_addr_ASUS.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr_ASUS.sin_port = htons(PNT_SVR_PORT_ASUS); if(bind(sockfd_ASUS,(struct sockaddr *)&serv_addr_ASUS , sizeof(serv_addr_ASUS)) < 0 ) { //perror("can't bind:"); exit(0); } setsockopt(sockfd_ASUS, SOL_SOCKET, SO_RCVBUF, (char *)&windowsize, sizeof(windowsize)); listen(sockfd_ASUS , 15); /*set the fds*/ nfds=MAX(sockfd, sockfd_ASUS); FD_ZERO(&afds); #endif while(TRUE) { //if (busy) syslog(LOG_NOTICE, "busying %d %d\n", lptstatus.busy, busy); bin_sem_wait(); // by Jiahao for U2EC. 20080808. tcapi_get("USB_Entry", "MFP_busy", value); if(lptstatus.busy==FALSE && strcmp(value, "2")) // if (lptstatus.busy==FALSE && nvram_invmatch("MFP_busy", "2")) { busy=FALSE; // nvram_set("MFP_busy", "0"); tcapi_set("USB_Entry", "MFP_busy", "0"); // nvram_set("u2ec_busyip", ""); tcapi_set("USB_Entry", "u2ec_busyip", ""); } bin_sem_post(); #ifdef Raw_Printing_with_ASUS //Lisa FD_SET(netfd, &afds); #endif #ifdef LPR_with_ASUS//JY1112 FD_SET(sockfd, &afds); FD_SET(sockfd_ASUS, &afds); memcpy(&rfds, &afds, sizeof(rfds)); if((err_select=select(nfds+1, &rfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0 )) < 0) { //JY1120 printf("select error on sockfd: error=%d\n", errno); /**/ // printf("sockfd_FD_ISSET=%d\n", FD_ISSET(sockfd, &rfds)); //JY1120 printf("sockfd_ASUS FD_ISSET=%d\n", FD_ISSET(sockfd_ASUS, &rfds)); /**/ // if(errno != 4)//JY1113: delete //syslog(LOG_NOTICE, "select error %d\n", err_select); continue; } #endif clilen = sizeof(cli_addr); if(FD_ISSET(sockfd_ASUS, &rfds)) { LPRflag = 0; clisockfd = accept(sockfd_ASUS,(struct sockaddr *)&cli_addr, &clilen); } #ifdef LPR_with_ASUS//JY1112 else if(FD_ISSET(sockfd, &rfds)) { LPRflag = 1; clisockfd = accept(sockfd,(struct sockaddr *)&cli_addr, &clilen); } #endif #ifdef Raw_Printing_with_ASUS //Lisa // else if(FD_ISSET(netfd, &rfds) && busy==FALSE) else if(FD_ISSET(netfd, &rfds)) { bin_sem_wait(); // by Jiahao for U2EC. 20080808. tcapi_get("USB_Entry", "MFP_busy", value); if( !strcmp(value, "0") ) // if (nvram_match("MFP_busy", "0")) { bin_sem_post(); LPRflag = 2; clisockfd = accept(netfd, (struct sockaddr*) &cli_addr, &clilen); } else { bin_sem_post(); sleep(2); continue; } } #endif else { //syslog(LOG_NOTICE, "No select\n"); sleep(2); continue; } strcpy(clientaddr , inet_ntoa(cli_addr.sin_addr)); if(clisockfd < 0) { //syslog(LOG_NOTICE, "LPD error: No clisockfd %d\n", LPRflag); continue; } bin_sem_wait(); // if (busy!=FALSE) /* 2004/09/10 by Joey, process nack in parent for LPR and Remote Prot */ tcapi_get("USB_Entry", "MFP_busy", value); if(strcmp(value, "0")) // if (nvram_invmatch("MFP_busy", "0")) // by Jiahao for U2EC. 20080808. { bin_sem_post(); //syslog(LOG_NOTICE, "Printing others 1 %d %d\n", LPRflag, clisockfd); if (LPRflag==0) processReq(clisockfd); else if (LPRflag==1) processReq_LPR(clisockfd, 0); //syslog(LOG_NOTICE, "Printing others %d %d\n", LPRflag, clisockfd); close(clisockfd); // For Raw printing, don't resonse to client while busy sleep(5); continue; } // nvram_set("MFP_busy", "1"); tcapi_set("USB_Entry", "MFP_busy", "1"); // if (nvram_match("lan_ipaddr_t", "")) // nvram_set("u2ec_busyip", nvram_safe_get("lan_ipaddr")); // else // nvram_set("u2ec_busyip", nvram_safe_get("lan_ipaddr_t")); tcapi_get("Lan_Entry0", "IP", value); tcapi_set("USB_Entry", "u2ec_busyip", value); bin_sem_post(); if( (childpid = fork() ) < 0) { } else if(childpid == 0) { //syslog(LOG_NOTICE, "Printing %d\n", LPRflag); if(LPRflag==0) processReq(clisockfd); #ifdef LPR_with_ASUS//JY1114 else if(LPRflag==1) processReq_LPR(clisockfd, 1); #endif #ifdef Raw_Printing_with_ASUS //Lisa else if(LPRflag == 2) processReq_Raw(clisockfd); #endif close(sockfd); close(sockfd_ASUS); close(netfd); exit(0); } //syslog(0, "Printing Process %d %d %d\n", busy, lptstatus.busy, childpid); //parents process goes on here //remark PRINT("fork -- childpid %d\n",childpid); if(lptstatus.busy == FALSE) { lptstatus.busy = TRUE; // busy = TRUE; // remarked by Jiahao for U2EC. 20080808. strcpy(lptstatus.addr , inet_ntoa(cli_addr.sin_addr)); lptstatus.pid = childpid; } close(clisockfd); } }
void reset_client_setting(int unit){ char node[MAXLEN_NODE_NAME]; char nv[32]; #if defined(TCSUPPORT_ADD_JFFS) || defined(TCSUPPORT_SQUASHFS_ADD_YAFFS) char file_path[128] ={0}; #endif snprintf(node, sizeof(node), "OpenVPN_Entry%d", unit+CLIENT_IF_START); tcapi_set(node, "custom", ""); tcapi_set(node, "comp", "-1"); tcapi_set(node, "reneg", "-1"); tcapi_set(node, "hmac", "-1"); tcapi_set(node, "retry", "-1"); tcapi_set(node, "cipher", "default"); tcapi_set(node, "tlsremote", "0"); tcapi_set(node, "common_name", ""); tcapi_set(node, "userauth", "0"); tcapi_set(node, "username", ""); tcapi_set(node, "password", ""); sprintf(nv, "vpn_crt_client%d_ca", unit); tcapi_set(node, nv, ""); #if defined(TCSUPPORT_ADD_JFFS) || defined(TCSUPPORT_SQUASHFS_ADD_YAFFS) snprintf(file_path, sizeof(file_path) -1, "%s/%s", OVPN_FS_PATH, nv); unlink(file_path); #endif sprintf(nv, "vpn_crt_client%d_crt", unit); tcapi_set(node, nv, ""); #if defined(TCSUPPORT_ADD_JFFS) || defined(TCSUPPORT_SQUASHFS_ADD_YAFFS) snprintf(file_path, sizeof(file_path) -1, "%s/%s", OVPN_FS_PATH, nv); unlink(file_path); #endif sprintf(nv, "vpn_crt_client%d_key", unit); tcapi_set(node, nv, ""); #if defined(TCSUPPORT_ADD_JFFS) || defined(TCSUPPORT_SQUASHFS_ADD_YAFFS) snprintf(file_path, sizeof(file_path) -1, "%s/%s", OVPN_FS_PATH, nv); unlink(file_path); #endif sprintf(nv, "vpn_crt_client%d_static", unit); tcapi_set(node, nv, ""); #if defined(TCSUPPORT_ADD_JFFS) || defined(TCSUPPORT_SQUASHFS_ADD_YAFFS) snprintf(file_path, sizeof(file_path) -1, "%s/%s", OVPN_FS_PATH, nv); unlink(file_path); #endif sprintf(nv, "vpn_crt_client%d_crl", unit); tcapi_set(node, nv, ""); #if defined(TCSUPPORT_ADD_JFFS) || defined(TCSUPPORT_SQUASHFS_ADD_YAFFS) snprintf(file_path, sizeof(file_path) -1, "%s/%s", OVPN_FS_PATH, nv); unlink(file_path); #endif }
static int add_option (char *p[], int line, int unit) { char buf[32] = {0}; FILE *fp; char file_path[128] ={0}; char node[MAXLEN_NODE_NAME]; snprintf(node, sizeof(node), "OpenVPN_Entry%d", unit + CLIENT_IF_START); if (streq (p[0], "dev") && p[1]) { if(!strncmp(p[1], "tun", 3)) tcapi_set(node, "iface", "tun"); else if(!strncmp(p[1], "tap", 3)) tcapi_set(node, "iface", "tap"); } else if (streq (p[0], "proto") && p[1]) { tcapi_set(node, "proto", p[1]); } else if (streq (p[0], "remote") && p[1]) { tcapi_set(node, "addr", p[1]); if(p[2]) tcapi_set(node, "port", p[2]); else tcapi_set(node, "port", "1194"); } else if (streq (p[0], "resolv-retry") && p[1]) { if (streq (p[1], "infinite")) tcapi_set(node, "retry", "-1"); else tcapi_set(node, "retry", p[1]); } else if (streq (p[0], "comp-lzo")) { sprintf(buf, "vpn_client%d_comp", unit); if(p[1]) tcapi_set(node, "comp", p[1]); else tcapi_set(node, "comp", "adaptive"); } else if (streq (p[0], "cipher") && p[1]) { tcapi_set(node, "cipher", p[1]); } else if (streq (p[0], "verb") && p[1]) { tcapi_set(node, "loglevel", p[1]); } else if (streq (p[0], "ca") && p[1]) { tcapi_set(node, "crypt", "tls"); if (streq (p[1], INLINE_FILE_TAG) && p[2]) { sprintf(buf, "vpn_crt_client%d_ca", unit); #if defined(TCSUPPORT_ADD_JFFS) || defined(TCSUPPORT_SQUASHFS_ADD_YAFFS) snprintf(file_path, sizeof(file_path) -1, "%s/%s", OVPN_FS_PATH, buf); fp = fopen(file_path, "w"); if(fp) { fprintf(fp, "%s", strstr(p[2], "-----BEGIN")); fclose(fp); } else { _dprintf("write %s file to %s failed\n", buf, OVPN_FS_PATH); logmessage ("OVPN", "Write CA to filesystem failed"); return -1; } #else tcapi_set(node, buf, strstr(p[2], "-----BEGIN")); #endif } else { return VPN_UPLOAD_NEED_CA_CERT; } } else if (streq (p[0], "cert") && p[1]) { if (streq (p[1], INLINE_FILE_TAG) && p[2]) { sprintf(buf, "vpn_crt_client%d_crt", unit); #if defined(TCSUPPORT_ADD_JFFS) || defined(TCSUPPORT_SQUASHFS_ADD_YAFFS) snprintf(file_path, sizeof(file_path) -1, "%s/%s", OVPN_FS_PATH, buf); fp = fopen(file_path, "w"); if(fp) { fprintf(fp, "%s", strstr(p[2], "-----BEGIN")); fclose(fp); } else { _dprintf("write %s file to %s failed\n", buf, OVPN_FS_PATH); logmessage ("OVPN", "Write Certificate to filesystem failed"); return -1; } #else tcapi_set(node, buf, strstr(p[2], "-----BEGIN")); #endif } else { return VPN_UPLOAD_NEED_CERT; } } else if (streq (p[0], "key") && p[1]) { if (streq (p[1], INLINE_FILE_TAG) && p[2]) { sprintf(buf, "vpn_crt_client%d_key", unit); #if defined(TCSUPPORT_ADD_JFFS) || defined(TCSUPPORT_SQUASHFS_ADD_YAFFS) snprintf(file_path, sizeof(file_path) -1, "%s/%s", OVPN_FS_PATH, buf); fp = fopen(file_path, "w"); if(fp) { fprintf(fp, "%s", strstr(p[2], "-----BEGIN")); fclose(fp); } else { _dprintf("write %s file to %s failed\n", buf, OVPN_FS_PATH); logmessage ("OVPN", "Write key to filesystem failed"); return -1; } #else tcapi_set(node, buf, strstr(p[2], "-----BEGIN")); #endif } else { return VPN_UPLOAD_NEED_KEY; } } else if (streq (p[0], "tls-auth") && p[1]) { if (streq (p[1], INLINE_FILE_TAG) && p[2]) { sprintf(buf, "vpn_crt_client%d_static", unit); #if defined(TCSUPPORT_ADD_JFFS) || defined(TCSUPPORT_SQUASHFS_ADD_YAFFS) snprintf(file_path, sizeof(file_path) -1, "%s/%s", OVPN_FS_PATH, buf); fp = fopen(file_path, "w"); if(fp) { fprintf(fp, "%s", strstr(p[2], "-----BEGIN")); fclose(fp); } else { _dprintf("write %s file to %s failed\n", buf, OVPN_FS_PATH); logmessage ("OVPN", "Write static-key to filesystem failed"); return -1; } #else tcapi_set(node, buf, strstr(p[2], "-----BEGIN")); #endif //key-direction sprintf(buf, "vpn_crt_client%d_hmac", unit); if(tcapi_match(node, "hmac", "-1")) //default, disable tcapi_set(node, "hmac", "2"); //openvpn default value: KEY_DIRECTION_BIDIRECTIONAL } else { if(p[2]) { tcapi_set(node, "hmac", p[2]); } return VPN_UPLOAD_NEED_STATIC; } } else if (streq (p[0], "secret") && p[1]) { tcapi_set(node, "crypt", "secret"); if (streq (p[1], INLINE_FILE_TAG) && p[2]) { sprintf(buf, "vpn_crt_client%d_static", unit); #if defined(TCSUPPORT_ADD_JFFS) || defined(TCSUPPORT_SQUASHFS_ADD_YAFFS) snprintf(file_path, sizeof(file_path) -1, "%s/%s", OVPN_FS_PATH, buf); fp = fopen(file_path, "w"); if(fp) { fprintf(fp, "%s", strstr(p[2], "-----BEGIN")); fclose(fp); } else { _dprintf("write %s file to %s failed\n", buf, OVPN_FS_PATH); logmessage ("OVPN", "Write static-key to filesystem failed"); return -1; } #else tcapi_set(node, buf, strstr(p[2], "-----BEGIN")); #endif } else { return VPN_UPLOAD_NEED_STATIC; } } else if (streq (p[0], "auth-user-pass")) { tcapi_set(node, "userauth", "1"); } else if (streq (p[0], "tls-remote") && p[1]) { tcapi_set(node, "tlsremote", "1"); tcapi_set(node, "common_name", p[1]); } else if (streq (p[0], "key-direction") && p[1]) { tcapi_set(node, "hmac", p[1]); } else if (streq (p[0], "crl-verify") && p[1]) { if (p[2] && streq(p[2], "dir")) ;//TODO: not support? return VPN_UPLOAD_NEED_CRL; } else { if ( streq (p[0], "client") || streq (p[0], "nobind") || streq (p[0], "persist-key") || streq (p[0], "persist-tun") ) { ;//ignore } else { add_custom(unit, p); } } return 0; }
int read_header(request * req) { int bytes, buf_bytes_left; char *check, *buffer; char *pstr; char str_lang[6]; char str_type[4] = {0}; int nIndex = 1; char ttc[16] = {0}; check = req->client_stream + req->parse_pos; buffer = req->client_stream; bytes = req->client_stream_pos; #ifdef VERY_FASCIST_LOGGING if (check < (buffer + bytes)) { buffer[bytes] = '\0'; log_error_time(); fprintf(stderr, "1) %s:%d - Parsing headers (\"%s\")\n", __FILE__, __LINE__, check); } #endif tcapi_get("SysInfo_Entry", "TerritoryCode", ttc); /* Paul add 2013/3/7, for retrieve Language type from HTTP header */ tcapi_get("LanguageSwitch_Entry", "Type", str_type); if(strlen(str_type)) nIndex = atoi(str_type); if(nIndex == 0) /* Language never been set before, start checking */ { tcapi_get("WebCurSet_Entry", "detected_lang_type", str_type); if(strlen(str_type)) nIndex = atoi(str_type); if(nIndex == 0) { // Language never been detect before, start checking memset(str_lang, 0, sizeof(str_lang)); if(pstr = strstr(check,"Accept-Language:")) { strncpy (str_lang, &check[pstr-check+17], 6); str_lang[5] = '\0'; int i, len = strlen(str_lang); for(i=0; i<len; ++i) { if(isupper(str_lang[i])){ str_lang[i] = tolower(str_lang[i]); } } //choose proper language type sprintf(str_type, "%d", getLangType(ttc, str_lang) ); tcapi_set("WebCurSet_Entry", "detected_lang_type", str_type); initandparserfile(); } } } while (check < (buffer + bytes)) { switch (req->status) { case READ_HEADER: if (*check == '\r') { req->status = ONE_CR; req->header_end = check; } else if (*check == '\n') { req->status = ONE_LF; req->header_end = check; } break; case ONE_CR: if (*check == '\n') req->status = ONE_LF; else if (*check != '\r') req->status = READ_HEADER; break; case ONE_LF: /* if here, we've found the end (for sure) of a header */ if (*check == '\r') /* could be end o headers */ req->status = TWO_CR; else if (*check == '\n') req->status = BODY_READ; else req->status = READ_HEADER; break; case TWO_CR: if (*check == '\n') req->status = BODY_READ; else if (*check != '\r') req->status = READ_HEADER; break; default: break; } #if 0 #ifdef VERY_FASCIST_LOGGING log_error_time(); fprintf(stderr, "status, check: %d, %d\n", req->status, *check); #endif #endif req->parse_pos++; /* update parse position */ check++; if (req->status == ONE_LF) { *req->header_end = '\0'; /* terminate string that begins at req->header_line */ if (req->logline) { if (process_option_line(req) == 0) { return 0; } } else { if (process_logline(req) == 0) return 0; if (req->simple) return process_header_end(req); } /* set header_line to point to beginning of new header */ req->header_line = check; } else if (req->status == BODY_READ) { #ifdef VERY_FASCIST_LOGGING int retval; log_error_time(); fprintf(stderr, "%s:%d -- got to body read.\n", __FILE__, __LINE__); retval = process_header_end(req); #else int retval = process_header_end(req); #endif /* process_header_end inits non-POST cgi's */ if (retval && req->method == M_POST) { /* for body_{read,write}, set header_line to start of data, and header_end to end of data */ req->header_line = check; req->header_end = req->client_stream + req->client_stream_pos; req->status = BODY_WRITE; /* so write it */ /* have to write first, or read will be confused * because of the special case where the * filesize is less than we have already read. */ /* As quoted from RFC1945: A valid Content-Length is required on all HTTP/1.0 POST requests. An HTTP/1.0 server should respond with a 400 (bad request) message if it cannot determine the length of the request message's content. */ if (req->content_length) { int content_length; content_length = boa_atoi(req->content_length); #if 0 //Content-Length is required but don't limit to larger then zero. Sam, 2013/7/3 /* Is a content-length of 0 legal? */ if (content_length <= 0) { log_error_time(); fprintf(stderr, "Invalid Content-Length [%s] on POST!\n", req->content_length); send_r_bad_request(req); return 0; } #endif if (single_post_limit && content_length > single_post_limit) { log_error_time(); fprintf(stderr, "Content-Length [%d] > SinglePostLimit [%d] on POST!\n", content_length, single_post_limit); send_r_bad_request(req); return 0; } req->filesize = content_length; req->filepos = 0; if (req->header_end - req->header_line > req->filesize) { req->header_end = req->header_line + req->filesize; } } else { log_error_time(); fprintf(stderr, "Unknown Content-Length POST!\n"); send_r_bad_request(req); return 0; } } /* either process_header_end failed or req->method != POST */ return retval; /* 0 - close it done, 1 - keep on ready */ } /* req->status == BODY_READ */ } /* done processing available buffer */ #ifdef VERY_FASCIST_LOGGING log_error_time(); fprintf(stderr, "%s:%d - Done processing buffer. Status: %d\n", __FILE__, __LINE__, req->status); #endif if (req->status < BODY_READ) { /* only reached if request is split across more than one packet */ buf_bytes_left = CLIENT_STREAM_SIZE - req->client_stream_pos; if (buf_bytes_left < 1) { log_error_time(); fputs("buffer overrun - read.c, read_header - closing\n", stderr); req->status = DEAD; return 0; } #if defined(TCSUPPORT_WEBSERVER_SSL) if(req->ssl == NULL) #endif { bytes = read(req->fd, buffer + req->client_stream_pos, buf_bytes_left); } #if defined(TCSUPPORT_WEBSERVER_SSL) else{ bytes = boa_sslRead(req->ssl, buffer + req->client_stream_pos, buf_bytes_left); } #endif if (bytes < 0) { if (errno == EINTR) return 1; if (errno == EAGAIN || errno == EWOULDBLOCK) /* request blocked */ return -1; /* else if (errno == EBADF || errno == EPIPE) { req->status = DEAD; return 0; */ #if 0 //lee 9-25 log_error_doc(req); perror("header read"); /* don't need to save errno because log_error_doc does */ #endif return 0; } else if (bytes == 0) { /* log_error_time(); fputs("unexpected end of headers\n", stderr); */ return 0; } /* bytes is positive */ req->client_stream_pos += bytes; #ifdef FASCIST_LOGGING1 log_error_time(); req->client_stream[req->client_stream_pos] = '\0'; fprintf(stderr, "%s:%d -- We read %d bytes: \"%s\"\n", __FILE__, __LINE__, bytes, #ifdef VERY_FASCIST_LOGGING2 req->client_stream + req->client_stream_pos - bytes); #else ""); #endif #endif return 1; }
int tcapi_set_int(char* node, char* attr, int value) { char szValue[16]; snprintf(szValue, sizeof(szValue), "%d", value); return tcapi_set(node, attr, szValue); }
/* * Parse and set the UI type list. e.g. <1>aa>bb>cc>...<2>dd>ee>ff>... * @param base_node base name of tcapi node * @param base_attr base name of tcapi attribute * @param list_type 0: merge number to base_node and then set base_attr * i.e. parse list_value and then set base_attr in XXX_Entry0 node, base_attr in XXX_Entry1 ... * 1: merge number to base_attr and set data to base_node * i.e. parse list_value and then set base_attr, base_attr1, base_attr2, ... to base_node * list_num total number of data to set * list_value data to be set * @return 0: succsess * -1: fail */ int _tcapi_set_list(char* base_node, char* base_attr , int list_type, int list_num , char* list_value) { char node[MAXLEN_NODE_NAME]; char attr[MAXLEN_ATTR_NAME]; char value[MAXLEN_TCAPI_MSG]; int i; char *pch_start, *pch_end; if(list_type == TCAPI_LIST_TYPE_ATTR) //fixed node, varied attr strncpy(node, base_node, MAXLEN_NODE_NAME-1); else //varied node, fixed attr strncpy(attr, base_attr, MAXLEN_ATTR_NAME-1); if(!strcmp(base_attr, "MULTIFILTER_MACFILTER_DAYTIME") || !strcmp(base_attr, "qos_bw_rulelist") ) pch_start = list_value; //first else pch_start = strchr(list_value,'<'); //first for(i = 0; i < list_num; i++) { //update node/attr if(list_type == TCAPI_LIST_TYPE_ATTR) { if(i) snprintf(attr, sizeof(attr), "%s%d", base_attr, i); else snprintf(attr, sizeof(attr), "%s", base_attr); } else { snprintf(node, sizeof(node), "%s%d", base_node, i); } if(!pch_start) { //wrong format or null string _dprintf("set %s=\"\"\n", attr); tcapi_set(node, attr, ""); continue; } if(*(pch_start+1)) { if(!strcmp(base_attr, "MULTIFILTER_MACFILTER_DAYTIME")) pch_end = strchr(pch_start+1,'#'); else pch_end = strchr(pch_start+1,'<'); if(pch_end) { snprintf(value, pch_end - pch_start + 1, "%s", pch_start); if(!strcmp(base_attr, "MULTIFILTER_MACFILTER_DAYTIME")) pch_start = pch_end + 1; else pch_start = pch_end; } else { //last sprintf(value, "%s", pch_start); pch_start = NULL; } _dprintf("set %s=%s\n", attr, value); tcapi_set(node, attr, value); } else { _dprintf("set %s=\"\"\n", attr); tcapi_set(node, attr, ""); } } return 0; }