mirror of
https://github.com/fluencelabs/redis
synced 2025-03-19 17:10:50 +00:00
Use getaddrinfo(3) in anetResolve. #apichange
Change anetResolve() function to use getaddrinfo(3) to resolve hostnames. Resolved hostnames are limited to those reachable by the AF_INET address family. API Change: anetResolve requires additional argument. additional argument required to specify the length of the character buffer the IP address is written to in order to comply with inet_ntop(3) function semantics. inet_ntop(3) replaces inet_ntoa(3) as it has been designed to be compatible with more address families.
This commit is contained in:
parent
64b3b9b1d4
commit
071963c855
24
src/anet.c
24
src/anet.c
@ -163,22 +163,26 @@ int anetTcpKeepAlive(char *err, int fd)
|
|||||||
return ANET_OK;
|
return ANET_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
int anetResolve(char *err, char *host, char *ipbuf)
|
int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len)
|
||||||
{
|
{
|
||||||
struct sockaddr_in sa;
|
struct addrinfo hints, *info;
|
||||||
|
void *addr;
|
||||||
|
int rv;
|
||||||
|
|
||||||
sa.sin_family = AF_INET;
|
memset(&hints,0,sizeof(hints));
|
||||||
if (inet_aton(host, &sa.sin_addr) == 0) {
|
hints.ai_family = AF_INET;
|
||||||
struct hostent *he;
|
|
||||||
|
|
||||||
he = gethostbyname(host);
|
if ((rv = getaddrinfo(host, NULL, &hints, &info)) != 0) {
|
||||||
if (he == NULL) {
|
anetSetError(err, "%s", gai_strerror(rv));
|
||||||
anetSetError(err, "can't resolve: %s", host);
|
|
||||||
return ANET_ERR;
|
return ANET_ERR;
|
||||||
}
|
}
|
||||||
memcpy(&sa.sin_addr, he->h_addr, sizeof(struct in_addr));
|
if (info->ai_family == AF_INET) {
|
||||||
|
struct sockaddr_in *sa = (struct sockaddr_in *)info->ai_addr;
|
||||||
|
addr = &(sa->sin_addr);
|
||||||
}
|
}
|
||||||
strcpy(ipbuf,inet_ntoa(sa.sin_addr));
|
|
||||||
|
inet_ntop(info->ai_family, addr, ipbuf, ipbuf_len);
|
||||||
|
freeaddrinfo(info);
|
||||||
return ANET_OK;
|
return ANET_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ int anetTcpNonBlockConnect(char *err, char *addr, int port);
|
|||||||
int anetUnixConnect(char *err, char *path);
|
int anetUnixConnect(char *err, char *path);
|
||||||
int anetUnixNonBlockConnect(char *err, char *path);
|
int anetUnixNonBlockConnect(char *err, char *path);
|
||||||
int anetRead(int fd, char *buf, int count);
|
int anetRead(int fd, char *buf, int count);
|
||||||
int anetResolve(char *err, char *host, char *ipbuf);
|
int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len);
|
||||||
int anetTcpServer(char *err, int port, char *bindaddr);
|
int anetTcpServer(char *err, int port, char *bindaddr);
|
||||||
int anetUnixServer(char *err, char *path, mode_t perm);
|
int anetUnixServer(char *err, char *path, mode_t perm);
|
||||||
int anetTcpAccept(char *err, int serversock, char *ip, int *port);
|
int anetTcpAccept(char *err, int serversock, char *ip, int *port);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user