ICMP是(Internet Control Message Protocol)Internet控制报文协议。它是TCP/IP协议族的一个子协议,用于在IP主机、路由器之间传递控制消息。控制消息是指网络通不通、主机是否可达、路由是否可用等网络本身的消息。这些控制消息虽然并不传输用户数据,但是对于用户数据的传递起着重要的作用。
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, ret = SUCCESS;
pid_t pid;
int maxfds = 0, CountNum = 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)
{
syslog(LOG_INFO,"ip:%s,socket error",ips);
return ERROR;
}
timeo.tv_sec &#61; timeout / 1000;
timeo.tv_usec &#61; timeout % 1000;
if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)) &#61;&#61; -1)
{
syslog(LOG_INFO,"ip:%s,setsockopt error",ips);
close(sockfd);
return ERROR;
}
memset(sendpacket, 0, sizeof(sendpacket));
// PID ping Sequence ID
pid&#61;getpid();
int i,packsize;
icmp&#61;(struct icmp*)sendpacket;
icmp->icmp_type&#61;ICMP_ECHO;
icmp->icmp_code&#61;0;
icmp->icmp_cksum&#61;0;
icmp->icmp_seq&#61;0;
icmp->icmp_id&#61;pid;
packsize&#61;8&#43;56;
tval&#61; (struct timeval *)icmp->icmp_data;
gettimeofday(tval,NULL);
icmp->icmp_cksum&#61;cal_chksum((unsigned short *)icmp,packsize);
n &#61; sendto(sockfd, (char *)&sendpacket, packsize, 0, (struct sockaddr *)&addr, sizeof(addr));
if (n <1)
{
printf("ip:%s,sendto error.\n",ips);
close(sockfd);
return ERROR;
}
CountNum &#61; 0;
while(CountNum&#43;&#43;
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
maxfds &#61; sockfd &#43; 1;
n &#61; select(maxfds, &readfds, NULL, NULL, &timeo);
if (n <&#61; 0)
{
printf("ip:%s,Time out error.\n",ips);
ret&#61; ERROR;
break;
}
//accept
memset(recvpacket, 0, sizeof(recvpacket));
int fromlen &#61; sizeof(from);
n &#61; recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, (struct sockaddr *)&from, &fromlen);
if (n <1)
{
printf("ip:%s, recvfrom error.\n",ips);
ret&#61; ERROR;
break;
}
// Check this echo reply if belong to myself
char *from_ip &#61; (char *)inet_ntoa(from.sin_addr);
printf("fomr ip:%s.\n",from_ip);
if (strcmp(from_ip,ips) !&#61; 0)
{
printf("ip:%s,Ip wrong.\n",ips);
ret &#61; ERROR;
break;
}
iph &#61; (struct ip *)recvpacket;
icmp&#61;(struct icmp *)(recvpacket &#43; (iph->ip_hl<<2));
printf("ip:%s,icmp->icmp_type:%d,icmp->icmp_id:%d.\n",ips,icmp->icmp_type,icmp->icmp_id);
//check reply contents
if (icmp->icmp_type &#61;&#61; ICMP_ECHOREPLY && icmp->icmp_id &#61;&#61; pid)
{
//ping success
ret &#61; SUCCESS;
break;
}
else
{
//continue wait
continue;
}
}
close(sockfd);
if(ret &#61;&#61; SUCCESS)
{
printf("Connect:%s success!\n",ips);
}
else
{
printf("Connect:%s FAIL!!!\n",ips);
}
return ret;
}