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-08 15:57:22 +0200
commite7b34e8dc3569b5e1065013e46357b5085967cbe (patch)
tree53f1c9ff00a4d477edb48cee05f047fabf64b142
parent2345cee33572cc5485f0b8ea190c1d8cb226bc3f (diff)
downloadredis-e7b34e8dc3569b5e1065013e46357b5085967cbe.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;
}