From ca80a82d961a05c33407ceae1962d779419247e5 Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 19 Nov 2025 06:39:24 -0600 Subject: [PATCH] add: print --- src/interval.c | 11 +++++++++++ src/interval.h | 5 +++++ src/main.c | 18 +++++++++++++++--- src/print.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/print.h | 11 ++++++++++- src/statistics.h | 9 +++++++++ 6 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 src/interval.c create mode 100644 src/interval.h create mode 100644 src/statistics.h diff --git a/src/interval.c b/src/interval.c new file mode 100644 index 0000000..b8e147a --- /dev/null +++ b/src/interval.c @@ -0,0 +1,11 @@ +#include + +double interval_to_ms(struct timeval const *t) +{ + return (double)t->tv_sec * 1000.0 + (double)t->tv_usec / 1000.0; +} + +double get_interval(struct timeval const *start, struct timeval const *stop) +{ + return interval_to_ms(stop) - interval_to_ms(start); +} \ No newline at end of file diff --git a/src/interval.h b/src/interval.h new file mode 100644 index 0000000..84ad949 --- /dev/null +++ b/src/interval.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +double get_interval(struct timeval const *start, struct timeval const *stop); \ No newline at end of file diff --git a/src/main.c b/src/main.c index 8b14f83..3b529d9 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,8 @@ #include "parsing.h" #include "print.h" #include "setting.h" +#include "statistics.h" + #include #include #include @@ -100,6 +102,7 @@ static int get_setting(char * const *av, struct setting *setting) int main(int ac, char **av) { (void) ac; + struct statistics stats; struct setting settings; if (get_setting(av + 1, &settings)) @@ -116,7 +119,6 @@ int main(int ac, char **av) settings.dest.ip.sin_port = htons(0); inet_pton(AF_INET, settings.dest.ipstr, &settings.dest.ip.sin_addr); - dns_reverse_lookup(&settings.dest); int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); @@ -146,6 +148,9 @@ int main(int ac, char **av) struct sockaddr_in sender; socklen_t len = sizeof(sender); + bzero(&stats, sizeof(struct statistics)); + print_header(&settings); + while (loop) { struct timeval stop, start; @@ -159,6 +164,8 @@ int main(int ac, char **av) return 2; } + stats.packets_sent++; + do { if (recvfrom(sockfd, buffer, packet_size, 0, (struct sockaddr *) &sender, &len) < 0) @@ -173,9 +180,11 @@ int main(int ac, char **av) } while (len == packet_size && packet_check(buffer, packet_size) == 0 && packet_compare(packet, buffer, packet_size) == 0); - double time_interval = ((stop.tv_sec - start.tv_sec) * 1000 + ((double)stop.tv_usec - (double) start.tv_usec) / 1000); + stats.packets_received++; + struct icmphdr *hdr = (struct icmphdr *) packet; - printf("%zu bytes from %s (%s): icmp_seq=%d, ttl=%u, time=%fms\n", packet_size, settings.dest.reverse_dns, settings.dest.ipstr, htons(hdr->un.echo.sequence), 116, time_interval); + + print_ping(&settings, hdr, &start, &stop); sleep(1); @@ -186,6 +195,9 @@ int main(int ac, char **av) free(packet); } } + free(packet); free(buffer); + + print_statistics(&stats, &settings); } \ No newline at end of file diff --git a/src/print.c b/src/print.c index 7c3c914..e3a86c7 100644 --- a/src/print.c +++ b/src/print.c @@ -1,7 +1,15 @@ +#include "interval.h" +#include "setting.h" +#include "statistics.h" +#include +#include +#include +#include #include #include +#include #include void print_err(const char *format, ...) @@ -13,4 +21,42 @@ void print_err(const char *format, ...) vdprintf(2, format, args); va_end(args); write(2, "\n", 1); +} + +void print_header(const struct setting *settings) +{ + printf("PING %s (%s) %zu(%zu) bytes of data", + settings->dest.hostname, + settings->dest.ipstr, + settings->payload_size, + settings->payload_size + sizeof(struct icmphdr) + sizeof(struct iphdr) + ); + if (settings->verbose) + { + unsigned short pid = getpid(); + printf(", id 0x%x = %d", htons(getpid()), pid); + } + printf("\n"); +} + +void print_statistics(struct statistics const *stats, struct setting const *settings) +{ + printf("--- %s ping statistics ---\n", settings->dest.hostname); + printf("%zu packets transmitted, %zu packets received, %.3f%% packet loss\n", + stats->packets_sent, + stats->packets_received, + ((double) stats->packets_sent) * 100 / (double) stats->packets_received + ); +} + +void print_ping(const struct setting *settings, const struct icmphdr *header, const struct timeval *start, const struct timeval *stop) +{ + + printf("%zu bytes from %s: icmp_seq=%d ttl=%zu time=%.3f ms\n", + settings->payload_size + sizeof(struct icmphdr), + settings->dest.ipstr, + htons(header->un.echo.sequence), + settings->ttl, + get_interval(start, stop) + ); } \ No newline at end of file diff --git a/src/print.h b/src/print.h index d36fa59..06455fc 100644 --- a/src/print.h +++ b/src/print.h @@ -1,3 +1,12 @@ #pragma once -void print_err(const char *format, ...); \ No newline at end of file +#include "setting.h" +#include "statistics.h" +#include +#include + +void print_err(const char *format, ...); + +void print_header(struct setting const *settings); +void print_statistics(struct statistics const *stats, struct setting const *settings); +void print_ping(struct setting const *settings, struct icmphdr const *header, struct timeval const *start, struct timeval const *stop); \ No newline at end of file diff --git a/src/statistics.h b/src/statistics.h new file mode 100644 index 0000000..35a9c4a --- /dev/null +++ b/src/statistics.h @@ -0,0 +1,9 @@ +#pragma once + +#include +#include + +struct statistics { + size_t packets_sent; + size_t packets_received; +}; \ No newline at end of file