diff options
Diffstat (limited to 'src/anet.c')
-rw-r--r-- | src/anet.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/anet.c b/src/anet.c index 1e5d85495..76e9b67ae 100644 --- a/src/anet.c +++ b/src/anet.c @@ -589,6 +589,23 @@ error: return -1; } +/* Format an IP,port pair into something easy to parse. If IP is IPv6 + * (matches for ":"), the ip is surrounded by []. IP and port are just + * separated by colons. This the standard to display addresses within Redis. */ +int anetFormatAddr(char *buf, size_t buf_len, char *ip, int port) { + return snprintf(buf,buf_len, strchr(ip,':') ? + "[%s]:%d" : "%s:%d", ip, port); +} + +/* Like anetFormatAddr() but extract ip and port from the socket's peer. */ +int anetFormatPeer(int fd, char *buf, size_t buf_len) { + char ip[INET6_ADDRSTRLEN]; + int port; + + anetPeerToString(fd,ip,sizeof(ip),&port); + return anetFormatAddr(buf, buf_len, ip, port); +} + int anetSockName(int fd, char *ip, size_t ip_len, int *port) { struct sockaddr_storage sa; socklen_t salen = sizeof(sa); @@ -610,3 +627,11 @@ int anetSockName(int fd, char *ip, size_t ip_len, int *port) { } return 0; } + +int anetFormatSock(int fd, char *fmt, size_t fmt_len) { + char ip[INET6_ADDRSTRLEN]; + int port; + + anetSockName(fd,ip,sizeof(ip),&port); + return anetFormatAddr(fmt, fmt_len, ip, port); +} |