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 <map_ports> value.
This commit is contained in:
Aurelien DARRAGON 2025-03-10 22:25:09 +01:00
parent 2de62d0461
commit 47f14be9f3
2 changed files with 6 additions and 0 deletions

View 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.

View 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