// 接受
memset(recvpacket, 0, sizeof(recvpacket));
int fromlen = sizeof(from);
n = recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, (struct sockaddr *)&from, &fromlen);
if (n < 1) {
break;
}
// 判断是否是自己Ping的回复
char *from_ip = (char *)inet_ntoa(from.sin_addr);
syslog(LOG_INFO,"fomr ip:%s",from_ip);
if (strcmp(from_ip,ips) != 0)
{
syslog(LOG_INFO,"ip:%s,Ip wang",ips);
break;
}
iph = (struct ip *)recvpacket;
icmp=(struct icmp *)(recvpacket + (iph->ip_hl<<2));
syslog(LOG_INFO,"ip:%s,icmp->icmp_type:%d,icmp->icmp_id:%d",ips,icmp->icmp_type,icmp->icmp_id);
// 判断Ping回复包的状态
if (icmp->icmp_type == ICMP_ECHOREPLY && icmp->icmp_id == pid)
{
// 正常就退出循环
break;
}
else
{
// 否则继续等
continue;
}
}
// 关闭socket
close(sockfd);
syslog(LOG_INFO,"ip:%s,Success",ips);
return SUCCESS;
}