]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/pick_address: Fix IP address stringification.
authorTommi Virtanen <tommi.virtanen@dreamhost.com>
Wed, 23 Nov 2011 01:48:40 +0000 (17:48 -0800)
committerSage Weil <sage.weil@dreamhost.com>
Wed, 23 Nov 2011 04:52:16 +0000 (20:52 -0800)
Different sockaddr_* have the actual address (sin_addr, sin6_addr)
at different offsets, and sockaddr->sa_data just isn't enough.
inet_ntop conspires by taking a void*. I could figure out the right
offset with a switch (found->sa_family), but let's go for the
supposedly write-once-run-with-any-AF solution, getnameinfo.

Which, naturally, takes an extra length argument that is AF-specific,
and not provided anywhere nicely by getifaddrs. Huzzah!

Signed-off-by: Tommi Virtanen <tommi.virtanen@dreamhost.com>
src/common/pick_address.cc

index 120fe9d274c22a8bc5cc140756c4908a878deb51..677ffa213813d15564ade5b80352472ac8eb2a4b 100644 (file)
@@ -41,12 +41,21 @@ static void fill_in_one_address(CephContext *cct,
   }
 
   char buf[INET6_ADDRSTRLEN];
-  const char *ok = inet_ntop(found->sa_family, found, buf, sizeof(buf));
-  if (!ok) {
-    string err = cpp_strerror(errno);
-    lderr(cct) << "unable to convert chosen address to string: " << err << dendl;
+  int err;
+
+  err = getnameinfo(found,
+                   (found->sa_family == AF_INET)
+                   ? sizeof(struct sockaddr_in)
+                   : sizeof(struct sockaddr_in6),
+
+                   buf, sizeof(buf),
+                   NULL, 0,
+                   NI_NUMERICHOST);
+  if (err != 0) {
+    lderr(cct) << "unable to convert chosen address to string: " << gai_strerror(err) << dendl;
     exit(1);
   }
+
   cct->_conf->set_val_or_die(conf_var, buf);
   cct->_conf->apply_changes(NULL);
 }