Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions so3/usr/src/ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
/* ping: send ICMP echo requests to a host and report the replies, including
* the round-trip times. */

#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <syscall.h>
Expand Down Expand Up @@ -233,7 +234,7 @@ int main(int argc, char **argv)
s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);

if (s < 0) {
printf("Impossible to obtain a socket file descriptor!!\n");
printf("Cannot open the ICMP socket: %s\n", strerror(errno));
return 1;
}

Expand Down Expand Up @@ -265,18 +266,24 @@ int main(int argc, char **argv)
gettimeofday(&start, NULL);

if (sendto(s, &packet, sizeof(packet), 0, (struct sockaddr *) &ping_addr, sizeof(ping_addr)) <= 0) {
printf("Packet sending failed!!\n");
printf("Cannot send icmp_seq=%d: %s\n", msg_count, strerror(errno));
continue;
}

size = sizeof(recv_addr);

len = recvfrom(s, reply, sizeof(reply), 0, (struct sockaddr *) &recv_addr, &size);

/* A timeout (SO_RCVTIMEO) lands here too, which is the normal
* outcome for a host that never answers. */
/* A host that never answers is the normal case, not a failure:
* SO_RCVTIMEO expires and lwIP reports it as EWOULDBLOCK (EAGAIN,
* the same value in musl). Anything else is a real error and says
* which one. */
if (len <= 0) {
printf("Packet receive failed!!\n");
if ((len < 0) && (errno != EAGAIN))
printf("Cannot receive icmp_seq=%d: %s\n", msg_count, strerror(errno));
else
printf("Request timeout for icmp_seq=%d\n", msg_count);

continue;
}

Expand Down
Loading