add: improved print

This commit is contained in:
2025-10-30 10:38:15 -05:00
parent 8744d4490b
commit e2a082a5e1
2 changed files with 17 additions and 6 deletions

8
src/host.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include <netinet/in.h>
struct host {
char ip[INET_ADDRSTRLEN];
char *hostname;
};

View File

@ -1,3 +1,5 @@
#include "dns.h"
#include "host.h"
#include "packet.h"
#include "print.h"
#include <signal.h>
@ -32,21 +34,21 @@ int main(int ac, char **av)
{
(void) ac;
(void) av;
struct host hostname;
size_t payload_size = 48;
size_t packet_size = sizeof(struct icmphdr) + payload_size;
signal(SIGINT, signal_handler);
char *ipstr = dns_lookup("google.com");
if (ipstr == NULL)
if (dns_lookup("localhost", hostname.ip))
return 5;
struct sockaddr_in dest;
dest.sin_family = AF_INET;
dest.sin_port = htons(0);
inet_pton(AF_INET, ipstr, &dest.sin_addr);
free(ipstr);
inet_pton(AF_INET, hostname.ip, &dest.sin_addr);
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
@ -102,8 +104,9 @@ 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 + (stop.tv_usec - start.tv_usec)) / 1000;
printf("icmp reply received %fms\n", time_interval);
double time_interval = ((stop.tv_sec - start.tv_sec) * 1000 + ((double)stop.tv_usec - (double) start.tv_usec) / 1000);
struct icmphdr *hdr = (struct icmphdr *) packet;
printf("%zu bytes from %p (%s): icmp_seq=%d, ttl=%u, time=%fms\n", payload_size, NULL, hostname.ip, htons(hdr->un.echo.sequence), 116, time_interval);
sleep(1);