diff options
author | Geoff Garside <geoff@geoffgarside.co.uk> | 2011-06-18 14:47:38 +0100 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2013-07-08 15:57:22 +0200 |
commit | fa723d98d6d4a76a8c64fdc4324f2a25cd4c104b (patch) | |
tree | a97bfbde45c5b1d1f1c2041ef53ed24926610ef6 | |
parent | e7b34e8dc3569b5e1065013e46357b5085967cbe (diff) | |
download | redis-fa723d98d6d4a76a8c64fdc4324f2a25cd4c104b.tar.gz |
Update anetTcpAccept to handle AF_INET6 addresses.
Change the sockaddr_in to sockaddr_storage which is capable of storing
both AF_INET and AF_INET6 sockets. Uses the sockaddr_storage ss_family
to correctly return the printable IP address and port.
-rw-r--r-- | src/anet.c | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/anet.c b/src/anet.c index d75a4802a..0d6a358e3 100644 --- a/src/anet.c +++ b/src/anet.c @@ -435,13 +435,20 @@ static int anetGenericAccept(char *err, int s, struct sockaddr *sa, socklen_t *l int anetTcpAccept(char *err, int s, char *ip, size_t ip_len, int *port) { int fd; - struct sockaddr_in sa; + struct sockaddr_storage sa; socklen_t salen = sizeof(sa); if ((fd = anetGenericAccept(err,s,(struct sockaddr*)&sa,&salen)) == ANET_ERR) return ANET_ERR; - if (ip) inet_ntop(sa.sin_family,(void*)&(sa.sin_addr),ip,ip_len); - if (port) *port = ntohs(sa.sin_port); + if (sa.ss_family == AF_INET) { + struct sockaddr_in *s = (struct sockaddr_in *)&sa; + if (ip) inet_ntop(AF_INET,(void*)&(s->sin_addr),ip,ip_len); + if (port) *port = ntohs(s->sin_port); + } else { + struct sockaddr_in6 *s = (struct sockaddr_in6 *)&sa; + if (ip) inet_ntop(AF_INET6,(void*)&(s->sin6_addr),ip,ip_len); + if (port) *port = ntohs(s->sin6_port); + } return fd; } |