diff --git a/src/anet.c b/src/anet.c index 3e39751d..76e9b67a 100644 --- a/src/anet.c +++ b/src/anet.c @@ -589,20 +589,21 @@ error: return -1; } -int anetFormatIP(char *fmt, size_t fmt_len, char *ip, int port) { - if (port >= 0) - return snprintf(fmt,fmt_len, - strchr(ip,':') ? "[%s]:%d" : "%s:%d", ip, port); - else - return snprintf(fmt, fmt_len, strchr(ip,':') ? "[%s]" : "%s", ip); +/* 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); } -int anetFormatPeer(int fd, char *fmt, size_t fmt_len) { +/* 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 anetFormatIP(fmt, fmt_len, ip, port); + return anetFormatAddr(buf, buf_len, ip, port); } int anetSockName(int fd, char *ip, size_t ip_len, int *port) { @@ -632,5 +633,5 @@ int anetFormatSock(int fd, char *fmt, size_t fmt_len) { int port; anetSockName(fd,ip,sizeof(ip),&port); - return anetFormatIP(fmt, fmt_len, ip, port); + return anetFormatAddr(fmt, fmt_len, ip, port); } diff --git a/src/anet.h b/src/anet.h index 84675807..ea9c77f2 100644 --- a/src/anet.h +++ b/src/anet.h @@ -70,7 +70,7 @@ int anetSendTimeout(char *err, int fd, long long ms); int anetPeerToString(int fd, char *ip, size_t ip_len, int *port); int anetKeepAlive(char *err, int fd, int interval); int anetSockName(int fd, char *ip, size_t ip_len, int *port); -int anetFormatIP(char *fmt, size_t fmt_len, char *ip, int port); +int anetFormatAddr(char *fmt, size_t fmt_len, char *ip, int port); int anetFormatPeer(int fd, char *fmt, size_t fmt_len); int anetFormatSock(int fd, char *fmt, size_t fmt_len); diff --git a/src/cluster.c b/src/cluster.c index ef952186..d6c1c4c1 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1553,7 +1553,7 @@ int clusterProcessPacket(clusterLink *link) { { memcpy(myself->ip,ip,REDIS_IP_STR_LEN); - anetFormatIP(ip, sizeof(ip), myself->ip, -1); + anetFormatAddr(ip, sizeof(ip), myself->ip, -1); redisLog(REDIS_WARNING,"IP address for this node updated to %s", ip); clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG); diff --git a/src/redis-cli.c b/src/redis-cli.c index f143863d..2a703ad7 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -130,7 +130,7 @@ static void cliRefreshPrompt(void) { len = snprintf(config.prompt,sizeof(config.prompt),"redis %s", config.hostsocket); else - len = anetFormatIP(config.prompt, sizeof(config.prompt), + len = anetFormatAddr(config.prompt, sizeof(config.prompt), config.hostip, config.hostport); /* Add [dbnum] if needed */ if (config.dbnum != 0 && config.last_cmd_type != REDIS_REPLY_ERROR) diff --git a/src/replication.c b/src/replication.c index 46db9ea1..7e36c3e9 100644 --- a/src/replication.c +++ b/src/replication.c @@ -56,7 +56,7 @@ char *replicationGetSlaveName(redisClient *c) { buf[0] = '\0'; if (anetPeerToString(c->fd,ip,sizeof(ip),NULL) != -1) { if (c->slave_listening_port) - anetFormatIP(buf,sizeof(buf),ip,c->slave_listening_port); + anetFormatAddr(buf,sizeof(buf),ip,c->slave_listening_port); else snprintf(buf,sizeof(buf),"%s:",ip); } else { diff --git a/src/sentinel.c b/src/sentinel.c index afe01fed..89b80e0a 100644 --- a/src/sentinel.c +++ b/src/sentinel.c @@ -908,7 +908,7 @@ sentinelRedisInstance *createSentinelRedisInstance(char *name, int flags, char * /* For slaves and sentinel we use ip:port as name. */ if (flags & (SRI_SLAVE|SRI_SENTINEL)) { - anetFormatIP(slavename, sizeof(slavename), hostname, port); + anetFormatAddr(slavename, sizeof(slavename), hostname, port); name = slavename; }