From b4c24be141ffb3a06c9e6f0ecea6ab7159b21416 Mon Sep 17 00:00:00 2001 From: Jolan Rathelot Date: Thu, 3 Apr 2025 11:32:28 -0400 Subject: [PATCH] FQDN done --- srcs/main.c | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/srcs/main.c b/srcs/main.c index 7b06630..0d02638 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -63,7 +63,7 @@ static bool stop = false; int sock = 0; -static struct sockaddr_in destination; +static struct sockaddr_in destination = {0}; struct timespec sendt; @@ -146,25 +146,41 @@ int main(int argc, char** argv) { struct arguments arguments; int timeout = 3000; struct timespec recvt; + struct addrinfo hints = {0}; + struct addrinfo *res = NULL; struct pollfd fds[1]; double min = INT32_MAX; double max = 0; double avg = 0; double stddev = 0; + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + arguments.verbose = 0; arguments.ttl = 64; argp_parse(&argp, argc, argv, 0, 0, &arguments); - memset(&destination, 0, sizeof(destination)); + // memset(&destination, 0, sizeof(destination)); - destination.sin_family = AF_INET; - destination.sin_addr.s_addr = inet_addr(arguments.args[0]); - if (destination.sin_addr.s_addr == INADDR_NONE) { - dprintf(sock, "%s: unknown host\n", argv[0]); - exit(1); + char ip_str[INET_ADDRSTRLEN] = {0}; + + int status = getaddrinfo(arguments.args[0], NULL, &hints, &res); + if (status != 0) { + destination.sin_addr.s_addr = inet_addr(arguments.args[0]); + if (destination.sin_addr.s_addr == INADDR_NONE) { + dprintf(STDERR_FILENO, "%s: unknown host\n", argv[0]); + exit(1); + } + destination.sin_family = AF_INET; + destination.sin_port = htons(IPPORT_ECHO); + memcpy(ip_str, arguments.args[0], INET_ADDRSTRLEN); + } else { + memcpy(&destination, res->ai_addr, sizeof(struct sockaddr_in)); + destination.sin_family = AF_INET; + destination.sin_port = htons(IPPORT_ECHO); + inet_ntop(res->ai_family, &destination.sin_addr, ip_str, INET_ADDRSTRLEN); } - destination.sin_port = htons(IPPORT_ECHO); sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); @@ -189,7 +205,7 @@ int main(int argc, char** argv) { sigaction(SIGINT, &sig_handle, 0); sigaction(SIGALRM, &sig_handle, 0); - printf("PING %s (%s): %ld data bytes", arguments.args[0], arguments.args[0], 64 - sizeof(struct icmphdr)); + printf("PING %s (%s): %ld data bytes", arguments.args[0], ip_str, 64 - sizeof(struct icmphdr)); if (arguments.verbose) { printf(", id 0x%04x = %u", getpid(), getpid()); } @@ -225,7 +241,7 @@ int main(int argc, char** argv) { icmp_reply->checksum = 0; if (old_checksum != calc_checksum(icmp_reply, bytes_received - (ip_header->ip_hl << 2))) { - dprintf(STDERR_FILENO, "checksum mismatch from %s\n", arguments.args[0]); + dprintf(STDERR_FILENO, "checksum mismatch from %s\n", ip_str); rept++; pause(); continue; @@ -238,7 +254,7 @@ int main(int argc, char** argv) { tmp += ((double)((recvt.tv_nsec - sendt.tv_nsec) % 1000000) / 1000000.0); printf("%zd bytes from %s: icmp_seq=%d ttl=%d time=%.3f ms \n", bytes_received - sizeof(struct ip), - arguments.args[0], icmp_reply->un.echo.sequence, ip_header->ip_ttl, tmp); + ip_str, icmp_reply->un.echo.sequence, ip_header->ip_ttl, tmp); min = (((min) < (tmp)) ? (min) : (tmp)); max = (((max) > (tmp)) ? (max) : (tmp));