1. <span style="font-family:Courier New;">
  2. #include <sys/types.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <sys/ioctl.h>
  6. #include <sys/stat.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <net/if.h>
  11. #include <sys/utsname.h>
  12. #include <limits.h>
  13. #include <ctype.h>
  14. #include <sys/socket.h>
  15. #include <arpa/inet.h>
  16. #include <linux/sockios.h>
  17. #include <sys/socket.h>
  18. #include <netinet/ip.h>
  19. #include <netinet/ip_icmp.h>
  20. #include <netdb.h>
  21. #include <netinet/in.h>
  22. #define JOSEPH_LOOP_INTERFACE           "lo"
  23. #define JOSEPH_ETH_INTERFACE            "eth0"
  24. #define JOSEPH_WIRLESS_INTERFACE        "wlan0"
  25. #define ETHTOOL_GLINK                   0x0000000a   /* Get link status (ethtool_value) */
  26. #define JSOEPH_NET_CHECK_PACKET_SIZE    4096
  27. #define JOSEPH_NET_CHECK_TIME           3000
  28. typedef struct Joseph_Net_Interface_Info
  29. {
  30. int net_device_type;// 0 ~ eth0 ; 1 ~ wlan ;3 ~ ppp0
  31. int net_device_priority;// 0 ~ eth0 ; 1 ~ wlan ;3 ~ ppp0
  32. int net_device_status;//0 ~ down; 1 ~ up
  33. int net_device_link_status;//0 ~ no ;1 ~ yes
  34. char net_device_name[8];
  35. char net_device_ip[16];
  36. char net_device_mac_info[32];
  37. char net_device_gw_info[16];
  38. char net_device_mask_info[16];
  39. char net_device_broadcast_info[16];
  40. }JOSEPH_NET_INTERFACE_INFO;
  41. typedef struct Joseph_Ethtool_Value {
  42. unsigned int   cmd;
  43. unsigned int   data;
  44. }JOSEPH_ETHTOOL_VALUE;
  45. enum Joseph_Net_Device_Type
  46. {
  47. JOSEPH_ETH = 0,
  48. JOSEPH_WIFI = 1,
  49. JOSEPH_3G = 2,
  50. }JOSEPH_NET_DEVICE_TYPE;
  51. unsigned short Joseph_Cal_Chksum(unsigned short *addr, int len)
  52. {
  53. int nleft=len;
  54. int sum=0;
  55. unsigned short *w=addr;
  56. unsigned short answer=0;
  57. while(nleft > 1)
  58. {
  59. sum += *w++;
  60. nleft -= 2;
  61. }
  62. if( nleft == 1)
  63. {
  64. *(unsigned char *)(&answer) = *(unsigned char *)w;
  65. sum += answer;
  66. }
  67. sum = (sum >> 16) + (sum & 0xffff);
  68. sum += (sum >> 16);
  69. answer = ~sum;
  70. return answer;
  71. }
  72. int Joseph_Ping( char *ips,char *srcip , int timeout)
  73. {
  74. int n;
  75. pid_t pid;
  76. int maxfds = 0;
  77. fd_set readfds;
  78. struct ip *iph;
  79. struct icmp *icmp;
  80. struct timeval *tval;
  81. struct sockaddr_in addr;
  82. struct sockaddr_in from;
  83. struct ifreq ifr;
  84. bzero(&addr,sizeof(addr));
  85. addr.sin_family = AF_INET;
  86. addr.sin_addr.s_addr = inet_addr(ips);
  87. int sockfd;
  88. sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
  89. if (sockfd < 0)
  90. {
  91. printf("ip:%s,socket error\n",ips);
  92. return -1;
  93. }
  94. struct timeval timeo;
  95. timeo.tv_sec = timeout / 1000;
  96. timeo.tv_usec = timeout % 1000;
  97. #if 0
  98. /*set src ip*/
  99. bzero(&from,sizeof(from));  /* 设定Ip信息 */
  100. from.sin_family = AF_INET;
  101. from.sin_addr.s_addr = inet_addr(srcip);
  102. if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_IF,(struct sockaddr *)&from, sizeof(from)) == -1)
  103. {
  104. printf("ip:%s,setsockopt error \n",srcip);
  105. return ERROR;
  106. }
  107. bind(sockfd,(struct sockaddr *)&addr, sizeof(addr));
  108. #else
  109. strcpy(ifr.ifr_name, srcip);
  110. if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) == -1)
  111. {
  112. printf("can't bind to interface %s\n",ifr.ifr_name);
  113. }
  114. #endif
  115. if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)) == -1)
  116. {
  117. printf("ip:%s,setsockopt error\n",ips);
  118. return -1;
  119. }
  120. else
  121. {
  122. ;
  123. }
  124. char sendpacket[JSOEPH_NET_CHECK_PACKET_SIZE];
  125. char recvpacket[JSOEPH_NET_CHECK_PACKET_SIZE];
  126. memset(sendpacket, 0, sizeof(sendpacket));
  127. pid = getpid();
  128. icmp=(struct icmp*)sendpacket;
  129. icmp->icmp_type = ICMP_ECHO;
  130. icmp->icmp_code = 0;
  131. icmp->icmp_cksum = 0;
  132. icmp->icmp_seq = 0;
  133. icmp->icmp_id = pid;
  134. tval = (struct timeval *)icmp->icmp_data;
  135. gettimeofday(tval,NULL);
  136. icmp->icmp_cksum=Joseph_Cal_Chksum((unsigned short *)icmp,sizeof(struct icmp));
  137. n = sendto(sockfd, (char *)&sendpacket, sizeof(struct icmp), 0, (struct sockaddr *)&addr, sizeof(addr));
  138. if (n < 1)
  139. {
  140. printf("ip:%s,sendto error\n",ips);
  141. return -1;
  142. }
  143. while(1)
  144. {
  145. FD_ZERO(&readfds);
  146. FD_SET(sockfd, &readfds);
  147. maxfds = sockfd + 1;
  148. n = select(maxfds, &readfds, NULL, NULL, &timeo);
  149. if (n <= 0)
  150. {
  151. printf("ip:%s,Time out error\n",ips);
  152. close(sockfd);
  153. return -1;
  154. }
  155. memset(recvpacket, 0, sizeof(recvpacket));
  156. int fromlen = sizeof(from);
  157. n = recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, (struct sockaddr *)&from, (socklen_t *)&fromlen);
  158. if (n < 1)
  159. {
  160. return -1;
  161. }
  162. char *from_ip = (char *)inet_ntoa(from.sin_addr);
  163. if (strcmp(from_ip,ips) != 0)
  164. {
  165. printf("NowPingip:%s Fromip:%s\nNowPingip is not same to Fromip,so Joseph_Ping wrong!\n",ips,from_ip);
  166. continue;
  167. }
  168. iph = (struct ip *)recvpacket;
  169. icmp = (struct icmp *)(recvpacket + (iph->ip_hl << 2));
  170. if (icmp->icmp_type == ICMP_ECHOREPLY && icmp->icmp_id == pid)
  171. return 0;
  172. else
  173. continue;
  174. }
  175. return 0;
  176. }
  177. int Joseph_Check_Net_Status(char *if_name)
  178. {
  179. int Qy_Ret = 0;
  180. if(strlen(if_name) <= 0)
  181. {
  182. Qy_Ret = -1;
  183. return Qy_Ret;
  184. }
  185. char aPing[16]="202.108.22.5";  /* Joseph_Ping form ip  */
  186. if(Joseph_Ping(aPing,if_name,JOSEPH_NET_CHECK_TIME) == 0)
  187. {
  188. printf("Network is Ok!\n");
  189. Qy_Ret = 0;
  190. }
  191. else
  192. {
  193. printf("Network is Bad!\n");
  194. Qy_Ret = -1;
  195. }
  196. return Qy_Ret;
  197. }
  198. /*
  199. Author : kj
  200. Time : 2014-08-09
  201. Function :
  202. get ip gw mac broadcast
  203. */
  204. int Joseph_Get_Net_Device_Info(JOSEPH_NET_INTERFACE_INFO *Joseph_Net_Interface_Info_In)
  205. {
  206. int Qy_Ret = 0;
  207. int device_itertion = 0;
  208. int Joseph_Net_Interface_Sfd = 0;
  209. int Joseph_Net_Interface_exist = 0;
  210. struct ifreq Joseph_Net_Ifr;
  211. struct ifconf Joseph_Ifc;
  212. struct sockaddr_in *sin = (struct sockaddr_in*)&Joseph_Net_Ifr.ifr_addr;
  213. struct sockaddr_in *broadcast = (struct sockaddr_in*)&Joseph_Net_Ifr.ifr_broadaddr;
  214. if(Joseph_Net_Interface_Info_In == NULL)
  215. {
  216. Qy_Ret = -1;
  217. return Qy_Ret;
  218. }
  219. Joseph_Net_Interface_Sfd = socket(AF_INET,SOCK_DGRAM,0);
  220. if(Joseph_Net_Interface_Sfd < 0){
  221. perror("socket error");
  222. return -1;
  223. }
  224. memset(&Joseph_Net_Ifr,0,sizeof(Joseph_Net_Ifr));
  225. Joseph_Ifc.ifc_len = sizeof(Joseph_Net_Ifr);
  226. Joseph_Ifc.ifc_buf = (caddr_t)&Joseph_Net_Ifr;
  227. for(device_itertion = 1;device_itertion < 5;device_itertion++)
  228. {
  229. Joseph_Net_Ifr.ifr_ifindex = device_itertion;
  230. Qy_Ret = ioctl(Joseph_Net_Interface_Sfd,SIOCGIFNAME,&Joseph_Net_Ifr);
  231. if(Qy_Ret)
  232. {
  233. Joseph_Net_Interface_exist = 0;
  234. }
  235. else
  236. {
  237. if(strcmp(Joseph_Net_Ifr.ifr_name,Joseph_Net_Interface_Info_In->net_device_name) == 0)
  238. {
  239. printf("The %dst net device is : %s\n",Joseph_Net_Ifr.ifr_ifindex,Joseph_Net_Ifr.ifr_name);
  240. Joseph_Net_Interface_exist = 1;
  241. /*Judge card type of net device*/
  242. Qy_Ret = ioctl (Joseph_Net_Interface_Sfd, SIOCGIFFLAGS,&Joseph_Net_Ifr);
  243. if(!Qy_Ret)
  244. {
  245. /*judge the status of net device*/
  246. if (Joseph_Net_Ifr.ifr_flags & IFF_UP)
  247. {
  248. puts("the interface status is UP");
  249. }
  250. else
  251. {
  252. puts("the interface status is DOWN");
  253. }
  254. }
  255. break;
  256. }
  257. }
  258. }
  259. if(Joseph_Net_Interface_exist == 0)
  260. {
  261. printf("%s:[%d] No Such Device of %s !\n",__FUNCTION__,__LINE__,Joseph_Net_Interface_Info_In->net_device_name);
  262. return -1;
  263. }
  264. /*get net device mac addr*/
  265. Qy_Ret = ioctl(Joseph_Net_Interface_Sfd,SIOCGIFHWADDR,&Joseph_Net_Ifr);
  266. if(!Qy_Ret)
  267. {
  268. sprintf(Joseph_Net_Interface_Info_In->net_device_mac_info,"%02x:%02x:%02x:%02x:%02x:%02x",\
  269. (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[0],\
  270. (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[1],\
  271. (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[2],\
  272. (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[3],\
  273. (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[4],\
  274. (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[5]);
  275. printf("Mac address is : %02x:%02x:%02x:%02x:%02x:%02x\n",\
  276. (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[0],\
  277. (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[1],\
  278. (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[2],\
  279. (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[3],\
  280. (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[4],\
  281. (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[5]);
  282. }
  283. else
  284. {
  285. printf("Mac address is : 00:00:00:00:00:00\n");
  286. }
  287. /*get net device ip */
  288. memset(Joseph_Net_Interface_Info_In->net_device_ip,0,16);
  289. Qy_Ret = ioctl(Joseph_Net_Interface_Sfd,SIOCGIFADDR,&Joseph_Net_Ifr);
  290. if(!Qy_Ret)
  291. {
  292. inet_ntop(AF_INET,&sin->sin_addr.s_addr,Joseph_Net_Interface_Info_In->net_device_ip,16);
  293. printf("IP address is : %s\n",Joseph_Net_Interface_Info_In->net_device_ip);
  294. }else
  295. {
  296. printf("IP address is : 0.0.0.0\n");
  297. }
  298. /*get broadcast addr*/
  299. memset(Joseph_Net_Interface_Info_In->net_device_broadcast_info,0,16);
  300. Qy_Ret = ioctl(Joseph_Net_Interface_Sfd,SIOCGIFBRDADDR,&Joseph_Net_Ifr);
  301. if(!Qy_Ret)
  302. {
  303. inet_ntop(AF_INET,&broadcast->sin_addr.s_addr,Joseph_Net_Interface_Info_In->net_device_broadcast_info,16);
  304. printf("BROADCAST IP is : %s\n",Joseph_Net_Interface_Info_In->net_device_broadcast_info);
  305. }else
  306. {
  307. printf("BROADCAST IP is : 0.0.0.0\n");
  308. }
  309. /*get mask info*/
  310. Qy_Ret = ioctl(Joseph_Net_Interface_Sfd,SIOCGIFNETMASK,&Joseph_Net_Ifr);
  311. if (!Qy_Ret)
  312. {
  313. inet_ntop(AF_INET,&sin->sin_addr.s_addr,Joseph_Net_Interface_Info_In->net_device_mask_info,16);
  314. printf("NetMask is : %s\n",Joseph_Net_Interface_Info_In->net_device_mask_info);
  315. }
  316. return Qy_Ret;
  317. }
  318. /****************************************************************
  319. return value:
  320. -1 -- error , details can check errno
  321. 1  -- interface link up
  322. 0  -- interface link down.
  323. ****************************************************************/
  324. int Joseph_Get_Netlink_Status(const char *if_name)
  325. {
  326. int skfd;
  327. struct ifreq ifr;
  328. JOSEPH_ETHTOOL_VALUE edata;
  329. edata.cmd = ETHTOOL_GLINK;
  330. edata.data = 0;
  331. memset(&ifr, 0, sizeof(ifr));
  332. strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name) - 1);
  333. ifr.ifr_data = (char *)&edata;
  334. if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) == 0)
  335. return -1;
  336. if (ioctl(skfd, SIOCETHTOOL, &ifr) == -1)
  337. {
  338. close(skfd);
  339. return -1;
  340. }
  341. close(skfd);
  342. return edata.data;
  343. }
  344. int main(int argc,char *argv[])
  345. {
  346. int Qy_Ret = 0;
  347. JOSEPH_NET_INTERFACE_INFO Joseph_Net_Interface_Info;
  348. memset(&Joseph_Net_Interface_Info,0,sizeof(JOSEPH_NET_INTERFACE_INFO));
  349. if(argc < 2)
  350. {
  351. Qy_Ret = -1;
  352. return Qy_Ret;
  353. }
  354. system("ifconfig eth0 up");
  355. sleep(3);
  356. strcpy(Joseph_Net_Interface_Info.net_device_name,argv[1]);
  357. Qy_Ret = Joseph_Get_Netlink_Status(argv[1]);
  358. if(Qy_Ret == 1)
  359. {
  360. printf("%s:[%d] The netlink is up !\n",__FUNCTION__,__LINE__);
  361. }
  362. else
  363. {
  364. printf("%s:[%d] The netlink is down , Begin go to wifi !\n",__FUNCTION__,__LINE__);
  365. return -1;
  366. }
  367. NET_INIT:
  368. /*after 5s , if no ip ,then killall udhcpc ,then go to wifi*/
  369. system("killall -9 udhcpc");
  370. system("udhcpc -i eth0");
  371. Qy_Ret = Joseph_Check_Net_Status(argv[1]);
  372. if(Qy_Ret == 0)
  373. {
  374. Joseph_Get_Net_Device_Info(&Joseph_Net_Interface_Info);
  375. }
  376. NET_RUN:
  377. while(1)
  378. {
  379. Qy_Ret = Joseph_Get_Netlink_Status(argv[1]);
  380. if(Qy_Ret == 1)
  381. {
  382. printf("Net link status: %s\n", Qy_Ret == 1 ? "up" : "down");
  383. Qy_Ret = Joseph_Check_Net_Status(argv[1]);
  384. if(Qy_Ret < 0)
  385. {
  386. break;//do nothing
  387. }
  388. }
  389. else
  390. {
  391. printf("%s:[%d] The netlink is down !\n",__FUNCTION__,__LINE__);
  392. }
  393. sleep(1);
  394. }
  395. goto NET_INIT;
  396. return Qy_Ret;
  397. }
  398. </span>

嵌入式 hi3518平台以太网网络模块设计包括重连机制和网线检测机制的更多相关文章

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

    /********************************** (C) COPYRIGHT ******************************* * File Name        ...

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

    版权声明:本文为博主原创文章,未经博主允许不得转载. /********************************** (C) COPYRIGHT *********************** ...

  3. 嵌入式 hi3518平台获取网络环境中的ip、netmask、broadcast等信息

    <span style="font-family:Courier New;"> /********************************** (C) COPY ...

  4. 嵌入式 hi3518平台获取网关

    </pre><pre code_snippet_id="495447" snippet_file_name="blog_20141024_1_70650 ...

  5. 嵌入式 hi3518平台增加路由代码

    <span style="font-family:Courier New;"> /********************************** (C) COPY ...

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

    <span style="font-family:Courier New;"> /******************************************* ...

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

    首先贴出来我的bootargs的设置(注没有换行符!!!): setenv bootargs noinitrd mem=64M root=/dev/nfs init=/linuxrc rw nfsro ...

  8. 嵌入式开发平台-iTOP-4418开发板

    详情转自:http://topeetboard.com S5P4418核心板可以无缝支持核心系统S5P6818,并保持底板设计不变,将兼顾更高端 的应用领域,为项目和产品提供更好的灵活性以及可伸缩性. ...

  9. 英伟达GPU 嵌入式开发平台

    英伟达GPU  嵌入式开发平台 1.         JETSON TX1 开发者组件 JETSON TX1 开发者组件是视觉计算的全功能 开发平台,旨在让您能够快速地安装和运行. 该组件带有 Lin ...

随机推荐

  1. iOS多线程 GCD

    iOS多线程 GCD Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main que ...

  2. 如何使用SPY++查找

    自动化测试中,如果需要做UI automation的时候往往需要抓取object的工具.实际上这种工具有很多,认识的大牛使用SPY++,于是我也选择了这个.下面列出基本使用方法.有点像XACC spy ...

  3. Android核心分析之十八Android电话系统之RIL-Java

    Android RIL-Java 123.jpg (2.09 KB, 下载次数: 1) 下载附件  保存到相册 2012-3-21 10:47 上传   RIL-Java在本质上就是一个RIL代理,起 ...

  4. J2ee 巴巴网站制作(二)

    导入spring jar包:

  5. SQL Server 高性能写入的一些总结

    1.1.1 摘要 在开发过程中,我们不时会遇到系统性能瓶颈问题,而引起这一问题原因可以很多,有可能是代码不够高效.有可能是硬件或网络问题,也有可能是数据库设计的问题. 本篇博文将针对一些常用的数据库性 ...

  6. Spring Security资料

    Spring Security学习总结一 Spring Security3.1登陆验证 Spring security初探

  7. javascript whenReady

    var whenReady=(function(){ var funcs=[]; var ready=false; function handler(e){ if (ready) { return; ...

  8. linux RTC 驱动模型分析【转】

    转自:http://blog.csdn.net/yaozhenguo2006/article/details/6824970 RTC(real time clock)实时时钟,主要作用是给Linux系 ...

  9. dojo 三 类和继承 dojo/_base/declare

    这里要讲有关类的定义.继承和实现.官方教程:http://dojotoolkit.org/documentation/tutorials/1.7/declare/类的声明是通过declare 这个方法 ...

  10. 《c程序设计语言》读书笔记--子函数原型和声明的形参

    #include <stdio.h> #define Num 20 int power(int base,int n) { int p = 1; int i; for(i = 0;i &l ...