From: Michael Wallner Date: Fri, 23 Oct 2020 11:44:55 +0000 (+0200) Subject: src/libmemcachedprotocol: apply clang-format X-Git-Tag: 1.1.0-beta1~174 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=f48bae7d4b6a832b0d3a71812125770f09c76239;p=awesomized%2Flibmemcached src/libmemcachedprotocol: apply clang-format --- diff --git a/src/libmemcachedprotocol/ascii_handler.c b/src/libmemcachedprotocol/ascii_handler.c index 44c70e0d..83a71828 100644 --- a/src/libmemcachedprotocol/ascii_handler.c +++ b/src/libmemcachedprotocol/ascii_handler.c @@ -1,38 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcachedprotocol/common.h" @@ -42,82 +21,43 @@ #include #include +static void print_ascii_command(memcached_protocol_client_st *client) { + if (client->is_verbose) { + switch (client->ascii_command) { + case SET_CMD: fprintf(stderr, "%s:%d SET_CMD\n", __FILE__, __LINE__); break; -static void print_ascii_command(memcached_protocol_client_st *client) -{ - if (client->is_verbose) - { - switch (client->ascii_command) - { - case SET_CMD: - fprintf(stderr, "%s:%d SET_CMD\n", __FILE__, __LINE__); - break; + case ADD_CMD: fprintf(stderr, "%s:%d ADD_CMD\n", __FILE__, __LINE__); break; - case ADD_CMD: - fprintf(stderr, "%s:%d ADD_CMD\n", __FILE__, __LINE__); - break; + case REPLACE_CMD: fprintf(stderr, "%s:%d REPLACE_CMD\n", __FILE__, __LINE__); break; - case REPLACE_CMD: - fprintf(stderr, "%s:%d REPLACE_CMD\n", __FILE__, __LINE__); - break; + case CAS_CMD: fprintf(stderr, "%s:%d CAS_CMD\n", __FILE__, __LINE__); break; - case CAS_CMD: - fprintf(stderr, "%s:%d CAS_CMD\n", __FILE__, __LINE__); - break; - - case APPEND_CMD: - fprintf(stderr, "%s:%d APPEND_CMD\n", __FILE__, __LINE__); - break; + case APPEND_CMD: fprintf(stderr, "%s:%d APPEND_CMD\n", __FILE__, __LINE__); break; - case PREPEND_CMD: - fprintf(stderr, "%s:%d PREPEND_CMD\n", __FILE__, __LINE__); - break; + case PREPEND_CMD: fprintf(stderr, "%s:%d PREPEND_CMD\n", __FILE__, __LINE__); break; - case DELETE_CMD: - fprintf(stderr, "%s:%d DELETE_CMD\n", __FILE__, __LINE__); - break; + case DELETE_CMD: fprintf(stderr, "%s:%d DELETE_CMD\n", __FILE__, __LINE__); break; - case INCR_CMD: /* FALLTHROUGH */ - fprintf(stderr, "%s:%d INCR_CMD\n", __FILE__, __LINE__); - break; + case INCR_CMD: /* FALLTHROUGH */ fprintf(stderr, "%s:%d INCR_CMD\n", __FILE__, __LINE__); break; - case DECR_CMD: - fprintf(stderr, "%s:%d DECR_CMD\n", __FILE__, __LINE__); - break; + case DECR_CMD: fprintf(stderr, "%s:%d DECR_CMD\n", __FILE__, __LINE__); break; - case STATS_CMD: - fprintf(stderr, "%s:%d STATS_CMD\n", __FILE__, __LINE__); - break; + case STATS_CMD: fprintf(stderr, "%s:%d STATS_CMD\n", __FILE__, __LINE__); break; - case FLUSH_ALL_CMD: - fprintf(stderr, "%s:%d FLUSH_ALL_CMD\n", __FILE__, __LINE__); - break; + case FLUSH_ALL_CMD: fprintf(stderr, "%s:%d FLUSH_ALL_CMD\n", __FILE__, __LINE__); break; - case VERSION_CMD: - fprintf(stderr, "%s:%d VERSION_CMD\n", __FILE__, __LINE__); - break; + case VERSION_CMD: fprintf(stderr, "%s:%d VERSION_CMD\n", __FILE__, __LINE__); break; - case QUIT_CMD: - fprintf(stderr, "%s:%d QUIT_CMD\n", __FILE__, __LINE__); - break; + case QUIT_CMD: fprintf(stderr, "%s:%d QUIT_CMD\n", __FILE__, __LINE__); break; - case VERBOSITY_CMD: - fprintf(stderr, "%s:%d VERBOSITY_CMD\n", __FILE__, __LINE__); - break; + case VERBOSITY_CMD: fprintf(stderr, "%s:%d VERBOSITY_CMD\n", __FILE__, __LINE__); break; - case GET_CMD: - fprintf(stderr, "%s:%d GET_CMD\n", __FILE__, __LINE__); - break; + case GET_CMD: fprintf(stderr, "%s:%d GET_CMD\n", __FILE__, __LINE__); break; - case GETS_CMD: - fprintf(stderr, "%s:%d GETS_CMD\n", __FILE__, __LINE__); - break; + case GETS_CMD: fprintf(stderr, "%s:%d GETS_CMD\n", __FILE__, __LINE__); break; default: - case UNKNOWN_CMD: - fprintf(stderr, "%s:%d UNKNOWN_CMD\n", __FILE__, __LINE__); - break; - + case UNKNOWN_CMD: fprintf(stderr, "%s:%d UNKNOWN_CMD\n", __FILE__, __LINE__); break; } } } @@ -129,27 +69,22 @@ static void print_ascii_command(memcached_protocol_client_st *client) * characters or invalid length) * @todo add length! */ -static uint16_t parse_ascii_key(char **start) -{ - uint16_t len= 0; - char *c= *start; +static uint16_t parse_ascii_key(char **start) { + uint16_t len = 0; + char *c = *start; /* Strip leading whitespaces */ - while (isspace(*c)) - { + while (isspace(*c)) { ++c; } - *start= c; + *start = c; - while (*c != '\0' && !isspace(*c) && !iscntrl(*c)) - { + while (*c != '\0' && !isspace(*c) && !iscntrl(*c)) { ++c; ++len; } - - if (len == 0 || len > 240 || (*c != '\0' && *c != '\r' && iscntrl(*c))) - { + if (len == 0 || len > 240 || (*c != '\0' && *c != '\r' && iscntrl(*c))) { return 0; } @@ -162,15 +97,13 @@ static uint16_t parse_ascii_key(char **start) * @param text the text to spool * @return status of the spool operation */ -static protocol_binary_response_status ascii_raw_response_handler(memcached_protocol_client_st *client, const char *text) -{ - if (client->is_verbose) - { +static protocol_binary_response_status +ascii_raw_response_handler(memcached_protocol_client_st *client, const char *text) { + if (client->is_verbose) { fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, text); } - if (client->root->drain(client) == false) - { + if (client->root->drain(client) == false) { return PROTOCOL_BINARY_RESPONSE_EINTERNAL; } @@ -214,27 +147,30 @@ static protocol_binary_response_status ascii_raw_response_handler(memcached_prot * format of the command being sent * @param client the client to send the message to */ -static void send_command_usage(memcached_protocol_client_st *client) -{ - const char *errmsg[]= { - [GET_CMD]= "CLIENT_ERROR: Syntax error: get *\r\n", - [GETS_CMD]= "CLIENT_ERROR: Syntax error: gets *\r\n", - [SET_CMD]= "CLIENT_ERROR: Syntax error: set [noreply]\r\n", - [ADD_CMD]= "CLIENT_ERROR: Syntax error: add [noreply]\r\n", - [REPLACE_CMD]= "CLIENT_ERROR: Syntax error: replace [noreply]\r\n", - [CAS_CMD]= "CLIENT_ERROR: Syntax error: cas [noreply]\r\n", - [APPEND_CMD]= "CLIENT_ERROR: Syntax error: append [noreply]\r\n", - [PREPEND_CMD]= "CLIENT_ERROR: Syntax error: prepend [noreply]\r\n", - [DELETE_CMD]= "CLIENT_ERROR: Syntax error: delete_object [noreply]\r\n", - [INCR_CMD]= "CLIENT_ERROR: Syntax error: incr [noreply]\r\n", - [DECR_CMD]= "CLIENT_ERROR: Syntax error: decr [noreply]\r\n", - [STATS_CMD]= "CLIENT_ERROR: Syntax error: stats [key]\r\n", - [FLUSH_ALL_CMD]= "CLIENT_ERROR: Syntax error: flush_all [timeout] [noreply]\r\n", - [VERSION_CMD]= "CLIENT_ERROR: Syntax error: version\r\n", - [QUIT_CMD]="CLIENT_ERROR: Syntax error: quit\r\n", - - [VERBOSITY_CMD]= "CLIENT_ERROR: Syntax error: verbosity \r\n", - [UNKNOWN_CMD]= "CLIENT_ERROR: Unknown command\r\n", +static void send_command_usage(memcached_protocol_client_st *client) { + const char *errmsg[] = { + [GET_CMD] = "CLIENT_ERROR: Syntax error: get *\r\n", + [GETS_CMD] = "CLIENT_ERROR: Syntax error: gets *\r\n", + [SET_CMD] = "CLIENT_ERROR: Syntax error: set [noreply]\r\n", + [ADD_CMD] = "CLIENT_ERROR: Syntax error: add [noreply]\r\n", + [REPLACE_CMD] = + "CLIENT_ERROR: Syntax error: replace [noreply]\r\n", + [CAS_CMD] = + "CLIENT_ERROR: Syntax error: cas [noreply]\r\n", + [APPEND_CMD] = + "CLIENT_ERROR: Syntax error: append [noreply]\r\n", + [PREPEND_CMD] = + "CLIENT_ERROR: Syntax error: prepend [noreply]\r\n", + [DELETE_CMD] = "CLIENT_ERROR: Syntax error: delete_object [noreply]\r\n", + [INCR_CMD] = "CLIENT_ERROR: Syntax error: incr [noreply]\r\n", + [DECR_CMD] = "CLIENT_ERROR: Syntax error: decr [noreply]\r\n", + [STATS_CMD] = "CLIENT_ERROR: Syntax error: stats [key]\r\n", + [FLUSH_ALL_CMD] = "CLIENT_ERROR: Syntax error: flush_all [timeout] [noreply]\r\n", + [VERSION_CMD] = "CLIENT_ERROR: Syntax error: version\r\n", + [QUIT_CMD] = "CLIENT_ERROR: Syntax error: quit\r\n", + + [VERBOSITY_CMD] = "CLIENT_ERROR: Syntax error: verbosity \r\n", + [UNKNOWN_CMD] = "CLIENT_ERROR: Unknown command\r\n", }; client->mute = false; @@ -247,11 +183,9 @@ static void send_command_usage(memcached_protocol_client_st *client) * @param text the length of the body * @param textlen the length of the body */ -static protocol_binary_response_status ascii_version_response_handler(const void *cookie, - const void *text, - uint32_t textlen) -{ - memcached_protocol_client_st *client= (memcached_protocol_client_st*)cookie; +static protocol_binary_response_status +ascii_version_response_handler(const void *cookie, const void *text, uint32_t textlen) { + memcached_protocol_client_st *client = (memcached_protocol_client_st *) cookie; ascii_raw_response_handler(client, "VERSION "); client->root->spool(client, text, textlen); ascii_raw_response_handler(client, "\r\n"); @@ -269,28 +203,18 @@ static protocol_binary_response_status ascii_version_response_handler(const void * @param cas the CAS id for the item */ static protocol_binary_response_status -ascii_get_response_handler(const void *cookie, - const void *key, - uint16_t keylen, - const void *body, - uint32_t bodylen, - uint32_t flags, - uint64_t cas) -{ - memcached_protocol_client_st *client= (void*)cookie; +ascii_get_response_handler(const void *cookie, const void *key, uint16_t keylen, const void *body, + uint32_t bodylen, uint32_t flags, uint64_t cas) { + memcached_protocol_client_st *client = (void *) cookie; char buffer[300]; strcpy(buffer, "VALUE "); - const char *source= key; - char *dest= buffer + 6; + const char *source = key; + char *dest = buffer + 6; - for (int x= 0; x < keylen; ++x) - { - if (*source != '\0' && !isspace(*source) && !iscntrl(*source)) - { - *dest= *source; - } - else - { + for (int x = 0; x < keylen; ++x) { + if (*source != '\0' && !isspace(*source) && !iscntrl(*source)) { + *dest = *source; + } else { return PROTOCOL_BINARY_RESPONSE_EINVAL; /* key constraints in ascii */ } @@ -298,15 +222,11 @@ ascii_get_response_handler(const void *cookie, ++source; } - size_t used= (size_t)(dest - buffer); + size_t used = (size_t)(dest - buffer); - if (client->ascii_command == GETS_CMD) - { - snprintf(dest, sizeof(buffer) - used, " %u %u %" PRIu64 "\r\n", flags, - bodylen, cas); - } - else - { + if (client->ascii_command == GETS_CMD) { + snprintf(dest, sizeof(buffer) - used, " %u %u %" PRIu64 "\r\n", flags, bodylen, cas); + } else { snprintf(dest, sizeof(buffer) - used, " %u %u\r\n", flags, bodylen); } @@ -326,24 +246,18 @@ ascii_get_response_handler(const void *cookie, * @param bodylen the length of the body */ static protocol_binary_response_status ascii_stat_response_handler(const void *cookie, - const void *key, - uint16_t keylen, + const void *key, uint16_t keylen, const void *body, - uint32_t bodylen) -{ + uint32_t bodylen) { + memcached_protocol_client_st *client = (void *) cookie; - memcached_protocol_client_st *client= (void*)cookie; - - if (key != NULL) - { + if (key != NULL) { ascii_raw_response_handler(client, "STAT "); client->root->spool(client, key, keylen); ascii_raw_response_handler(client, " "); client->root->spool(client, body, bodylen); ascii_raw_response_handler(client, "\r\n"); - } - else - { + } else { ascii_raw_response_handler(client, "END\r\n"); } @@ -356,35 +270,27 @@ static protocol_binary_response_status ascii_stat_response_handler(const void *c * @param buffer the complete get(s) command * @param end the last character in the command */ -static void ascii_process_gets(memcached_protocol_client_st *client, - char *buffer, char *end) -{ - char *key= buffer; +static void ascii_process_gets(memcached_protocol_client_st *client, char *buffer, char *end) { + char *key = buffer; /* Skip command */ key += (client->ascii_command == GETS_CMD) ? 5 : 4; - int num_keys= 0; - while (key < end) - { - uint16_t nkey= parse_ascii_key(&key); - if (nkey == 0) /* Invalid key... stop processing this line */ - { + int num_keys = 0; + while (key < end) { + uint16_t nkey = parse_ascii_key(&key); + if (nkey == 0) /* Invalid key... stop processing this line */ { break; } - (void)client->root->callback->interface.v1.get(client, key, nkey, - ascii_get_response_handler); + (void) client->root->callback->interface.v1.get(client, key, nkey, ascii_get_response_handler); key += nkey; ++num_keys; } - if (num_keys == 0) - { + if (num_keys == 0) { send_command_usage(client); - } - else - { + } else { client->root->spool(client, "END\r\n", 5); } } @@ -398,37 +304,31 @@ static void ascii_process_gets(memcached_protocol_client_st *client, * @size the number of elements in the vector * @return the number of tokens in the vector */ -static int ascii_tokenize_command(char *str, char *end, char **vec, int size) -{ - int elem= 0; +static int ascii_tokenize_command(char *str, char *end, char **vec, int size) { + int elem = 0; - while (str < end) - { + while (str < end) { /* Skip leading blanks */ - while (str < end && isspace(*str)) - { + while (str < end && isspace(*str)) { ++str; } - if (str == end) - { + if (str == end) { return elem; } - vec[elem++]= str; + vec[elem++] = str; /* find the next non-blank field */ - while (str < end && !isspace(*str)) - { + while (str < end && !isspace(*str)) { ++str; } /* zero-terminate it for easier parsing later on */ - *str= '\0'; + *str = '\0'; ++str; /* Is the vector full? */ - if (elem == size) - { + if (elem == size) { break; } } @@ -445,55 +345,48 @@ static int ascii_tokenize_command(char *str, char *end, char **vec, int size) * @param start pointer to the first character in the buffer to recover * @param end pointer to the last character in the buffer to recover */ -static void recover_tokenize_command(char *start, char *end) -{ - while (start < end) - { +static void recover_tokenize_command(char *start, char *end) { + while (start < end) { if (*start == '\0') - *start= ' '; + *start = ' '; ++start; } - *end= '\n'; + *end = '\n'; } /** * Convert the textual command into a comcode */ -static enum ascii_cmd ascii_to_cmd(char *start, size_t length) -{ +static enum ascii_cmd ascii_to_cmd(char *start, size_t length) { struct { const char *cmd; size_t len; enum ascii_cmd cc; - } commands[]= { - { .cmd= "get", .len= 3, .cc= GET_CMD }, - { .cmd= "gets", .len= 4, .cc= GETS_CMD }, - { .cmd= "set", .len= 3, .cc= SET_CMD }, - { .cmd= "add", .len= 3, .cc= ADD_CMD }, - { .cmd= "replace", .len= 7, .cc= REPLACE_CMD }, - { .cmd= "cas", .len= 3, .cc= CAS_CMD }, - { .cmd= "append", .len= 6, .cc= APPEND_CMD }, - { .cmd= "prepend", .len= 7, .cc= PREPEND_CMD }, - { .cmd= "delete_object", .len= 6, .cc= DELETE_CMD }, - { .cmd= "incr", .len= 4, .cc= INCR_CMD }, - { .cmd= "decr", .len= 4, .cc= DECR_CMD }, - { .cmd= "stats", .len= 5, .cc= STATS_CMD }, - { .cmd= "flush_all", .len= 9, .cc= FLUSH_ALL_CMD }, - { .cmd= "version", .len= 7, .cc= VERSION_CMD }, - { .cmd= "quit", .len= 4, .cc= QUIT_CMD }, - { .cmd= "verbosity", .len= 9, .cc= VERBOSITY_CMD }, - { .cmd= NULL, .len= 0, .cc= UNKNOWN_CMD }}; - - int x= 0; + } commands[] = {{.cmd = "get", .len = 3, .cc = GET_CMD}, + {.cmd = "gets", .len = 4, .cc = GETS_CMD}, + {.cmd = "set", .len = 3, .cc = SET_CMD}, + {.cmd = "add", .len = 3, .cc = ADD_CMD}, + {.cmd = "replace", .len = 7, .cc = REPLACE_CMD}, + {.cmd = "cas", .len = 3, .cc = CAS_CMD}, + {.cmd = "append", .len = 6, .cc = APPEND_CMD}, + {.cmd = "prepend", .len = 7, .cc = PREPEND_CMD}, + {.cmd = "delete_object", .len = 6, .cc = DELETE_CMD}, + {.cmd = "incr", .len = 4, .cc = INCR_CMD}, + {.cmd = "decr", .len = 4, .cc = DECR_CMD}, + {.cmd = "stats", .len = 5, .cc = STATS_CMD}, + {.cmd = "flush_all", .len = 9, .cc = FLUSH_ALL_CMD}, + {.cmd = "version", .len = 7, .cc = VERSION_CMD}, + {.cmd = "quit", .len = 4, .cc = QUIT_CMD}, + {.cmd = "verbosity", .len = 9, .cc = VERBOSITY_CMD}, + {.cmd = NULL, .len = 0, .cc = UNKNOWN_CMD}}; + + int x = 0; while (commands[x].len > 0) { - if (length >= commands[x].len) - { - if (strncmp(start, commands[x].cmd, commands[x].len) == 0) - { + if (length >= commands[x].len) { + if (strncmp(start, commands[x].cmd, commands[x].len) == 0) { /* Potential hit */ - if (length == commands[x].len || isspace(*(start + commands[x].len))) - { + if (length == commands[x].len || isspace(*(start + commands[x].len))) { return commands[x].cc; } } @@ -511,101 +404,73 @@ static enum ascii_cmd ascii_to_cmd(char *start, size_t length) * @param tokens the command as a vector * @param ntokens the number of items in the vector */ -static void process_delete(memcached_protocol_client_st *client, - char **tokens, int ntokens) -{ - char *key= tokens[1]; +static void process_delete(memcached_protocol_client_st *client, char **tokens, int ntokens) { + char *key = tokens[1]; uint16_t nkey; - if (ntokens != 2 || (nkey= parse_ascii_key(&key)) == 0) - { + if (ntokens != 2 || (nkey = parse_ascii_key(&key)) == 0) { send_command_usage(client); return; } - if (client->root->callback->interface.v1.delete_object == NULL) - { + if (client->root->callback->interface.v1.delete_object == NULL) { ascii_raw_response_handler(client, "SERVER_ERROR: callback not implemented\r\n"); return; } - protocol_binary_response_status rval= client->root->callback->interface.v1.delete_object(client, key, nkey, 0); + protocol_binary_response_status rval = + client->root->callback->interface.v1.delete_object(client, key, nkey, 0); - if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS) - { + if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS) { ascii_raw_response_handler(client, "DELETED\r\n"); - } - else if (rval == PROTOCOL_BINARY_RESPONSE_KEY_ENOENT) - { + } else if (rval == PROTOCOL_BINARY_RESPONSE_KEY_ENOENT) { ascii_raw_response_handler(client, "NOT_FOUND\r\n"); - } - else - { + } else { char msg[80]; - snprintf(msg, sizeof(msg), "SERVER_ERROR: delete_object failed %u\r\n",(uint32_t)rval); + snprintf(msg, sizeof(msg), "SERVER_ERROR: delete_object failed %u\r\n", (uint32_t) rval); ascii_raw_response_handler(client, msg); } } -static void process_arithmetic(memcached_protocol_client_st *client, - char **tokens, int ntokens) -{ - char *key= tokens[1]; +static void process_arithmetic(memcached_protocol_client_st *client, char **tokens, int ntokens) { + char *key = tokens[1]; uint16_t nkey; - if (ntokens != 3 || (nkey= parse_ascii_key(&key)) == 0) - { + if (ntokens != 3 || (nkey = parse_ascii_key(&key)) == 0) { send_command_usage(client); return; } uint64_t cas; uint64_t result; - errno= 0; - uint64_t delta= strtoull(tokens[2], NULL, 10); - if (errno != 0) - { + errno = 0; + uint64_t delta = strtoull(tokens[2], NULL, 10); + if (errno != 0) { return; // Error } protocol_binary_response_status rval; - if (client->ascii_command == INCR_CMD) - { - if (client->root->callback->interface.v1.increment == NULL) - { + if (client->ascii_command == INCR_CMD) { + if (client->root->callback->interface.v1.increment == NULL) { ascii_raw_response_handler(client, "SERVER_ERROR: callback not implemented\r\n"); return; } - rval= client->root->callback->interface.v1.increment(client, - key, nkey, - delta, 0, - 0, - &result, - &cas); - } - else - { - if (client->root->callback->interface.v1.decrement == NULL) - { + rval = client->root->callback->interface.v1.increment(client, key, nkey, delta, 0, 0, &result, + &cas); + } else { + if (client->root->callback->interface.v1.decrement == NULL) { ascii_raw_response_handler(client, "SERVER_ERROR: callback not implemented\r\n"); return; } - rval= client->root->callback->interface.v1.decrement(client, - key, nkey, - delta, 0, - 0, - &result, - &cas); + rval = client->root->callback->interface.v1.decrement(client, key, nkey, delta, 0, 0, &result, + &cas); } - if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS) - { + if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS) { char buffer[80]; - snprintf(buffer, sizeof(buffer), "%"PRIu64"\r\n", result); + snprintf(buffer, sizeof(buffer), "%" PRIu64 "\r\n", result); ascii_raw_response_handler(client, buffer); - } - else - { + } else { ascii_raw_response_handler(client, "NOT_FOUND\r\n"); } } @@ -615,73 +480,57 @@ static void process_arithmetic(memcached_protocol_client_st *client, * @param key pointer to the first character after "stats" * @param end pointer to the "\n" */ -static void process_stats(memcached_protocol_client_st *client, - char *key, char *end) -{ - if (client->root->callback->interface.v1.stat == NULL) - { +static void process_stats(memcached_protocol_client_st *client, char *key, char *end) { + if (client->root->callback->interface.v1.stat == NULL) { ascii_raw_response_handler(client, "SERVER_ERROR: callback not implemented\r\n"); return; } - while (isspace(*key)) - { + while (isspace(*key)) { key++; } - uint16_t nkey= (uint16_t)(end - key); - (void)client->root->callback->interface.v1.stat(client, key, nkey, - ascii_stat_response_handler); + uint16_t nkey = (uint16_t)(end - key); + (void) client->root->callback->interface.v1.stat(client, key, nkey, ascii_stat_response_handler); } -static void process_version(memcached_protocol_client_st *client, - char **tokens, int ntokens) -{ - (void)tokens; - if (ntokens != 1) - { +static void process_version(memcached_protocol_client_st *client, char **tokens, int ntokens) { + (void) tokens; + if (ntokens != 1) { send_command_usage(client); return; } - if (client->root->callback->interface.v1.version == NULL) - { + if (client->root->callback->interface.v1.version == NULL) { ascii_raw_response_handler(client, "SERVER_ERROR: callback not implemented\r\n"); return; } - client->root->callback->interface.v1.version(client, - ascii_version_response_handler); + client->root->callback->interface.v1.version(client, ascii_version_response_handler); } -static void process_flush(memcached_protocol_client_st *client, - char **tokens, int ntokens) -{ - if (ntokens > 2) - { +static void process_flush(memcached_protocol_client_st *client, char **tokens, int ntokens) { + if (ntokens > 2) { send_command_usage(client); return; } - if (client->root->callback->interface.v1.flush_object == NULL) - { + if (client->root->callback->interface.v1.flush_object == NULL) { ascii_raw_response_handler(client, "SERVER_ERROR: callback not implemented\r\n"); return; } - uint32_t timeout= 0; - if (ntokens == 2) - { - errno= 0; - timeout= (uint32_t)strtoul(tokens[1], NULL, 10); - if (errno != 0) - { + uint32_t timeout = 0; + if (ntokens == 2) { + errno = 0; + timeout = (uint32_t) strtoul(tokens[1], NULL, 10); + if (errno != 0) { return; // Error } } protocol_binary_response_status rval; - rval= client->root->callback->interface.v1.flush_object(client, timeout); + rval = client->root->callback->interface.v1.flush_object(client, timeout); if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS) ascii_raw_response_handler(client, "OK\r\n"); else @@ -702,111 +551,80 @@ static void process_flush(memcached_protocol_client_st *client, * 0 storage command completed, continue processing * 1 We need more data, so just go ahead and wait for more! */ -static inline int process_storage_command(memcached_protocol_client_st *client, - char **tokens, int ntokens, char *start, - char **end, ssize_t length) -{ - (void)ntokens; /* already checked */ - char *key= tokens[1]; - uint16_t nkey= parse_ascii_key(&key); - if (nkey == 0) - { +static inline int process_storage_command(memcached_protocol_client_st *client, char **tokens, + int ntokens, char *start, char **end, ssize_t length) { + (void) ntokens; /* already checked */ + char *key = tokens[1]; + uint16_t nkey = parse_ascii_key(&key); + if (nkey == 0) { /* return error */ ascii_raw_response_handler(client, "CLIENT_ERROR: bad key\r\n"); return -1; } - errno= 0; - uint32_t flags= (uint32_t)strtoul(tokens[2], NULL, 10); - if (errno != 0) - { + errno = 0; + uint32_t flags = (uint32_t) strtoul(tokens[2], NULL, 10); + if (errno != 0) { /* return error */ ascii_raw_response_handler(client, "CLIENT_ERROR: bad key\r\n"); return -1; } - uint32_t timeout= (uint32_t)strtoul(tokens[3], NULL, 10); - if (errno != 0) - { + uint32_t timeout = (uint32_t) strtoul(tokens[3], NULL, 10); + if (errno != 0) { /* return error */ ascii_raw_response_handler(client, "CLIENT_ERROR: bad key\r\n"); return -1; } - unsigned long nbytes= strtoul(tokens[4], NULL, 10); - if (errno != 0) - { + unsigned long nbytes = strtoul(tokens[4], NULL, 10); + if (errno != 0) { /* return error */ ascii_raw_response_handler(client, "CLIENT_ERROR: bad key\r\n"); return -1; } /* Do we have all data? */ - unsigned long need= nbytes + (unsigned long)((*end - start) + 1) + 2; /* \n\r\n */ - if ((ssize_t)need > length) - { + unsigned long need = nbytes + (unsigned long) ((*end - start) + 1) + 2; /* \n\r\n */ + if ((ssize_t) need > length) { /* Keep on reading */ recover_tokenize_command(start, *end); return 1; } - void *data= (*end) + 1; - uint64_t cas= 0; + void *data = (*end) + 1; + uint64_t cas = 0; uint64_t result_cas; protocol_binary_response_status rval; - switch (client->ascii_command) - { + switch (client->ascii_command) { case SET_CMD: - rval= client->root->callback->interface.v1.set(client, key, - (uint16_t)nkey, - data, - (uint32_t)nbytes, - flags, - timeout, cas, - &result_cas); + rval = client->root->callback->interface.v1.set( + client, key, (uint16_t) nkey, data, (uint32_t) nbytes, flags, timeout, cas, &result_cas); break; case ADD_CMD: - rval= client->root->callback->interface.v1.add(client, key, - (uint16_t)nkey, - data, - (uint32_t)nbytes, - flags, - timeout, &result_cas); + rval = client->root->callback->interface.v1.add(client, key, (uint16_t) nkey, data, + (uint32_t) nbytes, flags, timeout, &result_cas); break; case CAS_CMD: - errno= 0; - cas= strtoull(tokens[5], NULL, 10); - if (errno != 0) - { + errno = 0; + cas = strtoull(tokens[5], NULL, 10); + if (errno != 0) { /* return error */ ascii_raw_response_handler(client, "CLIENT_ERROR: bad key\r\n"); return -1; } /* FALLTHROUGH */ case REPLACE_CMD: - rval= client->root->callback->interface.v1.replace(client, key, - (uint16_t)nkey, - data, - (uint32_t)nbytes, - flags, - timeout, cas, - &result_cas); + rval = client->root->callback->interface.v1.replace( + client, key, (uint16_t) nkey, data, (uint32_t) nbytes, flags, timeout, cas, &result_cas); break; case APPEND_CMD: - rval= client->root->callback->interface.v1.append(client, key, - (uint16_t)nkey, - data, - (uint32_t)nbytes, - cas, - &result_cas); + rval = client->root->callback->interface.v1.append(client, key, (uint16_t) nkey, data, + (uint32_t) nbytes, cas, &result_cas); break; case PREPEND_CMD: - rval= client->root->callback->interface.v1.prepend(client, key, - (uint16_t)nkey, - data, - (uint32_t)nbytes, - cas, - &result_cas); + rval = client->root->callback->interface.v1.prepend(client, key, (uint16_t) nkey, data, + (uint32_t) nbytes, cas, &result_cas); break; /* gcc complains if I don't put all of the enums in here.. */ @@ -821,33 +639,21 @@ static inline int process_storage_command(memcached_protocol_client_st *client, case QUIT_CMD: case VERBOSITY_CMD: case UNKNOWN_CMD: - default: - abort(); /* impossible */ + default: abort(); /* impossible */ } - if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS) - { + if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS) { ascii_raw_response_handler(client, "STORED\r\n"); - } - else - { - if (client->ascii_command == CAS_CMD) - { - if (rval == PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS) - { + } else { + if (client->ascii_command == CAS_CMD) { + if (rval == PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS) { ascii_raw_response_handler(client, "EXISTS\r\n"); - } - else if (rval == PROTOCOL_BINARY_RESPONSE_KEY_ENOENT) - { + } else if (rval == PROTOCOL_BINARY_RESPONSE_KEY_ENOENT) { ascii_raw_response_handler(client, "NOT_FOUND\r\n"); - } - else - { + } else { ascii_raw_response_handler(client, "NOT_STORED\r\n"); } - } - else - { + } else { ascii_raw_response_handler(client, "NOT_STORED\r\n"); } } @@ -857,18 +663,14 @@ static inline int process_storage_command(memcached_protocol_client_st *client, return 0; } -static int process_cas_command(memcached_protocol_client_st *client, - char **tokens, int ntokens, char *start, - char **end, ssize_t length) -{ - if (ntokens != 6) - { +static int process_cas_command(memcached_protocol_client_st *client, char **tokens, int ntokens, + char *start, char **end, ssize_t length) { + if (ntokens != 6) { send_command_usage(client); return false; } - if (client->root->callback->interface.v1.replace == NULL) - { + if (client->root->callback->interface.v1.replace == NULL) { ascii_raw_response_handler(client, "SERVER_ERROR: callback not implemented\r\n"); return false; } @@ -876,18 +678,14 @@ static int process_cas_command(memcached_protocol_client_st *client, return process_storage_command(client, tokens, ntokens, start, end, length); } -static int process_set_command(memcached_protocol_client_st *client, - char **tokens, int ntokens, char *start, - char **end, ssize_t length) -{ - if (ntokens != 5) - { +static int process_set_command(memcached_protocol_client_st *client, char **tokens, int ntokens, + char *start, char **end, ssize_t length) { + if (ntokens != 5) { send_command_usage(client); return false; } - if (client->root->callback->interface.v1.set == NULL) - { + if (client->root->callback->interface.v1.set == NULL) { ascii_raw_response_handler(client, "SERVER_ERROR: callback not implemented\r\n"); return false; } @@ -895,18 +693,14 @@ static int process_set_command(memcached_protocol_client_st *client, return process_storage_command(client, tokens, ntokens, start, end, length); } -static int process_add_command(memcached_protocol_client_st *client, - char **tokens, int ntokens, char *start, - char **end, ssize_t length) -{ - if (ntokens != 5) - { +static int process_add_command(memcached_protocol_client_st *client, char **tokens, int ntokens, + char *start, char **end, ssize_t length) { + if (ntokens != 5) { send_command_usage(client); return false; } - if (client->root->callback->interface.v1.add == NULL) - { + if (client->root->callback->interface.v1.add == NULL) { ascii_raw_response_handler(client, "SERVER_ERROR: callback not implemented\r\n"); return false; } @@ -914,18 +708,14 @@ static int process_add_command(memcached_protocol_client_st *client, return process_storage_command(client, tokens, ntokens, start, end, length); } -static int process_replace_command(memcached_protocol_client_st *client, - char **tokens, int ntokens, char *start, - char **end, ssize_t length) -{ - if (ntokens != 5) - { +static int process_replace_command(memcached_protocol_client_st *client, char **tokens, int ntokens, + char *start, char **end, ssize_t length) { + if (ntokens != 5) { send_command_usage(client); return false; } - if (client->root->callback->interface.v1.replace == NULL) - { + if (client->root->callback->interface.v1.replace == NULL) { ascii_raw_response_handler(client, "SERVER_ERROR: callback not implemented\r\n"); return false; } @@ -933,18 +723,14 @@ static int process_replace_command(memcached_protocol_client_st *client, return process_storage_command(client, tokens, ntokens, start, end, length); } -static int process_append_command(memcached_protocol_client_st *client, - char **tokens, int ntokens, char *start, - char **end, ssize_t length) -{ - if (ntokens != 5) - { +static int process_append_command(memcached_protocol_client_st *client, char **tokens, int ntokens, + char *start, char **end, ssize_t length) { + if (ntokens != 5) { send_command_usage(client); return false; } - if (client->root->callback->interface.v1.append == NULL) - { + if (client->root->callback->interface.v1.append == NULL) { ascii_raw_response_handler(client, "SERVER_ERROR: callback not implemented\r\n"); return false; } @@ -952,18 +738,14 @@ static int process_append_command(memcached_protocol_client_st *client, return process_storage_command(client, tokens, ntokens, start, end, length); } -static int process_prepend_command(memcached_protocol_client_st *client, - char **tokens, int ntokens, char *start, - char **end, ssize_t length) -{ - if (ntokens != 5) - { +static int process_prepend_command(memcached_protocol_client_st *client, char **tokens, int ntokens, + char *start, char **end, ssize_t length) { + if (ntokens != 5) { send_command_usage(client); return false; } - if (client->root->callback->interface.v1.prepend == NULL) - { + if (client->root->callback->interface.v1.prepend == NULL) { ascii_raw_response_handler(client, "SERVER_ERROR: callback not implemented\r\n"); return false; } @@ -976,136 +758,102 @@ static int process_prepend_command(memcached_protocol_client_st *client, * a optimal ascii support, I just convert the ASCII commands to the binary * protocol and calls back into the command handlers for the binary protocol ;) */ -memcached_protocol_event_t memcached_ascii_protocol_process_data(memcached_protocol_client_st *client, ssize_t *length, void **endptr) -{ - char *ptr= (char*)client->root->input_buffer; - *endptr= ptr; +memcached_protocol_event_t +memcached_ascii_protocol_process_data(memcached_protocol_client_st *client, ssize_t *length, + void **endptr) { + char *ptr = (char *) client->root->input_buffer; + *endptr = ptr; do { /* Do we have \n (indicating the command preamble)*/ - char *end= memchr(ptr, '\n', (size_t)*length); - if (end == NULL) - { - *endptr= ptr; + char *end = memchr(ptr, '\n', (size_t) *length); + if (end == NULL) { + *endptr = ptr; return MEMCACHED_PROTOCOL_READ_EVENT; } - client->ascii_command= ascii_to_cmd(ptr, (size_t)(*length)); + client->ascii_command = ascii_to_cmd(ptr, (size_t)(*length)); /* we got all data available, execute the callback! */ - if (client->root->callback->pre_execute != NULL) - { + if (client->root->callback->pre_execute != NULL) { client->root->callback->pre_execute(client, NULL); } - /* A multiget lists all of the keys, and I don't want to have an * avector of let's say 512 pointers to tokenize all of them, so let's * just handle them immediately */ - if (client->ascii_command == GET_CMD || - client->ascii_command == GETS_CMD) - { - if (client->root->callback->interface.v1.get != NULL) - { + if (client->ascii_command == GET_CMD || client->ascii_command == GETS_CMD) { + if (client->root->callback->interface.v1.get != NULL) { ascii_process_gets(client, ptr, end); - } - else - { + } else { ascii_raw_response_handler(client, "SERVER_ERROR: Command not implemented\n"); } - } - else - { + } else { /* None of the defined commands takes 10 parameters, so lets just use * that as a maximum limit. - */ + */ char *tokens[10]; - int ntokens= ascii_tokenize_command(ptr, end, tokens, 10); + int ntokens = ascii_tokenize_command(ptr, end, tokens, 10); - if (ntokens < 10) - { - client->mute= strcmp(tokens[ntokens - 1], "noreply") == 0; - if (client->mute) - { + if (ntokens < 10) { + client->mute = strcmp(tokens[ntokens - 1], "noreply") == 0; + if (client->mute) { --ntokens; /* processed noreply token*/ } } - int error= 0; + int error = 0; print_ascii_command(client); - switch (client->ascii_command) - { - case SET_CMD: - error= process_set_command(client, tokens, ntokens, ptr, &end, *length); - break; + switch (client->ascii_command) { + case SET_CMD: error = process_set_command(client, tokens, ntokens, ptr, &end, *length); break; - case ADD_CMD: - error= process_add_command(client, tokens, ntokens, ptr, &end, *length); - break; + case ADD_CMD: error = process_add_command(client, tokens, ntokens, ptr, &end, *length); break; case REPLACE_CMD: - error= process_replace_command(client, tokens, ntokens, ptr, &end, *length); + error = process_replace_command(client, tokens, ntokens, ptr, &end, *length); break; - case CAS_CMD: - error= process_cas_command(client, tokens, ntokens, ptr, &end, *length); - break; + case CAS_CMD: error = process_cas_command(client, tokens, ntokens, ptr, &end, *length); break; case APPEND_CMD: - error= process_append_command(client, tokens, ntokens, ptr, &end, *length); + error = process_append_command(client, tokens, ntokens, ptr, &end, *length); break; case PREPEND_CMD: - error= process_prepend_command(client, tokens, ntokens, ptr, &end, *length); + error = process_prepend_command(client, tokens, ntokens, ptr, &end, *length); break; - case DELETE_CMD: - process_delete(client, tokens, ntokens); - break; + case DELETE_CMD: process_delete(client, tokens, ntokens); break; case INCR_CMD: /* FALLTHROUGH */ - case DECR_CMD: - process_arithmetic(client, tokens, ntokens); - break; + case DECR_CMD: process_arithmetic(client, tokens, ntokens); break; case STATS_CMD: - if (client->mute) - { + if (client->mute) { send_command_usage(client); - } - else - { + } else { recover_tokenize_command(ptr, end); process_stats(client, ptr + 6, end); } break; - case FLUSH_ALL_CMD: - process_flush(client, tokens, ntokens); - break; + case FLUSH_ALL_CMD: process_flush(client, tokens, ntokens); break; case VERSION_CMD: - if (client->mute) - { + if (client->mute) { send_command_usage(client); - } - else - { + } else { process_version(client, tokens, ntokens); } break; case QUIT_CMD: - if (ntokens != 1 || client->mute) - { + if (ntokens != 1 || client->mute) { send_command_usage(client); - } - else - { - if (client->root->callback->interface.v1.quit != NULL) - { + } else { + if (client->root->callback->interface.v1.quit != NULL) { client->root->callback->interface.v1.quit(client); } @@ -1114,19 +862,14 @@ memcached_protocol_event_t memcached_ascii_protocol_process_data(memcached_proto break; case VERBOSITY_CMD: - if (ntokens != 2) - { + if (ntokens != 2) { send_command_usage(client); - } - else - { + } else { ascii_raw_response_handler(client, "OK\r\n"); } break; - case UNKNOWN_CMD: - send_command_usage(client); - break; + case UNKNOWN_CMD: send_command_usage(client); break; case GET_CMD: case GETS_CMD: @@ -1135,27 +878,23 @@ memcached_protocol_event_t memcached_ascii_protocol_process_data(memcached_proto abort(); } - if (error == -1) - { + if (error == -1) { return MEMCACHED_PROTOCOL_ERROR_EVENT; - } - else if (error == 1) - { + } else if (error == 1) { return MEMCACHED_PROTOCOL_READ_EVENT; } } - if (client->root->callback->post_execute != NULL) - { + if (client->root->callback->post_execute != NULL) { client->root->callback->post_execute(client, NULL); } /* Move past \n */ ++end; *length -= end - ptr; - ptr= end; + ptr = end; } while (*length > 0); - *endptr= ptr; + *endptr = ptr; return MEMCACHED_PROTOCOL_READ_EVENT; } diff --git a/src/libmemcachedprotocol/ascii_handler.h b/src/libmemcachedprotocol/ascii_handler.h index 02f8831e..730e77ed 100644 --- a/src/libmemcachedprotocol/ascii_handler.h +++ b/src/libmemcachedprotocol/ascii_handler.h @@ -1,40 +1,21 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once LIBMEMCACHED_LOCAL -memcached_protocol_event_t memcached_ascii_protocol_process_data(memcached_protocol_client_st *client, ssize_t *length, void **endptr); +memcached_protocol_event_t +memcached_ascii_protocol_process_data(memcached_protocol_client_st *client, ssize_t *length, + void **endptr); diff --git a/src/libmemcachedprotocol/binary_handler.c b/src/libmemcachedprotocol/binary_handler.c index 8435cc98..51fa60be 100644 --- a/src/libmemcachedprotocol/binary_handler.c +++ b/src/libmemcachedprotocol/binary_handler.c @@ -1,38 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "libmemcachedprotocol/common.h" @@ -59,52 +38,38 @@ * @param response the packet to send * @return The status of the operation */ -static protocol_binary_response_status binary_raw_response_handler(const void *cookie, - protocol_binary_request_header *request, - protocol_binary_response_header *response) -{ - memcached_protocol_client_st *client= (void*)cookie; - - if (client->root->pedantic && - !memcached_binary_protocol_pedantic_check_response(request, response)) - { +static protocol_binary_response_status +binary_raw_response_handler(const void *cookie, protocol_binary_request_header *request, + protocol_binary_response_header *response) { + memcached_protocol_client_st *client = (void *) cookie; + + if (client->root->pedantic + && !memcached_binary_protocol_pedantic_check_response(request, response)) { return PROTOCOL_BINARY_RESPONSE_EINVAL; } - if (client->root->drain(client) == false) - { + if (client->root->drain(client) == false) { return PROTOCOL_BINARY_RESPONSE_EINTERNAL; } - size_t len= sizeof(protocol_binary_response_header) + htonl(response->response.bodylen); - size_t offset= 0; - char *ptr= (void*)response; + size_t len = sizeof(protocol_binary_response_header) + htonl(response->response.bodylen); + size_t offset = 0; + char *ptr = (void *) response; - if (client->output == NULL) - { + if (client->output == NULL) { /* I can write directly to the socket.... */ - do - { - size_t num_bytes= len - offset; - ssize_t nw= client->root->send(client, - client->sock, - ptr + offset, - num_bytes); - if (nw == -1) - { - if (get_socket_errno() == EWOULDBLOCK) - { + do { + size_t num_bytes = len - offset; + ssize_t nw = client->root->send(client, client->sock, ptr + offset, num_bytes); + if (nw == -1) { + if (get_socket_errno() == EWOULDBLOCK) { break; - } - else if (get_socket_errno() != EINTR) - { - client->error= errno; + } else if (get_socket_errno() != EINTR) { + client->error = errno; return PROTOCOL_BINARY_RESPONSE_EINTERNAL; } - } - else - { - offset += (size_t)nw; + } else { + offset += (size_t) nw; } } while (offset < len); } @@ -112,74 +77,195 @@ static protocol_binary_response_status binary_raw_response_handler(const void *c return client->root->spool(client, ptr, len - offset); } -static void print_cmd(protocol_binary_command cmd) -{ - switch (cmd) - { - case PROTOCOL_BINARY_CMD_GET: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GET\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_SET: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_SET\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_ADD: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_ADD\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_REPLACE: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_REPLACE\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_DELETE: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_DELETE\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_INCREMENT: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_INCREMENT\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_DECREMENT: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_DECREMENT\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_QUIT: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_QUIT\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_FLUSH: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_FLUSH\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_GETQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GETQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_NOOP: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_NOOP\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_VERSION: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_VERSION\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_GETK: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GETK\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_GETKQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GETKQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_APPEND: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_APPEND\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_PREPEND: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_PREPEND\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_STAT: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_STAT\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_SETQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_SETQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_ADDQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_ADDQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_REPLACEQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_REPLACEQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_DELETEQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_DELETEQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_INCREMENTQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_INCREMENTQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_DECREMENTQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_DECREMENTQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_QUITQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_QUITQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_FLUSHQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_FLUSHQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_APPENDQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_APPENDQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_PREPENDQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_PREPENDQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_VERBOSITY: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_VERBOSITY\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_TOUCH: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TOUCH\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_GAT: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GAT\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_GATQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GATQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_SASL_LIST_MECHS: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_SASL_LIST_MECHS\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_SASL_AUTH: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_SASL_AUTH\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_SASL_STEP: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_SASL_STEP\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_RGET: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RGET\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_RSET: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RSET\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_RSETQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RSETQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_RAPPEND: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RAPPEND\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_RAPPENDQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RAPPENDQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_RPREPEND: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RPREPEND\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_RPREPENDQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RPREPENDQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_RDELETE: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RDELETE\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_RDELETEQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RDELETEQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_RINCR: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RINCR\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_RINCRQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RINCRQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_RDECR: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RDECR\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_RDECRQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RDECRQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_SET_VBUCKET: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_SET_VBUCKET\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_GET_VBUCKET: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GET_VBUCKET\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_DEL_VBUCKET: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_DEL_VBUCKET\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_TAP_CONNECT: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TAP_CONNECT\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_TAP_MUTATION: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TAP_MUTATION\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_TAP_DELETE: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TAP_DELETE\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_TAP_FLUSH: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TAP_FLUSH\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_TAP_OPAQUE: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TAP_OPAQUE\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_TAP_VBUCKET_SET: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TAP_VBUCKET_SET\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_TAP_CHECKPOINT_START: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TAP_CHECKPOINT_START\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_TAP_CHECKPOINT_END: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TAP_CHECKPOINT_END\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_LAST_RESERVED: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_LAST_RESERVED\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_GATK: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GATK\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_GATKQ: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GATKQ\n", __FILE__, __LINE__); return; - case PROTOCOL_BINARY_CMD_SCRUB: fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_SCRUB\n", __FILE__, __LINE__); return; - default: - abort(); +static void print_cmd(protocol_binary_command cmd) { + switch (cmd) { + case PROTOCOL_BINARY_CMD_GET: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GET\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_SET: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_SET\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_ADD: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_ADD\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_REPLACE: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_REPLACE\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_DELETE: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_DELETE\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_INCREMENT: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_INCREMENT\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_DECREMENT: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_DECREMENT\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_QUIT: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_QUIT\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_FLUSH: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_FLUSH\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_GETQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GETQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_NOOP: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_NOOP\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_VERSION: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_VERSION\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_GETK: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GETK\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_GETKQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GETKQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_APPEND: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_APPEND\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_PREPEND: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_PREPEND\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_STAT: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_STAT\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_SETQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_SETQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_ADDQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_ADDQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_REPLACEQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_REPLACEQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_DELETEQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_DELETEQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_INCREMENTQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_INCREMENTQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_DECREMENTQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_DECREMENTQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_QUITQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_QUITQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_FLUSHQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_FLUSHQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_APPENDQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_APPENDQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_PREPENDQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_PREPENDQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_VERBOSITY: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_VERBOSITY\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_TOUCH: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TOUCH\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_GAT: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GAT\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_GATQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GATQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_SASL_LIST_MECHS: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_SASL_LIST_MECHS\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_SASL_AUTH: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_SASL_AUTH\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_SASL_STEP: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_SASL_STEP\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_RGET: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RGET\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_RSET: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RSET\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_RSETQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RSETQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_RAPPEND: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RAPPEND\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_RAPPENDQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RAPPENDQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_RPREPEND: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RPREPEND\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_RPREPENDQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RPREPENDQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_RDELETE: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RDELETE\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_RDELETEQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RDELETEQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_RINCR: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RINCR\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_RINCRQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RINCRQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_RDECR: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RDECR\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_RDECRQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_RDECRQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_SET_VBUCKET: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_SET_VBUCKET\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_GET_VBUCKET: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GET_VBUCKET\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_DEL_VBUCKET: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_DEL_VBUCKET\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_TAP_CONNECT: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TAP_CONNECT\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_TAP_MUTATION: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TAP_MUTATION\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_TAP_DELETE: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TAP_DELETE\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_TAP_FLUSH: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TAP_FLUSH\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_TAP_OPAQUE: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TAP_OPAQUE\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_TAP_VBUCKET_SET: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TAP_VBUCKET_SET\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_TAP_CHECKPOINT_START: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TAP_CHECKPOINT_START\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_TAP_CHECKPOINT_END: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_TAP_CHECKPOINT_END\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_LAST_RESERVED: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_LAST_RESERVED\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_GATK: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GATK\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_GATKQ: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_GATKQ\n", __FILE__, __LINE__); + return; + case PROTOCOL_BINARY_CMD_SCRUB: + fprintf(stderr, "%s:%d PROTOCOL_BINARY_CMD_SCRUB\n", __FILE__, __LINE__); + return; + default: abort(); } } @@ -201,42 +287,38 @@ static void print_cmd(protocol_binary_command cmd) * @param flags the flags for the item * @param cas the CAS id for the item */ -static protocol_binary_response_status get_response_handler(const void *cookie, - const void *key, - uint16_t keylen, - const void *body, - uint32_t bodylen, - uint32_t flags, - uint64_t cas) -{ - memcached_protocol_client_st *client= (void*)cookie; - uint8_t opcode= client->current_command->request.opcode; - - if (opcode == PROTOCOL_BINARY_CMD_GET || opcode == PROTOCOL_BINARY_CMD_GETQ) - { - keylen= 0; +static protocol_binary_response_status get_response_handler(const void *cookie, const void *key, + uint16_t keylen, const void *body, + uint32_t bodylen, uint32_t flags, + uint64_t cas) { + memcached_protocol_client_st *client = (void *) cookie; + uint8_t opcode = client->current_command->request.opcode; + + if (opcode == PROTOCOL_BINARY_CMD_GET || opcode == PROTOCOL_BINARY_CMD_GETQ) { + keylen = 0; } - protocol_binary_response_get response= { - .message.header.response= { - .magic= PROTOCOL_BINARY_RES, - .opcode= opcode, - .status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), - .opaque= client->current_command->request.opaque, - .cas= memcached_htonll(cas), - .keylen= htons(keylen), - .extlen= 4, - .bodylen= htonl(bodylen + keylen + 4), - }, + protocol_binary_response_get response = { + .message.header.response = + { + .magic = PROTOCOL_BINARY_RES, + .opcode = opcode, + .status = htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), + .opaque = client->current_command->request.opaque, + .cas = memcached_htonll(cas), + .keylen = htons(keylen), + .extlen = 4, + .bodylen = htonl(bodylen + keylen + 4), + }, }; - response.message.body.flags= htonl(flags); + response.message.body.flags = htonl(flags); protocol_binary_response_status rval; - const protocol_binary_response_status success= PROTOCOL_BINARY_RESPONSE_SUCCESS; - if ((rval= client->root->spool(client, response.bytes, sizeof(response.bytes))) != success || - (rval= client->root->spool(client, key, keylen)) != success || - (rval= client->root->spool(client, body, bodylen)) != success) + const protocol_binary_response_status success = PROTOCOL_BINARY_RESPONSE_SUCCESS; + if ((rval = client->root->spool(client, response.bytes, sizeof(response.bytes))) != success + || (rval = client->root->spool(client, key, keylen)) != success + || (rval = client->root->spool(client, body, bodylen)) != success) { return rval; } @@ -252,32 +334,26 @@ static protocol_binary_response_status get_response_handler(const void *cookie, * @param body the length of the body * @param bodylen the length of the body */ -static protocol_binary_response_status stat_response_handler(const void *cookie, - const void *key, - uint16_t keylen, - const void *body, - uint32_t bodylen) -{ - - memcached_protocol_client_st *client= (void*)cookie; - - protocol_binary_response_no_extras response= { - .message.header.response= { - .magic= PROTOCOL_BINARY_RES, - .opcode= client->current_command->request.opcode, - .status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), - .opaque= client->current_command->request.opaque, - .keylen= htons(keylen), - .bodylen= htonl(bodylen + keylen), - .cas= 0 - }, +static protocol_binary_response_status stat_response_handler(const void *cookie, const void *key, + uint16_t keylen, const void *body, + uint32_t bodylen) { + memcached_protocol_client_st *client = (void *) cookie; + + protocol_binary_response_no_extras response = { + .message.header.response = {.magic = PROTOCOL_BINARY_RES, + .opcode = client->current_command->request.opcode, + .status = htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), + .opaque = client->current_command->request.opaque, + .keylen = htons(keylen), + .bodylen = htonl(bodylen + keylen), + .cas = 0}, }; protocol_binary_response_status rval; - const protocol_binary_response_status success= PROTOCOL_BINARY_RESPONSE_SUCCESS; - if ((rval= client->root->spool(client, response.bytes, sizeof(response.bytes))) != success || - (rval= client->root->spool(client, key, keylen)) != success || - (rval= client->root->spool(client, body, bodylen)) != success) + const protocol_binary_response_status success = PROTOCOL_BINARY_RESPONSE_SUCCESS; + if ((rval = client->root->spool(client, response.bytes, sizeof(response.bytes))) != success + || (rval = client->root->spool(client, key, keylen)) != success + || (rval = client->root->spool(client, body, bodylen)) != success) { return rval; } @@ -291,28 +367,23 @@ static protocol_binary_response_status stat_response_handler(const void *cookie, * @param text the length of the body * @param textlen the length of the body */ -static protocol_binary_response_status version_response_handler(const void *cookie, - const void *text, - uint32_t textlen) -{ - - memcached_protocol_client_st *client= (void*)cookie; - - protocol_binary_response_no_extras response= { - .message.header.response= { - .magic= PROTOCOL_BINARY_RES, - .opcode= client->current_command->request.opcode, - .status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), - .opaque= client->current_command->request.opaque, - .bodylen= htonl(textlen), - .cas= 0 - }, +static protocol_binary_response_status +version_response_handler(const void *cookie, const void *text, uint32_t textlen) { + memcached_protocol_client_st *client = (void *) cookie; + + protocol_binary_response_no_extras response = { + .message.header.response = {.magic = PROTOCOL_BINARY_RES, + .opcode = client->current_command->request.opcode, + .status = htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), + .opaque = client->current_command->request.opaque, + .bodylen = htonl(textlen), + .cas = 0}, }; protocol_binary_response_status rval; - const protocol_binary_response_status success= PROTOCOL_BINARY_RESPONSE_SUCCESS; - if ((rval= client->root->spool(client, response.bytes, sizeof(response.bytes))) != success || - (rval= client->root->spool(client, text, textlen)) != success) + const protocol_binary_response_status success = PROTOCOL_BINARY_RESPONSE_SUCCESS; + if ((rval = client->root->spool(client, response.bytes, sizeof(response.bytes))) != success + || (rval = client->root->spool(client, text, textlen)) != success) { return rval; } @@ -328,49 +399,37 @@ static protocol_binary_response_status version_response_handler(const void *cook * @return the result of the operation */ static protocol_binary_response_status -add_command_handler(const void *cookie, - protocol_binary_request_header *header, - memcached_binary_protocol_raw_response_handler response_handler) -{ +add_command_handler(const void *cookie, protocol_binary_request_header *header, + memcached_binary_protocol_raw_response_handler response_handler) { protocol_binary_response_status rval; - memcached_protocol_client_st *client= (void*)cookie; - if (client->root->callback->interface.v1.add != NULL) - { - uint16_t keylen= ntohs(header->request.keylen); - uint32_t datalen= ntohl(header->request.bodylen) - keylen - 8; - protocol_binary_request_add *request= (void*)header; - uint32_t flags= ntohl(request->message.body.flags); - uint32_t timeout= ntohl(request->message.body.expiration); - char *key= ((char*)header) + sizeof(*header) + 8; - char *data= key + keylen; + memcached_protocol_client_st *client = (void *) cookie; + if (client->root->callback->interface.v1.add != NULL) { + uint16_t keylen = ntohs(header->request.keylen); + uint32_t datalen = ntohl(header->request.bodylen) - keylen - 8; + protocol_binary_request_add *request = (void *) header; + uint32_t flags = ntohl(request->message.body.flags); + uint32_t timeout = ntohl(request->message.body.expiration); + char *key = ((char *) header) + sizeof(*header) + 8; + char *data = key + keylen; uint64_t cas; - rval= client->root->callback->interface.v1.add(cookie, key, keylen, - data, datalen, flags, - timeout, &cas); + rval = client->root->callback->interface.v1.add(cookie, key, keylen, data, datalen, flags, + timeout, &cas); - if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS && - header->request.opcode == PROTOCOL_BINARY_CMD_ADD) - { + if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS + && header->request.opcode == PROTOCOL_BINARY_CMD_ADD) { /* Send a positive request */ - protocol_binary_response_no_extras response= { - .message= { - .header.response= { - .magic= PROTOCOL_BINARY_RES, - .opcode= PROTOCOL_BINARY_CMD_ADD, - .status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), - .opaque= header->request.opaque, - .cas= memcached_ntohll(cas) - } - } - }; - rval= response_handler(cookie, header, (void*)&response); + protocol_binary_response_no_extras response = { + .message = {.header.response = {.magic = PROTOCOL_BINARY_RES, + .opcode = PROTOCOL_BINARY_CMD_ADD, + .status = htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), + .opaque = header->request.opaque, + .cas = memcached_ntohll(cas)}}}; + rval = response_handler(cookie, header, (void *) &response); } - } - else - { - rval= PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; + } else { + rval = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; } return rval; @@ -384,51 +443,40 @@ add_command_handler(const void *cookie, * @return the result of the operation */ static protocol_binary_response_status -decrement_command_handler(const void *cookie, - protocol_binary_request_header *header, - memcached_binary_protocol_raw_response_handler response_handler) -{ - (void)response_handler; +decrement_command_handler(const void *cookie, protocol_binary_request_header *header, + memcached_binary_protocol_raw_response_handler response_handler) { + (void) response_handler; protocol_binary_response_status rval; - memcached_protocol_client_st *client= (void*)cookie; - if (client->root->callback->interface.v1.decrement != NULL) - { - uint16_t keylen= ntohs(header->request.keylen); - protocol_binary_request_decr *request= (void*)header; - uint64_t init= memcached_ntohll(request->message.body.initial); - uint64_t delta= memcached_ntohll(request->message.body.delta); - uint32_t timeout= ntohl(request->message.body.expiration); - void *key= request->bytes + sizeof(request->bytes); + memcached_protocol_client_st *client = (void *) cookie; + if (client->root->callback->interface.v1.decrement != NULL) { + uint16_t keylen = ntohs(header->request.keylen); + protocol_binary_request_decr *request = (void *) header; + uint64_t init = memcached_ntohll(request->message.body.initial); + uint64_t delta = memcached_ntohll(request->message.body.delta); + uint32_t timeout = ntohl(request->message.body.expiration); + void *key = request->bytes + sizeof(request->bytes); uint64_t result; uint64_t cas; - rval= client->root->callback->interface.v1.decrement(cookie, key, keylen, - delta, init, timeout, - &result, &cas); - if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS && - header->request.opcode == PROTOCOL_BINARY_CMD_DECREMENT) + rval = client->root->callback->interface.v1.decrement(cookie, key, keylen, delta, init, timeout, + &result, &cas); + if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS + && header->request.opcode == PROTOCOL_BINARY_CMD_DECREMENT) { /* Send a positive request */ - protocol_binary_response_decr response= { - .message= { - .header.response= { - .magic= PROTOCOL_BINARY_RES, - .opcode= PROTOCOL_BINARY_CMD_DECREMENT, - .status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), - .opaque= header->request.opaque, - .cas= memcached_ntohll(cas), - .bodylen= htonl(8) - }, - .body.value= memcached_htonll(result) - } - }; - rval= response_handler(cookie, header, (void*)&response); + protocol_binary_response_decr response = { + .message = {.header.response = {.magic = PROTOCOL_BINARY_RES, + .opcode = PROTOCOL_BINARY_CMD_DECREMENT, + .status = htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), + .opaque = header->request.opaque, + .cas = memcached_ntohll(cas), + .bodylen = htonl(8)}, + .body.value = memcached_htonll(result)}}; + rval = response_handler(cookie, header, (void *) &response); } - } - else - { - rval= PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; + } else { + rval = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; } return rval; @@ -441,40 +489,33 @@ decrement_command_handler(const void *cookie, * @param response_handler not used * @return the result of the operation */ -static protocol_binary_response_status delete_command_handler(const void *cookie, - protocol_binary_request_header *header, - memcached_binary_protocol_raw_response_handler response_handler) -{ - (void)response_handler; +static protocol_binary_response_status +delete_command_handler(const void *cookie, protocol_binary_request_header *header, + memcached_binary_protocol_raw_response_handler response_handler) { + (void) response_handler; protocol_binary_response_status rval; - memcached_protocol_client_st *client= (void*)cookie; - if (client->root->callback->interface.v1.delete_object != NULL) - { - uint16_t keylen= ntohs(header->request.keylen); - void *key= (header +1); - uint64_t cas= memcached_ntohll(header->request.cas); - rval= client->root->callback->interface.v1.delete_object(cookie, key, keylen, cas); - if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS && - header->request.opcode == PROTOCOL_BINARY_CMD_DELETE) + memcached_protocol_client_st *client = (void *) cookie; + if (client->root->callback->interface.v1.delete_object != NULL) { + uint16_t keylen = ntohs(header->request.keylen); + void *key = (header + 1); + uint64_t cas = memcached_ntohll(header->request.cas); + rval = client->root->callback->interface.v1.delete_object(cookie, key, keylen, cas); + if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS + && header->request.opcode == PROTOCOL_BINARY_CMD_DELETE) { /* Send a positive request */ - protocol_binary_response_no_extras response= { - .message= { - .header.response= { - .magic= PROTOCOL_BINARY_RES, - .opcode= PROTOCOL_BINARY_CMD_DELETE, - .status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), - .opaque= header->request.opaque, - } - } - }; - rval= response_handler(cookie, header, (void*)&response); + protocol_binary_response_no_extras response = { + .message = {.header.response = { + .magic = PROTOCOL_BINARY_RES, + .opcode = PROTOCOL_BINARY_CMD_DELETE, + .status = htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), + .opaque = header->request.opaque, + }}}; + rval = response_handler(cookie, header, (void *) &response); } - } - else - { - rval= PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; + } else { + rval = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; } return rval; @@ -488,44 +529,34 @@ static protocol_binary_response_status delete_command_handler(const void *cookie * @return the result of the operation */ static protocol_binary_response_status -flush_command_handler(const void *cookie, - protocol_binary_request_header *header, - memcached_binary_protocol_raw_response_handler response_handler) -{ - (void)response_handler; +flush_command_handler(const void *cookie, protocol_binary_request_header *header, + memcached_binary_protocol_raw_response_handler response_handler) { + (void) response_handler; protocol_binary_response_status rval; - memcached_protocol_client_st *client= (void*)cookie; - if (client->root->callback->interface.v1.flush_object != NULL) - { - protocol_binary_request_flush *flush_object= (void*)header; - uint32_t timeout= 0; - if (htonl(header->request.bodylen) == 4) - { - timeout= ntohl(flush_object->message.body.expiration); + memcached_protocol_client_st *client = (void *) cookie; + if (client->root->callback->interface.v1.flush_object != NULL) { + protocol_binary_request_flush *flush_object = (void *) header; + uint32_t timeout = 0; + if (htonl(header->request.bodylen) == 4) { + timeout = ntohl(flush_object->message.body.expiration); } - rval= client->root->callback->interface.v1.flush_object(cookie, timeout); - if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS && - header->request.opcode == PROTOCOL_BINARY_CMD_FLUSH) - { + rval = client->root->callback->interface.v1.flush_object(cookie, timeout); + if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS + && header->request.opcode == PROTOCOL_BINARY_CMD_FLUSH) { /* Send a positive request */ - protocol_binary_response_no_extras response= { - .message= { - .header.response= { - .magic= PROTOCOL_BINARY_RES, - .opcode= PROTOCOL_BINARY_CMD_FLUSH, - .status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), - .opaque= header->request.opaque, - } - } - }; - rval= response_handler(cookie, header, (void*)&response); + protocol_binary_response_no_extras response = { + .message = {.header.response = { + .magic = PROTOCOL_BINARY_RES, + .opcode = PROTOCOL_BINARY_CMD_FLUSH, + .status = htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), + .opaque = header->request.opaque, + }}}; + rval = response_handler(cookie, header, (void *) &response); } - } - else - { - rval= PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; + } else { + rval = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; } return rval; @@ -539,32 +570,26 @@ flush_command_handler(const void *cookie, * @return the result of the operation */ static protocol_binary_response_status -get_command_handler(const void *cookie, - protocol_binary_request_header *header, - memcached_binary_protocol_raw_response_handler response_handler) -{ - (void)response_handler; +get_command_handler(const void *cookie, protocol_binary_request_header *header, + memcached_binary_protocol_raw_response_handler response_handler) { + (void) response_handler; protocol_binary_response_status rval; - memcached_protocol_client_st *client= (void*)cookie; - if (client->root->callback->interface.v1.get != NULL) - { - uint16_t keylen= ntohs(header->request.keylen); - void *key= (header + 1); - rval= client->root->callback->interface.v1.get(cookie, key, keylen, - get_response_handler); - - if (rval == PROTOCOL_BINARY_RESPONSE_KEY_ENOENT && - (header->request.opcode == PROTOCOL_BINARY_CMD_GETQ || - header->request.opcode == PROTOCOL_BINARY_CMD_GETKQ)) + memcached_protocol_client_st *client = (void *) cookie; + if (client->root->callback->interface.v1.get != NULL) { + uint16_t keylen = ntohs(header->request.keylen); + void *key = (header + 1); + rval = client->root->callback->interface.v1.get(cookie, key, keylen, get_response_handler); + + if (rval == PROTOCOL_BINARY_RESPONSE_KEY_ENOENT + && (header->request.opcode == PROTOCOL_BINARY_CMD_GETQ + || header->request.opcode == PROTOCOL_BINARY_CMD_GETKQ)) { /* Quiet commands shouldn't respond on cache misses */ - rval= PROTOCOL_BINARY_RESPONSE_SUCCESS; + rval = PROTOCOL_BINARY_RESPONSE_SUCCESS; } - } - else - { - rval= PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; + } else { + rval = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; } return rval; @@ -578,52 +603,41 @@ get_command_handler(const void *cookie, * @return the result of the operation */ static protocol_binary_response_status -increment_command_handler(const void *cookie, - protocol_binary_request_header *header, - memcached_binary_protocol_raw_response_handler response_handler) -{ - (void)response_handler; +increment_command_handler(const void *cookie, protocol_binary_request_header *header, + memcached_binary_protocol_raw_response_handler response_handler) { + (void) response_handler; protocol_binary_response_status rval; - memcached_protocol_client_st *client= (void*)cookie; - if (client->root->callback->interface.v1.increment != NULL) - { - uint16_t keylen= ntohs(header->request.keylen); - protocol_binary_request_incr *request= (void*)header; - uint64_t init= memcached_ntohll(request->message.body.initial); - uint64_t delta= memcached_ntohll(request->message.body.delta); - uint32_t timeout= ntohl(request->message.body.expiration); - void *key= request->bytes + sizeof(request->bytes); + memcached_protocol_client_st *client = (void *) cookie; + if (client->root->callback->interface.v1.increment != NULL) { + uint16_t keylen = ntohs(header->request.keylen); + protocol_binary_request_incr *request = (void *) header; + uint64_t init = memcached_ntohll(request->message.body.initial); + uint64_t delta = memcached_ntohll(request->message.body.delta); + uint32_t timeout = ntohl(request->message.body.expiration); + void *key = request->bytes + sizeof(request->bytes); uint64_t cas; uint64_t result; - rval= client->root->callback->interface.v1.increment(cookie, key, keylen, - delta, init, timeout, - &result, &cas); - if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS && - header->request.opcode == PROTOCOL_BINARY_CMD_INCREMENT) + rval = client->root->callback->interface.v1.increment(cookie, key, keylen, delta, init, timeout, + &result, &cas); + if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS + && header->request.opcode == PROTOCOL_BINARY_CMD_INCREMENT) { /* Send a positive request */ - protocol_binary_response_incr response= { - .message= { - .header.response= { - .magic= PROTOCOL_BINARY_RES, - .opcode= PROTOCOL_BINARY_CMD_INCREMENT, - .status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), - .opaque= header->request.opaque, - .cas= memcached_ntohll(cas), - .bodylen= htonl(8) - }, - .body.value= memcached_htonll(result) - } - }; - - rval= response_handler(cookie, header, (void*)&response); + protocol_binary_response_incr response = { + .message = {.header.response = {.magic = PROTOCOL_BINARY_RES, + .opcode = PROTOCOL_BINARY_CMD_INCREMENT, + .status = htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), + .opaque = header->request.opaque, + .cas = memcached_ntohll(cas), + .bodylen = htonl(8)}, + .body.value = memcached_htonll(result)}}; + + rval = response_handler(cookie, header, (void *) &response); } - } - else - { - rval= PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; + } else { + rval = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; } return rval; @@ -639,28 +653,22 @@ increment_command_handler(const void *cookie, * @return the result of the operation */ static protocol_binary_response_status -noop_command_handler(const void *cookie, - protocol_binary_request_header *header, - memcached_binary_protocol_raw_response_handler response_handler) -{ - memcached_protocol_client_st *client= (void*)cookie; - if (client->root->callback->interface.v1.noop != NULL) - { +noop_command_handler(const void *cookie, protocol_binary_request_header *header, + memcached_binary_protocol_raw_response_handler response_handler) { + memcached_protocol_client_st *client = (void *) cookie; + if (client->root->callback->interface.v1.noop != NULL) { client->root->callback->interface.v1.noop(cookie); } - protocol_binary_response_no_extras response= { - .message= { - .header.response= { - .magic= PROTOCOL_BINARY_RES, - .opcode= PROTOCOL_BINARY_CMD_NOOP, - .status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), - .opaque= header->request.opaque, - } - } - }; + protocol_binary_response_no_extras response = { + .message = {.header.response = { + .magic = PROTOCOL_BINARY_RES, + .opcode = PROTOCOL_BINARY_CMD_NOOP, + .status = htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), + .opaque = header->request.opaque, + }}}; - return response_handler(cookie, header, (void*)&response); + return response_handler(cookie, header, (void *) &response); } /** @@ -671,47 +679,41 @@ noop_command_handler(const void *cookie, * @return the result of the operation */ static protocol_binary_response_status -append_command_handler(const void *cookie, - protocol_binary_request_header *header, - memcached_binary_protocol_raw_response_handler response_handler) -{ - (void)response_handler; +append_command_handler(const void *cookie, protocol_binary_request_header *header, + memcached_binary_protocol_raw_response_handler response_handler) { + (void) response_handler; protocol_binary_response_status rval; - memcached_protocol_client_st *client= (void*)cookie; - if (client->root->callback->interface.v1.append != NULL) - { - uint16_t keylen= ntohs(header->request.keylen); - uint32_t datalen= ntohl(header->request.bodylen) - keylen; - char *key= (void*)(header +1); - char *data= key +keylen; - uint64_t cas= memcached_ntohll(header->request.cas); + memcached_protocol_client_st *client = (void *) cookie; + if (client->root->callback->interface.v1.append != NULL) { + uint16_t keylen = ntohs(header->request.keylen); + uint32_t datalen = ntohl(header->request.bodylen) - keylen; + char *key = (void *) (header + 1); + char *data = key + keylen; + uint64_t cas = memcached_ntohll(header->request.cas); uint64_t result_cas; - rval= client->root->callback->interface.v1.append(cookie, key, keylen, - data, datalen, cas, - &result_cas); - if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS && - header->request.opcode == PROTOCOL_BINARY_CMD_APPEND) + rval = client->root->callback->interface.v1.append(cookie, key, keylen, data, datalen, cas, + &result_cas); + if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS + && header->request.opcode == PROTOCOL_BINARY_CMD_APPEND) { /* Send a positive request */ - protocol_binary_response_no_extras response= { - .message= { - .header.response= { - .magic= PROTOCOL_BINARY_RES, - .opcode= PROTOCOL_BINARY_CMD_APPEND, - .status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), - .opaque= header->request.opaque, - .cas= memcached_ntohll(result_cas), - }, - } - }; - rval= response_handler(cookie, header, (void*)&response); + protocol_binary_response_no_extras response = { + .message = { + .header.response = + { + .magic = PROTOCOL_BINARY_RES, + .opcode = PROTOCOL_BINARY_CMD_APPEND, + .status = htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), + .opaque = header->request.opaque, + .cas = memcached_ntohll(result_cas), + }, + }}; + rval = response_handler(cookie, header, (void *) &response); } - } - else - { - rval= PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; + } else { + rval = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; } return rval; @@ -725,46 +727,40 @@ append_command_handler(const void *cookie, * @return the result of the operation */ static protocol_binary_response_status -prepend_command_handler(const void *cookie, - protocol_binary_request_header *header, - memcached_binary_protocol_raw_response_handler response_handler) -{ - (void)response_handler; +prepend_command_handler(const void *cookie, protocol_binary_request_header *header, + memcached_binary_protocol_raw_response_handler response_handler) { + (void) response_handler; protocol_binary_response_status rval; - memcached_protocol_client_st *client= (void*)cookie; - if (client->root->callback->interface.v1.prepend != NULL) - { - uint16_t keylen= ntohs(header->request.keylen); - uint32_t datalen= ntohl(header->request.bodylen) - keylen; - char *key= (char*)(header + 1); - char *data= key + keylen; - uint64_t cas= memcached_ntohll(header->request.cas); + memcached_protocol_client_st *client = (void *) cookie; + if (client->root->callback->interface.v1.prepend != NULL) { + uint16_t keylen = ntohs(header->request.keylen); + uint32_t datalen = ntohl(header->request.bodylen) - keylen; + char *key = (char *) (header + 1); + char *data = key + keylen; + uint64_t cas = memcached_ntohll(header->request.cas); uint64_t result_cas; - rval= client->root->callback->interface.v1.prepend(cookie, key, keylen, - data, datalen, cas, - &result_cas); - if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS && - header->request.opcode == PROTOCOL_BINARY_CMD_PREPEND) + rval = client->root->callback->interface.v1.prepend(cookie, key, keylen, data, datalen, cas, + &result_cas); + if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS + && header->request.opcode == PROTOCOL_BINARY_CMD_PREPEND) { /* Send a positive request */ - protocol_binary_response_no_extras response= { - .message= { - .header.response= { - .magic= PROTOCOL_BINARY_RES, - .opcode= PROTOCOL_BINARY_CMD_PREPEND, - .status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), - .opaque= header->request.opaque, - .cas= memcached_ntohll(result_cas), - }, - } - }; - rval= response_handler(cookie, header, (void*)&response); + protocol_binary_response_no_extras response = { + .message = { + .header.response = + { + .magic = PROTOCOL_BINARY_RES, + .opcode = PROTOCOL_BINARY_CMD_PREPEND, + .status = htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), + .opaque = header->request.opaque, + .cas = memcached_ntohll(result_cas), + }, + }}; + rval = response_handler(cookie, header, (void *) &response); } - } - else - { - rval= PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; + } else { + rval = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; } return rval; @@ -778,30 +774,21 @@ prepend_command_handler(const void *cookie, * @return the result of the operation */ static protocol_binary_response_status -quit_command_handler(const void *cookie, - protocol_binary_request_header *header, - memcached_binary_protocol_raw_response_handler response_handler) -{ - memcached_protocol_client_st *client= (void*)cookie; - if (client->root->callback->interface.v1.quit != NULL) - { +quit_command_handler(const void *cookie, protocol_binary_request_header *header, + memcached_binary_protocol_raw_response_handler response_handler) { + memcached_protocol_client_st *client = (void *) cookie; + if (client->root->callback->interface.v1.quit != NULL) { client->root->callback->interface.v1.quit(cookie); } - protocol_binary_response_no_extras response= { - .message= { - .header.response= { - .magic= PROTOCOL_BINARY_RES, - .opcode= PROTOCOL_BINARY_CMD_QUIT, - .status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), - .opaque= header->request.opaque - } - } - }; + protocol_binary_response_no_extras response = { + .message = {.header.response = {.magic = PROTOCOL_BINARY_RES, + .opcode = PROTOCOL_BINARY_CMD_QUIT, + .status = htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), + .opaque = header->request.opaque}}}; - if (header->request.opcode == PROTOCOL_BINARY_CMD_QUIT) - { - response_handler(cookie, header, (void*)&response); + if (header->request.opcode == PROTOCOL_BINARY_CMD_QUIT) { + response_handler(cookie, header, (void *) &response); } /* I need a better way to signal to close the connection */ @@ -816,51 +803,44 @@ quit_command_handler(const void *cookie, * @return the result of the operation */ static protocol_binary_response_status -replace_command_handler(const void *cookie, - protocol_binary_request_header *header, - memcached_binary_protocol_raw_response_handler response_handler) -{ - (void)response_handler; +replace_command_handler(const void *cookie, protocol_binary_request_header *header, + memcached_binary_protocol_raw_response_handler response_handler) { + (void) response_handler; protocol_binary_response_status rval; - memcached_protocol_client_st *client= (void*)cookie; - if (client->root->callback->interface.v1.replace != NULL) - { - uint16_t keylen= ntohs(header->request.keylen); - uint32_t datalen= ntohl(header->request.bodylen) - keylen - 8; - protocol_binary_request_replace *request= (void*)header; - uint32_t flags= ntohl(request->message.body.flags); - uint32_t timeout= ntohl(request->message.body.expiration); - char *key= ((char*)header) + sizeof(*header) + 8; - char *data= key + keylen; - uint64_t cas= memcached_ntohll(header->request.cas); + memcached_protocol_client_st *client = (void *) cookie; + if (client->root->callback->interface.v1.replace != NULL) { + uint16_t keylen = ntohs(header->request.keylen); + uint32_t datalen = ntohl(header->request.bodylen) - keylen - 8; + protocol_binary_request_replace *request = (void *) header; + uint32_t flags = ntohl(request->message.body.flags); + uint32_t timeout = ntohl(request->message.body.expiration); + char *key = ((char *) header) + sizeof(*header) + 8; + char *data = key + keylen; + uint64_t cas = memcached_ntohll(header->request.cas); uint64_t result_cas; - rval= client->root->callback->interface.v1.replace(cookie, key, keylen, - data, datalen, flags, - timeout, cas, - &result_cas); - if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS && - header->request.opcode == PROTOCOL_BINARY_CMD_REPLACE) + rval = client->root->callback->interface.v1.replace(cookie, key, keylen, data, datalen, flags, + timeout, cas, &result_cas); + if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS + && header->request.opcode == PROTOCOL_BINARY_CMD_REPLACE) { /* Send a positive request */ - protocol_binary_response_no_extras response= { - .message= { - .header.response= { - .magic= PROTOCOL_BINARY_RES, - .opcode= PROTOCOL_BINARY_CMD_REPLACE, - .status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), - .opaque= header->request.opaque, - .cas= memcached_ntohll(result_cas), - }, - } - }; - rval= response_handler(cookie, header, (void*)&response); + protocol_binary_response_no_extras response = { + .message = { + .header.response = + { + .magic = PROTOCOL_BINARY_RES, + .opcode = PROTOCOL_BINARY_CMD_REPLACE, + .status = htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), + .opaque = header->request.opaque, + .cas = memcached_ntohll(result_cas), + }, + }}; + rval = response_handler(cookie, header, (void *) &response); } - } - else - { - rval= PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; + } else { + rval = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; } return rval; @@ -873,51 +853,44 @@ replace_command_handler(const void *cookie, * @param response_handler not used * @return the result of the operation */ -static protocol_binary_response_status set_command_handler(const void *cookie, - protocol_binary_request_header *header, - memcached_binary_protocol_raw_response_handler response_handler) -{ - (void)response_handler; +static protocol_binary_response_status +set_command_handler(const void *cookie, protocol_binary_request_header *header, + memcached_binary_protocol_raw_response_handler response_handler) { + (void) response_handler; protocol_binary_response_status rval; - memcached_protocol_client_st *client= (void*)cookie; - if (client->root->callback->interface.v1.set != NULL) - { - uint16_t keylen= ntohs(header->request.keylen); - uint32_t datalen= ntohl(header->request.bodylen) - keylen - 8; - protocol_binary_request_replace *request= (void*)header; - uint32_t flags= ntohl(request->message.body.flags); - uint32_t timeout= ntohl(request->message.body.expiration); - char *key= ((char*)header) + sizeof(*header) + 8; - char *data= key + keylen; - uint64_t cas= memcached_ntohll(header->request.cas); + memcached_protocol_client_st *client = (void *) cookie; + if (client->root->callback->interface.v1.set != NULL) { + uint16_t keylen = ntohs(header->request.keylen); + uint32_t datalen = ntohl(header->request.bodylen) - keylen - 8; + protocol_binary_request_replace *request = (void *) header; + uint32_t flags = ntohl(request->message.body.flags); + uint32_t timeout = ntohl(request->message.body.expiration); + char *key = ((char *) header) + sizeof(*header) + 8; + char *data = key + keylen; + uint64_t cas = memcached_ntohll(header->request.cas); uint64_t result_cas; - - rval= client->root->callback->interface.v1.set(cookie, key, keylen, - data, datalen, flags, - timeout, cas, &result_cas); - if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS && - header->request.opcode == PROTOCOL_BINARY_CMD_SET) - { + rval = client->root->callback->interface.v1.set(cookie, key, keylen, data, datalen, flags, + timeout, cas, &result_cas); + if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS + && header->request.opcode == PROTOCOL_BINARY_CMD_SET) { /* Send a positive request */ - protocol_binary_response_no_extras response= { - .message= { - .header.response= { - .magic= PROTOCOL_BINARY_RES, - .opcode= PROTOCOL_BINARY_CMD_SET, - .status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), - .opaque= header->request.opaque, - .cas= memcached_ntohll(result_cas), - }, - } - }; - rval= response_handler(cookie, header, (void*)&response); + protocol_binary_response_no_extras response = { + .message = { + .header.response = + { + .magic = PROTOCOL_BINARY_RES, + .opcode = PROTOCOL_BINARY_CMD_SET, + .status = htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), + .opaque = header->request.opaque, + .cas = memcached_ntohll(result_cas), + }, + }}; + rval = response_handler(cookie, header, (void *) &response); } - } - else - { - rval= PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; + } else { + rval = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; } return rval; @@ -931,26 +904,19 @@ static protocol_binary_response_status set_command_handler(const void *cookie, * @return the result of the operation */ static protocol_binary_response_status -stat_command_handler(const void *cookie, - protocol_binary_request_header *header, - memcached_binary_protocol_raw_response_handler response_handler) -{ - (void)response_handler; +stat_command_handler(const void *cookie, protocol_binary_request_header *header, + memcached_binary_protocol_raw_response_handler response_handler) { + (void) response_handler; protocol_binary_response_status rval; - memcached_protocol_client_st *client= (void*)cookie; - if (client->root->callback->interface.v1.stat != NULL) - { - uint16_t keylen= ntohs(header->request.keylen); + memcached_protocol_client_st *client = (void *) cookie; + if (client->root->callback->interface.v1.stat != NULL) { + uint16_t keylen = ntohs(header->request.keylen); - rval= client->root->callback->interface.v1.stat(cookie, - (void*)(header + 1), - keylen, - stat_response_handler); - } - else - { - rval= PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; + rval = client->root->callback->interface.v1.stat(cookie, (void *) (header + 1), keylen, + stat_response_handler); + } else { + rval = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; } return rval; @@ -964,23 +930,17 @@ stat_command_handler(const void *cookie, * @return the result of the operation */ static protocol_binary_response_status -version_command_handler(const void *cookie, - protocol_binary_request_header *header, - memcached_binary_protocol_raw_response_handler response_handler) -{ - (void)response_handler; - (void)header; +version_command_handler(const void *cookie, protocol_binary_request_header *header, + memcached_binary_protocol_raw_response_handler response_handler) { + (void) response_handler; + (void) header; protocol_binary_response_status rval; - memcached_protocol_client_st *client= (void*)cookie; - if (client->root->callback->interface.v1.version != NULL) - { - rval= client->root->callback->interface.v1.version(cookie, - version_response_handler); - } - else - { - rval= PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; + memcached_protocol_client_st *client = (void *) cookie; + if (client->root->callback->interface.v1.version != NULL) { + rval = client->root->callback->interface.v1.version(cookie, version_response_handler); + } else { + rval = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; } return rval; @@ -989,34 +949,34 @@ version_command_handler(const void *cookie, /** * The map to remap between the com codes and the v1 logical setting */ -static memcached_binary_protocol_command_handler comcode_v0_v1_remap[256]= { - [PROTOCOL_BINARY_CMD_ADDQ]= add_command_handler, - [PROTOCOL_BINARY_CMD_ADD]= add_command_handler, - [PROTOCOL_BINARY_CMD_APPENDQ]= append_command_handler, - [PROTOCOL_BINARY_CMD_APPEND]= append_command_handler, - [PROTOCOL_BINARY_CMD_DECREMENTQ]= decrement_command_handler, - [PROTOCOL_BINARY_CMD_DECREMENT]= decrement_command_handler, - [PROTOCOL_BINARY_CMD_DELETEQ]= delete_command_handler, - [PROTOCOL_BINARY_CMD_DELETE]= delete_command_handler, - [PROTOCOL_BINARY_CMD_FLUSHQ]= flush_command_handler, - [PROTOCOL_BINARY_CMD_FLUSH]= flush_command_handler, - [PROTOCOL_BINARY_CMD_GETKQ]= get_command_handler, - [PROTOCOL_BINARY_CMD_GETK]= get_command_handler, - [PROTOCOL_BINARY_CMD_GETQ]= get_command_handler, - [PROTOCOL_BINARY_CMD_GET]= get_command_handler, - [PROTOCOL_BINARY_CMD_INCREMENTQ]= increment_command_handler, - [PROTOCOL_BINARY_CMD_INCREMENT]= increment_command_handler, - [PROTOCOL_BINARY_CMD_NOOP]= noop_command_handler, - [PROTOCOL_BINARY_CMD_PREPENDQ]= prepend_command_handler, - [PROTOCOL_BINARY_CMD_PREPEND]= prepend_command_handler, - [PROTOCOL_BINARY_CMD_QUITQ]= quit_command_handler, - [PROTOCOL_BINARY_CMD_QUIT]= quit_command_handler, - [PROTOCOL_BINARY_CMD_REPLACEQ]= replace_command_handler, - [PROTOCOL_BINARY_CMD_REPLACE]= replace_command_handler, - [PROTOCOL_BINARY_CMD_SETQ]= set_command_handler, - [PROTOCOL_BINARY_CMD_SET]= set_command_handler, - [PROTOCOL_BINARY_CMD_STAT]= stat_command_handler, - [PROTOCOL_BINARY_CMD_VERSION]= version_command_handler, +static memcached_binary_protocol_command_handler comcode_v0_v1_remap[256] = { + [PROTOCOL_BINARY_CMD_ADDQ] = add_command_handler, + [PROTOCOL_BINARY_CMD_ADD] = add_command_handler, + [PROTOCOL_BINARY_CMD_APPENDQ] = append_command_handler, + [PROTOCOL_BINARY_CMD_APPEND] = append_command_handler, + [PROTOCOL_BINARY_CMD_DECREMENTQ] = decrement_command_handler, + [PROTOCOL_BINARY_CMD_DECREMENT] = decrement_command_handler, + [PROTOCOL_BINARY_CMD_DELETEQ] = delete_command_handler, + [PROTOCOL_BINARY_CMD_DELETE] = delete_command_handler, + [PROTOCOL_BINARY_CMD_FLUSHQ] = flush_command_handler, + [PROTOCOL_BINARY_CMD_FLUSH] = flush_command_handler, + [PROTOCOL_BINARY_CMD_GETKQ] = get_command_handler, + [PROTOCOL_BINARY_CMD_GETK] = get_command_handler, + [PROTOCOL_BINARY_CMD_GETQ] = get_command_handler, + [PROTOCOL_BINARY_CMD_GET] = get_command_handler, + [PROTOCOL_BINARY_CMD_INCREMENTQ] = increment_command_handler, + [PROTOCOL_BINARY_CMD_INCREMENT] = increment_command_handler, + [PROTOCOL_BINARY_CMD_NOOP] = noop_command_handler, + [PROTOCOL_BINARY_CMD_PREPENDQ] = prepend_command_handler, + [PROTOCOL_BINARY_CMD_PREPEND] = prepend_command_handler, + [PROTOCOL_BINARY_CMD_QUITQ] = quit_command_handler, + [PROTOCOL_BINARY_CMD_QUIT] = quit_command_handler, + [PROTOCOL_BINARY_CMD_REPLACEQ] = replace_command_handler, + [PROTOCOL_BINARY_CMD_REPLACE] = replace_command_handler, + [PROTOCOL_BINARY_CMD_SETQ] = set_command_handler, + [PROTOCOL_BINARY_CMD_SET] = set_command_handler, + [PROTOCOL_BINARY_CMD_STAT] = stat_command_handler, + [PROTOCOL_BINARY_CMD_VERSION] = version_command_handler, }; /** @@ -1028,41 +988,35 @@ static memcached_binary_protocol_command_handler comcode_v0_v1_remap[256]= { * @return true if success or false if a fatal error occured so that the * connection should be shut down. */ -static protocol_binary_response_status execute_command(memcached_protocol_client_st *client, protocol_binary_request_header *header) -{ - if (client->root->pedantic && - memcached_binary_protocol_pedantic_check_request(header)) - { - /* @todo return invalid command packet */ +static protocol_binary_response_status execute_command(memcached_protocol_client_st *client, + protocol_binary_request_header *header) { + if (client->root->pedantic && memcached_binary_protocol_pedantic_check_request(header)) { + /* @todo return invalid command packet */ } /* we got all data available, execute the callback! */ - if (client->root->callback->pre_execute != NULL) - { + if (client->root->callback->pre_execute != NULL) { client->root->callback->pre_execute(client, header); } - protocol_binary_response_status rval= PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; - uint8_t cc= header->request.opcode; + protocol_binary_response_status rval = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; + uint8_t cc = header->request.opcode; - if (client->is_verbose) - { + if (client->is_verbose) { print_cmd(cc); } - switch (client->root->callback->interface_version) - { + switch (client->root->callback->interface_version) { case 0: - if (client->root->callback->interface.v0.comcode[cc] != NULL) - { - rval= client->root->callback->interface.v0.comcode[cc](client, header, binary_raw_response_handler); + if (client->root->callback->interface.v0.comcode[cc] != NULL) { + rval = client->root->callback->interface.v0.comcode[cc](client, header, + binary_raw_response_handler); } break; case 1: - if (comcode_v0_v1_remap[cc] != NULL) - { - rval= comcode_v0_v1_remap[cc](client, header, binary_raw_response_handler); + if (comcode_v0_v1_remap[cc] != NULL) { + rval = comcode_v0_v1_remap[cc](client, header, binary_raw_response_handler); } break; @@ -1074,32 +1028,26 @@ static protocol_binary_response_status execute_command(memcached_protocol_client abort(); } - - if (rval == PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND && - client->root->callback->unknown != NULL) - { - rval= client->root->callback->unknown(client, header, binary_raw_response_handler); + if (rval == PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND && client->root->callback->unknown != NULL) { + rval = client->root->callback->unknown(client, header, binary_raw_response_handler); } - if (rval != PROTOCOL_BINARY_RESPONSE_SUCCESS && - rval != PROTOCOL_BINARY_RESPONSE_EINTERNAL && - rval != PROTOCOL_BINARY_RESPONSE_NOT_SUPPORTED) + if (rval != PROTOCOL_BINARY_RESPONSE_SUCCESS && rval != PROTOCOL_BINARY_RESPONSE_EINTERNAL + && rval != PROTOCOL_BINARY_RESPONSE_NOT_SUPPORTED) { - protocol_binary_response_no_extras response= { - .message= { - .header.response= { - .magic= PROTOCOL_BINARY_RES, - .opcode= cc, - .status= htons(rval), - .opaque= header->request.opaque, - }, - } - }; - rval= binary_raw_response_handler(client, header, (void *) &response); + protocol_binary_response_no_extras response = {.message = { + .header.response = + { + .magic = PROTOCOL_BINARY_RES, + .opcode = cc, + .status = htons(rval), + .opaque = header->request.opaque, + }, + }}; + rval = binary_raw_response_handler(client, header, (void *) &response); } - if (client->root->callback->post_execute != NULL) - { + if (client->root->callback->post_execute != NULL) { client->root->callback->post_execute(client, header); } @@ -1111,55 +1059,48 @@ static protocol_binary_response_status execute_command(memcached_protocol_client ** "PROTOECTED" INTERFACE ** ********************************************************************** */ -memcached_protocol_event_t memcached_binary_protocol_process_data(memcached_protocol_client_st *client, ssize_t *length, void **endptr) -{ +memcached_protocol_event_t +memcached_binary_protocol_process_data(memcached_protocol_client_st *client, ssize_t *length, + void **endptr) { /* try to parse all of the received packets */ protocol_binary_request_header *header; - header= (void*)client->root->input_buffer; - if (header->request.magic != (uint8_t)PROTOCOL_BINARY_REQ) - { - client->error= EINVAL; + header = (void *) client->root->input_buffer; + if (header->request.magic != (uint8_t) PROTOCOL_BINARY_REQ) { + client->error = EINVAL; return MEMCACHED_PROTOCOL_ERROR_EVENT; } - ssize_t len= *length; + ssize_t len = *length; - while (len >= (ssize_t)sizeof(*header) && - (len >= (ssize_t)(sizeof(*header) + ntohl(header->request.bodylen)))) + while (len >= (ssize_t) sizeof(*header) + && (len >= (ssize_t)(sizeof(*header) + ntohl(header->request.bodylen)))) { /* I have the complete package */ - client->current_command= header; - protocol_binary_response_status rv= execute_command(client, header); + client->current_command = header; + protocol_binary_response_status rv = execute_command(client, header); - if (rv == PROTOCOL_BINARY_RESPONSE_EINTERNAL) - { - *length= len; - *endptr= (void*)header; + if (rv == PROTOCOL_BINARY_RESPONSE_EINTERNAL) { + *length = len; + *endptr = (void *) header; return MEMCACHED_PROTOCOL_ERROR_EVENT; - } - else if (rv == PROTOCOL_BINARY_RESPONSE_NOT_SUPPORTED) - { + } else if (rv == PROTOCOL_BINARY_RESPONSE_NOT_SUPPORTED) { return MEMCACHED_PROTOCOL_PAUSE_EVENT; } - ssize_t total= (ssize_t)(sizeof(*header) + ntohl(header->request.bodylen)); + ssize_t total = (ssize_t)(sizeof(*header) + ntohl(header->request.bodylen)); len -= total; - if (len > 0) - { - intptr_t ptr= (intptr_t)header; + if (len > 0) { + intptr_t ptr = (intptr_t) header; ptr += total; - if ((ptr % 8) == 0) - { - header= (void*)ptr; - } - else - { + if ((ptr % 8) == 0) { + header = (void *) ptr; + } else { /* Fix alignment */ - memmove(client->root->input_buffer, (void*)ptr, (size_t)len); - header= (void*)client->root->input_buffer; + memmove(client->root->input_buffer, (void *) ptr, (size_t) len); + header = (void *) client->root->input_buffer; } } - *length= len; - *endptr= (void*)header; + *length = len; + *endptr = (void *) header; } return MEMCACHED_PROTOCOL_READ_EVENT; @@ -1170,29 +1111,26 @@ memcached_protocol_event_t memcached_binary_protocol_process_data(memcached_prot ** PUBLIC INTERFACE ** ********************************************************************** */ -memcached_binary_protocol_callback_st *memcached_binary_protocol_get_callbacks(memcached_protocol_st *instance) -{ +memcached_binary_protocol_callback_st * +memcached_binary_protocol_get_callbacks(memcached_protocol_st *instance) { return instance->callback; } -void memcached_binary_protocol_set_callbacks(memcached_protocol_st *instance, memcached_binary_protocol_callback_st *callback) -{ - instance->callback= callback; +void memcached_binary_protocol_set_callbacks(memcached_protocol_st *instance, + memcached_binary_protocol_callback_st *callback) { + instance->callback = callback; } -memcached_binary_protocol_raw_response_handler memcached_binary_protocol_get_raw_response_handler(const void *cookie) -{ - (void)cookie; +memcached_binary_protocol_raw_response_handler +memcached_binary_protocol_get_raw_response_handler(const void *cookie) { + (void) cookie; return binary_raw_response_handler; } -void memcached_binary_protocol_set_pedantic(memcached_protocol_st *instance, bool enable) -{ - instance->pedantic= enable; +void memcached_binary_protocol_set_pedantic(memcached_protocol_st *instance, bool enable) { + instance->pedantic = enable; } -bool memcached_binary_protocol_get_pedantic(memcached_protocol_st *instance) -{ +bool memcached_binary_protocol_get_pedantic(memcached_protocol_st *instance) { return instance->pedantic; } - diff --git a/src/libmemcachedprotocol/binary_handler.h b/src/libmemcachedprotocol/binary_handler.h index d5a74e78..f3299e9b 100644 --- a/src/libmemcachedprotocol/binary_handler.h +++ b/src/libmemcachedprotocol/binary_handler.h @@ -1,47 +1,29 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once LIBMEMCACHED_LOCAL -bool memcached_binary_protocol_pedantic_check_request(const protocol_binary_request_header *request); +bool memcached_binary_protocol_pedantic_check_request( + const protocol_binary_request_header *request); LIBMEMCACHED_LOCAL -bool memcached_binary_protocol_pedantic_check_response(const protocol_binary_request_header *request, - const protocol_binary_response_header *response); +bool memcached_binary_protocol_pedantic_check_response( + const protocol_binary_request_header *request, const protocol_binary_response_header *response); LIBMEMCACHED_LOCAL -memcached_protocol_event_t memcached_binary_protocol_process_data(memcached_protocol_client_st *client, ssize_t *length, void **endptr); +memcached_protocol_event_t +memcached_binary_protocol_process_data(memcached_protocol_client_st *client, ssize_t *length, + void **endptr); diff --git a/src/libmemcachedprotocol/cache.c b/src/libmemcachedprotocol/cache.c index c68cc254..637665d7 100644 --- a/src/libmemcachedprotocol/cache.c +++ b/src/libmemcachedprotocol/cache.c @@ -1,40 +1,18 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #include "mem_config.h" #include @@ -42,7 +20,7 @@ #include #ifndef NDEBUG -#include +# include #endif #include "libmemcachedprotocol/common.h" @@ -54,136 +32,133 @@ int cache_error = 0; const size_t initial_pool_size = 64; -cache_t* cache_create(const char *name, size_t bufsize, size_t align, - cache_constructor_t* constructor, - cache_destructor_t* destructor) { - cache_t* ret = calloc(1, sizeof(cache_t)); - size_t name_length= strlen(name); - char* nm= calloc(1, (sizeof(char) * name_length) +1); - memcpy(nm, name, name_length); - void** ptr = calloc(initial_pool_size, bufsize); - if (ret == NULL || nm == NULL || ptr == NULL || - pthread_mutex_init(&ret->mutex, NULL) == -1) { - free(ret); - free(nm); - free(ptr); - return NULL; - } - - ret->name = nm; - ret->ptr = ptr; - ret->freetotal = initial_pool_size; - ret->constructor = constructor; - ret->destructor = destructor; +cache_t *cache_create(const char *name, size_t bufsize, size_t align, + cache_constructor_t *constructor, cache_destructor_t *destructor) { + cache_t *ret = calloc(1, sizeof(cache_t)); + size_t name_length = strlen(name); + char *nm = calloc(1, (sizeof(char) * name_length) + 1); + memcpy(nm, name, name_length); + void **ptr = calloc(initial_pool_size, bufsize); + if (ret == NULL || nm == NULL || ptr == NULL || pthread_mutex_init(&ret->mutex, NULL) == -1) { + free(ret); + free(nm); + free(ptr); + return NULL; + } + + ret->name = nm; + ret->ptr = ptr; + ret->freetotal = initial_pool_size; + ret->constructor = constructor; + ret->destructor = destructor; #ifndef NDEBUG - ret->bufsize = bufsize + 2 * sizeof(redzone_pattern); + ret->bufsize = bufsize + 2 * sizeof(redzone_pattern); #else - ret->bufsize = bufsize; + ret->bufsize = bufsize; #endif - (void)align; + (void) align; - return ret; + return ret; } -static inline void* get_object(void *ptr) { +static inline void *get_object(void *ptr) { #ifndef NDEBUG - uint64_t *pre = ptr; - return pre + 1; + uint64_t *pre = ptr; + return pre + 1; #else - return ptr; + return ptr; #endif } void cache_destroy(cache_t *cache) { - while (cache->freecurr > 0) { - void *ptr = cache->ptr[--cache->freecurr]; - if (cache->destructor) { - cache->destructor(get_object(ptr), NULL); - } - free(ptr); + while (cache->freecurr > 0) { + void *ptr = cache->ptr[--cache->freecurr]; + if (cache->destructor) { + cache->destructor(get_object(ptr), NULL); } - free(cache->name); - free(cache->ptr); - pthread_mutex_destroy(&cache->mutex); + free(ptr); + } + free(cache->name); + free(cache->ptr); + pthread_mutex_destroy(&cache->mutex); } -void* cache_alloc(cache_t *cache) { - void *ret; - void *object; - pthread_mutex_lock(&cache->mutex); - if (cache->freecurr > 0) { - ret = cache->ptr[--cache->freecurr]; - object = get_object(ret); - } else { - object = ret = malloc(cache->bufsize); - if (ret != NULL) { - object = get_object(ret); - - if (cache->constructor != NULL && - cache->constructor(object, NULL, 0) != 0) { - free(ret); - object = NULL; - } - } +void *cache_alloc(cache_t *cache) { + void *ret; + void *object; + pthread_mutex_lock(&cache->mutex); + if (cache->freecurr > 0) { + ret = cache->ptr[--cache->freecurr]; + object = get_object(ret); + } else { + object = ret = malloc(cache->bufsize); + if (ret != NULL) { + object = get_object(ret); + + if (cache->constructor != NULL && cache->constructor(object, NULL, 0) != 0) { + free(ret); + object = NULL; + } } - pthread_mutex_unlock(&cache->mutex); + } + pthread_mutex_unlock(&cache->mutex); #ifndef NDEBUG - if (object != NULL) { - /* add a simple form of buffer-check */ - uint64_t *pre = ret; - *pre = redzone_pattern; - ret = pre+1; - memcpy(((char*)ret) + cache->bufsize - (2 * sizeof(redzone_pattern)), - &redzone_pattern, sizeof(redzone_pattern)); - } + if (object != NULL) { + /* add a simple form of buffer-check */ + uint64_t *pre = ret; + *pre = redzone_pattern; + ret = pre + 1; + memcpy(((char *) ret) + cache->bufsize - (2 * sizeof(redzone_pattern)), &redzone_pattern, + sizeof(redzone_pattern)); + } #endif - return object; + return object; } void cache_free(cache_t *cache, void *ptr) { - pthread_mutex_lock(&cache->mutex); + pthread_mutex_lock(&cache->mutex); #ifndef NDEBUG - /* validate redzone... */ - if (memcmp(((char*)ptr) + cache->bufsize - (2 * sizeof(redzone_pattern)), - &redzone_pattern, sizeof(redzone_pattern)) != 0) { - raise(SIGABRT); - cache_error = 1; - pthread_mutex_unlock(&cache->mutex); - return; - } - uint64_t *pre = ptr; - --pre; - if (*pre != redzone_pattern) { - raise(SIGABRT); - cache_error = -1; - pthread_mutex_unlock(&cache->mutex); - return; - } - ptr = pre; + /* validate redzone... */ + if (memcmp(((char *) ptr) + cache->bufsize - (2 * sizeof(redzone_pattern)), &redzone_pattern, + sizeof(redzone_pattern)) + != 0) + { + raise(SIGABRT); + cache_error = 1; + pthread_mutex_unlock(&cache->mutex); + return; + } + uint64_t *pre = ptr; + --pre; + if (*pre != redzone_pattern) { + raise(SIGABRT); + cache_error = -1; + pthread_mutex_unlock(&cache->mutex); + return; + } + ptr = pre; #endif - if (cache->freecurr < cache->freetotal) { - cache->ptr[cache->freecurr++] = ptr; + if (cache->freecurr < cache->freetotal) { + cache->ptr[cache->freecurr++] = ptr; + } else { + /* try to enlarge free connections array */ + size_t newtotal = cache->freetotal * 2; + void **new_free = realloc(cache->ptr, sizeof(char *) * newtotal); + if (new_free) { + cache->freetotal = newtotal; + cache->ptr = new_free; + cache->ptr[cache->freecurr++] = ptr; } else { - /* try to enlarge free connections array */ - size_t newtotal = cache->freetotal * 2; - void **new_free = realloc(cache->ptr, sizeof(char *) * newtotal); - if (new_free) { - cache->freetotal = newtotal; - cache->ptr = new_free; - cache->ptr[cache->freecurr++] = ptr; - } else { - if (cache->destructor) { - cache->destructor(ptr, NULL); - } - free(ptr); - - } + if (cache->destructor) { + cache->destructor(ptr, NULL); + } + free(ptr); } - pthread_mutex_unlock(&cache->mutex); + } + pthread_mutex_unlock(&cache->mutex); } - diff --git a/src/libmemcachedprotocol/cache.h b/src/libmemcachedprotocol/cache.h index 6f84feaa..616909f7 100644 --- a/src/libmemcachedprotocol/cache.h +++ b/src/libmemcachedprotocol/cache.h @@ -1,56 +1,34 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ -/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ #pragma once #include #ifdef HAVE_UMEM_H -# include -# define cache_t umem_cache_t -# define cache_alloc(a) umem_cache_alloc(a, UMEM_DEFAULT) -# define cache_free(a, b) umem_cache_free(a, b) -# define cache_create(a,b,c,d,e) umem_cache_create((char*)a, b, c, d, e, NULL, NULL, NULL, 0) -# define cache_destroy(a) umem_cache_destroy(a); +# include +# define cache_t umem_cache_t +# define cache_alloc(a) umem_cache_alloc(a, UMEM_DEFAULT) +# define cache_free(a, b) umem_cache_free(a, b) +# define cache_create(a, b, c, d, e) umem_cache_create((char *) a, b, c, d, e, NULL, NULL, NULL, 0) +# define cache_destroy(a) umem_cache_destroy(a); #else -# ifndef NDEBUG +# ifndef NDEBUG /* may be used for debug purposes */ extern int cache_error; -# endif +# endif /** * Constructor used to initialize allocated objects @@ -60,7 +38,7 @@ extern int cache_error; * @param notused2 This parameter is currently not used. * @return you should return 0, but currently this is not checked */ -typedef int cache_constructor_t(void* obj, void* notused1, int notused2); +typedef int cache_constructor_t(void *obj, void *notused1, int notused2); /** * Destructor used to clean up allocated objects before they are * returned to the operating system. @@ -70,7 +48,7 @@ typedef int cache_constructor_t(void* obj, void* notused1, int notused2); * @param notused2 This parameter is currently not used. * @return you should return 0, but currently this is not checked */ -typedef void cache_destructor_t(void* obj, void* notused); +typedef void cache_destructor_t(void *obj, void *notused); /** * Definition of the structure to keep track of the internal details of @@ -78,22 +56,22 @@ typedef void cache_destructor_t(void* obj, void* notused); * undefined behavior. */ typedef struct { - /** Mutex to protect access to the structure */ - pthread_mutex_t mutex; - /** Name of the cache objects in this cache (provided by the caller) */ - char *name; - /** List of pointers to available buffers in this cache */ - void **ptr; - /** The size of each element in this cache */ - size_t bufsize; - /** The capacity of the list of elements */ - size_t freetotal; - /** The current number of free elements */ - size_t freecurr; - /** The constructor to be called each time we allocate more memory */ - cache_constructor_t* constructor; - /** The destructor to be called each time before we release memory */ - cache_destructor_t* destructor; + /** Mutex to protect access to the structure */ + pthread_mutex_t mutex; + /** Name of the cache objects in this cache (provided by the caller) */ + char *name; + /** List of pointers to available buffers in this cache */ + void **ptr; + /** The size of each element in this cache */ + size_t bufsize; + /** The capacity of the list of elements */ + size_t freetotal; + /** The current number of free elements */ + size_t freecurr; + /** The constructor to be called each time we allocate more memory */ + cache_constructor_t *constructor; + /** The destructor to be called each time before we release memory */ + cache_destructor_t *destructor; } cache_t; /** @@ -114,9 +92,8 @@ typedef struct { * to the os. * @return a handle to an object cache if successful, NULL otherwise. */ -cache_t* cache_create(const char* name, size_t bufsize, size_t align, - cache_constructor_t* constructor, - cache_destructor_t* destructor); +cache_t *cache_create(const char *name, size_t bufsize, size_t align, + cache_constructor_t *constructor, cache_destructor_t *destructor); /** * Destroy an object cache. * @@ -126,7 +103,7 @@ cache_t* cache_create(const char* name, size_t bufsize, size_t align, * * @param handle the handle to the object cache to destroy. */ -void cache_destroy(cache_t* handle); +void cache_destroy(cache_t *handle); /** * Allocate an object from the cache. * @@ -134,7 +111,7 @@ void cache_destroy(cache_t* handle); * @return a pointer to an initialized object from the cache, or NULL if * the allocation cannot be satisfied. */ -void* cache_alloc(cache_t* handle); +void *cache_alloc(cache_t *handle); /** * Return an object back to the cache. * @@ -144,5 +121,5 @@ void* cache_alloc(cache_t* handle); * @param handle handle to the object cache to return the object to * @param ptr pointer to the object to return. */ -void cache_free(cache_t* handle, void* ptr); +void cache_free(cache_t *handle, void *ptr); #endif // HAVE_UMEM_H diff --git a/src/libmemcachedprotocol/common.h b/src/libmemcachedprotocol/common.h index 20ebe985..ee27ab4a 100644 --- a/src/libmemcachedprotocol/common.h +++ b/src/libmemcachedprotocol/common.h @@ -1,38 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once @@ -53,8 +32,7 @@ */ typedef bool (*drain_func)(memcached_protocol_client_st *client); typedef protocol_binary_response_status (*spool_func)(memcached_protocol_client_st *client, - const void *data, - size_t length); + const void *data, size_t length); /** * Definition of the per instance structure. @@ -101,7 +79,8 @@ struct chunk_st { #define CHUNK_BUFFERSIZE 2048 -typedef memcached_protocol_event_t (*process_data)(struct memcached_protocol_client_st *client, ssize_t *length, void **endptr); +typedef memcached_protocol_event_t (*process_data)(struct memcached_protocol_client_st *client, + ssize_t *length, void **endptr); enum ascii_cmd { GET_CMD, diff --git a/src/libmemcachedprotocol/handler.c b/src/libmemcachedprotocol/handler.c index 24bd74a9..885c798f 100644 --- a/src/libmemcachedprotocol/handler.c +++ b/src/libmemcachedprotocol/handler.c @@ -1,40 +1,18 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ -/* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */ #include "libmemcachedprotocol/common.h" #include @@ -66,12 +44,8 @@ * @param nbytes the number of bytes to read * @return the number of bytes transferred of -1 upon error */ -static ssize_t default_recv(const void *cookie, - memcached_socket_t sock, - void *buf, - size_t nbytes) -{ - (void)cookie; +static ssize_t default_recv(const void *cookie, memcached_socket_t sock, void *buf, size_t nbytes) { + (void) cookie; return recv(sock, buf, nbytes, 0); } @@ -86,12 +60,9 @@ static ssize_t default_recv(const void *cookie, * @param nbytes the number of bytes to send * @return the number of bytes transferred of -1 upon error */ -static ssize_t default_send(const void *cookie, - memcached_socket_t fd, - const void *buf, - size_t nbytes) -{ - (void)cookie; +static ssize_t default_send(const void *cookie, memcached_socket_t fd, const void *buf, + size_t nbytes) { + (void) cookie; return send(fd, buf, nbytes, MSG_NOSIGNAL); } @@ -103,46 +74,34 @@ static ssize_t default_send(const void *cookie, * true otherwise (please note that there may be more data to * left in the buffer to send) */ -static bool drain_output(struct memcached_protocol_client_st *client) -{ - if (client->is_verbose) - { - fprintf(stderr, "%s:%d %s mute:%d output:%s length:%d\n", __FILE__, __LINE__, __func__, (int)client->mute, - client->output ? "yes" : "no", - client->output ? (int)(client->output->nbytes - client->output->offset) : 0); +static bool drain_output(struct memcached_protocol_client_st *client) { + if (client->is_verbose) { + fprintf(stderr, "%s:%d %s mute:%d output:%s length:%d\n", __FILE__, __LINE__, __func__, + (int) client->mute, client->output ? "yes" : "no", + client->output ? (int) (client->output->nbytes - client->output->offset) : 0); } /* Do we have pending data to send? */ - while (client->output != NULL) - { - ssize_t len= client->root->send(client, - client->sock, - client->output->data + client->output->offset, - client->output->nbytes - client->output->offset); - - if (len == -1) - { - if (get_socket_errno() == EWOULDBLOCK) - { + while (client->output != NULL) { + ssize_t len = + client->root->send(client, client->sock, client->output->data + client->output->offset, + client->output->nbytes - client->output->offset); + + if (len == -1) { + if (get_socket_errno() == EWOULDBLOCK) { return true; - } - else if (get_socket_errno() != EINTR) - { - client->error= get_socket_errno(); + } else if (get_socket_errno() != EINTR) { + client->error = get_socket_errno(); return false; } - } - else - { - client->output->offset += (size_t)len; - if (client->output->offset == client->output->nbytes) - { + } else { + client->output->offset += (size_t) len; + if (client->output->offset == client->output->nbytes) { /* This was the complete buffer */ - struct chunk_st *old= client->output; - client->output= client->output->next; - if (client->output == NULL) - { - client->output_tail= NULL; + struct chunk_st *old = client->output; + client->output = client->output->next; + if (client->output == NULL) { + client->output_tail = NULL; } cache_free(client->root->buffer_cache, old); } @@ -158,27 +117,22 @@ static bool drain_output(struct memcached_protocol_client_st *client) * @param client the client that needs the buffer * @return pointer to the new chunk if the allocation succeeds, NULL otherwise */ -static struct chunk_st *allocate_output_chunk(struct memcached_protocol_client_st *client) -{ - struct chunk_st *ret= cache_alloc(client->root->buffer_cache); +static struct chunk_st *allocate_output_chunk(struct memcached_protocol_client_st *client) { + struct chunk_st *ret = cache_alloc(client->root->buffer_cache); - if (ret == NULL) - { + if (ret == NULL) { return NULL; } - ret->offset= ret->nbytes= 0; - ret->next= NULL; - ret->size= CHUNK_BUFFERSIZE; - ret->data= (void*)(ret + 1); - if (client->output == NULL) - { - client->output= client->output_tail= ret; - } - else - { - client->output_tail->next= ret; - client->output_tail= ret; + ret->offset = ret->nbytes = 0; + ret->next = NULL; + ret->size = CHUNK_BUFFERSIZE; + ret->data = (void *) (ret + 1); + if (client->output == NULL) { + client->output = client->output_tail = ret; + } else { + client->output_tail->next = ret; + client->output_tail = ret; } return ret; @@ -194,36 +148,29 @@ static struct chunk_st *allocate_output_chunk(struct memcached_protocol_client_s * PROTOCOL_BINARY_RESPONSE_ENOMEM if we failed to allocate memory */ static protocol_binary_response_status spool_output(struct memcached_protocol_client_st *client, - const void *data, - size_t length) -{ - if (client->is_verbose) - { - fprintf(stderr, "%s:%d %s mute:%d length:%d\n", __FILE__, __LINE__, __func__, (int)client->mute, (int)length); + const void *data, size_t length) { + if (client->is_verbose) { + fprintf(stderr, "%s:%d %s mute:%d length:%d\n", __FILE__, __LINE__, __func__, + (int) client->mute, (int) length); } - if (client->mute) - { + if (client->mute) { return PROTOCOL_BINARY_RESPONSE_SUCCESS; } - size_t offset= 0; + size_t offset = 0; - struct chunk_st *chunk= client->output; - while (offset < length) - { - if (chunk == NULL || (chunk->size - chunk->nbytes) == 0) - { - if ((chunk= allocate_output_chunk(client)) == NULL) - { + struct chunk_st *chunk = client->output; + while (offset < length) { + if (chunk == NULL || (chunk->size - chunk->nbytes) == 0) { + if ((chunk = allocate_output_chunk(client)) == NULL) { return PROTOCOL_BINARY_RESPONSE_ENOMEM; } } - size_t bulk= length - offset; - if (bulk > chunk->size - chunk->nbytes) - { - bulk= chunk->size - chunk->nbytes; + size_t bulk = length - offset; + if (bulk > chunk->size - chunk->nbytes) { + bulk = chunk->size - chunk->nbytes; } memcpy(chunk->data + chunk->nbytes, data, bulk); @@ -242,21 +189,18 @@ static protocol_binary_response_status spool_output(struct memcached_protocol_cl * so the implementors needs to provide an implementation of that interface * */ -static memcached_protocol_event_t determine_protocol(struct memcached_protocol_client_st *client, ssize_t *length, void **endptr) -{ - if (*client->root->input_buffer == (uint8_t)PROTOCOL_BINARY_REQ) - { - if (client->is_verbose) - { - fprintf(stderr, "%s:%d PROTOCOL: memcached_binary_protocol_process_data\n", __FILE__, __LINE__); +static memcached_protocol_event_t determine_protocol(struct memcached_protocol_client_st *client, + ssize_t *length, void **endptr) { + if (*client->root->input_buffer == (uint8_t) PROTOCOL_BINARY_REQ) { + if (client->is_verbose) { + fprintf(stderr, "%s:%d PROTOCOL: memcached_binary_protocol_process_data\n", __FILE__, + __LINE__); } - client->work= memcached_binary_protocol_process_data; - } - else if (client->root->callback->interface_version == 1) - { - if (client->is_verbose) - { - fprintf(stderr, "%s:%d PROTOCOL: memcached_ascii_protocol_process_data\n", __FILE__, __LINE__); + client->work = memcached_binary_protocol_process_data; + } else if (client->root->callback->interface_version == 1) { + if (client->is_verbose) { + fprintf(stderr, "%s:%d PROTOCOL: memcached_ascii_protocol_process_data\n", __FILE__, + __LINE__); } /* @@ -267,19 +211,16 @@ static memcached_protocol_event_t determine_protocol(struct memcached_protocol_c * for version 0 and 1 at the same time and set the preferred * interface to use... */ - client->work= memcached_ascii_protocol_process_data; - } - else - { - if (client->is_verbose) - { + client->work = memcached_ascii_protocol_process_data; + } else { + if (client->is_verbose) { fprintf(stderr, "%s:%d PROTOCOL: Unsupported protocol\n", __FILE__, __LINE__); } /* Let's just output a warning the way it is supposed to look like * in the ASCII protocol... */ - const char *err= "CLIENT_ERROR: Unsupported protocol\r\n"; + const char *err = "CLIENT_ERROR: Unsupported protocol\r\n"; client->root->spool(client, err, strlen(err)); client->root->drain(client); @@ -295,146 +236,121 @@ static memcached_protocol_event_t determine_protocol(struct memcached_protocol_c ** * See protocol_handler.h for function description ** ********************************************************************** */ -struct memcached_protocol_st *memcached_protocol_create_instance(void) -{ - struct memcached_protocol_st *ret= calloc(1, sizeof(*ret)); - if (ret != NULL) - { - ret->recv= default_recv; - ret->send= default_send; - ret->drain= drain_output; - ret->spool= spool_output; - ret->input_buffer_size= 1 * 1024 * 1024; - ret->input_buffer= malloc(ret->input_buffer_size); - if (ret->input_buffer == NULL) - { +struct memcached_protocol_st *memcached_protocol_create_instance(void) { + struct memcached_protocol_st *ret = calloc(1, sizeof(*ret)); + if (ret != NULL) { + ret->recv = default_recv; + ret->send = default_send; + ret->drain = drain_output; + ret->spool = spool_output; + ret->input_buffer_size = 1 * 1024 * 1024; + ret->input_buffer = malloc(ret->input_buffer_size); + if (ret->input_buffer == NULL) { free(ret); - ret= NULL; + ret = NULL; return NULL; } - ret->buffer_cache= cache_create("protocol_handler", - CHUNK_BUFFERSIZE + sizeof(struct chunk_st), - 0, NULL, NULL); - if (ret->buffer_cache == NULL) - { + ret->buffer_cache = + cache_create("protocol_handler", CHUNK_BUFFERSIZE + sizeof(struct chunk_st), 0, NULL, NULL); + if (ret->buffer_cache == NULL) { free(ret->input_buffer); free(ret); - ret= NULL; + ret = NULL; } } return ret; } -void memcached_protocol_destroy_instance(struct memcached_protocol_st *instance) -{ +void memcached_protocol_destroy_instance(struct memcached_protocol_st *instance) { cache_destroy(instance->buffer_cache); free(instance->input_buffer); free(instance); } -struct memcached_protocol_client_st *memcached_protocol_create_client(struct memcached_protocol_st *instance, memcached_socket_t sock) -{ - struct memcached_protocol_client_st *ret= calloc(1, sizeof(memcached_protocol_client_st)); - if (ret != NULL) - { - ret->root= instance; - ret->sock= sock; - ret->work= determine_protocol; +struct memcached_protocol_client_st * +memcached_protocol_create_client(struct memcached_protocol_st *instance, memcached_socket_t sock) { + struct memcached_protocol_client_st *ret = calloc(1, sizeof(memcached_protocol_client_st)); + if (ret != NULL) { + ret->root = instance; + ret->sock = sock; + ret->work = determine_protocol; } return ret; } -void memcached_protocol_client_destroy(struct memcached_protocol_client_st *client) -{ +void memcached_protocol_client_destroy(struct memcached_protocol_client_st *client) { free(client); } -void memcached_protocol_client_set_verbose(struct memcached_protocol_client_st *client, bool arg) -{ - if (client) - { - client->is_verbose= arg; +void memcached_protocol_client_set_verbose(struct memcached_protocol_client_st *client, bool arg) { + if (client) { + client->is_verbose = arg; } } -memcached_protocol_event_t memcached_protocol_client_work(struct memcached_protocol_client_st *client) -{ +memcached_protocol_event_t +memcached_protocol_client_work(struct memcached_protocol_client_st *client) { /* Try to send data and read from the socket */ - bool more_data= true; - do - { - ssize_t len= client->root->recv(client, - client->sock, - client->root->input_buffer + client->input_buffer_offset, - client->root->input_buffer_size - client->input_buffer_offset); - - if (len > 0) - { + bool more_data = true; + do { + ssize_t len = client->root->recv(client, client->sock, + client->root->input_buffer + client->input_buffer_offset, + client->root->input_buffer_size - client->input_buffer_offset); + + if (len > 0) { /* Do we have the complete packet? */ - if (client->input_buffer_offset > 0) - { - memcpy(client->root->input_buffer, client->input_buffer, - client->input_buffer_offset); - len += (ssize_t)client->input_buffer_offset; + if (client->input_buffer_offset > 0) { + memcpy(client->root->input_buffer, client->input_buffer, client->input_buffer_offset); + len += (ssize_t) client->input_buffer_offset; /* @todo use buffer-cache! */ free(client->input_buffer); - client->input_buffer_offset= 0; + client->input_buffer_offset = 0; } void *endptr; - memcached_protocol_event_t events= client->work(client, &len, &endptr); - if (events == MEMCACHED_PROTOCOL_ERROR_EVENT) - { + memcached_protocol_event_t events = client->work(client, &len, &endptr); + if (events == MEMCACHED_PROTOCOL_ERROR_EVENT) { return MEMCACHED_PROTOCOL_ERROR_EVENT; } - if (len > 0) - { + if (len > 0) { /* save the data for later on */ /* @todo use buffer-cache */ - client->input_buffer= malloc((size_t)len); - if (client->input_buffer == NULL) - { - client->error= ENOMEM; + client->input_buffer = malloc((size_t) len); + if (client->input_buffer == NULL) { + client->error = ENOMEM; return MEMCACHED_PROTOCOL_ERROR_EVENT; } - memcpy(client->input_buffer, endptr, (size_t)len); - client->input_buffer_offset= (size_t)len; - more_data= false; + memcpy(client->input_buffer, endptr, (size_t) len); + client->input_buffer_offset = (size_t) len; + more_data = false; } - } - else if (len == 0) - { + } else if (len == 0) { /* Connection closed */ drain_output(client); return MEMCACHED_PROTOCOL_ERROR_EVENT; - } - else - { - if (get_socket_errno() != EWOULDBLOCK) - { - client->error= get_socket_errno(); + } else { + if (get_socket_errno() != EWOULDBLOCK) { + client->error = get_socket_errno(); /* mark this client as terminated! */ return MEMCACHED_PROTOCOL_ERROR_EVENT; } - more_data= false; + more_data = false; } } while (more_data); - if (!drain_output(client)) - { + if (!drain_output(client)) { return MEMCACHED_PROTOCOL_ERROR_EVENT; } - memcached_protocol_event_t ret= MEMCACHED_PROTOCOL_READ_EVENT; - if (client->output) - { - ret|= MEMCACHED_PROTOCOL_READ_EVENT; + memcached_protocol_event_t ret = MEMCACHED_PROTOCOL_READ_EVENT; + if (client->output) { + ret |= MEMCACHED_PROTOCOL_READ_EVENT; } return ret; diff --git a/src/libmemcachedprotocol/pedantic.c b/src/libmemcachedprotocol/pedantic.c index cfbc8194..85624f24 100644 --- a/src/libmemcachedprotocol/pedantic.c +++ b/src/libmemcachedprotocol/pedantic.c @@ -1,58 +1,39 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -/* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #include "libmemcachedprotocol/common.h" #include -#define ensure(a) if (!(a)) { return false; } +#define ensure(a) \ + if (!(a)) { \ + return false; \ + } -bool memcached_binary_protocol_pedantic_check_request(const protocol_binary_request_header *request) -{ +bool memcached_binary_protocol_pedantic_check_request( + const protocol_binary_request_header *request) { ensure(request->request.magic == PROTOCOL_BINARY_REQ); ensure(request->request.datatype == PROTOCOL_BINARY_RAW_BYTES); ensure(request->bytes[6] == 0); ensure(request->bytes[7] == 0); - uint8_t opcode= request->request.opcode; - uint16_t keylen= ntohs(request->request.keylen); - uint8_t extlen= request->request.extlen; - uint32_t bodylen= ntohl(request->request.bodylen); + uint8_t opcode = request->request.opcode; + uint16_t keylen = ntohs(request->request.keylen); + uint8_t extlen = request->request.extlen; + uint32_t bodylen = ntohl(request->request.bodylen); ensure(bodylen >= (keylen + extlen)); @@ -125,26 +106,26 @@ bool memcached_binary_protocol_pedantic_check_request(const protocol_binary_requ ensure(extlen == 0); ensure(keylen > 0); break; - default: - /* Unknown command */ - ; + default + : + /* Unknown command */ + ; } return true; } -bool memcached_binary_protocol_pedantic_check_response(const protocol_binary_request_header *request, - const protocol_binary_response_header *response) -{ +bool memcached_binary_protocol_pedantic_check_response( + const protocol_binary_request_header *request, + const protocol_binary_response_header *response) { ensure(response->response.magic == PROTOCOL_BINARY_RES); ensure(response->response.datatype == PROTOCOL_BINARY_RAW_BYTES); ensure(response->response.opaque == request->request.opaque); - uint16_t status= ntohs(response->response.status); - uint8_t opcode= response->response.opcode; + uint16_t status = ntohs(response->response.status); + uint8_t opcode = response->response.opcode; - if (status == PROTOCOL_BINARY_RESPONSE_SUCCESS) - { + if (status == PROTOCOL_BINARY_RESPONSE_SUCCESS) { switch (opcode) { case PROTOCOL_BINARY_CMD_ADDQ: case PROTOCOL_BINARY_CMD_APPENDQ: @@ -158,8 +139,7 @@ bool memcached_binary_protocol_pedantic_check_response(const protocol_binary_req case PROTOCOL_BINARY_CMD_SETQ: /* Quiet command shouldn't return on success */ return false; - default: - break; + default: break; } switch (opcode) { @@ -222,13 +202,10 @@ bool memcached_binary_protocol_pedantic_check_response(const protocol_binary_req /* Undefined command code */ break; } - } - else - { + } else { ensure(response->response.cas == 0); ensure(response->response.extlen == 0); - if (opcode != PROTOCOL_BINARY_CMD_GETK) - { + if (opcode != PROTOCOL_BINARY_CMD_GETK) { ensure(response->response.keylen == 0); } }