added min/max calcs

This commit is contained in:
2025-04-01 07:33:51 -04:00
parent 5eb1c9bca2
commit 936f754941
2 changed files with 49 additions and 9 deletions

View File

@ -28,4 +28,9 @@ struct ping_pkt {
char msg[64 - sizeof(struct icmphdr)];
};
struct ping_delta {
int ms_top;
int ms_bottom;
};
#endif //MAIN_H

View File

@ -108,11 +108,45 @@ void sig_handler(int sig) {
}
}
struct ping_delta get_min(struct ping_delta a, struct ping_delta b) {
if (a.ms_top < b.ms_top) {
return a;
} else if (b.ms_top < a.ms_top) {
return b;
} else {
if (a.ms_bottom < b.ms_bottom) {
return a;
}
return b;
}
}
struct ping_delta get_max(struct ping_delta a, struct ping_delta b) {
if (a.ms_top > b.ms_top) {
return a;
} else if (b.ms_top > a.ms_top) {
return b;
} else {
if (a.ms_bottom > b.ms_bottom) {
return a;
}
return b;
}
}
int main(int argc, char** argv) {
struct arguments arguments;
int timeout = 3000;
struct timespec recvt;
struct pollfd fds[1];
struct ping_delta min;
struct ping_delta max;
min.ms_bottom = INT32_MAX;
min.ms_top = INT32_MAX;
max.ms_bottom = 0;
max.ms_top = 0;
arguments.verbose = 0;
arguments.ttl = 64;
@ -153,7 +187,7 @@ int main(int argc, char** argv) {
send_ping();
while (true) {
while (!stop) {
int ret = poll(fds, 1, timeout);
if (ret > 0) {
if (fds[0].revents & POLLIN) {
@ -171,13 +205,18 @@ int main(int argc, char** argv) {
struct ip* ip_header = (struct ip*)buf;
struct icmphdr* icmp_reply = (struct icmphdr*)(buf + (ip_header->ip_hl << 2));
int rtt_n =
struct ping_delta tmp;
tmp.ms_top =
((recvt.tv_sec * 1000 + recvt.tv_nsec / 1000000) - (sendt.tv_sec * 1000 + sendt.tv_nsec / 1000000));
int rtt_r = ((recvt.tv_nsec - sendt.tv_nsec) % 1000000 / 1000);
tmp.ms_bottom = ((recvt.tv_nsec - sendt.tv_nsec) % 1000000 / 1000);
printf("%zd bytes from %s: icmp_seq=%d ttl=%d time=%d.%d ms \n", bytes_received, arguments.args[0],
icmp_reply->un.echo.sequence, ip_header->ip_ttl, rtt_n, rtt_r);
icmp_reply->un.echo.sequence, ip_header->ip_ttl, tmp.ms_top, tmp.ms_bottom);
recvd++;
max = get_max(max, tmp);
min = get_min(min, tmp);
}
} else if (ret == 0) {
printf("No reply received within the timeout period\n");
@ -186,14 +225,10 @@ int main(int argc, char** argv) {
}
pause();
if (stop) {
break;
}
}
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);
// printf("round-trip min/avg/max/stddev = %f/%f/%f/%f ms\n");
printf("round-trip min/avg/max/stddev = %d.%d/%d.%d/%%.3f/%%.3f ms\n", min.ms_top, min.ms_bottom, max.ms_top, max.ms_bottom);
close(sock);
}