X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/7d3ee4172f52bf94185960799c1d12c16ed96ff7..d310fbedabd3101505b694f5c25a2e48480a3c2b:/src/anet.c diff --git a/src/anet.c b/src/anet.c index 434d945c..4b52425c 100644 --- a/src/anet.c +++ b/src/anet.c @@ -367,3 +367,18 @@ int anetPeerToString(int fd, char *ip, int *port) { if (port) *port = ntohs(sa.sin_port); return 0; } + +int anetSockName(int fd, char *ip, int *port) { + struct sockaddr_in sa; + socklen_t salen = sizeof(sa); + + if (getsockname(fd,(struct sockaddr*)&sa,&salen) == -1) { + *port = 0; + ip[0] = '?'; + ip[1] = '\0'; + return -1; + } + if (ip) strcpy(ip,inet_ntoa(sa.sin_addr)); + if (port) *port = ntohs(sa.sin_port); + return 0; +}