From 491881e13b73ab1686328efdceb4cdf8bde64046 Mon Sep 17 00:00:00 2001 From: Matt Stancliff Date: Thu, 23 Oct 2014 13:09:58 -0400 Subject: [PATCH] Cleanup all IP formatting code Instead of manually checking for strchr(n,':') everywhere, we can use our new centralized IP formatting functions. --- src/cluster.c | 4 +++- src/networking.c | 24 +++--------------------- src/redis-cli.c | 5 ++--- src/replication.c | 2 +- src/sentinel.c | 10 +++------- 5 files changed, 12 insertions(+), 33 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index bb688425..ef952186 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1552,8 +1552,10 @@ int clusterProcessPacket(clusterLink *link) { strcmp(ip,myself->ip)) { memcpy(myself->ip,ip,REDIS_IP_STR_LEN); + + anetFormatIP(ip, sizeof(ip), myself->ip, -1); redisLog(REDIS_WARNING,"IP address for this node updated to %s", - myself->ip); + ip); clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG); } } diff --git a/src/networking.c b/src/networking.c index 5a780a59..71233ce2 100644 --- a/src/networking.c +++ b/src/networking.c @@ -1219,17 +1219,6 @@ void getClientsMaxBuffers(unsigned long *longest_output_list, *biggest_input_buffer = bib; } -/* This is a helper function for genClientPeerId(). - * It writes the specified ip/port to "peerid" as a null termiated string - * in the form ip:port if ip does not contain ":" itself, otherwise - * [ip]:port format is used (for IPv6 addresses basically). */ -void formatPeerId(char *peerid, size_t peerid_len, char *ip, int port) { - if (strchr(ip,':')) - snprintf(peerid,peerid_len,"[%s]:%d",ip,port); - else - snprintf(peerid,peerid_len,"%s:%d",ip,port); -} - /* A Redis "Peer ID" is a colon separated ip:port pair. * For IPv4 it's in the form x.y.z.k:port, example: "127.0.0.1:1234". * For IPv6 addresses we use [] around the IP part, like in "[::1]:1234". @@ -1238,24 +1227,17 @@ void formatPeerId(char *peerid, size_t peerid_len, char *ip, int port) { * A Peer ID always fits inside a buffer of REDIS_PEER_ID_LEN bytes, including * the null term. * - * The function returns REDIS_OK on succcess, and REDIS_ERR on failure. - * * On failure the function still populates 'peerid' with the "?:0" string * in case you want to relax error checking or need to display something * anyway (see anetPeerToString implementation for more info). */ -int genClientPeerId(redisClient *client, char *peerid, size_t peerid_len) { - char ip[REDIS_IP_STR_LEN]; - int port; - +static void genClientPeerId(redisClient *client, char *peerid, + size_t peerid_len) { if (client->flags & REDIS_UNIX_SOCKET) { /* Unix socket client. */ snprintf(peerid,peerid_len,"%s:0",server.unixsocket); - return REDIS_OK; } else { /* TCP client. */ - int retval = anetPeerToString(client->fd,ip,sizeof(ip),&port); - formatPeerId(peerid,peerid_len,ip,port); - return (retval == -1) ? REDIS_ERR : REDIS_OK; + anetFormatPeer(client->fd,peerid,peerid_len); } } diff --git a/src/redis-cli.c b/src/redis-cli.c index 51b4ccc2..f143863d 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -130,9 +130,8 @@ static void cliRefreshPrompt(void) { len = snprintf(config.prompt,sizeof(config.prompt),"redis %s", config.hostsocket); else - len = snprintf(config.prompt,sizeof(config.prompt), - strchr(config.hostip,':') ? "[%s]:%d" : "%s:%d", - config.hostip, config.hostport); + len = anetFormatIP(config.prompt, sizeof(config.prompt), + config.hostip, config.hostport); /* Add [dbnum] if needed */ if (config.dbnum != 0 && config.last_cmd_type != REDIS_REPLY_ERROR) len += snprintf(config.prompt+len,sizeof(config.prompt)-len,"[%d]", diff --git a/src/replication.c b/src/replication.c index 41f67625..46db9ea1 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) - snprintf(buf,sizeof(buf),"%s:%d",ip,c->slave_listening_port); + anetFormatIP(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 12f15ff3..afe01fed 100644 --- a/src/sentinel.c +++ b/src/sentinel.c @@ -897,7 +897,7 @@ sentinelRedisInstance *createSentinelRedisInstance(char *name, int flags, char * sentinelRedisInstance *ri; sentinelAddr *addr; dict *table = NULL; - char slavename[128], *sdsname; + char slavename[REDIS_PEER_ID_LEN], *sdsname; redisAssert(flags & (SRI_MASTER|SRI_SLAVE|SRI_SENTINEL)); redisAssert((flags & SRI_MASTER) || master != NULL); @@ -908,9 +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)) { - snprintf(slavename,sizeof(slavename), - strchr(hostname,':') ? "[%s]:%d" : "%s:%d", - hostname,port); + anetFormatIP(slavename, sizeof(slavename), hostname, port); name = slavename; } @@ -1035,9 +1033,7 @@ sentinelRedisInstance *sentinelRedisInstanceLookupSlave( sentinelRedisInstance *slave; redisAssert(ri->flags & SRI_MASTER); - key = sdscatprintf(sdsempty(), - strchr(ip,':') ? "[%s]:%d" : "%s:%d", - ip,port); + key = sdsformatip(ip, port); slave = dictFetchValue(ri->slaves,key); sdsfree(key); return slave;