in netutils/ping/icmp_ping.c [166:416]
void icmp_ping(FAR const struct ping_info_s *info)
{
struct ping_result_s result;
struct sockaddr_in destaddr;
struct sockaddr_in fromaddr;
struct icmp_hdr_s outhdr;
FAR struct icmp_hdr_s *inhdr;
struct pollfd recvfd;
FAR uint8_t *iobuffer;
FAR uint8_t *ptr;
long elapsed;
clock_t kickoff;
clock_t start;
socklen_t addrlen;
ssize_t nsent;
ssize_t nrecvd;
bool retry;
int sockfd;
int ret;
int ch;
int i;
g_exiting = false;
signal(SIGINT, sigexit);
/* Initialize result structure */
memset(&result, 0, sizeof(result));
result.info = info;
result.id = ping_newid();
result.outsize = ICMP_IOBUFFER_SIZE(info->datalen);
if (ping_gethostip(info->hostname, &result.dest) < 0)
{
icmp_callback(&result, ICMP_E_HOSTIP, 0);
return;
}
/* Allocate memory to hold ping buffer */
iobuffer = (FAR uint8_t *)malloc(result.outsize);
if (iobuffer == NULL)
{
icmp_callback(&result, ICMP_E_MEMORY, 0);
return;
}
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
if (sockfd < 0)
{
icmp_callback(&result, ICMP_E_SOCKET, errno);
free(iobuffer);
return;
}
kickoff = clock();
memset(&destaddr, 0, sizeof(struct sockaddr_in));
destaddr.sin_family = AF_INET;
destaddr.sin_port = 0;
destaddr.sin_addr.s_addr = result.dest.s_addr;
memset(&outhdr, 0, sizeof(struct icmp_hdr_s));
outhdr.type = ICMP_ECHO_REQUEST;
outhdr.id = htons(result.id);
outhdr.seqno = htons(result.seqno);
icmp_callback(&result, ICMP_I_BEGIN, 0);
while (result.nrequests < info->count)
{
if (g_exiting)
{
break;
}
/* Copy the ICMP header into the I/O buffer */
memcpy(iobuffer, &outhdr, sizeof(struct icmp_hdr_s));
/* Add some easily verifiable payload data */
ptr = &iobuffer[sizeof(struct icmp_hdr_s)];
ch = 0x20;
for (i = 0; i < info->datalen; i++)
{
*ptr++ = ch;
if (++ch > 0x7e)
{
ch = 0x20;
}
}
start = clock();
nsent = sendto(sockfd, iobuffer, result.outsize, 0,
(FAR struct sockaddr *)&destaddr,
sizeof(struct sockaddr_in));
if (nsent < 0)
{
icmp_callback(&result, ICMP_E_SENDTO, errno);
goto done;
}
else if (nsent != result.outsize)
{
icmp_callback(&result, ICMP_E_SENDSMALL, nsent);
goto done;
}
result.nrequests++;
elapsed = 0;
do
{
retry = false;
recvfd.fd = sockfd;
recvfd.events = POLLIN;
recvfd.revents = 0;
ret = poll(&recvfd, 1, info->timeout - elapsed / USEC_PER_MSEC);
if (ret < 0)
{
icmp_callback(&result, ICMP_E_POLL, errno);
goto done;
}
else if (ret == 0)
{
icmp_callback(&result, ICMP_W_TIMEOUT, info->timeout);
continue;
}
/* Get the ICMP response (ignoring the sender) */
addrlen = sizeof(struct sockaddr_in);
nrecvd = recvfrom(sockfd, iobuffer, result.outsize, 0,
(FAR struct sockaddr *)&fromaddr, &addrlen);
if (nrecvd < 0)
{
icmp_callback(&result, ICMP_E_RECVFROM, errno);
goto done;
}
else if (nrecvd < sizeof(struct icmp_hdr_s))
{
icmp_callback(&result, ICMP_E_RECVSMALL, nrecvd);
goto done;
}
elapsed = TICK2USEC(clock() - start);
inhdr = (FAR struct icmp_hdr_s *)iobuffer;
if (inhdr->type == ICMP_ECHO_REPLY)
{
#ifndef CONFIG_SIM_NETUSRSOCK
if (ntohs(inhdr->id) != result.id)
{
icmp_callback(&result, ICMP_W_IDDIFF, ntohs(inhdr->id));
retry = true;
}
else
#endif
if (ntohs(inhdr->seqno) > result.seqno)
{
icmp_callback(&result, ICMP_W_SEQNOBIG,
ntohs(inhdr->seqno));
retry = true;
}
else if (ntohs(inhdr->seqno) < result.seqno)
{
icmp_callback(&result, ICMP_W_SEQNOSMALL,
ntohs(inhdr->seqno));
retry = true;
}
else
{
bool verified = true;
long pktdelay = elapsed;
icmp_callback(&result, ICMP_I_ROUNDTRIP, pktdelay);
/* Verify the payload data */
if (nrecvd != result.outsize)
{
icmp_callback(&result, ICMP_W_RECVBIG, nrecvd);
verified = false;
}
else
{
ptr = &iobuffer[sizeof(struct icmp_hdr_s)];
ch = 0x20;
for (i = 0; i < info->datalen; i++, ptr++)
{
if (*ptr != ch)
{
icmp_callback(&result, ICMP_W_DATADIFF, 0);
verified = false;
break;
}
if (++ch > 0x7e)
{
ch = 0x20;
}
}
}
/* Only count the number of good replies */
if (verified)
{
result.nreplies++;
}
}
}
else
{
icmp_callback(&result, ICMP_W_TYPE, inhdr->type);
}
}
while (retry && info->delay > elapsed / USEC_PER_MSEC &&
info->timeout > elapsed / USEC_PER_MSEC);
/* Wait if necessary to preserved the requested ping rate */
elapsed = TICK2MSEC(clock() - start);
if (elapsed < info->delay)
{
struct timespec rqt;
unsigned int remaining;
unsigned int sec;
unsigned int frac; /* In deciseconds */
remaining = info->delay - elapsed;
sec = remaining / MSEC_PER_SEC;
frac = remaining - MSEC_PER_SEC * sec;
rqt.tv_sec = sec;
rqt.tv_nsec = frac * NSEC_PER_MSEC;
nanosleep(&rqt, NULL);
}
outhdr.seqno = htons(++result.seqno);
}
done:
icmp_callback(&result, ICMP_I_FINISH, TICK2USEC(clock() - kickoff));
close(sockfd);
free(iobuffer);
}