Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
e4c019e7
Commit
e4c019e7
authored
Jul 09, 2013
by
antirez
Browse files
getClientPeerId() now reports errors.
We now also use it in CLIENT KILL implementation.
parent
5cdc5da9
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/networking.c
View file @
e4c019e7
...
@@ -1134,25 +1134,27 @@ void getClientsMaxBuffers(unsigned long *longest_output_list,
...
@@ -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
* A Peer ID always fits inside a buffer of REDIS_PEER_ID_LEN bytes, including
* the null term.
* the null term.
*
*
* The function is always successful, but if the IP or port can't be extracted
* The function returns REDIS_OK on succcess, and REDIS_ERR on failure.
* for some reason, "?" and "0" are used (this is the semantics of
*
* anetPeerToString() from anet.c). In practical terms this should never
* On failure the function still populates 'peerid' with the "?:0" string
* happen. */
* in case you want to relax error checking or need to display something
void
getClientPeerId
(
redisClient
*
client
,
char
*
peerid
,
size_t
peerid_len
)
{
* anyway (see anetPeerToString implementation for more info). */
int
getClientPeerId
(
redisClient
*
client
,
char
*
peerid
,
size_t
peerid_len
)
{
char
ip
[
REDIS_IP_STR_LEN
];
char
ip
[
REDIS_IP_STR_LEN
];
int
port
;
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
;
return
REDIS_OK
;
}
else
{
}
else
{
/* TCP client. */
/* TCP client. */
anetPeerToString
(
client
->
fd
,
ip
,
sizeof
(
ip
),
&
port
);
int
retval
=
anetPeerToString
(
client
->
fd
,
ip
,
sizeof
(
ip
),
&
port
);
if
(
strchr
(
ip
,
':'
))
if
(
strchr
(
ip
,
':'
))
snprintf
(
peerid
,
peerid_len
,
"[%s]:%d"
,
ip
,
port
);
snprintf
(
peerid
,
peerid_len
,
"[%s]:%d"
,
ip
,
port
);
else
else
snprintf
(
peerid
,
peerid_len
,
"%s:%d"
,
ip
,
port
);
snprintf
(
peerid
,
peerid_len
,
"%s:%d"
,
ip
,
port
);
return
(
retval
==
-
1
)
?
REDIS_ERR
:
REDIS_OK
;
}
}
}
}
...
@@ -1237,14 +1239,12 @@ void clientCommand(redisClient *c) {
...
@@ -1237,14 +1239,12 @@ void clientCommand(redisClient *c) {
}
else
if
(
!
strcasecmp
(
c
->
argv
[
1
]
->
ptr
,
"kill"
)
&&
c
->
argc
==
3
)
{
}
else
if
(
!
strcasecmp
(
c
->
argv
[
1
]
->
ptr
,
"kill"
)
&&
c
->
argc
==
3
)
{
listRewind
(
server
.
clients
,
&
li
);
listRewind
(
server
.
clients
,
&
li
);
while
((
ln
=
listNext
(
&
li
))
!=
NULL
)
{
while
((
ln
=
listNext
(
&
li
))
!=
NULL
)
{
char
ip
[
REDIS_IP_STR_LEN
],
addr
[
REDIS_IP_STR_LEN
+
64
];
char
peerid
[
REDIS_PEER_ID_LEN
];
int
port
;
client
=
listNodeValue
(
ln
);
client
=
listNodeValue
(
ln
);
if
(
anetPeerToString
(
client
->
fd
,
ip
,
sizeof
(
ip
),
&
port
)
==
-
1
)
continue
;
if
(
getClientPeerId
(
client
,
peerid
,
sizeof
(
peerid
))
==
REDIS_ERR
)
/* IPV6: might want to wrap a v6 address in [] */
continue
;
snprintf
(
addr
,
sizeof
(
addr
),
"%s:%d"
,
ip
,
port
);
if
(
strcmp
(
peerid
,
c
->
argv
[
2
]
->
ptr
)
==
0
)
{
if
(
strcmp
(
addr
,
c
->
argv
[
2
]
->
ptr
)
==
0
)
{
addReply
(
c
,
shared
.
ok
);
addReply
(
c
,
shared
.
ok
);
if
(
c
==
client
)
{
if
(
c
==
client
)
{
client
->
flags
|=
REDIS_CLOSE_AFTER_REPLY
;
client
->
flags
|=
REDIS_CLOSE_AFTER_REPLY
;
...
...
src/redis.h
View file @
e4c019e7
...
@@ -1073,7 +1073,7 @@ void copyClientOutputBuffer(redisClient *dst, redisClient *src);
...
@@ -1073,7 +1073,7 @@ void copyClientOutputBuffer(redisClient *dst, redisClient *src);
void
*
dupClientReplyValue
(
void
*
o
);
void
*
dupClientReplyValue
(
void
*
o
);
void
getClientsMaxBuffers
(
unsigned
long
*
longest_output_list
,
void
getClientsMaxBuffers
(
unsigned
long
*
longest_output_list
,
unsigned
long
*
biggest_input_buffer
);
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
getClientInfoString
(
redisClient
*
client
);
sds
getAllClientsInfoString
(
void
);
sds
getAllClientsInfoString
(
void
);
void
rewriteClientCommandVector
(
redisClient
*
c
,
int
argc
,
...);
void
rewriteClientCommandVector
(
redisClient
*
c
,
int
argc
,
...);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment