From e7b34e8dc3569b5e1065013e46357b5085967cbe Mon Sep 17 00:00:00 2001 From: Geoff Garside Date: Sat, 18 Jun 2011 13:25:31 +0100 Subject: [PATCH] Update anetResolve to resolve AF_INET6 as well. Change the getaddrinfo(3) hints family from AF_INET to AF_UNSPEC to allow resolution of IPv6 addresses as well as IPv4 addresses. The function will return the IP address of whichever address family is preferenced by the operating system. Most current operating systems will preference AF_INET6 over AF_INET. Unfortunately without attempting to establish a connection to the remote address we can't know if the host is capable of using the returned IP address. It might be desirable to have anetResolve accept an additional argument specifying the AF_INET/AF_INET6 address the caller would like to receive. Currently though it does not appear as though the anetResolve function is ever used within Redis. --- src/anet.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/anet.c b/src/anet.c index 358802b1..d75a4802 100644 --- a/src/anet.c +++ b/src/anet.c @@ -166,11 +166,11 @@ int anetTcpKeepAlive(char *err, int fd) int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len) { struct addrinfo hints, *info; - void *addr; int rv; memset(&hints,0,sizeof(hints)); - hints.ai_family = AF_INET; + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; /* specify socktype to avoid dups */ if ((rv = getaddrinfo(host, NULL, &hints, &info)) != 0) { anetSetError(err, "%s", gai_strerror(rv)); @@ -178,10 +178,12 @@ int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len) } if (info->ai_family == AF_INET) { struct sockaddr_in *sa = (struct sockaddr_in *)info->ai_addr; - addr = &(sa->sin_addr); + inet_ntop(AF_INET, &(sa->sin_addr), ipbuf, ipbuf_len); + } else { + struct sockaddr_in6 *sa = (struct sockaddr_in6 *)info->ai_addr; + inet_ntop(AF_INET6, &(sa->sin6_addr), ipbuf, ipbuf_len); } - inet_ntop(info->ai_family, addr, ipbuf, ipbuf_len); freeaddrinfo(info); return ANET_OK; } -- GitLab