continued error management and -v flag
This commit is contained in:
30
srcs/main.c
30
srcs/main.c
@ -69,6 +69,7 @@ struct timespec sendt;
|
||||
|
||||
unsigned long xmit = 0;
|
||||
unsigned long recvd = 0;
|
||||
unsigned long rept = 0;
|
||||
|
||||
uint16_t calc_checksum(void* b, size_t len) {
|
||||
unsigned short* buf = b;
|
||||
@ -188,7 +189,11 @@ int main(int argc, char** argv) {
|
||||
sigaction(SIGINT, &sig_handle, 0);
|
||||
sigaction(SIGALRM, &sig_handle, 0);
|
||||
|
||||
printf("PING %s (%s): %ld data bytes\n", arguments.args[0], arguments.args[0], 64 - sizeof(struct icmphdr));
|
||||
printf("PING %s (%s): %ld data bytes", arguments.args[0], arguments.args[0], 64 - sizeof(struct icmphdr));
|
||||
if (arguments.verbose) {
|
||||
printf(", id 0x%04x = %u", getpid(), getpid());
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
send_ping();
|
||||
|
||||
@ -221,24 +226,29 @@ int main(int argc, char** argv) {
|
||||
|
||||
if (old_checksum != calc_checksum(icmp_reply, bytes_received - (ip_header->ip_hl << 2))) {
|
||||
dprintf(STDERR_FILENO, "checksum mismatch from %s\n", arguments.args[0]);
|
||||
rept++;
|
||||
pause();
|
||||
continue;
|
||||
}
|
||||
|
||||
icmp_reply->checksum = old_checksum;
|
||||
|
||||
float tmp =
|
||||
double tmp =
|
||||
((recvt.tv_sec * 1000 + recvt.tv_nsec / 1000000) - (sendt.tv_sec * 1000 + sendt.tv_nsec / 1000000));
|
||||
tmp += ((float)((recvt.tv_nsec - sendt.tv_nsec) % 1000000) / 1000000.0);
|
||||
tmp += ((double)((recvt.tv_nsec - sendt.tv_nsec) % 1000000) / 1000000.0);
|
||||
|
||||
printf("%zd bytes from %s: icmp_seq=%d ttl=%d time=%.3f ms \n", bytes_received - sizeof(struct ip),
|
||||
arguments.args[0], icmp_reply->un.echo.sequence + 1, ip_header->ip_ttl, tmp);
|
||||
arguments.args[0], icmp_reply->un.echo.sequence, ip_header->ip_ttl, tmp);
|
||||
|
||||
min = (((min) < (tmp)) ? (min) : (tmp));
|
||||
max = (((max) > (tmp)) ? (max) : (tmp));
|
||||
avg += tmp;
|
||||
stddev += tmp * tmp;
|
||||
// if (icmp_reply->un.echo.sequence <= recvd) {
|
||||
// rept++;
|
||||
// } else {
|
||||
recvd++;
|
||||
// }
|
||||
}
|
||||
} else if (ret == 0) {
|
||||
printf("No reply received within the timeout period\n");
|
||||
@ -251,10 +261,14 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
fflush(stdout);
|
||||
printf("---- %s ping statistics ----\n", arguments.args[0]);
|
||||
printf("%zu packets transmitted, %zu packets received, %zu%% packet loss\n", xmit, recvd,
|
||||
(xmit - recvd) / xmit * 100);
|
||||
avg /= recvd;
|
||||
stddev = stddev / recvd - avg * avg;
|
||||
printf("%zu packets transmitted, %zu packets received, ", xmit, recvd);
|
||||
if (rept) {
|
||||
printf("+%zu duplicates, ", rept);
|
||||
}
|
||||
printf("%zu%% packet loss\n", (xmit - recvd) / xmit * 100);
|
||||
double total = recvd + rept;
|
||||
avg /= total;
|
||||
stddev = stddev / total - avg * avg;
|
||||
printf("round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", min, max, avg, nsqrt(stddev, 0.0005));
|
||||
close(sock);
|
||||
}
|
Reference in New Issue
Block a user