int rewrite_tokenfile_and_nv(){ int i, j; int exist; //printf("rewrite_tokenfile_and_nv start\n"); if(smb_config.dir_num > smb_config_stop.dir_num) { for(i = 0; i < smb_config.dir_num; i++) { exist = 0; for(j = 0; j < smb_config_stop.dir_num; j++) { if(!strcmp(smb_config_stop.multrule[j]->server_root_path, smb_config.multrule[i]->server_root_path)) { exist = 1; break; } } if(!exist) { //printf("del form nv\n"); char *new_nv; delete_tokenfile_info(smb_config.multrule[i]->server_root_path, smb_config.multrule[i]->client_root_path); new_nv = delete_nvram_contents(smb_config.multrule[i]->server_root_path, smb_config.multrule[i]->client_root_path); //printf("new_nv = %s\n", new_nv); write_to_tokenfile(smb_config.multrule[i]->mount_path); #ifdef NVRAM_ write_to_nvram(new_nv, "usb_tokenfile"); #else write_to_smb_tokenfile(new_nv); #endif free(new_nv); } } } else { for(i = 0; i < smb_config_stop.dir_num; i++) { exist = 0; for(j = 0; j < smb_config.dir_num; j++) { if(!strcmp(smb_config_stop.multrule[i]->server_root_path, smb_config.multrule[j]->server_root_path)) { exist = 1; break; } } if(!exist) { char *new_nv; add_tokenfile_info(smb_config_stop.multrule[i]->server_root_path, smb_config_stop.multrule[i]->client_root_path, smb_config_stop.multrule[i]->mount_path); new_nv = add_nvram_contents(smb_config_stop.multrule[i]->server_root_path, smb_config_stop.multrule[i]->client_root_path); write_to_tokenfile(smb_config_stop.multrule[i]->mount_path); #ifdef NVRAM_ write_to_nvram(new_nv, "usb_tokenfile"); #else write_to_smb_tokenfile(new_nv); #endif free(new_nv); } } } return 0; }
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; 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; FILE *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 = strdup(nvram_safe_get("nmp_client_list")); NMP_DEBUG_M("NMP Client:\n%s\n", nmp_client_list); signal(SIGUSR2, reset_db); #endif //Get Router's IP/Mac strcpy(router_ipaddr, nvram_safe_get("lan_ipaddr")); #ifdef RTCONFIG_RGMII_BRCM5301X strcpy(router_mac, nvram_safe_get("et1macaddr")); #else strcpy(router_mac, nvram_safe_get("et0macaddr")); #endif inet_aton(router_ipaddr, &router_addr.sin_addr); memcpy(my_ipaddr, &router_addr.sin_addr, 4); //Prepare scan networkmap_fullscan = 1; nvram_set("networkmap_fullscan", "1"); if (argc > 1) { if (strcmp(argv[1], "--bootwait") == 0) { sleep(30); } } 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(1)//main while loop { while(1) { //full scan and reflush recv buffer fullscan: if(networkmap_fullscan == 1) { //Scan all IP address in the subnetwork if(scan_count == 0) { 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"); 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; } } 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"); 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) ) { swapbytes16(arp_ptr->message_type); if( //ARP packet to router (arp_ptr->message_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 || (arp_ptr->message_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", (int *)arp_ptr->source_ipaddr[0],(int *)arp_ptr->source_ipaddr[1], (int *)arp_ptr->source_ipaddr[2],(int *)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 /* 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"); 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_nvram(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 } //End of main while loop close(arp_sockfd); return 0; }
int check_config_path(int is_read_config) { DEBUG("check_config_path start\n"); int i; int flag; char *nv; char *nvp; char *new_nv; int nv_len; int is_path_change = 0; #ifdef NVRAM_ #ifdef USE_TCAPI char tmp[MAXLEN_TCAPI_MSG] = {0}; tcapi_get(AICLOUD, "usb_tokenfile", tmp); nv = my_malloc(strlen(tmp) + 1); //2014.10.20 by sherry malloc申请内存是否成功 //if(nv==NULL) // return NULL; sprintf(nv, "%s", tmp); #else nv = strdup(nvram_safe_get("usb_tokenfile")); #endif #else FILE *fp; fp = fopen(TOKENFILE_RECORD, "r"); if(fp == NULL) { nv = my_malloc(2); //2014.10.20 by sherry malloc申请内存是否成功 //if(nv==NULL) // return NULL; sprintf(nv, ""); } else { fseek( fp, 0, SEEK_END ); int file_size; file_size = ftell( fp ); fseek(fp, 0, SEEK_SET); //nv = (char *)malloc( file_size * sizeof( char ) ); nv = my_malloc(file_size + 2); //2014.10.20 by sherry malloc申请内存是否成功 //if(nv==NULL) //return NULL; //fread(nv , file_size , sizeof(char) , fp); fscanf(fp, "%[^\n]%*c", nv); fclose(fp); } #endif nv_len = strlen(nv); DEBUG("nv_len = %d\n", nv_len); for(i = 0; i < smb_config.dir_num; i++) { flag = 0; tokenfile_info_tmp = tokenfile_info_start->next; while(tokenfile_info_tmp != NULL) { if( !strcmp(tokenfile_info_tmp->url, smb_config.multrule[i]->server_root_path) && !strcmp(tokenfile_info_tmp->folder, smb_config.multrule[i]->client_root_path)) { if(strcmp(tokenfile_info_tmp->mountpath, smb_config.multrule[i]->mount_path)) { memset(smb_config.multrule[i]->mount_path, 0, sizeof(smb_config.multrule[i]->mount_path)); sprintf(smb_config.multrule[i]->mount_path, "%s", tokenfile_info_tmp->mountpath); memset(smb_config.multrule[i]->client_root_path, 0, sizeof(smb_config.multrule[i]->client_root_path)); sprintf(smb_config.multrule[i]->client_root_path, "%s%s", tokenfile_info_tmp->mountpath,tokenfile_info_tmp->folder); is_path_change = 1; } if(!is_read_config) { if(g_pSyncList[i]->sync_disk_exist == 0) is_path_change = 1; //plug the disk and the mout_path not change } flag = 1; break; } tokenfile_info_tmp = tokenfile_info_tmp->next; } //printf("flag = %d\n", flag); if(!flag) { nvp = my_malloc(strlen(smb_config.multrule[i]->server_root_path) + strlen(smb_config.multrule[i]->client_root_path) + 2); //2014.10.20 by sherry malloc申请内存是否成功 //if(nvp==NULL) // return NULL; sprintf(nvp, "%s>%s", smb_config.multrule[i]->server_root_path, smb_config.multrule[i]->client_root_path); if(!is_read_config) { if(g_pSyncList[i]->sync_disk_exist == 1) is_path_change = 2; //remove the disk and the mout_path not change } DEBUG("write nvram and tokenfile if before\n"); if(strstr(nv, nvp) == NULL) { DEBUG("write nvram and tokenfile if behind"); if(initial_tokenfile_info_data(&tokenfile_info_tmp) == NULL) { return -1; } tokenfile_info_tmp->url = my_malloc(strlen(smb_config.multrule[i]->server_root_path) + 1); //2014.10.20 by sherry malloc申请内存是否成功 // if(tokenfile_info_tmp->url==NULL) // return NULL; sprintf(tokenfile_info_tmp->url, "%s", smb_config.multrule[i]->server_root_path); tokenfile_info_tmp->mountpath = my_malloc(strlen(smb_config.multrule[i]->mount_path) + 1); //2014.10.20 by sherry malloc申请内存是否成功 //if(tokenfile_info_tmp->mountpath==NULL) // return NULL; sprintf(tokenfile_info_tmp->mountpath, "%s", smb_config.multrule[i]->mount_path); tokenfile_info_tmp->folder = my_malloc(strlen(smb_config.multrule[i]->client_root_path) + 1); //2014.10.20 by sherry malloc申请内存是否成功 //if(tokenfile_info_tmp->folder==NULL) // return NULL; sprintf(tokenfile_info_tmp->folder, "%s", smb_config.multrule[i]->client_root_path); tokenfile_info->next = tokenfile_info_tmp; tokenfile_info = tokenfile_info_tmp; write_to_tokenfile(smb_config.multrule[i]->mount_path); if(nv_len) { new_nv = my_malloc(strlen(nv) + strlen(nvp) + 2); //2014.10.20 by sherry malloc申请内存是否成功 // if(new_nv==NULL) // return NULL; sprintf(new_nv, "%s<%s", nv,nvp); } else { new_nv = my_malloc(strlen(nvp) + 1); //2014.10.20 by sherry malloc申请内存是否成功 //if(new_nv==NULL) // return NULL; sprintf(new_nv, "%s", nvp); } #ifdef NVRAM_ write_to_nvram(new_nv, "usb_tokenfile"); #else write_to_smb_tokenfile(new_nv); #endif free(new_nv); } free(nvp); } } free(nv); return is_path_change; }