Skip to content

Commit

Permalink
Update anetTcpAccept to handle AF_INET6 addresses.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
geoffgarside authored and antirez committed Jul 8, 2013
1 parent e7b34e8 commit fa723d9
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/anet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit fa723d9

Please sign in to comment.