mirror of
https://github.com/fluencelabs/redis
synced 2025-03-19 17:10: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))
|
strcmp(ip,myself->ip))
|
||||||
{
|
{
|
||||||
memcpy(myself->ip,ip,REDIS_IP_STR_LEN);
|
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",
|
redisLog(REDIS_WARNING,"IP address for this node updated to %s",
|
||||||
myself->ip);
|
ip);
|
||||||
clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);
|
clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1219,17 +1219,6 @@ void getClientsMaxBuffers(unsigned long *longest_output_list,
|
|||||||
*biggest_input_buffer = bib;
|
*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.
|
/* 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 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".
|
* 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
|
* A Peer ID always fits inside a buffer of REDIS_PEER_ID_LEN bytes, including
|
||||||
* the null term.
|
* 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
|
* On failure the function still populates 'peerid' with the "?:0" string
|
||||||
* in case you want to relax error checking or need to display something
|
* in case you want to relax error checking or need to display something
|
||||||
* anyway (see anetPeerToString implementation for more info). */
|
* anyway (see anetPeerToString implementation for more info). */
|
||||||
int genClientPeerId(redisClient *client, char *peerid, size_t peerid_len) {
|
static void genClientPeerId(redisClient *client, char *peerid,
|
||||||
char ip[REDIS_IP_STR_LEN];
|
size_t peerid_len) {
|
||||||
int port;
|
|
||||||
|
|
||||||
if (client->flags & REDIS_UNIX_SOCKET) {
|
if (client->flags & REDIS_UNIX_SOCKET) {
|
||||||
/* Unix socket client. */
|
/* Unix socket client. */
|
||||||
snprintf(peerid,peerid_len,"%s:0",server.unixsocket);
|
snprintf(peerid,peerid_len,"%s:0",server.unixsocket);
|
||||||
return REDIS_OK;
|
|
||||||
} else {
|
} else {
|
||||||
/* TCP client. */
|
/* TCP client. */
|
||||||
int retval = anetPeerToString(client->fd,ip,sizeof(ip),&port);
|
anetFormatPeer(client->fd,peerid,peerid_len);
|
||||||
formatPeerId(peerid,peerid_len,ip,port);
|
|
||||||
return (retval == -1) ? REDIS_ERR : REDIS_OK;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,8 +130,7 @@ static void cliRefreshPrompt(void) {
|
|||||||
len = snprintf(config.prompt,sizeof(config.prompt),"redis %s",
|
len = snprintf(config.prompt,sizeof(config.prompt),"redis %s",
|
||||||
config.hostsocket);
|
config.hostsocket);
|
||||||
else
|
else
|
||||||
len = snprintf(config.prompt,sizeof(config.prompt),
|
len = anetFormatIP(config.prompt, sizeof(config.prompt),
|
||||||
strchr(config.hostip,':') ? "[%s]:%d" : "%s:%d",
|
|
||||||
config.hostip, config.hostport);
|
config.hostip, config.hostport);
|
||||||
/* Add [dbnum] if needed */
|
/* Add [dbnum] if needed */
|
||||||
if (config.dbnum != 0 && config.last_cmd_type != REDIS_REPLY_ERROR)
|
if (config.dbnum != 0 && config.last_cmd_type != REDIS_REPLY_ERROR)
|
||||||
|
@ -56,7 +56,7 @@ char *replicationGetSlaveName(redisClient *c) {
|
|||||||
buf[0] = '\0';
|
buf[0] = '\0';
|
||||||
if (anetPeerToString(c->fd,ip,sizeof(ip),NULL) != -1) {
|
if (anetPeerToString(c->fd,ip,sizeof(ip),NULL) != -1) {
|
||||||
if (c->slave_listening_port)
|
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
|
else
|
||||||
snprintf(buf,sizeof(buf),"%s:<unknown-slave-port>",ip);
|
snprintf(buf,sizeof(buf),"%s:<unknown-slave-port>",ip);
|
||||||
} else {
|
} else {
|
||||||
|
@ -897,7 +897,7 @@ sentinelRedisInstance *createSentinelRedisInstance(char *name, int flags, char *
|
|||||||
sentinelRedisInstance *ri;
|
sentinelRedisInstance *ri;
|
||||||
sentinelAddr *addr;
|
sentinelAddr *addr;
|
||||||
dict *table = NULL;
|
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|SRI_SLAVE|SRI_SENTINEL));
|
||||||
redisAssert((flags & SRI_MASTER) || master != NULL);
|
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. */
|
/* For slaves and sentinel we use ip:port as name. */
|
||||||
if (flags & (SRI_SLAVE|SRI_SENTINEL)) {
|
if (flags & (SRI_SLAVE|SRI_SENTINEL)) {
|
||||||
snprintf(slavename,sizeof(slavename),
|
anetFormatIP(slavename, sizeof(slavename), hostname, port);
|
||||||
strchr(hostname,':') ? "[%s]:%d" : "%s:%d",
|
|
||||||
hostname,port);
|
|
||||||
name = slavename;
|
name = slavename;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1035,9 +1033,7 @@ sentinelRedisInstance *sentinelRedisInstanceLookupSlave(
|
|||||||
sentinelRedisInstance *slave;
|
sentinelRedisInstance *slave;
|
||||||
|
|
||||||
redisAssert(ri->flags & SRI_MASTER);
|
redisAssert(ri->flags & SRI_MASTER);
|
||||||
key = sdscatprintf(sdsempty(),
|
key = sdsformatip(ip, port);
|
||||||
strchr(ip,':') ? "[%s]:%d" : "%s:%d",
|
|
||||||
ip,port);
|
|
||||||
slave = dictFetchValue(ri->slaves,key);
|
slave = dictFetchValue(ri->slaves,key);
|
||||||
sdsfree(key);
|
sdsfree(key);
|
||||||
return slave;
|
return slave;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user