原文地址:
http://www.linuxidc.com/Linux/2011-08/40336.htm
在项目中遇到一个问题,嵌入式Linux设备工作一段时间后网络会出现故障,网线虽然连着,但却不能与外部主机通信。此时用串口调试内核,用ifconfig eth0 up命令可以再度启动网络。所以现在的需要在网络故障时检测出来,然后复位网络。
如何检测网络故障是一个问题,在网上搜索了半天也没有找到好的解决方法。突然想到可以自己实现linux中的ping命令,然后定时ping网关,依据是否能ping通网关就可以判断出网络是否故障。
参考自定义ping的代码,见
http://www.linuxidc.com/Linux/2011-08/40337.htm
然后把这个程序嵌入我的程序中,实现了功能。下面是ping函数部分的代码:
-
- #define PACKET_SIZE 4096
- #define ERROR 0
- #define SUCCESS 1
- #define PING_TIME 10000 //10S
- #define PING_TIME_OUT 1000 //1S
-
- unsigned short cal_chksum(unsigned short *addr, int len)
- {
- int nleft=len;
- int sum=0;
- unsigned short *w=addr;
- unsigned short answer=0;
-
- while(nleft > 1)
- {
- sum += *w++;
- nleft -= 2;
- }
-
- if( nleft == 1)
- {
- *(unsigned char *)(&answer) = *(unsigned char *)w;
- sum += answer;
- }
-
- sum = (sum >> 16) + (sum & 0xffff);
- sum += (sum >> 16);
- answer = ~sum;
-
- return answer;
- }
-
-
- int ping( char *ips, int timeout)
- {
- struct timeval timeo;
- int sockfd;
- struct sockaddr_in addr;
- struct sockaddr_in from;
-
- struct timeval *tval;
- struct ip *iph;
- struct icmp *icmp;
-
- char sendpacket[PACKET_SIZE];
- char recvpacket[PACKET_SIZE];
-
- int n;
- pid_t pid;
- int maxfds = 0;
- fd_set readfds;
-
-
- bzero(&addr,sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = inet_addr(ips);
-
-
- sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
- if (sockfd < 0)
- {
- printf("ip:%s,socket error",ips);
- return ERROR;
- }
-
-
- timeo.tv_sec = timeout / 1000;
- timeo.tv_usec = timeout % 1000;
-
- if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)) == -1)
- {
- printf("ip:%s,setsockopt error",ips);
- return ERROR;
- }
-
-
- memset(sendpacket, 0, sizeof(sendpacket));
-
-
- pid=getpid();
- int i,packsize;
- icmp=(struct icmp*)sendpacket;
- icmp->icmp_type=ICMP_ECHO;
- icmp->icmp_code=0;
- icmp->icmp_cksum=0;
- icmp->icmp_seq=0;
- icmp->icmp_id=pid;
- packsize=8+56;
- tval= (struct timeval *)icmp->icmp_data;
- gettimeofday(tval,NULL);
- icmp->icmp_cksum=cal_chksum((unsigned short *)icmp,packsize);
-
-
- n = sendto(sockfd, (char *)&sendpacket, packsize, 0, (struct sockaddr *)&addr, sizeof(addr));
- if (n < 1)
- {
- printf("ip:%s,sendto error",ips);
- return ERROR;
- }
-
-
-
- while(1)
- {
-
- FD_ZERO(&readfds);
- FD_SET(sockfd, &readfds);
- maxfds = sockfd + 1;
- n = select(maxfds, &readfds, NULL, NULL, &timeo);
- if (n <= 0)
- {
- printf("ip:%s,Time out error",ips);
- close(sockfd);
- return ERROR;
- }
-
-
- memset(recvpacket, 0, sizeof(recvpacket));
- int fromlen = sizeof(from);
- n = recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, (struct sockaddr *)&from, (socklen_t *)&fromlen);
- if (n < 1) {
- break;
- }
-
-
- char *from_ip = (char *)inet_ntoa(from.sin_addr);
- printf("fomr ip:%s",from_ip);
- if (strcmp(from_ip,ips) != 0)
- {
- printf("ip:%s,Ip wang",ips);
- break;
- }
-
- iph = (struct ip *)recvpacket;
-
- icmp=(struct icmp *)(recvpacket + (iph->ip_hl<<2));
-
- printf("ip:%s,icmp->icmp_type:%d,icmp->icmp_id:%d",ips,icmp->icmp_type,icmp->icmp_id);
-
- if (icmp->icmp_type == ICMP_ECHOREPLY && icmp->icmp_id == pid)
- {
-
- break;
- }
- else
- {
-
- continue;
- }
- }
-
-
- close(sockfd);
-
- printf("ip:%s,Success",ips);
- return SUCCESS;
- }
以上是ping函数的实现。然后我开了个定时器每隔10S启动一次,一旦检测到网络故障则自动恢复:
-
- void alarmInterface::slot_ping_timer()
- {
-
- if (ping(Gate_Way_Ip.data(),PING_TIME_OUT))
- {
-
- }
- else
- {
-
- system("ifconfig eth0 down");
- sleep(2);
- system("ifconfig eth0 up");
- }
- }
工作完成。