]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: only print address in sa2str() when port == -1
authorAurelien DARRAGON <adarragon@haproxy.com>
Mon, 10 Mar 2025 21:25:09 +0000 (22:25 +0100)
committerAurelien DARRAGON <adarragon@haproxy.com>
Wed, 12 Mar 2025 09:51:20 +0000 (10:51 +0100)
Support special value for port in sa2str: if port is equal to -1, only
print the address without the port, also ignoring <map_ports> value.

include/haproxy/tools.h
src/tools.c

index 177717ea12c4d3d6a815caa3038bb5f14e2164bb..bf2ca90a0ff7e30016908db46c20baba9eb91d84 100644 (file)
@@ -324,6 +324,7 @@ struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int
 /* converts <addr> and <port> 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 <map_ports> 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 "<addr>:<port>".
  * AF_UNIX will either be just the path (if using a pathname) or "abns@<path>" if it is abstract.
index b0089cf8647830489729247b091a3fb814695ef8..2a8813ed52a6e26c116b57fa0a02d78deac9b747 100644 (file)
@@ -1455,6 +1455,7 @@ struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int
 /* converts <addr> and <port> 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 <map_ports> 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 "<addr>:<port>".
  * AF_UNIX will either be just the path (if using a pathname) or "abns@<path>" 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