continued error management and -v flag

This commit is contained in:
2025-04-03 05:54:08 -04:00
parent 566213c178
commit e5596c3348
2 changed files with 23 additions and 9 deletions

View File

@ -28,7 +28,7 @@ all: $(NAME)
re: fclean all re: fclean all
$(BUILD_DIR): CMakeLists.txt $(C_SRCS) $(INCLUDES) $(SUBDIRECTORIES) $(BUILD_DIR): Makefile CMakeLists.txt $(C_SRCS) $(INCLUDES) $(SUBDIRECTORIES)
@MAKE_NAME="$(NAME)" MAKE_C_SRCS="$(C_SRCS)" MAKE_CFLAGS="$(CFLAGS)" MAKE_INCLUDE_DIRS="$(INCLUDE_DIRS)" \ @MAKE_NAME="$(NAME)" MAKE_C_SRCS="$(C_SRCS)" MAKE_CFLAGS="$(CFLAGS)" MAKE_INCLUDE_DIRS="$(INCLUDE_DIRS)" \
MAKE_INCLUDE_SRCS="$(INCLUDE_SRCS)" MAKE_INSTALL_DIR="$(INSTALL_DIR)" MAKE_NAME="$(NAME)" \ MAKE_INCLUDE_SRCS="$(INCLUDE_SRCS)" MAKE_INSTALL_DIR="$(INSTALL_DIR)" MAKE_NAME="$(NAME)" \
MAKE_SUBDIRECTORIES="$(SUBDIRECTORIES)" \ MAKE_SUBDIRECTORIES="$(SUBDIRECTORIES)" \

View File

@ -69,6 +69,7 @@ struct timespec sendt;
unsigned long xmit = 0; unsigned long xmit = 0;
unsigned long recvd = 0; unsigned long recvd = 0;
unsigned long rept = 0;
uint16_t calc_checksum(void* b, size_t len) { uint16_t calc_checksum(void* b, size_t len) {
unsigned short* buf = b; unsigned short* buf = b;
@ -188,7 +189,11 @@ int main(int argc, char** argv) {
sigaction(SIGINT, &sig_handle, 0); sigaction(SIGINT, &sig_handle, 0);
sigaction(SIGALRM, &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(); 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))) { 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]); dprintf(STDERR_FILENO, "checksum mismatch from %s\n", arguments.args[0]);
rept++;
pause(); pause();
continue; continue;
} }
icmp_reply->checksum = old_checksum; 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)); ((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), 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)); min = (((min) < (tmp)) ? (min) : (tmp));
max = (((max) > (tmp)) ? (max) : (tmp)); max = (((max) > (tmp)) ? (max) : (tmp));
avg += tmp; avg += tmp;
stddev += tmp * tmp; stddev += tmp * tmp;
// if (icmp_reply->un.echo.sequence <= recvd) {
// rept++;
// } else {
recvd++; recvd++;
// }
} }
} else if (ret == 0) { } else if (ret == 0) {
printf("No reply received within the timeout period\n"); printf("No reply received within the timeout period\n");
@ -251,10 +261,14 @@ int main(int argc, char** argv) {
} }
fflush(stdout); fflush(stdout);
printf("---- %s ping statistics ----\n", arguments.args[0]); printf("---- %s ping statistics ----\n", arguments.args[0]);
printf("%zu packets transmitted, %zu packets received, %zu%% packet loss\n", xmit, recvd, printf("%zu packets transmitted, %zu packets received, ", xmit, recvd);
(xmit - recvd) / xmit * 100); if (rept) {
avg /= recvd; printf("+%zu duplicates, ", rept);
stddev = stddev / recvd - avg * avg; }
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)); printf("round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", min, max, avg, nsqrt(stddev, 0.0005));
close(sock); close(sock);
} }