版权声明:本文为博主原创文章,未经博主允许不得转载。
- /********************************** (C) COPYRIGHT *******************************
- * File Name : netstatus_check.c
- * Author : skdkjzz
- * Date : 2014/08/07
- * Description : 检测本机是否连通外部网络(Joseph_Ping 百度)。
- *********************************************************************************/
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netinet/ip.h>
- #include <netinet/ip_icmp.h>
- #include <netdb.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <sys/types.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/ioctl.h>
- #include <sys/stat.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <net/if.h>
- #include <sys/utsname.h>
- #include <limits.h>
- #include <ctype.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <linux/sockios.h>
- #define PACKET_SIZE 4096
- #define ERROR -1
- #define SUCCESS 0
- /*效验算法*/
- unsigned short Joseph_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 Joseph_Ping( char *ips,char *srcip , int timeout) /* Ping函数 */
- {
- struct timeval *tval;
- int maxfds = 0;
- fd_set readfds;
- struct sockaddr_in addr;
- struct sockaddr_in from;
- struct ifreq ifr;
- bzero(&addr,sizeof(addr)); /* 设定Ip信息 */
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = inet_addr(ips);
- int sockfd;
- sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); /* 取得socket */
- if (sockfd < 0)
- {
- printf("ip:%s,socket error\n",ips);
- return ERROR;
- }
- struct timeval timeo;
- timeo.tv_sec = timeout / 1000; /* 设定TimeOut时间 */
- timeo.tv_usec = timeout % 1000;
- #if 0
- /*set src ip*/
- bzero(&from,sizeof(from)); /* 设定Ip信息 */
- from.sin_family = AF_INET;
- from.sin_addr.s_addr = inet_addr(srcip);
- if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_IF,(struct sockaddr *)&from, sizeof(from)) == -1)
- {
- printf("ip:%s,setsockopt error \n",srcip);
- return ERROR;
- }
- bind(sockfd,(struct sockaddr *)&addr, sizeof(addr));
- #else
- strcpy(ifr.ifr_name, srcip);
- if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) == -1)
- {
- printf("can't bind to interface %s\n",ifr.ifr_name);
- }
- #endif
- printf("%s %d\n",__FUNCTION__,__LINE__);
- if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)) == -1)
- {
- printf("ip:%s,setsockopt error\n",ips);
- return ERROR;
- }
- else
- {
- printf("ip:%s,setsockopt ok \n",ips);
- }
- char sendpacket[PACKET_SIZE];
- char recvpacket[PACKET_SIZE];
- memset(sendpacket, 0, sizeof(sendpacket)); /* 设定Ping包 */
- pid_t pid;
- pid=getpid(); /* 取得PID,作为Ping的Sequence ID */
- struct ip *iph;
- struct icmp *icmp;
- 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;
- tval= (struct timeval *)icmp->icmp_data;
- gettimeofday(tval,NULL);
- icmp->icmp_cksum=Joseph_Cal_Chksum((unsigned short *)icmp,sizeof(struct icmp)); /* 校验 */
- int n; /* 发包 */
- n = sendto(sockfd, (char *)&sendpacket, sizeof(struct icmp), 0, (struct sockaddr *)&addr, sizeof(addr));
- if (n < 1)
- {
- printf("ip:%s,sendto error\n",ips);
- return ERROR;
- }
- while(1) /* 接受 由于可能接受到其他Ping的应答消息,所以这里要用循环 */
- {
- FD_ZERO(&readfds); /* 设定TimeOut时间,这次才是真正起作用的 */
- FD_SET(sockfd, &readfds);
- maxfds = sockfd + 1;
- n = select(maxfds, &readfds, NULL, NULL, &timeo);
- if (n <= 0)
- {
- printf("ip:%s,Time out error\n",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) {
- return ERROR;
- }
- char *from_ip = (char *)inet_ntoa(from.sin_addr);
- if (strcmp(from_ip,ips) != 0) /* 判断是否是自己Ping的回复 */
- {
- printf("Now Pingip:%s Fromip:%s\n Now Pingip is not same to Fromip,so Joseph_Ping wrong!\n",ips,from_ip);
- return ERROR;
- }
- iph = (struct ip *)recvpacket;
- icmp=(struct icmp *)(recvpacket + (iph->ip_hl<<2));
- if (icmp->icmp_type == ICMP_ECHOREPLY && icmp->icmp_id == pid) /* 判断Ping回复包的状态 ICMP_ECHOREPLY回显应答 */
- {
- return SUCCESS;
- } /* 正常退出循环 */
- else
- continue; /* 否则继续等 */
- }
- return 0;
- }
- int main(int argc ,char *argv[])
- {
- int Qy_Ret = 0;
- struct hostent *h=NULL;
- char hostname[16]="www.baidu.com";
- char aPing[16]="202.108.22.5"; /* Joseph_Ping form ip */
- Qy_Ret = Joseph_Ping(aPing,argv[1],3000);
- printf("Qy_Ret is %d\n",Qy_Ret);
- if(Qy_Ret == 0)
- {
- printf("Network is Ok!\n");
- return 0;
- }
- else
- {
- printf("Network is Bad!\n");
- return -1;
- }
- sprintf(hostname,"%s",(char *)inet_ntoa(*((struct in_addr *)h->h_addr))); /* Joseph_Ping form hostname */
- if(Joseph_Ping(hostname,argv[1],3000))
- {
- printf("Network is Ok!\n");
- return 0;
- }
- else
- {
- printf("Network is Bad!\n");
- return -1;
- }
- }
- </span>