mirror of
https://github.com/fluencelabs/redis
synced 2025-03-18 16:40:50 +00:00
Cleanup all IP formatting code
Instead of manually checking for strchr(n,':') everywhere, we can use our new centralized IP formatting functions.
This commit is contained in:
parent
2d90619f88
commit
491881e13b
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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]",
|
||||
|
@ -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:<unknown-slave-port>",ip);
|
||||
} else {
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user