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