From: Michael Tremer Date: Mon, 1 Feb 2021 17:55:37 +0000 (+0000) Subject: Revert "client: Use getaddrinfo to resolve any hostnames given" X-Git-Tag: 0.1.0~24 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0ec017a4da5e00c405d63782d34009f486dc9478;p=fireperf.git Revert "client: Use getaddrinfo to resolve any hostnames given" This reverts commit e5c9467df37a2d6914164ec9c2cd9b35cfb0c3cb. Signed-off-by: Michael Tremer --- diff --git a/src/main.c b/src/main.c index 26b1b8a..d0f4ea1 100644 --- a/src/main.c +++ b/src/main.c @@ -21,13 +21,11 @@ #include #include #include -#include #include #include #include #include #include -#include #include #include @@ -36,32 +34,29 @@ #include "logging.h" #include "server.h" -static int parse_address(struct fireperf_config* conf, const char* string, struct in6_addr* address6) { - struct addrinfo hints = { - .ai_family = AF_UNSPEC, - .ai_socktype = SOCK_STREAM, - }; - - struct addrinfo* results; - - int r = getaddrinfo(string, NULL, &hints, &results); - if (r) { - ERROR(conf, "getaddrinfo() failed: %s\n", gai_strerror(r)); - return 1; +static int parse_address(const char* string, struct in6_addr* address6) { + // Try parsing this address + int r = inet_pton(AF_INET6, string, address6); + + // Success! + if (r == 1) + return 0; + + // Try parsing this as an IPv4 address + struct in_addr address4; + r = inet_pton(AF_INET, string, &address4); + if (r == 1) { + // Convert to IPv6-mapped address + address6->s6_addr32[0] = htonl(0x0000); + address6->s6_addr32[1] = htonl(0x0000); + address6->s6_addr32[2] = htonl(0xffff); + address6->s6_addr32[3] = address4.s_addr; + + return 0; } - for (struct addrinfo* result = results; result; result = result->ai_next) { - address6 = malloc(result->ai_addrlen); - if (!address6) - return 1; - - memcpy(address6, result->ai_addr, result->ai_addrlen); - break; - } - - freeaddrinfo(results); - - return 0; + // Could not parse this + return 1; } static int set_limits(struct fireperf_config* conf) { @@ -124,7 +119,7 @@ static int parse_argv(int argc, char* argv[], struct fireperf_config* conf) { conf->mode = FIREPERF_MODE_CLIENT; // Parse the given IP address - int r = parse_address(conf, optarg, &conf->address); + int r = parse_address(optarg, &conf->address); if (r) { fprintf(stderr, "Could not parse IP address %s\n", optarg); return 2;