X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fresponse.cc;h=67b0999f7f2afff67e462ec9c2c5a4894577aea8;hb=674c7578fa870c3b57e81e765c355ce98434b310;hp=511cf786db0bbfb5151ef2fc024eae5a4ed121bc;hpb=dcc4f9ceee29ec101b2dcc4c662d7d768eda190a;p=m6w6%2Flibmemcached diff --git a/libmemcached/response.cc b/libmemcached/response.cc index 511cf786..67b0999f 100644 --- a/libmemcached/response.cc +++ b/libmemcached/response.cc @@ -38,7 +38,7 @@ #include #include -static memcached_return_t textual_value_fetch(memcached_server_write_instance_st instance, +static memcached_return_t textual_value_fetch(org::libmemcached::Instance* instance, char *buffer, memcached_result_st *result) { @@ -55,6 +55,9 @@ static memcached_return_t textual_value_fetch(memcached_server_write_instance_st string_ptr+= 6; /* "VALUE " */ + // Just used for cases of AES decrypt currently + memcached_return_t rc= MEMCACHED_SUCCESS; + /* We load the key */ { char *key= result->item_key; @@ -169,7 +172,34 @@ static memcached_return_t textual_value_fetch(memcached_server_write_instance_st memcached_string_set_length(&result->value, value_length); } - return MEMCACHED_SUCCESS; + if (memcached_is_encrypted(instance->root) and memcached_result_length(result)) + { + hashkit_string_st *destination; + + if ((destination= hashkit_decrypt(&instance->root->hashkit, + memcached_result_value(result), memcached_result_length(result))) == NULL) + { + rc= memcached_set_error(*instance->root, MEMCACHED_FAILURE, + MEMCACHED_AT, memcached_literal_param("hashkit_decrypt() failed")); + } + else + { + memcached_result_reset_value(result); + if (memcached_failed(memcached_result_set_value(result, hashkit_string_c_str(destination), hashkit_string_length(destination)))) + { + rc= memcached_set_error(*instance->root, MEMCACHED_FAILURE, + MEMCACHED_AT, memcached_literal_param("hashkit_decrypt() failed")); + } + } + + if (memcached_failed(rc)) + { + memcached_result_reset(result); + } + hashkit_string_free(destination); + } + + return rc; read_error: memcached_io_reset(instance); @@ -177,7 +207,7 @@ read_error: return MEMCACHED_PARTIAL_READ; } -static memcached_return_t textual_read_one_response(memcached_server_write_instance_st instance, +static memcached_return_t textual_read_one_response(org::libmemcached::Instance* instance, char *buffer, const size_t buffer_length, memcached_result_st *result) { @@ -206,35 +236,31 @@ static memcached_return_t textual_read_one_response(memcached_server_write_insta { /* Find the space, and then move one past it to copy version */ char *response_ptr= index(buffer, ' '); - response_ptr++; - long int version= strtol(response_ptr, (char **)NULL, 10); + char *endptr; + long int version= strtol(response_ptr, &endptr, 10); if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX or version == 0) { instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX; - return memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version")); + return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version")); } instance->major_version= uint8_t(version); - response_ptr= index(response_ptr, '.'); - response_ptr++; - - version= strtol(response_ptr, (char **)NULL, 10); + endptr++; + version= strtol(endptr, &endptr, 10); if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX) { instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX; - return memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse minor version")); + return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse minor version")); } instance->minor_version= uint8_t(version); - response_ptr= index(response_ptr, '.'); - response_ptr++; - - version= strtol(response_ptr, (char **)NULL, 10); + endptr++; + version= strtol(endptr, &endptr, 10); if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX) { instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX; - return memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version")); + return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version")); } instance->micro_version= uint8_t(version); @@ -444,7 +470,7 @@ static memcached_return_t textual_read_one_response(memcached_server_write_insta buffer, total_read); } -static memcached_return_t binary_read_one_response(memcached_server_write_instance_st instance, +static memcached_return_t binary_read_one_response(org::libmemcached::Instance* instance, char *buffer, const size_t buffer_length, memcached_result_st *result) { @@ -558,6 +584,21 @@ static memcached_return_t binary_read_one_response(memcached_server_write_instan break; case PROTOCOL_BINARY_CMD_SASL_LIST_MECHS: + { + if (header.response.keylen != 0 || bodylen + 1 > buffer_length) + { + return MEMCACHED_UNKNOWN_READ_FAILURE; + } + else + { + if ((rc= memcached_safe_read(instance, buffer, bodylen)) != MEMCACHED_SUCCESS) + { + return MEMCACHED_UNKNOWN_READ_FAILURE; + } + } + } + break; + case PROTOCOL_BINARY_CMD_VERSION: { char version_buffer[32]; // @todo document this number @@ -568,28 +609,30 @@ static memcached_return_t binary_read_one_response(memcached_server_write_instan return MEMCACHED_UNKNOWN_READ_FAILURE; } - char *p; - long int version= strtol(version_buffer, &p, 10); + char *endptr; + long int version= strtol(version_buffer, &endptr, 10); if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX or version == 0) { instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX; - return memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version")); + return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version")); } instance->major_version= uint8_t(version); - version= strtol(p +1, &p, 10); + endptr++; + version= strtol(endptr, &endptr, 10); if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX) { instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX; - return memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version")); + return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse minor version")); } instance->minor_version= uint8_t(version); - version= strtol(p + 1, NULL, 10); - if (errno == ERANGE) + endptr++; + version= strtol(endptr, &endptr, 10); + if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX) { instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX; - return memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version")); + return memcached_set_error(*instance, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version")); } instance->micro_version= uint8_t(version); } @@ -744,7 +787,7 @@ static memcached_return_t binary_read_one_response(memcached_server_write_instan return rc; } -static memcached_return_t _read_one_response(memcached_server_write_instance_st instance, +static memcached_return_t _read_one_response(org::libmemcached::Instance* instance, char *buffer, const size_t buffer_length, memcached_result_st *result) { @@ -764,7 +807,6 @@ static memcached_return_t _read_one_response(memcached_server_write_instance_st else { rc= textual_read_one_response(instance, buffer, buffer_length, result); - assert(rc != MEMCACHED_PROTOCOL_ERROR); } if (memcached_fatal(rc)) @@ -775,7 +817,7 @@ static memcached_return_t _read_one_response(memcached_server_write_instance_st return rc; } -memcached_return_t memcached_read_one_response(memcached_server_write_instance_st instance, +memcached_return_t memcached_read_one_response(org::libmemcached::Instance* instance, memcached_result_st *result) { char buffer[SMALL_STRING_LEN]; @@ -789,7 +831,7 @@ memcached_return_t memcached_read_one_response(memcached_server_write_instance_s return _read_one_response(instance, buffer, sizeof(buffer), result); } -memcached_return_t memcached_response(memcached_server_write_instance_st instance, +memcached_return_t memcached_response(org::libmemcached::Instance* instance, memcached_result_st *result) { char buffer[1024]; @@ -797,7 +839,7 @@ memcached_return_t memcached_response(memcached_server_write_instance_st instanc return memcached_response(instance, buffer, sizeof(buffer), result); } -memcached_return_t memcached_response(memcached_server_write_instance_st instance, +memcached_return_t memcached_response(org::libmemcached::Instance* instance, char *buffer, size_t buffer_length, memcached_result_st *result) {