From: Aurelien DARRAGON Date: Mon, 10 Mar 2025 21:25:09 +0000 (+0100) Subject: MINOR: tools: only print address in sa2str() when port == -1 X-Git-Tag: v3.2-dev8~111 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=47f14be9f31ca13a68c4f514ca5d1528aef9a07f;p=thirdparty%2Fhaproxy.git MINOR: tools: only print address in sa2str() when port == -1 Support special value for port in sa2str: if port is equal to -1, only print the address without the port, also ignoring value. --- diff --git a/include/haproxy/tools.h b/include/haproxy/tools.h index 177717ea1..bf2ca90a0 100644 --- a/include/haproxy/tools.h +++ b/include/haproxy/tools.h @@ -324,6 +324,7 @@ struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int /* converts and into a string representation of the address and port. This is sort * of an inverse of str2sa_range, with some restrictions. The supported families are AF_INET, * AF_INET6, AF_UNIX, and AF_CUST_SOCKPAIR. If the family is unsopported NULL is returned. + * If port is special value '-1', then only the address is represented and is ignored. * If map_ports is true, then the sign of the port is included in the output, to indicate it is * relative to the incoming port. AF_INET and AF_INET6 will be in the form ":". * AF_UNIX will either be just the path (if using a pathname) or "abns@" if it is abstract. diff --git a/src/tools.c b/src/tools.c index b0089cf86..2a8813ed5 100644 --- a/src/tools.c +++ b/src/tools.c @@ -1455,6 +1455,7 @@ struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int /* converts and into a string representation of the address and port. This is sort * of an inverse of str2sa_range, with some restrictions. The supported families are AF_INET, * AF_INET6, AF_UNIX, and AF_CUST_SOCKPAIR. If the family is unsopported NULL is returned. + * If port is special value '-1', then only the address is represented and is ignored. * If map_ports is true, then the sign of the port is included in the output, to indicate it is * relative to the incoming port. AF_INET and AF_INET6 will be in the form ":". * AF_UNIX will either be just the path (if using a pathname) or "abns@" if it is abstract. @@ -1496,6 +1497,10 @@ char * sa2str(const struct sockaddr_storage *addr, int port, int map_ports) BUG_ON(errno == ENOSPC); return NULL; } + + if (port == -1) + return strdup(buffer); // address only + if (map_ports) return memprintf(&out, "%s:%+d", buffer, port); else