嵌入式 hi3518平台指定网卡测试是否通外网

版权声明:本文为博主原创文章,未经博主允许不得转载。

[html] view plain copy

 
 

  1.   

[html] view plain copy

 
 

  1. /********************************** (C) COPYRIGHT *******************************  
  2.   * File Name          : netstatus_check.c  
  3.   * Author             : skdkjzz  
  4.   * Date               : 2014/08/07  
  5.   * Description        : 检测本机是否连通外部网络(Joseph_Ping 百度)。  
  6.   *********************************************************************************/  
  7.     
  8. #include <stdio.h>    
  9. #include <string.h>  
  10. #include <stdlib.h>   
  11. #include <sys/socket.h>     
  12. #include <netinet/in.h>     
  13. #include <netinet/ip.h>     
  14. #include <netinet/ip_icmp.h>     
  15. #include <netdb.h>    
  16. #include <errno.h>  
  17. #include <sys/types.h>  
  18. #include <netinet/in.h>  
  19. #include <sys/types.h>    
  20. #include <string.h>    
  21. #include <stdlib.h>    
  22. #include <sys/types.h>    
  23. #include <sys/ioctl.h>    
  24. #include <sys/stat.h>    
  25. #include <stdio.h>    
  26. #include <string.h>    
  27. #include <errno.h>    
  28. #include <net/if.h>    
  29. #include <sys/utsname.h>    
  30. #include <limits.h>    
  31. #include <ctype.h>       
  32. #include <sys/socket.h>    
  33. #include <netinet/in.h>    
  34. #include <arpa/inet.h>       
  35. #include <linux/sockios.h>   
  36.   
  37.   
  38. #define PACKET_SIZE     4096     
  39. #define ERROR           -1     
  40. #define SUCCESS         0     
  41.       
  42. /*效验算法*/   
  43. unsigned short Joseph_Cal_Chksum(unsigned short *addr, int len)   
  44. {    
  45.     int nleft=len;    
  46.     int sum=0;    
  47.     unsigned short *w=addr;    
  48.     unsigned short answer=0;      
  49.   
  50.     while(nleft > 1)   
  51.     {    
  52.       sum += *w++;    
  53.       nleft -= 2;    
  54.     }    
  55.   
  56.     if( nleft == 1)    
  57.     {         
  58.       *(unsigned char *)(&answer) = *(unsigned char *)w;    
  59.       sum += answer;    
  60.     }    
  61.   
  62.     sum = (sum >> 16) + (sum & 0xffff);    
  63.     sum += (sum >> 16);    
  64.     answer = ~sum;    
  65.   
  66.     return answer;    
  67. }    
  68.   
  69. int Joseph_Ping( char *ips,char *srcip , int timeout)    /* Ping函数 */    
  70. {        
  71.     struct timeval *tval;          
  72.     int maxfds = 0;    
  73.     fd_set readfds;    
  74.   
  75.     struct sockaddr_in addr;        
  76.     struct sockaddr_in from;  
  77.     struct ifreq ifr;  
  78.     bzero(&addr,sizeof(addr));  /* 设定Ip信息 */    
  79.     addr.sin_family = AF_INET;    
  80.     addr.sin_addr.s_addr = inet_addr(ips);    
  81.   
  82.     int sockfd;    
  83.     sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);   /* 取得socket */     
  84.     if (sockfd < 0)     
  85.     {      
  86.       printf("ip:%s,socket error\n",ips);      
  87.       return ERROR;      
  88.     }      
  89.   
  90.     struct timeval timeo;  
  91.     timeo.tv_sec = timeout / 1000;  /* 设定TimeOut时间  */  
  92.     timeo.tv_usec = timeout % 1000;    
  93.   
  94. #if 0  
  95.     /*set src ip*/  
  96.     bzero(&from,sizeof(from));  /* 设定Ip信息 */  
  97.     from.sin_family = AF_INET;    
  98.     from.sin_addr.s_addr = inet_addr(srcip);      
  99.   
  100.     if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_IF,(struct sockaddr *)&from, sizeof(from)) == -1)      
  101.     {      
  102.       printf("ip:%s,setsockopt error \n",srcip);       
  103.       return ERROR;  
  104.     }    
  105.     bind(sockfd,(struct sockaddr *)&addr, sizeof(addr));  
  106. #else  
  107.     strcpy(ifr.ifr_name, srcip);  
  108.   
  109.     if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) == -1)  
  110.     {  
  111.         printf("can't bind to interface %s\n",ifr.ifr_name);  
  112.     }  
  113.       
  114. #endif  
  115.   
  116.     printf("%s %d\n",__FUNCTION__,__LINE__);  
  117.   
  118.     if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)) == -1)      
  119.     {      
  120.       printf("ip:%s,setsockopt error\n",ips);      
  121.       return ERROR;      
  122.     }  
  123.     else  
  124.     {  
  125.         printf("ip:%s,setsockopt ok \n",ips);    
  126.     }  
  127.   
  128.     char sendpacket[PACKET_SIZE];      
  129.     char recvpacket[PACKET_SIZE];      
  130.   
  131.     memset(sendpacket, 0, sizeof(sendpacket));   /* 设定Ping包 */    
  132.     pid_t pid;    
  133.   
  134.     pid=getpid();   /* 取得PID,作为Ping的Sequence ID */     
  135.     struct ip *iph;    
  136.     struct icmp *icmp;    
  137.           
  138.     icmp=(struct icmp*)sendpacket;   
  139.   
  140.     icmp->icmp_type=ICMP_ECHO;   /* 回显请求 */    
  141.     icmp->icmp_code=0;     
  142.     icmp->icmp_cksum=0;     
  143.     icmp->icmp_seq=0;      
  144.     icmp->icmp_id=pid;     
  145.     tval= (struct timeval *)icmp->icmp_data;     
  146.     gettimeofday(tval,NULL);        
  147.     icmp->icmp_cksum=Joseph_Cal_Chksum((unsigned short *)icmp,sizeof(struct icmp));  /* 校验 */      
  148.   
  149.     int n;  /* 发包  */     
  150.     n = sendto(sockfd, (char *)&sendpacket, sizeof(struct icmp), 0, (struct sockaddr *)&addr, sizeof(addr));        
  151.     if (n < 1)     
  152.     {      
  153.       printf("ip:%s,sendto error\n",ips);      
  154.       return ERROR;      
  155.     }      
  156.          
  157.     while(1)      /* 接受 由于可能接受到其他Ping的应答消息,所以这里要用循环 */    
  158.     {      
  159.       FD_ZERO(&readfds);    /* 设定TimeOut时间,这次才是真正起作用的  */   
  160.       FD_SET(sockfd, &readfds);      
  161.       maxfds = sockfd + 1;      
  162.       n = select(maxfds, &readfds, NULL, NULL, &timeo);      
  163.       if (n <= 0)       
  164.       {      
  165.           printf("ip:%s,Time out error\n",ips);      
  166.           close(sockfd);      
  167.           return ERROR;      
  168.       }      
  169.           
  170.       memset(recvpacket, 0, sizeof(recvpacket));      
  171.       int fromlen = sizeof(from);      /* 接受    */   
  172.       n = recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, (struct sockaddr *)&from, (socklen_t *)&fromlen);      
  173.       if (n < 1) {  
  174.           
  175.           return ERROR;   
  176.       }      
  177.               
  178.       char *from_ip = (char *)inet_ntoa(from.sin_addr);        
  179.       if (strcmp(from_ip,ips) != 0)    /* 判断是否是自己Ping的回复 */    
  180.       {      
  181.           printf("Now Pingip:%s Fromip:%s\n Now Pingip is not same to Fromip,so Joseph_Ping wrong!\n",ips,from_ip);    
  182.           return ERROR;   
  183.       }      
  184.           
  185.       iph = (struct ip *)recvpacket;          
  186.       icmp=(struct icmp *)(recvpacket + (iph->ip_hl<<2));     
  187.              
  188.   
  189.       if (icmp->icmp_type == ICMP_ECHOREPLY && icmp->icmp_id == pid)   /* 判断Ping回复包的状态 ICMP_ECHOREPLY回显应答 */    
  190.       {  
  191.           return SUCCESS;  
  192.       }   /* 正常退出循环 */     
  193.       else         
  194.           continue;    /* 否则继续等  */    
  195.     }  
  196.   
  197.     return 0;  
  198. }  
  199.     
  200. int main(int argc ,char *argv[])    
  201. {    
  202.     int Qy_Ret = 0;  
  203.     struct hostent *h=NULL;  
  204.     char hostname[16]="www.baidu.com";  
  205.   
  206.     char aPing[16]="202.108.22.5";  /* Joseph_Ping form ip  */  
  207.     Qy_Ret = Joseph_Ping(aPing,argv[1],3000);  
  208.     printf("Qy_Ret is %d\n",Qy_Ret);  
  209.     if(Qy_Ret == 0)    
  210.     {    
  211.         printf("Network is Ok!\n");   
  212.         return 0;  
  213.     }    
  214.     else      
  215.     {    
  216.         printf("Network is Bad!\n");  
  217.         return -1;  
  218.     }  
  219.       
  220.     sprintf(hostname,"%s",(char *)inet_ntoa(*((struct in_addr *)h->h_addr))); /* Joseph_Ping form hostname */  
  221.     if(Joseph_Ping(hostname,argv[1],3000))    
  222.     {    
  223.         printf("Network is Ok!\n");    
  224.         return 0;  
  225.     }    
  226.     else      
  227.     {    
  228.         printf("Network is Bad!\n");   
  229.         return -1;  
  230.     }     
  231. }  
  232.   
  233. </span>  
时间: 2024-08-24 23:31:00

嵌入式 hi3518平台指定网卡测试是否通外网的相关文章

内网不能ping通外网解决办法

  问题描述: 1.把PIX放在固定IP环境(外网为固定IP),并设置正确的ACL允许ICMP放行,这种情况下内网可以正常上网也能ping外网. 2.然后把PIX放在ADSL环境(用PIX进行拨号),ACL还是之前的不变,PIX下的内网电脑就只能上网,无法ping外网了. 解决办法: 问题的起因是由于PIX没有开放对ICMP的审计,需要使用命令fixup protocol icmp来进行ICMP的审计并放行. pix(config)# fixup protocol icmp //其实只需要这一条

网络-linux能ping通外网,但是自带的火狐浏览器打不开网页

问题描述 linux能ping通外网,但是自带的火狐浏览器打不开网页 linux系统能ping通外网,但是自带的火狐浏览器打不开网页,是什么情况 解决方案 正如错误提示,https连接的公钥私钥创建不成立,加密通道创建失败无法连接.你ping的时候能成功应该用的是http协议. 解决方案二: ping是icmp协议,http是应用层协议,,不是一回事啊.建议用wget baidu.com试试,如果不行的话最好换个新版本的firefox. 解决方案三: 用wget baidu.com试试,可以的话

能ping通外网dns但不能上网一例

一个win7本本仅通过一个无线路由一个人上网,突然一天不能上网了,甚是奇怪,一看本地连接均是正常的.而且能ping通外网的dns,但无论如何就是打不开网页,表现为输入任何网址很迅速的显示该页无法显示,好像浏览器并没有提交任何url就做出了反应一样.其它客户端诸如QQ,迅雷等都一样不能访问因特网.   分析,由于能ping通外网,并且是自动从路由获得的ip,子网,网关,dns信息,所以可以肯定的是路由肯定是没有问题的,把dns手动更换为8.8.8.8也无济于事.由此可断问题,肯定出现在本机上. 这

嵌入式 hi3518平台uboot引导nfs文件系统

首先贴出来我的bootargs的设置(注没有换行符!!!): setenv bootargs noinitrd mem=64M root=/dev/nfs init=/linuxrc rw nfsroot=10.10.2.59:/opt/rootfs/ ip=10.10.1.156:10.10.2.59:10.10.1.1:255.255.255.0:skdkjzz:eth0:off console=ttyAMA0,115200   Linux编译生成zImage,但是uboot不能识别,ubo

嵌入式 hi3518平台多路码流添加osd

[html] view plain copy     <span style="font-family:Courier New;">      /******************************************************************************   function : overlay process   1) create some overlay regions   2) display overlay regi

嵌入式 hi3518平台检测网线是否插上

[html] view plain copy        [html] view plain copy     /********************************** (C) COPYRIGHT *******************************   * File Name          : linkstatus_check.c   * Author             : skdkjzz   * Date               : 2014/08/0

嵌入式 hi3518平台以太网网络模块设计包括重连机制和网线检测机制

[html] view plain copy     <span style="font-family:Courier New;">   #include <sys/types.h>     #include <string.h>     #include <stdlib.h>     #include <sys/ioctl.h>     #include <sys/stat.h>     #include <

DHCP中继 NAT 让客户端自动获取ip地址并ping通外网

首先给给c1和c2 分vlan 在 交换机R5上设置 R5>en R5#conf t R5(config)#vlan 10 R5(config-vlan)#vlan 20 R5(config-vlan)#inter fa1/1 R5(config-if)#switchport access vlan 10 R5(config-if)#inter fa1/2 R5(config-if)#switchport access vlan 20 R5(config-if)#inter fa1/0 R5(c

nova-network配置后无法ping通外网

问题描述 配置如下:network_manager=nova.network.manager.FlatDHCPManagerfirewall_driver=nova.virt.libvirt.firewall.IptalbesFirewallDrivernetwork_size=254allow_same_net_traffic=Falsemulti_host=Truesend_arp_for_ha=Trueshare_dhcp_address=Trueforce_dhcp_release=Tr