diff --git a/src/host.h b/src/host.h new file mode 100644 index 0000000..ad2c51a --- /dev/null +++ b/src/host.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +struct host { + char ip[INET_ADDRSTRLEN]; + char *hostname; +}; \ No newline at end of file diff --git a/src/main.c b/src/main.c index 672c369..4bb8f45 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,5 @@ +#include "dns.h" +#include "host.h" #include "packet.h" #include "print.h" #include @@ -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);