diff --git a/src/anet.c b/src/anet.c index 50b6e317..53656eca 100644 --- a/src/anet.c +++ b/src/anet.c @@ -529,12 +529,9 @@ int anetPeerToString(int fd, char *ip, size_t ip_len, int *port) { struct sockaddr_storage sa; socklen_t salen = sizeof(sa); - if (getpeername(fd,(struct sockaddr*)&sa,&salen) == -1) { - if (port) *port = 0; - ip[0] = '?'; - ip[1] = '\0'; - return -1; - } + if (getpeername(fd,(struct sockaddr*)&sa,&salen) == -1) goto error; + if (ip_len == 0) goto error; + 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); @@ -543,11 +540,25 @@ int anetPeerToString(int fd, char *ip, size_t ip_len, int *port) { 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); - } else { - if (ip) ip[0] = '\0'; + } else if (sa.ss_family == AF_UNIX) { + if (ip) strncpy(ip,"unixsocket",ip_len); if (port) *port = 0; + } else { + goto error; } return 0; + +error: + if (ip) { + if (ip_len >= 2) { + ip[0] = '?'; + ip[1] = '\0'; + } else if (ip_len == 1) { + ip[0] = '\0'; + } + } + if (port) *port = 0; + return -1; } int anetSockName(int fd, char *ip, size_t ip_len, int *port) {