common/pick_address: Fix IP address stringification.

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>
This commit is contained in:
Tommi Virtanen 2011-11-22 17:48:40 -08:00 committed by Sage Weil
parent 9870e2f77b
commit 414caa7d15

View 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);
}