diff --git a/src/dns.c b/src/dns.c index ec47f86..cd1958d 100644 --- a/src/dns.c +++ b/src/dns.c @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include int dns_lookup(const char *hostname, char *ipstr) @@ -30,7 +32,32 @@ int dns_lookup(const char *hostname, char *ipstr) return 0; } -int dns_reverse_lookup(const char *ipstr, char *hostname); +char *dns_reverse_lookup(const char *ipstr) { + struct sockaddr_in sa; + char host[NI_MAXHOST]; + bzero(&sa, sizeof(sa)); + sa.sin_family = AF_INET; + + printf("%s\n", ipstr); + + if (inet_pton(AF_INET, ipstr, &sa.sin_addr) <= 0) + { + print_err("inet_pton failed"); + return NULL; + } + + int ret = getnameinfo( + (struct sockaddr *)&sa, sizeof(sa), + host, sizeof(host), + NULL, 0, NI_NAMEREQD); + + if (ret != 0) + { + print_err("reverse dns failed."); + return NULL; + } + + return strdup(host); } \ No newline at end of file diff --git a/src/dns.h b/src/dns.h index ed07549..e2bbfb5 100644 --- a/src/dns.h +++ b/src/dns.h @@ -1,4 +1,4 @@ #pragma once int dns_lookup(const char *hostname, char *ipstr); -int dns_reverse_lookup(const char *ipstr, char *hostname); \ No newline at end of file +char *dns_reverse_lookup(const char *ipstr); \ No newline at end of file diff --git a/src/host.h b/src/host.h index ad2c51a..0190b75 100644 --- a/src/host.h +++ b/src/host.h @@ -5,4 +5,5 @@ struct host { char ip[INET_ADDRSTRLEN]; char *hostname; + char *reverse_dns; }; \ No newline at end of file diff --git a/src/main.c b/src/main.c index 4bb8f45..8b9374a 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,7 @@ #include "host.h" #include "packet.h" #include "print.h" +#include #include #include #include @@ -21,6 +22,7 @@ #include #include #include +#include bool loop = true; @@ -34,21 +36,26 @@ int main(int ac, char **av) { (void) ac; (void) av; - struct host hostname; - size_t payload_size = 48; + struct host host; + + host.hostname = "localhost"; + + size_t payload_size = 20; size_t packet_size = sizeof(struct icmphdr) + payload_size; signal(SIGINT, signal_handler); - if (dns_lookup("localhost", hostname.ip)) + if (dns_lookup(host.hostname, host.ip)) return 5; + host.reverse_dns = dns_reverse_lookup(host.ip); + struct sockaddr_in dest; dest.sin_family = AF_INET; dest.sin_port = htons(0); - inet_pton(AF_INET, hostname.ip, &dest.sin_addr); + inet_pton(AF_INET, host.ip, &dest.sin_addr); int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); @@ -106,7 +113,7 @@ int main(int ac, char **av) 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); + printf("%zu bytes from %s (%s): icmp_seq=%d, ttl=%u, time=%fms\n", payload_size + sizeof(struct icmphdr) + sizeof(struct iphdr) + sizeof(struct ether_header), host.reverse_dns, host.ip, htons(hdr->un.echo.sequence), 116, time_interval); sleep(1);