socket_util: unpack V4MAPPED addresses

Unpack IPv4 addresses which are packed inside an IPv6 address,
i.e. return "127.0.0.1" rather than "::ffff:127.0.0.1".
This commit is contained in:
Max Kellermann 2009-02-28 15:20:33 +01:00
parent b55d9fcdb8
commit e085deb944

View File

@ -29,9 +29,29 @@
char *
sockaddr_to_string(const struct sockaddr *sa, size_t length, GError **error)
{
#ifdef HAVE_IPV6
const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *)sa;
struct sockaddr_in a4;
#endif
int ret;
char host[NI_MAXHOST], serv[NI_MAXSERV];
#ifdef HAVE_IPV6
if (sa->sa_family == AF_INET6 &&
IN6_IS_ADDR_V4MAPPED(&a6->sin6_addr)) {
/* convert "::ffff:127.0.0.1" to "127.0.0.1" */
memset(&a4, 0, sizeof(a4));
a4.sin_family = AF_INET;
memcpy(&a4.sin_addr, ((const char *)&a6->sin6_addr) + 12,
sizeof(a4.sin_addr));
a4.sin_port = a6->sin6_port;
sa = (const struct sockaddr *)&a4;
length = sizeof(a4);
}
#endif
ret = getnameinfo(sa, length, host, sizeof(host), serv, sizeof(serv),
NI_NUMERICHOST|NI_NUMERICSERV);
if (ret != 0) {