summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeoff Garside <geoff@geoffgarside.co.uk>2011-06-18 13:25:31 +0100
committerantirez <antirez@gmail.com>2013-07-11 17:05:12 +0200
commit77ddec8582c1c7b9d8f406f166ce83676ebd2e7d (patch)
tree4aaeae8f11fe060f5f695064526e85f5cbf5c0d0
parent4b2e374e4a35d3e0173b4bcabe2f985d4ae6202f (diff)
downloadredis-77ddec8582c1c7b9d8f406f166ce83676ebd2e7d.tar.gz
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.
-rw-r--r--src/anet.c10
1 files changed, 6 insertions, 4 deletions
diff --git a/src/anet.c b/src/anet.c
index 358802b1c..d75a4802a 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;
}