Ejemplo n.º 1
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;
}
Ejemplo n.º 2
0
int main()
{
	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[512];
	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;

        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
        spinlock_lock(SPINLOCK_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");
            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;
	spinlock_unlock(SPINLOCK_Networkmap);	

	//Get Router's IP/Mac
	strcpy(router_ipaddr, nvram_safe_get("lan_ipaddr"));
	strcpy(router_mac, nvram_safe_get("et0macaddr"));
        inet_aton(router_ipaddr, &router_addr.sin_addr);
        memcpy(my_ipaddr,  &router_addr.sin_addr, 4);

	//Prepare scan 
        memset(scan_ipaddr, 0x00, 4);
        memcpy(scan_ipaddr, &router_addr.sin_addr, 3);
	networkmap_fullscan = 1;
	nvram_set("networkmap_fullscan", "1");

	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 = 10000;
		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) { 
	                arp_timeout.tv_sec = 0;
        	        arp_timeout.tv_usec = 10000;
                	setsockopt(arp_sockfd, SOL_SOCKET, SO_RCVTIMEO, &arp_timeout, sizeof(arp_timeout));//set receive timeout
			NMP_DEBUG("Starting full scan!\n");
			
                        //reset client tables
			spinlock_lock(SPINLOCK_Networkmap);
        		memset(p_client_detail_info_tab, 0x00, sizeof(CLIENT_DETAIL_INFO_TABLE));
        		p_client_detail_info_tab->detail_info_num = 0;
			spinlock_unlock(SPINLOCK_Networkmap);
		    }
		    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 = 1;
                	arp_timeout.tv_usec = 500000; //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

		arp_getlen=recvfrom(arp_sockfd, buffer, 512, 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, 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],
				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);

			//ARP Response packet to router
			if( 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
			{
			    //NMP_DEBUG("   It's an ARP Response to Router!\n");
                            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) ) 
                                    break;
                            }
			    //i=0, table is empty.
			    //i=num, no the same ip at table.
			    if(i==p_client_detail_info_tab->ip_mac_num){
				spinlock_lock(SPINLOCK_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->ip_mac_num++;
				spinlock_unlock(SPINLOCK_Networkmap);

			    #ifdef DEBUG  //Write client info to file
                		fp_ip=fopen("/var/client_ip_mac.txt", "a");
                		if (fp_ip==NULL) {
                    		printf("File Open Error!\n");
                		}
                		else {
                        	printf("Fill: %d-> %d.%d", 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) ) {
                                              	NMP_DEBUG_M("Find the same IP 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 IP\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

	    //Find All Application of clients
	    if(p_client_detail_info_tab->detail_info_num < p_client_detail_info_tab->ip_mac_num) {
		FindAllApp(my_ipaddr, 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) {
                        printf("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
		p_client_detail_info_tab->detail_info_num++;
	    }

	} //End of main while loop
	close(arp_sockfd);
	return 0;
}
Ejemplo n.º 3
0
/*
 * playwav:
 * This function reads the data from the given wave file
 * at a constant rate and passes them to wlan driver
 */
int playwav(int fd)
{

    wavheader_t hdr;
    sub_chunk_data_t sc;
    int tmpcount = 0;
    int ret = 0;
    int count = 0;
    int i = 0;
    char *audio_data;
    char *data;
    char *tmp;
    int total_bytes_read = 0;
    int iterations = 0;


    if (fd < 0)
        return -EINVAL;

    read(fd, &hdr, sizeof(hdr));

#if __BYTE_ORDER == __BIG_ENDIAN
    hdr.length = bswap_32(hdr.length);
    hdr.sub_chunk_length = bswap_32(hdr.sub_chunk_length);
    hdr.format = bswap_32(hdr.format);
    hdr.mode = bswap_32(hdr.mode);
    hdr.sample_freq = bswap_32(hdr.sample_freq);
    hdr.byte_per_sec = bswap_32(hdr.byte_per_sec);
    hdr.bit_per_sample = bswap_32(hdr.bit_per_sample);
#endif  /* __BYTE_ORDER */    


    if (hdr.sub_chunk_length == 16) {
        tmp = &hdr.pad[0];
        lseek(fd, -2, SEEK_CUR);
    } else if ( hdr.sub_chunk_length == 18) {
        tmp = &hdr.pad[2];
    } else {
        printf("Invalid Header\n");
        return -EINVAL;
    }        

    bcopy(tmp, &sc, sizeof(sc));

#if __BYTE_ORDER == __BIG_ENDIAN
    sc.chunk = bswap_32(sc.chunk);
    sc.length = bswap_32(sc.length);
#endif  /* __BYTE_ORDER */    

    if (buf_size <= 0) {
        buf_size = NUM_BYTES_READ;
    }        

#ifdef  MASTER_CLK    
    if (ioctl(fd, I2S_MCLK, mclk_sel) < 0) {
        perror("I2S_MCLK");
    }        
#endif  /* MASTER_CLK */    

    send_aow_start();
    audio_data = (char *)malloc(buf_size * sizeof(char));

    if (audio_data == NULL)
        return -ENOMEM;

    for (i = 0; i <= sc.length; i += buf_size) {

        count = buf_size;


        if ((i + count) > sc.length) {
            count = sc.length - i;
        }            

        iterations++;
        total_bytes_read += count;

        if ((count = read(fd, audio_data, count)) <= 0) {
            perror("Read audio data");
            break;
        }            
        
        if (hdr.bit_per_sample == 16) {
            swapbytes16(audio_data, count);
        }            
       
        /* Send the data to wlan device via ioctl interface
         * XXX Take care of errror
         */

        send2driver(data, tmpcount);
    }        
    send_aow_reset();
    printf("\nTotal bytes read = %d in %d iterations (wav file)\n", total_bytes_read, iterations);
    free(audio_data);
    return 0;
}
Ejemplo n.º 4
0
char *processPacket(int sockfd, char *pdubuf)
{

    apmib_reinit();

    unsigned char buffer[500];	//Edison
    int opmode;

    IBOX_COMM_PKT_HDR	*phdr;
    IBOX_COMM_PKT_HDR_EX *phdr_ex;
    IBOX_COMM_PKT_RES_EX *phdr_res;
    PKT_GET_INFO *ginfo;

#ifdef WAVESERVER	// eric++
    int fail = 0;
    pid_t pid;
    DIR *dir;
    int fd, ret, bytes;
    unsigned char tmp_buf[15];	// /proc/XXXXXX
    WS_INFO_T *wsinfo;
#endif
//#ifdef WL700G
    STORAGE_INFO_T *st;
//#endif
//    int i;
    char ftype[8], prinfo[128];	/* get disk type */
    int free_space;

    phdr = (IBOX_COMM_PKT_HDR *)pdubuf;  
    phdr_res = (IBOX_COMM_PKT_RES_EX *)pdubuf_res;
	swapbytes16(phdr->OpCode);	//Edison 2011.4.28 normal
	swapbytes32(phdr->Info);	//Edison 2011.4.28 normal
//    printf("Get: %x %x %x\n", phdr->ServiceID, phdr->PacketType, phdr->OpCode);
    
    if (phdr->ServiceID==NET_SERVICE_ID_IBOX_INFO && phdr->PacketType==NET_PACKET_TYPE_CMD)
    {	    
	if (phdr->OpCode!=NET_CMD_ID_GETINFO && phdr->OpCode!=NET_CMD_ID_GETINFO_MANU && phdr_res->OpCode==phdr->OpCode && phdr_res->Info==phdr->Info)
	{	
		// if transaction id is equal to the transaction id of the last response message, just re-send message again;
		return pdubuf_res;
	}	
	swapbytes16(phdr->OpCode);	//Edison 2011.4.28
	phdr_res->ServiceID=NET_SERVICE_ID_IBOX_INFO;
	phdr_res->PacketType=NET_PACKET_TYPE_RES;
	phdr_res->OpCode=phdr->OpCode;
	swapbytes16(phdr->OpCode);	//Edison 2011.4.28 normal
	if (phdr->OpCode!=NET_CMD_ID_GETINFO && phdr->OpCode!=NET_CMD_ID_GETINFO_MANU)
	{
		swapbytes32(phdr->Info);	//Edison 2011.4.28
		swapbytes16(phdr->OpCode);	//Edison 2011.4.28
		phdr_ex = (IBOX_COMM_PKT_HDR_EX *)pdubuf;	
		swapbytes32(phdr->Info);	//Edison 2011.4.28
		swapbytes16(phdr->OpCode);	//Edison 2011.4.28
		// Check Mac Address
		if (memcpy(phdr_ex->MacAddress, mac, 6)==0)
		{
			printf("Mac Error %2x%2x%2x%2x%2x%2x\n",
				(unsigned char)phdr_ex->MacAddress[0],
				(unsigned char)phdr_ex->MacAddress[1],
				(unsigned char)phdr_ex->MacAddress[2],
				(unsigned char)phdr_ex->MacAddress[3],
				(unsigned char)phdr_ex->MacAddress[4],
				(unsigned char)phdr_ex->MacAddress[5]
				);
			return NULL;
		}
		
		// Check Password
		//if (strcmp(phdr_ex->Password, "admin")!=0)
		//{
		//	phdr_res->OpCode = phdr->OpCode | NET_RES_ERR_PASSWORD;
		//	printf("Password Error %s\n", phdr_ex->Password);	
		//	return NULL;
		//}
		swapbytes32(phdr_ex->Info);	//Edison 2011.4.28 unormal
		phdr_res->Info = phdr_ex->Info;
		swapbytes32(phdr_ex->Info);	//Edison 2011.4.28 normal
		memcpy(phdr_res->MacAddress, phdr_ex->MacAddress, 6);
	}
	switch(phdr->OpCode)
	{
		case NET_CMD_ID_GETINFO:
		     ginfo=(PKT_GET_INFO *)(pdubuf_res+sizeof(IBOX_COMM_PKT_RES));
		     memset(ginfo, 0, sizeof(ginfo));
	
		     apmib_get( MIB_WLAN_SSID,  (void *)buffer);
		     strcpy(ssid_g, buffer);

		     strcpy(productid_g, MODEL_NAME);

   		     strcpy(ginfo->SSID, ssid_g);
		     strcpy(ginfo->NetMask, netmask_g);
		     strcpy(ginfo->ProductID, productid_g);	// disable for tmp
		     strcpy(ginfo->FirmwareVersion, firmver_g); // disable for tmp
		     
		     memcpy(ginfo->MacAddress, mac, 6);
#ifdef WCLIENT
		     ginfo->OperationMode = OPERATION_MODE_WB;
		     ginfo->Regulation = 0xff;
#endif

#ifdef WAVESERVER    // eric++
	     	     // search /tmp/waveserver and get information
	     	     wsinfo = (WS_INFO_T*) (pdubuf_res + sizeof (IBOX_COMM_PKT_RES) + sizeof (PKT_GET_INFO));

	     	     fd = open (WS_INFO_FILENAME, O_RDONLY);
	     	     if (fd != -1)	{
				bytes = sizeof (WS_INFO_T);
				while (bytes > 0)	{
			    	ret = read (fd, wsinfo, bytes); 
		    		if (ret > 0)			{ bytes -= ret;		} 
					else if (ret < 0)		{ fail++; break;	} 
					else if (ret == 0)		{ fail++; break;	}
		 		} /* while () */
			} else {
				fail++;
			}

			if (fail == 0 && bytes == 0)	{
				ret = read (fd, &pid, sizeof (pid_t));
				if (ret == sizeof (pid_t))	{
			    	sprintf (tmp_buf, "/proc/%d", pid);
			    	dir = opendir (tmp_buf);
			    	if (dir == NULL)	{	// file exist, but the process had been killed
		    		    fail++;
				    }
				    closedir (dir);
				} else {	// file not found or error occurred
					fail++;
				}
	   		}

			if (fail != 0)	{
		    		memset (wsinfo, 0, sizeof (WS_INFO_T));
			}
#endif /* #ifdef WAVESERVER */
	#ifdef WAVESERVER
	     		st = (STORAGE_INFO_T *) (pdubuf_res + sizeof (IBOX_COMM_PKT_RES) + sizeof (PKT_GET_INFO) + sizeof(WS_INFO_T));
	#else
	     		st = (STORAGE_INFO_T *) (pdubuf_res + sizeof (IBOX_COMM_PKT_RES) + sizeof (PKT_GET_INFO));
	#endif
		  	//getStorageStatus(st);	//Edison 2011.4.27 no use
			sendInfo(sockfd, pdubuf_res);
			return pdubuf_res;		     	

		case NET_CMD_ID_MANU_CMD:
		{
		     #define MAXSYSCMD 256
		     char cmdstr[MAXSYSCMD];
		     PKT_SYSCMD *syscmd;
		     PKT_SYSCMD_RES *syscmd_res;
		     FILE *fp;

		     syscmd = (PKT_SYSCMD *)(pdubuf+sizeof(IBOX_COMM_PKT_HDR_EX));
		     syscmd_res = (PKT_SYSCMD_RES *)(pdubuf_res+sizeof(IBOX_COMM_PKT_RES_EX));

		     if (syscmd->len>=MAXSYSCMD) syscmd->len=MAXSYSCMD;
		     syscmd->cmd[syscmd->len]=0;
		     syscmd->len=strlen(syscmd->cmd);
		     printf("system cmd: %d %s\n", syscmd->len, syscmd->cmd);
   
			if (!strcmp(syscmd->cmd,"nvram get sw_mode"))
			{
				FILE *fp;
				int opmode;
				fp=fopen("/var/sys_op","r");
				fscanf(fp,"%d",&opmode);
				fclose(fp);

				if (opmode==0){
					strcpy(cmdstr, "echo 1 > /tmp/syscmd.out");
				}else if(opmode==1){
					strcpy(cmdstr, "echo 3 > /tmp/syscmd.out");
				}
			}else{
				sprintf(cmdstr, "%s > /tmp/syscmd.out", syscmd->cmd);
			}
			system(cmdstr);

			printf("rund: %s\n", cmdstr);
			fp = fopen("/tmp/syscmd.out", "r");

			if (fp!=NULL)
			{
				strcpy(syscmd->cmd,"nvram get sw_mode");	//Edison 2011.4.27
				syscmd_res->len = fread(syscmd_res->res, 1, sizeof(syscmd_res->res), fp);
				fclose(fp);
			}
			else syscmd_res->len=0;
			printf("%d %s\n", syscmd_res->len, syscmd_res->res);
			/* repeat 3 times for MFG by Yen*/
			sendInfo(sockfd, pdubuf_res);
			sendInfo(sockfd, pdubuf_res);
			sendInfo(sockfd, pdubuf_res);
			/* end of MFG */
		     
	 	     return pdubuf_res;
		}
		default:
			return NULL;	
	}
    }
    return NULL;
}