From: Gerrit Renker <grenker@cscs.ch>
Date: Sun, 13 Jun 2010 09:30:46 +0000 (+0200)
Subject: net: host/port string convention
X-Git-Tag: v0.4.3~8^2~6
X-Git-Url: https://git.tuebingen.mpg.de/?a=commitdiff_plain;h=f10742c3fdc05a3ff719ad7e01eacfd5e8c0dae0;p=paraslash.git

net: host/port string convention

This updates the implementation of host_and_port() in such a way that its
output can again be used as input of parse_url():

 * the separator for host and port is now ':' instead of '#';
 * port numbers (services) are now always represented as numbers
   (reverting an earlier change);
 * IPv6 addresses are enclosed in square brackets.

This convention makes it easier to copy&paste output from paraslash commands
(including scripting), and to store target information in a format that is
easy to resolve/reuse at a later stage.
---

diff --git a/net.c b/net.c
index fa9575a6..be637ec7 100644
--- a/net.c
+++ b/net.c
@@ -598,25 +598,28 @@ int generic_max_transport_msg_size(int sockfd)
  *
  * \param sa The IPv4/IPv6 socket address to use.
  *
+ * \return Host string in numeric host:port format, \sa parse_url().
  * \sa getnameinfo(3), services(5), nsswitch.conf(5)
  */
 static char *host_and_port(const struct sockaddr_storage *ss)
 {
 	const struct sockaddr *sa = normalize_ip_address(ss);
 	char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
-	static char output[sizeof(hbuf) + sizeof(sbuf) + 2];
+	static char output[sizeof(hbuf) + sizeof(sbuf) + 4];
 	int ret;
 
 	ret = getnameinfo(sa, salen(sa),
 			  hbuf, sizeof(hbuf),
 			  sbuf, sizeof(sbuf),
-			  NI_NUMERICHOST);
-	if (ret == 0) {
-		snprintf(output, sizeof(output), "%s#%s", hbuf, sbuf);
-	} else {
+			  NI_NUMERICHOST | NI_NUMERICSERV);
+	if (ret) {
 		snprintf(output, sizeof(output), "(unknown)");
 		PARA_WARNING_LOG("hostname lookup error (%s).\n",
 				 gai_strerror(ret));
+	} else if (sa->sa_family == AF_INET6) {
+		snprintf(output, sizeof(output), "[%s]:%s", hbuf, sbuf);
+	} else {
+		snprintf(output, sizeof(output), "%s:%s", hbuf, sbuf);
 	}
 	return output;
 }