diff options
author | Viktor Söderqvist <viktor.soderqvist@est.tech> | 2022-06-26 13:34:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-26 14:34:59 +0300 |
commit | 6272ca609e67d1f35dda50a82027d7401892e383 (patch) | |
tree | f584684b145807e3c5f0da6f0f3293e2b65ee461 /src/networking.c | |
parent | d96cf3639a371574c212d87c70e5a80214fc721f (diff) | |
download | redis-6272ca609e67d1f35dda50a82027d7401892e383.tar.gz |
Add RM_SetClientNameById and RM_GetClientNameById (#10839)
Adding Module APIs to let the module read and set the client name of an arbitrary connection.
Diffstat (limited to 'src/networking.c')
-rw-r--r-- | src/networking.c | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/src/networking.c b/src/networking.c index 44aa50642..8cace10d4 100644 --- a/src/networking.c +++ b/src/networking.c @@ -2832,18 +2832,9 @@ sds getAllClientsInfoString(int type) { return o; } -/* This function implements CLIENT SETNAME, including replying to the - * user with an error if the charset is wrong (in that case C_ERR is - * returned). If the function succeeded C_OK is returned, and it's up - * to the caller to send a reply if needed. - * - * Setting an empty string as name has the effect of unsetting the - * currently set name: the client will remain unnamed. - * - * This function is also used to implement the HELLO SETNAME option. */ -int clientSetNameOrReply(client *c, robj *name) { - int len = sdslen(name->ptr); - char *p = name->ptr; +/* Returns C_OK if the name has been set or C_ERR if the name is invalid. */ +int clientSetName(client *c, robj *name) { + int len = (name != NULL) ? sdslen(name->ptr) : 0; /* Setting the client name to an empty string actually removes * the current name. */ @@ -2856,11 +2847,9 @@ int clientSetNameOrReply(client *c, robj *name) { /* Otherwise check if the charset is ok. We need to do this otherwise * CLIENT LIST format will break. You should always be able to * split by space to get the different fields. */ + char *p = name->ptr; for (int j = 0; j < len; j++) { if (p[j] < '!' || p[j] > '~') { /* ASCII is assumed. */ - addReplyError(c, - "Client names cannot contain spaces, " - "newlines or special characters."); return C_ERR; } } @@ -2870,6 +2859,25 @@ int clientSetNameOrReply(client *c, robj *name) { return C_OK; } +/* This function implements CLIENT SETNAME, including replying to the + * user with an error if the charset is wrong (in that case C_ERR is + * returned). If the function succeeded C_OK is returned, and it's up + * to the caller to send a reply if needed. + * + * Setting an empty string as name has the effect of unsetting the + * currently set name: the client will remain unnamed. + * + * This function is also used to implement the HELLO SETNAME option. */ +int clientSetNameOrReply(client *c, robj *name) { + int result = clientSetName(c, name); + if (result == C_ERR) { + addReplyError(c, + "Client names cannot contain spaces, " + "newlines or special characters."); + } + return result; +} + /* Reset the client state to resemble a newly connected client. */ void resetCommand(client *c) { |