add: print

This commit is contained in:
2025-11-19 06:39:24 -06:00
parent ee09a2e099
commit ca80a82d96
6 changed files with 96 additions and 4 deletions

View File

@@ -4,6 +4,8 @@
#include "parsing.h"
#include "print.h"
#include "setting.h"
#include "statistics.h"
#include <netinet/ip.h>
#include <signal.h>
#include <stddef.h>
@@ -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);
}