Skip to content

Commit

Permalink
getClientPeerId() now reports errors.
Browse files Browse the repository at this point in the history
We now also use it in CLIENT KILL implementation.
  • Loading branch information
antirez committed Jul 9, 2013
1 parent 5cdc5da commit e4c019e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions src/networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -1134,25 +1134,27 @@ void getClientsMaxBuffers(unsigned long *longest_output_list,
* A Peer ID always fits inside a buffer of REDIS_PEER_ID_LEN bytes, including
* the null term.
*
* The function is always successful, but if the IP or port can't be extracted
* for some reason, "?" and "0" are used (this is the semantics of
* anetPeerToString() from anet.c). In practical terms this should never
* happen. */
void getClientPeerId(redisClient *client, char *peerid, size_t peerid_len) {
* 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 getClientPeerId(redisClient *client, char *peerid, size_t peerid_len) {
char ip[REDIS_IP_STR_LEN];
int port;

if (client->flags & REDIS_UNIX_SOCKET) {
/* Unix socket client. */
snprintf(peerid,peerid_len,"%s:0",server.unixsocket);
return;
return REDIS_OK;
} else {
/* TCP client. */
anetPeerToString(client->fd,ip,sizeof(ip),&port);
int retval = anetPeerToString(client->fd,ip,sizeof(ip),&port);
if (strchr(ip,':'))
snprintf(peerid,peerid_len,"[%s]:%d",ip,port);
else
snprintf(peerid,peerid_len,"%s:%d",ip,port);
return (retval == -1) ? REDIS_ERR : REDIS_OK;
}
}

Expand Down Expand Up @@ -1237,14 +1239,12 @@ void clientCommand(redisClient *c) {
} else if (!strcasecmp(c->argv[1]->ptr,"kill") && c->argc == 3) {
listRewind(server.clients,&li);
while ((ln = listNext(&li)) != NULL) {
char ip[REDIS_IP_STR_LEN], addr[REDIS_IP_STR_LEN+64];
int port;
char peerid[REDIS_PEER_ID_LEN];

client = listNodeValue(ln);
if (anetPeerToString(client->fd,ip,sizeof(ip),&port) == -1) continue;
/* IPV6: might want to wrap a v6 address in [] */
snprintf(addr,sizeof(addr),"%s:%d",ip,port);
if (strcmp(addr,c->argv[2]->ptr) == 0) {
if (getClientPeerId(client,peerid,sizeof(peerid)) == REDIS_ERR)
continue;
if (strcmp(peerid,c->argv[2]->ptr) == 0) {
addReply(c,shared.ok);
if (c == client) {
client->flags |= REDIS_CLOSE_AFTER_REPLY;
Expand Down
2 changes: 1 addition & 1 deletion src/redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ void copyClientOutputBuffer(redisClient *dst, redisClient *src);
void *dupClientReplyValue(void *o);
void getClientsMaxBuffers(unsigned long *longest_output_list,
unsigned long *biggest_input_buffer);
void getClientPeerId(redisClient *client, char *peerid, size_t peerid_len);
int getClientPeerId(redisClient *client, char *peerid, size_t peerid_len);
sds getClientInfoString(redisClient *client);
sds getAllClientsInfoString(void);
void rewriteClientCommandVector(redisClient *c, int argc, ...);
Expand Down

0 comments on commit e4c019e

Please sign in to comment.