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

简介: [html] view plain copy        #include      #include      #include      #include      #include      #include      #include     ...
[html]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  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.   
  23. #define JOSEPH_LOOP_INTERFACE           "lo"  
  24. #define JOSEPH_ETH_INTERFACE            "eth0"  
  25. #define JOSEPH_WIRLESS_INTERFACE        "wlan0"  
  26.   
  27. #define ETHTOOL_GLINK                   0x0000000a   /* Get link status (ethtool_value) */    
  28. #define JSOEPH_NET_CHECK_PACKET_SIZE    4096     
  29. #define JOSEPH_NET_CHECK_TIME           3000  
  30.   
  31. typedef struct Joseph_Net_Interface_Info  
  32. {  
  33.     int net_device_type;// 0 ~ eth0 ; 1 ~ wlan ;3 ~ ppp0  
  34.     int net_device_priority;// 0 ~ eth0 ; 1 ~ wlan ;3 ~ ppp0  
  35.     int net_device_status;//0 ~ down; 1 ~ up  
  36.     int net_device_link_status;//0 ~ no ;1 ~ yes  
  37.     char net_device_name[8];  
  38.     char net_device_ip[16];  
  39.     char net_device_mac_info[32];  
  40.     char net_device_gw_info[16];  
  41.     char net_device_mask_info[16];  
  42.     char net_device_broadcast_info[16];  
  43.           
  44. }JOSEPH_NET_INTERFACE_INFO;  
  45.   
  46. typedef struct Joseph_Ethtool_Value {    
  47.     unsigned int   cmd;    
  48.     unsigned int   data;     
  49. }JOSEPH_ETHTOOL_VALUE;  
  50.   
  51. enum Joseph_Net_Device_Type  
  52. {  
  53.     JOSEPH_ETH = 0,  
  54.     JOSEPH_WIFI = 1,  
  55.     JOSEPH_3G = 2,  
  56. }JOSEPH_NET_DEVICE_TYPE;  
  57.   
  58. unsigned short Joseph_Cal_Chksum(unsigned short *addr, int len)   
  59. {    
  60.     int nleft=len;    
  61.     int sum=0;    
  62.     unsigned short *w=addr;    
  63.     unsigned short answer=0;      
  64.   
  65.     while(nleft > 1)   
  66.     {    
  67.       sum += *w++;    
  68.       nleft -= 2;    
  69.     }    
  70.   
  71.     if( nleft == 1)    
  72.     {         
  73.       *(unsigned char *)(&answer) = *(unsigned char *)w;    
  74.       sum += answer;    
  75.     }    
  76.   
  77.     sum = (sum >> 16) + (sum & 0xffff);    
  78.     sum += (sum >> 16);    
  79.     answer = ~sum;    
  80.   
  81.     return answer;    
  82. }    
  83.   
  84. int Joseph_Ping( char *ips,char *srcip , int timeout)  
  85. {        
  86.   
  87.     int n;  
  88.     pid_t pid;    
  89.     int maxfds = 0;    
  90.     fd_set readfds;    
  91.   
  92.     struct ip *iph;    
  93.     struct icmp *icmp;    
  94.     struct timeval *tval;          
  95.     struct sockaddr_in addr;        
  96.     struct sockaddr_in from;  
  97.     struct ifreq ifr;  
  98.       
  99.     bzero(&addr,sizeof(addr));  
  100.     addr.sin_family = AF_INET;    
  101.     addr.sin_addr.s_addr = inet_addr(ips);    
  102.   
  103.     int sockfd;    
  104.     sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);   
  105.     if (sockfd 0)     
  106.     {      
  107.       printf("ip:%s,socket error\n",ips);      
  108.       return -1;      
  109.     }      
  110.   
  111.     struct timeval timeo;  
  112.     timeo.tv_sec = timeout / 1000;  
  113.     timeo.tv_usec = timeout % 1000;    
  114.   
  115. #if 0  
  116.   
  117.     /*set src ip*/  
  118.     bzero(&from,sizeof(from));  /* 设定Ip信息 */  
  119.     from.sin_family = AF_INET;    
  120.     from.sin_addr.s_addr = inet_addr(srcip);      
  121.   
  122.     if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_IF,(struct sockaddr *)&from, sizeof(from)) == -1)      
  123.     {      
  124.       printf("ip:%s,setsockopt error \n",srcip);       
  125.       return ERROR;  
  126.     }    
  127.     bind(sockfd,(struct sockaddr *)&addr, sizeof(addr));  
  128. #else  
  129.   
  130.     strcpy(ifr.ifr_name, srcip);  
  131.     if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) == -1)  
  132.     {  
  133.         printf("can't bind to interface %s\n",ifr.ifr_name);  
  134.     }  
  135.       
  136. #endif  
  137.   
  138.     if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)) == -1)      
  139.     {      
  140.         printf("ip:%s,setsockopt error\n",ips);    
  141.         return -1;      
  142.     }  
  143.     else  
  144.     {  
  145.         ;  
  146.     }  
  147.   
  148.     char sendpacket[JSOEPH_NET_CHECK_PACKET_SIZE];      
  149.     char recvpacket[JSOEPH_NET_CHECK_PACKET_SIZE];      
  150.   
  151.     memset(sendpacket, 0, sizeof(sendpacket));    
  152.   
  153.     pid = getpid();     
  154.   
  155.     icmp=(struct icmp*)sendpacket;   
  156.     icmp->icmp_type = ICMP_ECHO;  
  157.     icmp->icmp_code = 0;   
  158.     icmp->icmp_cksum = 0;       
  159.     icmp->icmp_seq = 0;      
  160.     icmp->icmp_id = pid;     
  161.     tval = (struct timeval *)icmp->icmp_data;        
  162.     gettimeofday(tval,NULL);        
  163.     icmp->icmp_cksum=Joseph_Cal_Chksum((unsigned short *)icmp,sizeof(struct icmp));  
  164.   
  165.     n = sendto(sockfd, (char *)&sendpacket, sizeof(struct icmp), 0, (struct sockaddr *)&addr, sizeof(addr));        
  166.     if (n 1)     
  167.     {      
  168.         printf("ip:%s,sendto error\n",ips);    
  169.         return -1;      
  170.     }      
  171.          
  172.     while(1)   
  173.     {      
  174.       FD_ZERO(&readfds);  
  175.       FD_SET(sockfd, &readfds);      
  176.       maxfds = sockfd + 1;      
  177.       n = select(maxfds, &readfds, NULL, NULL, &timeo);      
  178.       if (n <= 0)       
  179.       {      
  180.           printf("ip:%s,Time out error\n",ips);      
  181.           close(sockfd);      
  182.           return -1;      
  183.       }      
  184.           
  185.       memset(recvpacket, 0, sizeof(recvpacket));      
  186.       int fromlen = sizeof(from);  
  187.       n = recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, (struct sockaddr *)&from, (socklen_t *)&fromlen);      
  188.       if (n 1)  
  189.       {     
  190.           return -1;  
  191.       }      
  192.               
  193.       char *from_ip = (char *)inet_ntoa(from.sin_addr);        
  194.       if (strcmp(from_ip,ips) != 0)   
  195.       {      
  196.           printf("NowPingip:%s Fromip:%s\nNowPingip is not same to Fromip,so Joseph_Ping wrong!\n",ips,from_ip);       
  197.           continue;    
  198.       }      
  199.           
  200.       iph = (struct ip *)recvpacket;          
  201.       icmp = (struct icmp *)(recvpacket + (iph->ip_hl <2));  
  202.   
  203.       if (icmp->icmp_type == ICMP_ECHOREPLY && icmp->icmp_id == pid)  
  204.           return 0;  
  205.       else         
  206.           continue;    
  207.     }   
  208.   
  209.     return 0;  
  210. }  
  211.     
  212. int Joseph_Check_Net_Status(char *if_name)    
  213. {    
  214.     int Qy_Ret = 0;  
  215.     if(strlen(if_name) <= 0)  
  216.     {  
  217.         Qy_Ret = -1;  
  218.         return Qy_Ret;  
  219.     }  
  220.       
  221.     char aPing[16]="202.108.22.5";  /* Joseph_Ping form ip  */  
  222.   
  223.     if(Joseph_Ping(aPing,if_name,JOSEPH_NET_CHECK_TIME) == 0)    
  224.     {    
  225.         printf("Network is Ok!\n");   
  226.         Qy_Ret = 0;  
  227.     }    
  228.     else      
  229.     {    
  230.         printf("Network is Bad!\n");  
  231.         Qy_Ret = -1;  
  232.     }  
  233.   
  234.     return Qy_Ret;  
  235. }  
  236.   
  237.   
  238. /*  
  239. Author : kj  
  240. Time : 2014-08-09  
  241. Function :  
  242.     get ip gw mac broadcast  
  243. */  
  244. int Joseph_Get_Net_Device_Info(JOSEPH_NET_INTERFACE_INFO *Joseph_Net_Interface_Info_In)  
  245. {  
  246.     int Qy_Ret = 0;  
  247.     int device_itertion = 0;  
  248.     int Joseph_Net_Interface_Sfd = 0;  
  249.     int Joseph_Net_Interface_exist = 0;  
  250.     struct ifreq Joseph_Net_Ifr;  
  251.     struct ifconf Joseph_Ifc;  
  252.     struct sockaddr_in *sin = (struct sockaddr_in*)&Joseph_Net_Ifr.ifr_addr;  
  253.     struct sockaddr_in *broadcast = (struct sockaddr_in*)&Joseph_Net_Ifr.ifr_broadaddr;  
  254.   
  255.     if(Joseph_Net_Interface_Info_In == NULL)  
  256.     {  
  257.         Qy_Ret = -1;  
  258.         return Qy_Ret;  
  259.     }  
  260.       
  261.     Joseph_Net_Interface_Sfd = socket(AF_INET,SOCK_DGRAM,0);  
  262.     if(Joseph_Net_Interface_Sfd 0){  
  263.         perror("socket error");  
  264.         return -1;  
  265.     }  
  266.       
  267.     memset(&Joseph_Net_Ifr,0,sizeof(Joseph_Net_Ifr));  
  268.   
  269.     Joseph_Ifc.ifc_len = sizeof(Joseph_Net_Ifr);  
  270.     Joseph_Ifc.ifc_buf = (caddr_t)&Joseph_Net_Ifr;  
  271.   
  272.               
  273.     for(device_itertion = 1;device_itertion 5;device_itertion++)  
  274.     {  
  275.         Joseph_Net_Ifr.ifr_ifindex = device_itertion;  
  276.         Qy_Ret = ioctl(Joseph_Net_Interface_Sfd,SIOCGIFNAME,&Joseph_Net_Ifr);  
  277.         if(Qy_Ret)  
  278.         {  
  279.             Joseph_Net_Interface_exist = 0;  
  280.         }  
  281.         else  
  282.         {  
  283.             if(strcmp(Joseph_Net_Ifr.ifr_name,Joseph_Net_Interface_Info_In->net_device_name) == 0)  
  284.             {  
  285.                 printf("The %dst net device is : %s\n",Joseph_Net_Ifr.ifr_ifindex,Joseph_Net_Ifr.ifr_name);  
  286.                 Joseph_Net_Interface_exist = 1;  
  287.   
  288.                 /*Judge card type of net device*/  
  289.                 Qy_Ret = ioctl (Joseph_Net_Interface_Sfd, SIOCGIFFLAGS,&Joseph_Net_Ifr);  
  290.   
  291.                 if(!Qy_Ret)  
  292.                 {  
  293.                     /*judge the status of net device*/  
  294.                     if (Joseph_Net_Ifr.ifr_flags & IFF_UP)  
  295.                     {  
  296.                         puts("the interface status is UP");  
  297.                       
  298.                     }  
  299.                     else  
  300.                     {  
  301.                         puts("the interface status is DOWN");  
  302.                     }  
  303.                 }  
  304.                 break;  
  305.             }  
  306.         }  
  307.   
  308.     }  
  309.     if(Joseph_Net_Interface_exist == 0)  
  310.     {  
  311.         printf("%s:[%d] No Such Device of %s !\n",__FUNCTION__,__LINE__,Joseph_Net_Interface_Info_In->net_device_name);  
  312.         return -1;  
  313.     }  
  314.   
  315.   
  316.     /*get net device mac addr*/  
  317.     Qy_Ret = ioctl(Joseph_Net_Interface_Sfd,SIOCGIFHWADDR,&Joseph_Net_Ifr);  
  318.     if(!Qy_Ret)  
  319.     {  
  320.         sprintf(Joseph_Net_Interface_Info_In->net_device_mac_info,"%02x:%02x:%02x:%02x:%02x:%02x",\  
  321.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[0],\  
  322.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[1],\  
  323.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[2],\  
  324.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[3],\  
  325.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[4],\  
  326.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[5]);  
  327.           
  328.         printf("Mac address is : %02x:%02x:%02x:%02x:%02x:%02x\n",\  
  329.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[0],\  
  330.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[1],\  
  331.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[2],\  
  332.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[3],\  
  333.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[4],\  
  334.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[5]);  
  335.   
  336.     }  
  337.     else  
  338.     {  
  339.         printf("Mac address is : 00:00:00:00:00:00\n");  
  340.     }  
  341.   
  342.     /*get net device ip */  
  343.     memset(Joseph_Net_Interface_Info_In->net_device_ip,0,16);  
  344.     Qy_Ret = ioctl(Joseph_Net_Interface_Sfd,SIOCGIFADDR,&Joseph_Net_Ifr);  
  345.     if(!Qy_Ret)  
  346.     {  
  347.         inet_ntop(AF_INET,&sin->sin_addr.s_addr,Joseph_Net_Interface_Info_In->net_device_ip,16);  
  348.         printf("IP address is : %s\n",Joseph_Net_Interface_Info_In->net_device_ip);  
  349.     }else  
  350.     {  
  351.         printf("IP address is : 0.0.0.0\n");  
  352.     }  
  353.   
  354.     /*get broadcast addr*/  
  355.     memset(Joseph_Net_Interface_Info_In->net_device_broadcast_info,0,16);  
  356.     Qy_Ret = ioctl(Joseph_Net_Interface_Sfd,SIOCGIFBRDADDR,&Joseph_Net_Ifr);  
  357.     if(!Qy_Ret)  
  358.     {  
  359.         inet_ntop(AF_INET,&broadcast->sin_addr.s_addr,Joseph_Net_Interface_Info_In->net_device_broadcast_info,16);  
  360.         printf("BROADCAST IP is : %s\n",Joseph_Net_Interface_Info_In->net_device_broadcast_info);  
  361.     }else  
  362.     {  
  363.         printf("BROADCAST IP is : 0.0.0.0\n");  
  364.     }  
  365.   
  366.     /*get mask info*/  
  367.     Qy_Ret = ioctl(Joseph_Net_Interface_Sfd,SIOCGIFNETMASK,&Joseph_Net_Ifr);  
  368.   
  369.     if (!Qy_Ret)      
  370.     {    
  371.         inet_ntop(AF_INET,&sin->sin_addr.s_addr,Joseph_Net_Interface_Info_In->net_device_mask_info,16);  
  372.         printf("NetMask is : %s\n",Joseph_Net_Interface_Info_In->net_device_mask_info);   
  373.     }    
  374.   
  375.     return Qy_Ret;  
  376. }  
  377.   
  378. /****************************************************************   
  379.    return value:    
  380.    -1 -- error , details can check errno    
  381.    1  -- interface link up    
  382.    0  -- interface link down.    
  383. ****************************************************************/  
  384. int Joseph_Get_Netlink_Status(const char *if_name)    
  385. {    
  386.     int skfd;    
  387.     struct ifreq ifr;    
  388.     JOSEPH_ETHTOOL_VALUE edata;    
  389.     edata.cmd = ETHTOOL_GLINK;    
  390.     edata.data = 0;    
  391.     memset(&ifr, 0, sizeof(ifr));    
  392.     strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name) - 1);    
  393.     ifr.ifr_data = (char *)&edata;    
  394.     if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) == 0)    
  395.         return -1;    
  396.     if (ioctl(skfd, SIOCETHTOOL, &ifr) == -1)    
  397.     {    
  398.        close(skfd);         
  399.        return -1;    
  400.     }    
  401.     close(skfd);    
  402.     return edata.data;    
  403. }    
  404.   
  405. int main(int argc,char *argv[])  
  406. {  
  407.     int Qy_Ret = 0;  
  408.   
  409.     JOSEPH_NET_INTERFACE_INFO Joseph_Net_Interface_Info;  
  410.     memset(&Joseph_Net_Interface_Info,0,sizeof(JOSEPH_NET_INTERFACE_INFO));  
  411.   
  412.     if(argc 2)  
  413.     {  
  414.         Qy_Ret = -1;  
  415.         return Qy_Ret;  
  416.     }  
  417.       
  418.     system("ifconfig eth0 up");  
  419.     sleep(3);  
  420.   
  421.     strcpy(Joseph_Net_Interface_Info.net_device_name,argv[1]);  
  422.   
  423.   
  424.     Qy_Ret = Joseph_Get_Netlink_Status(argv[1]);  
  425.     if(Qy_Ret == 1)  
  426.     {  
  427.         printf("%s:[%d] The netlink is up !\n",__FUNCTION__,__LINE__);  
  428.     }  
  429.     else  
  430.     {  
  431.         printf("%s:[%d] The netlink is down , Begin go to wifi !\n",__FUNCTION__,__LINE__);  
  432.         return -1;  
  433.     }  
  434.   
  435. NET_INIT:  
  436.   
  437.     /*after 5s , if no ip ,then killall udhcpc ,then go to wifi*/  
  438.     system("killall -9 udhcpc");  
  439.       
  440.     system("udhcpc -i eth0");  
  441.   
  442.     Qy_Ret = Joseph_Check_Net_Status(argv[1]);  
  443.     if(Qy_Ret == 0)  
  444.     {  
  445.         Joseph_Get_Net_Device_Info(&Joseph_Net_Interface_Info);       
  446.     }  
  447.   
  448. NET_RUN:  
  449.     while(1)  
  450.     {  
  451.         Qy_Ret = Joseph_Get_Netlink_Status(argv[1]);  
  452.         if(Qy_Ret == 1)  
  453.         {  
  454.             printf("Net link status: %s\n", Qy_Ret == 1 ? "up" : "down");  
  455.             Qy_Ret = Joseph_Check_Net_Status(argv[1]);  
  456.             if(Qy_Ret 0)  
  457.             {  
  458.                 break;//do nothing  
  459.             }  
  460.               
  461.         }  
  462.         else  
  463.         {  
  464.             printf("%s:[%d] The netlink is down !\n",__FUNCTION__,__LINE__);      
  465.         }  
  466.         sleep(1);  
  467.     }  
  468.       
  469.     goto NET_INIT;  
  470.   
  471.     return Qy_Ret;  
  472. }  
  473.   
  474. </span>  
目录
相关文章
|
17天前
|
数据可视化 数据挖掘
【视频】复杂网络分析CNA简介与R语言对婚礼数据聚类社区检测和可视化|数据分享
【视频】复杂网络分析CNA简介与R语言对婚礼数据聚类社区检测和可视化|数据分享
|
14天前
|
安全 网络安全 数据安全/隐私保护
【专栏】IT 知识百科:访问控制列表(ACL)是网络安全的关键机制,用于定义和管理网络资源的访问权限
【4月更文挑战第28天】访问控制列表(ACL)是网络安全的关键机制,用于定义和管理网络资源的访问权限。ACL工作原理包括定义规则、匹配规则和执行操作。标准ACL基于源IP过滤,扩展ACL则提供更多筛选条件。时间及用户基础的ACL提供更细化的控制。优点在于增强安全性和精细管理,但管理复杂性和性能影响也是挑战。未来,ACL将趋向智能化和自动化,与更多安全技术结合,以提升网络安全。**
|
3天前
|
算法
计算机网络:封装成帧 & 透明传输 & 差错检测
计算机网络:封装成帧 & 透明传输 & 差错检测
8 0
|
3天前
|
Android开发
android检测网络连接是否存在(一)
android检测网络连接是否存在(一)
10 2
|
7天前
|
机器学习/深度学习 存储 算法
m基于Yolov2深度学习网络的螺丝检测系统matlab仿真,带GUI界面
MATLAB 2022a中展示了YOLOv2算法的螺丝检测仿真结果,该系统基于深度学习的YOLOv2网络,有效检测和定位图像中的螺丝。YOLOv2通过批标准化、高分辨率分类器等优化实现速度和精度提升。核心代码部分涉及设置训练和测试数据,调整图像大小,加载预训练模型,构建YOLOv2网络并进行训练,最终保存检测器模型。
23 3
|
9天前
|
机器学习/深度学习 人工智能 安全
【AI 初识】人工智能如何用于欺诈检测和网络安全?
【5月更文挑战第3天】【AI 初识】人工智能如何用于欺诈检测和网络安全?
|
15天前
|
安全 网络协议 物联网
城域以太网:连接城市的高速网络
【4月更文挑战第22天】
22 0
|
17天前
|
机器学习/深度学习 算法 计算机视觉
m基于Yolov2深度学习网络的人体喝水行为视频检测系统matlab仿真,带GUI界面
MATLAB 2022a中使用YOLOv2算法对avi视频进行人体喝水行为检测,结果显示成功检测到目标。该算法基于全卷积网络,通过特征提取、锚框和损失函数优化实现。程序首先打乱并分割数据集,利用预训练的ResNet-50和YOLOv2网络结构进行训练,最后保存模型。
28 5
|
18天前
|
算法 数据可视化 数据挖掘
R语言社区发现算法检测心理学复杂网络:spinglass、探索性图分析walktrap算法与可视化
R语言社区发现算法检测心理学复杂网络:spinglass、探索性图分析walktrap算法与可视化