From 21d6539d3c504c9a99bb55c52dd8bfd5448befff Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Thu, 7 Apr 2022 15:18:01 +0100 Subject: [PATCH] mdns.c: Factorize ip_address_to_string() Factorize the very similar functions ipv4_address_to_string() and ipv6_address_to_string() into one _ip_address_to_string() + wrappers. Signed-off-by: Paul Cercueil --- mdns.c | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/mdns.c b/mdns.c index 433936d..0941b3b 100644 --- a/mdns.c +++ b/mdns.c @@ -61,15 +61,17 @@ typedef struct { } service_t; static mdns_string_t -ipv4_address_to_string(char* buffer, size_t capacity, const struct sockaddr_in* addr, - size_t addrlen) { +_ip_address_to_string(char* buffer, size_t capacity, const struct sockaddr* addr, size_t addrlen, + int is_ipv6) { char host[NI_MAXHOST] = {0}; char service[NI_MAXSERV] = {0}; - int ret = getnameinfo((const struct sockaddr*)addr, (socklen_t)addrlen, host, NI_MAXHOST, + int ret = getnameinfo(addr, (socklen_t)addrlen, host, NI_MAXHOST, service, NI_MAXSERV, NI_NUMERICSERV | NI_NUMERICHOST); int len = 0; if (ret == 0) { - if (addr->sin_port != 0) + if (is_ipv6 && ((struct sockaddr_in6 *)addr)->sin6_port != 0) + len = snprintf(buffer, capacity, "[%s]:%s", host, service); + else if (!is_ipv6 && ((struct sockaddr_in *)addr)->sin_port != 0) len = snprintf(buffer, capacity, "%s:%s", host, service); else len = snprintf(buffer, capacity, "%s", host); @@ -82,33 +84,21 @@ ipv4_address_to_string(char* buffer, size_t capacity, const struct sockaddr_in* return str; } +static mdns_string_t +ip_address_to_string(char* buffer, size_t capacity, const struct sockaddr* addr, size_t addrlen) { + return _ip_address_to_string(buffer, capacity, addr, addrlen, addr->sa_family == AF_INET6); +} + static mdns_string_t ipv6_address_to_string(char* buffer, size_t capacity, const struct sockaddr_in6* addr, size_t addrlen) { - char host[NI_MAXHOST] = {0}; - char service[NI_MAXSERV] = {0}; - int ret = getnameinfo((const struct sockaddr*)addr, (socklen_t)addrlen, host, NI_MAXHOST, - service, NI_MAXSERV, NI_NUMERICSERV | NI_NUMERICHOST); - int len = 0; - if (ret == 0) { - if (addr->sin6_port != 0) - len = snprintf(buffer, capacity, "[%s]:%s", host, service); - else - len = snprintf(buffer, capacity, "%s", host); - } - if (len >= (int)capacity) - len = (int)capacity - 1; - mdns_string_t str; - str.str = buffer; - str.length = len; - return str; + return _ip_address_to_string(buffer, capacity, (struct sockaddr *)addr, addrlen, 1); } static mdns_string_t -ip_address_to_string(char* buffer, size_t capacity, const struct sockaddr* addr, size_t addrlen) { - if (addr->sa_family == AF_INET6) - return ipv6_address_to_string(buffer, capacity, (const struct sockaddr_in6*)addr, addrlen); - return ipv4_address_to_string(buffer, capacity, (const struct sockaddr_in*)addr, addrlen); +ipv4_address_to_string(char* buffer, size_t capacity, const struct sockaddr_in* addr, + size_t addrlen) { + return _ip_address_to_string(buffer, capacity, (struct sockaddr *)addr, addrlen, 0); } // Callback handling parsing answers to queries sent