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

时间:2023-03-09 02:53:56
嵌入式 hi3518平台指定网卡测试是否通外网

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

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