提交 e7b34e8d 编写于 作者: G Geoff Garside 提交者: antirez

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.
上级 2345cee3
......@@ -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;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册