⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 speedtest.c

📁 这是关于网络侦测上传带宽的源代码。大家可以了解一下
💻 C
📖 第 1 页 / 共 2 页
字号:
	remove(DETECT_FILE);	//unlink(DETECT_FILE);	int i=0;	int ping=0;	//eval(cmd);	system(cmd);	//sleep(20);	if ((fp = fopen(DETECT_FILE, "r")) != NULL) 		{			while(i<16)			{				if( fgets(line, sizeof(line), fp) != NULL ) 				{					char *p=NULL;					p=index(line,'(');					if(!p)					{						printf("has no address!\n\n");						ping++;						if(ping>=3)						{							return 0;						}						continue;					}					int j=1;					while((*p)!=')')					{						p++;						get_ip[i][j-1]=*p;						j++;					}					get_ip[i][j-2]='\0';					//printf("%s\n\n",p[i]);									}				i++;			}			i=0;			while(i<16)			{				printf("aaaa%d:",i);				printf("%s\n\n",get_ip[i]);				sprintf(gateway_ip[i],"%s",get_ip[i]);				i++;			}			//printf("%s\n\n",get_ip[1]);			//printf("%s\n\n",get_ip[2]);			//sprintf(ip_3,"%s",get_ip[2]);			//sprintf(ip_4,"%s",get_ip[3]);			//sprintf(ip_4,"%s",get_ip[6]);			/*ip_4=get_ip[3];			printf("ip_3:%s\n",ip_3);			printf("ip_4:%s\n",ip_4);*/					}		fclose(fp);		return 1;	}/************************************************************************** * Function Name: qos_get_wan_rate * Description  : get the ADSL line rate * Parameters   : None * Returns      : Is successful when get the ADSL data rate? 			0 : Success			1 : Link fail **************************************************************************/int qos_get_wan_rate(void){	static struct wan_link_rate wanrate;	char us[256], ds[256] ;	char * gw_ip=NULL;	struct timeval deltaval;       	double delta_us = 0;	wanrate.us = wanrate.ds = 0;	char *ip = nvram_safe_get("wan_dns_t");	if( !check_wan_link(0) )		return WAN_LINK_FAIL;	gw_ip = get_gw_ip();	if((strchr(ip, ' ')) ||(!strcmp(ip, "")))	{		struct dns_lists *dns_list = get_dns_list(1);		int i;				for(i=0 ; i<dns_list->num_servers ; i++){			ip = dns_list->dns_server[i];			if(    (!strchr(ip, ' ')) 			    && (strcmp(ip, ""))			    && (strcmp(ip, gw_ip))) //WL600g auto assign LAN IP as DNS Server 				break;		}		free(dns_list);	}	//printf("wan_dns_t:%s\n\n",ip);	//printf("gw_ip:%s\n\n",gw_ip);	if( (strchr(ip, ' ')) || (!strcmp(ip, "")) || (!strcmp(ip, gw_ip)) ) {#ifdef QOS_DEBUG                                                		printf("	qos_get_wan_rate() ** GET DNS IP Error\n");#endif		nvram_set("qos_ubw_status", "fail");		nvram_set("qos_ubw_reason", "DNS server fail");	}	else 	{		int choose=1;		//char ip_3[30],ip_4[30];		choose=detect();		printf("aaaaa\n\n");		printf("choose:%d",choose);		if(choose==0)		{			return WAN_LINK_FAIL;		}		int i=0;		while(i<16)			{				printf("aaaa%d:",i);				printf("%s\n\n",gateway_ip[i]);				//sprintf(gateway_ip[i],"%s",get_ip[i]);				i++;			}		//printf("ip_3%s\n\n",gateway_ip[3]);		//printf("ip_4%s\n\n",gateway_ip[3]);		/*while(j<3)		{			if(ip_3[i]=='.')			{				j++;			}			ip_3_1[i]=ip_3[i];			i++;		}		printf("ip_3_1%s\n\n",ip_3_1);*/		//printf("aa\n");		//printf("%s\n",ip_3);		//printf("%s\n",ip_4);	        //deltatime(ip, &deltaval);		 deltatime(gateway_ip[3], &deltaval);		//deltatime("220.181.37.55", &deltaval);	}	free(gw_ip);	if( strcmp(nvram_safe_get("qos_ubw_status"), "fail") == 0 ) { #ifdef WL600g		if( !strcmp(nvram_safe_get("qos_ubw_reason"), "DNS server fail") )		{			//DownStream        		if ( glbAdslInfo.ulInterleavedDnStreamRate != 0 )         		    wanrate.ds = glbAdslInfo.ulInterleavedDnStreamRate / 1000;        		else            		    wanrate.ds =  glbAdslInfo.ulFastDnStreamRate / 1000;			//UpStream        		if ( glbAdslInfo.ulInterleavedUpStreamRate != 0 )		            wanrate.us = glbAdslInfo.ulInterleavedUpStreamRate / 1000;		        else            		    wanrate.us = glbAdslInfo.ulFastUpStreamRate / 1000;			//sprintf(ds, "%u", wanrate.ds);			//nvram_set("wan_ds", ds);			sprintf(us, "%u", wanrate.us);			nvram_set("qos_ubw", us);				nvram_set("qos_ubw_status", "success");			return WAN_RATE_SUCCESS;			}		else			return WAN_LINK_FAIL;#else				return WAN_LINK_FAIL;#endif	}#ifdef QOS_DEBUG        printf("\n      start time ** tv_sec=%ld: tv_usec=%ld\n",tstart.tv_sec, tstart.tv_usec);        printf("        end time   ** tv_sec=%ld : tv_usec=%ld\n", tend.tv_sec, tend.tv_usec);        printf("                      delta_sec=%ld : delta_usec=%ld\n", deltaval.tv_sec*1000000, deltaval.tv_usec);#endif                                                                                                         delta_us = (int)deltaval.tv_sec * 1000000 + deltaval.tv_usec;   	wanrate.us = ( (( (datalen+42)*pingcount*8 )/delta_us) )*1000000/1024;	printf("deltaval_sec %d,deltaval_usec %d\n",deltaval.tv_sec,deltaval.tv_usec); 	printf("wanrate.us=%d\n\n",wanrate.us);	if( wanrate.us!=0 ) 		nvram_set("qos_ubw_status", "success");	#ifdef 	QOS_DEBUG        printf("\nUpload Bandwidth = %.0u kbps\n", wanrate.us);#endif		wanrate.ds = 0;	/*int i=0;	for (i = 0; i < STRUCT_LEN(icmp_map); i++)		if (wanrate.us >= icmp_map[i].difference) {		    wanrate.us =icmp_map[i].rate;		    break;		}*/	//sprintf(ds, "%u", wanrate.ds);	//nvram_set("wan_ds", ds);	int i=4;	while(wanrate.us>11264 || wanrate.us<0)	{		deltatime(gateway_ip[i], &deltaval);		 printf("                      delta_sec=%ld : delta_usec=%ld\n", deltaval.tv_sec*1000000, deltaval.tv_usec);		printf("deltaval_sec %d,deltaval_usec %d\n",deltaval.tv_sec,deltaval.tv_usec); 		delta_us = (int)deltaval.tv_sec * 1000000 + deltaval.tv_usec;               	wanrate.us = ( (( (datalen+42)*pingcount*8 )/delta_us) )*1000000/1024;		printf("ip()%s\n",gateway_ip[i]);		printf("wanrate.us=%d\n\n",wanrate.us);		i++;		if(i==16)		{			return WAN_LINK_FAIL;			}	}		if(wanrate.us==0)		{			return WAN_LINK_FAIL;		}				sprintf(us, "%u", wanrate.us);		nvram_set("qos_ubw", us);			return WAN_RATE_SUCCESS;		}#ifdef WL600g//**************************************************************************// Function Name: Speedtest_init// Description  : init function, called by BcmNtwk_init() in ifcntwkapi.cpp// Returns      : 0//**************************************************************************int Speedtest_init(void) {        FILE *fp;        /* Run it under background */        switch (fork()) {        case -1:                exit(0);                break;        case 0:                // start in a new session                (void) setsid();                break;        default:                // parent process should just die                return 0;        }        /* write pid */        if ((fp=fopen("/var/run/speedtest.pid", "w"))!=NULL)        {                fprintf(fp, "%d", getpid());                fclose(fp);        }		nvram_set("qos_ubw_status", "initialing");	sleep(60);	if( nvram_match("qos_enable", "1") ) {		qos_get_wan_rate();		if(nvram_match("qos_ubw_status", "success") )			start_qos();	}	while(1){		pause();	}        return 0;}#else//extern void start_qos(void);int Speedtest_Init(void) {/*	nvram_set("qos_enable", "1");	nvram_set("qos_dfragment_enable", "1");	nvram_set("qos_dfragment_size", "10");	nvram_set("qos_global_enable", "1");	nvram_set("qos_service_enable", "1");	nvram_set("qos_service_ubw", "10");	nvram_set("qos_tos_prio", "1");	nvram_set("qos_pshack_prio", "1");	nvram_set("qos_shortpkt_prio", "1");	nvram_set("qos_userdef_enable", "1");	nvram_set("qos_userspec_app", "1");	nvram_set("qos_rulenum_x", "2");		nvram_set("qos_ip_x0", "192.168.6.54");	nvram_set("qos_port_x0", "3333");	nvram_set("qos_prio_x0", "3");	nvram_set("qos_ip_x1", "192.168.6.54");	nvram_set("qos_port_x1", "8888");	nvram_set("qos_prio_x1", "5");*/                nvram_set("qos_userspec_app", "0");                nvram_set("qos_global_enable", "0");                nvram_set("qos_userdef_enable", "0");                nvram_set("qos_enable", "0");        if ( nvram_invmatch("qos_rulenum_x", "0"))                nvram_set("qos_userspec_app", "1");        if( nvram_match("qos_tos_prio", "1") || nvram_match("qos_pshack_prio", "1") || nvram_match("qos_shortpkt_prio", "1") )                nvram_set("qos_global_enable", "1");        if( nvram_match("qos_userspec_app", "1") || nvram_match("qos_dfragment_enable", "1") || nvram_match("qos_service_enable", "1") )                nvram_set("qos_userdef_enable", "1");                                                                                                                                                       if( nvram_match("qos_global_enable", "1") || nvram_match("qos_userdef_enable", "1") ) {//                sleep(3);                nvram_set("qos_enable", "1");               qos_get_wan_rate();                if(nvram_match("qos_ubw_status", "success") )                        start_qos();        }                                                                                                                                               	return 0;}#endif #ifdef MYGCCint main(int argc, char** argv){	int i = 0;	char *thisarg;	printf("	%-20s	%-20s\n", "index", "value");	while( i<argc ) {	    printf("	%-20d	%-20s\n", i, argv[i]);	    i++;	}	argc--;	argv++;	while( argc>0 && **argv=='-' ) {	    	thisarg = *argv;		thisarg++;		switch (*thisarg) {		case 'c':			if (--argc <= 0)			        exit(1);			argv++;			pingcount = atoi(*argv);			break;		case 's':			if (--argc <= 0)			        exit(1);			argv++;			datalen = atoi(*argv);			break;		default:			exit(1);		}		argc--;		argv++;	}	if( argc<0 ) 	exit(1);	        struct timeval deltaval;        deltatime(*argv, &deltaval);		printf("\n	start time ** tv_sec=%u : tv_usec=%u\n",tstart.tv_sec, tstart.tv_usec);	printf("	end time   ** tv_sec=%u : tv_usec=%u\n", tend.tv_sec, tend.tv_usec);        printf("        	      delta_sec=%u : delta_usec=%u\n", deltaval.tv_sec*1000000, deltaval.tv_usec);        	double delta_us = 0;	delta_us = (int)deltaval.tv_sec * 1000000 + deltaval.tv_usec;        printf("        delta interval = %f us\n", delta_us);                                                                                                         double bw = ( (( (datalen+42)*pingcount*8 )/delta_us) )*1000000/1024;        printf("\n	bandwidth = %.0f kbps\n", bw);		return 0;}#endif 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -