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.
This commit is contained in:
Geoff Garside 2011-06-18 14:47:38 +01:00 committed by antirez
parent e7b34e8dc3
commit fa723d98d6

View File

@ -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 anetTcpAccept(char *err, int s, char *ip, size_t ip_len, int *port) {
int fd; int fd;
struct sockaddr_in sa; struct sockaddr_storage sa;
socklen_t salen = sizeof(sa); socklen_t salen = sizeof(sa);
if ((fd = anetGenericAccept(err,s,(struct sockaddr*)&sa,&salen)) == ANET_ERR) if ((fd = anetGenericAccept(err,s,(struct sockaddr*)&sa,&salen)) == ANET_ERR)
return ANET_ERR; return ANET_ERR;
if (ip) inet_ntop(sa.sin_family,(void*)&(sa.sin_addr),ip,ip_len); if (sa.ss_family == AF_INET) {
if (port) *port = ntohs(sa.sin_port); 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; return fd;
} }