mirror of
https://github.com/fluencelabs/redis
synced 2025-03-19 17:10:50 +00:00
Update anetPeerToString to handle AF_INET6 addrs.
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:
parent
fa723d98d6
commit
23f4d905ce
26
src/anet.c
26
src/anet.c
@ -463,7 +463,7 @@ int anetUnixAccept(char *err, int s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int anetPeerToString(int fd, char *ip, size_t ip_len, int *port) {
|
int anetPeerToString(int fd, char *ip, size_t ip_len, int *port) {
|
||||||
struct sockaddr_in sa;
|
struct sockaddr_storage sa;
|
||||||
socklen_t salen = sizeof(sa);
|
socklen_t salen = sizeof(sa);
|
||||||
|
|
||||||
if (getpeername(fd,(struct sockaddr*)&sa,&salen) == -1) {
|
if (getpeername(fd,(struct sockaddr*)&sa,&salen) == -1) {
|
||||||
@ -472,13 +472,20 @@ int anetPeerToString(int fd, char *ip, size_t ip_len, int *port) {
|
|||||||
ip[1] = '\0';
|
ip[1] = '\0';
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
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 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int anetSockName(int fd, char *ip, size_t ip_len, int *port) {
|
int anetSockName(int fd, char *ip, size_t ip_len, int *port) {
|
||||||
struct sockaddr_in sa;
|
struct sockaddr_storage sa;
|
||||||
socklen_t salen = sizeof(sa);
|
socklen_t salen = sizeof(sa);
|
||||||
|
|
||||||
if (getsockname(fd,(struct sockaddr*)&sa,&salen) == -1) {
|
if (getsockname(fd,(struct sockaddr*)&sa,&salen) == -1) {
|
||||||
@ -487,7 +494,14 @@ int anetSockName(int fd, char *ip, size_t ip_len, int *port) {
|
|||||||
ip[1] = '\0';
|
ip[1] = '\0';
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
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 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user