void _libmemcached_free(const memcached_st*, void *mem, void*)
{
if (mem)
+ {
free(mem);
+ }
}
void *_libmemcached_malloc(const memcached_st *, size_t size, void *)
{
void *ret = _libmemcached_malloc(self, nelem * size, context);
if (not ret)
+ {
memset(ret, 0, nelem * size);
+ }
return ret;
}
memcached_calloc_fn mem_calloc,
void *context)
{
+ if (self == NULL)
+ {
+ return MEMCACHED_INVALID_ARGUMENTS;
+ }
+
/* All should be set, or none should be set */
if (mem_malloc == NULL && mem_free == NULL && mem_realloc == NULL && mem_calloc == NULL)
{
}
else if (mem_malloc == NULL || mem_free == NULL || mem_realloc == NULL || mem_calloc == NULL)
{
- return MEMCACHED_FAILURE;
+ return memcached_set_error(*self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("NULL parameter provided for one or more allocators"));
}
else
{
memcached_realloc_fn *mem_realloc,
memcached_calloc_fn *mem_calloc)
{
+ if (self == NULL)
+ {
+ return;
+ }
+
*mem_malloc= self->allocators.malloc;
*mem_free= self->allocators.free;
*mem_realloc= self->allocators.realloc;
#ifdef __cplusplus
#include <libmemcached/backtrace.hpp>
#include <libmemcached/assert.hpp>
+#include <libmemcached/server.hpp>
#endif
#include <libmemcached/continuum.hpp>
LIBMEMCACHED_LOCAL
memcached_return_t memcached_purge(memcached_server_write_instance_st ptr);
-LIBMEMCACHED_LOCAL
- memcached_server_st *__server_create_with(const memcached_st *memc,
- memcached_server_write_instance_st host,
- const char *hostname,
- in_port_t port,
- uint32_t weight,
- memcached_connection_t type);
-
static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool binary)
{
hints.ai_protocol= IPPROTO_TCP;
}
+ server->address_info= NULL;
int errcode;
switch(errcode= getaddrinfo(server->hostname, str_port, &hints, &server->address_info))
{
#include <sys/time.h>
/* Protoypes (static) */
-static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
- in_port_t port,
- uint32_t weight,
- memcached_connection_t type);
-
static memcached_return_t update_continuum(memcached_st *ptr);
static int compare_servers(const void *p1, const void *p2)
return MEMCACHED_SUCCESS;
}
+static memcached_return_t server_add(memcached_st *ptr,
+ const memcached_string_t& hostname,
+ in_port_t port,
+ uint32_t weight,
+ memcached_connection_t type)
+{
+ assert_msg(ptr, "Programmer mistake, somehow server_add() was passed a NULL memcached_st");
+ if ( (ptr->flags.use_udp and type != MEMCACHED_CONNECTION_UDP)
+ or ( (type == MEMCACHED_CONNECTION_UDP) and (not ptr->flags.use_udp) ) )
+ {
+ return memcached_set_error(*ptr, MEMCACHED_INVALID_HOST_PROTOCOL, MEMCACHED_AT);
+ }
+
+ memcached_server_st *new_host_list= static_cast<memcached_server_st*>(libmemcached_realloc(ptr, memcached_server_list(ptr),
+ sizeof(memcached_server_st) * (ptr->number_of_hosts + 1)));
+
+ if (not new_host_list)
+ {
+ return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+ }
+
+ memcached_server_list_set(ptr, new_host_list);
+
+ /* TODO: Check return type */
+ memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
+
+ if (not __server_create_with(ptr, instance, hostname, port, weight, type))
+ {
+ return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
+ }
+
+ if (weight > 1)
+ {
+ ptr->ketama.weighted= true;
+ }
+
+ ptr->number_of_hosts++;
+
+ // @note we place the count in the bottom of the server list
+ instance= memcached_server_instance_fetch(ptr, 0);
+ memcached_servers_set_count(instance, memcached_server_count(ptr));
+
+ return run_distribution(ptr);
+}
+
memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_server_list_st list)
{
instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
WATCHPOINT_ASSERT(instance);
- if (not __server_create_with(ptr, instance, list[x].hostname,
- list[x].port, list[x].weight, list[x].type))
+ memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) };
+ if (__server_create_with(ptr, instance,
+ hostname,
+ list[x].port, list[x].weight, list[x].type) == NULL)
{
return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
}
const char *filename,
uint32_t weight)
{
- if (! filename)
+ if (ptr == NULL)
+ {
return MEMCACHED_FAILURE;
+ }
+
+ memcached_string_t _filename= { memcached_string_make_from_cstr(filename) };
+ if (memcached_is_valid_servername(_filename) == false)
+ {
+ memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid filename for socket provided"));
+ }
- return server_add(ptr, filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
+ return server_add(ptr, _filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
}
memcached_return_t memcached_server_add_udp(memcached_st *ptr,
in_port_t port,
uint32_t weight)
{
+ if (ptr == NULL)
+ {
+ return MEMCACHED_INVALID_ARGUMENTS;
+ }
+
if (not port)
+ {
port= MEMCACHED_DEFAULT_PORT;
+ }
if (not hostname)
+ {
hostname= "localhost";
+ }
- return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_UDP);
+ memcached_string_t _hostname= { memcached_string_make_from_cstr(hostname) };
+ if (memcached_is_valid_servername(_hostname) == false)
+ {
+ memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided"));
+ }
+
+ return server_add(ptr, _hostname, port, weight, MEMCACHED_CONNECTION_UDP);
}
memcached_return_t memcached_server_add(memcached_st *ptr,
in_port_t port,
uint32_t weight)
{
- if (not port)
- port= MEMCACHED_DEFAULT_PORT;
-
- if (not hostname)
- hostname= "localhost";
-
- return server_add(ptr, hostname, port, weight, hostname[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET : MEMCACHED_CONNECTION_TCP);
-}
-
-static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
- in_port_t port,
- uint32_t weight,
- memcached_connection_t type)
-{
-
- if ( (ptr->flags.use_udp and type != MEMCACHED_CONNECTION_UDP)
- or ( (type == MEMCACHED_CONNECTION_UDP) and (not ptr->flags.use_udp) ) )
+ if (ptr == NULL)
{
- return MEMCACHED_INVALID_HOST_PROTOCOL;
+ return MEMCACHED_INVALID_ARGUMENTS;
}
- memcached_server_st *new_host_list= static_cast<memcached_server_st*>(libmemcached_realloc(ptr, memcached_server_list(ptr),
- sizeof(memcached_server_st) * (ptr->number_of_hosts + 1)));
-
- if (not new_host_list)
- return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
-
- memcached_server_list_set(ptr, new_host_list);
-
- /* TODO: Check return type */
- memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
-
- if (not __server_create_with(ptr, instance, hostname, port, weight, type))
+ if (port == 0)
{
- return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
+ port= MEMCACHED_DEFAULT_PORT;
}
- if (weight > 1)
+ size_t hostname_length= hostname ? strlen(hostname) : 0;
+ if (hostname_length == 0)
{
- ptr->ketama.weighted= true;
+ hostname= "localhost";
+ hostname_length= sizeof("localhost") -1;
}
- ptr->number_of_hosts++;
+ memcached_string_t _hostname= { hostname, hostname_length };
- // @note we place the count in the bottom of the server list
- instance= memcached_server_instance_fetch(ptr, 0);
- memcached_servers_set_count(instance, memcached_server_count(ptr));
+ if (memcached_is_valid_servername(_hostname) == false)
+ {
+ return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided"));
+ }
- return run_distribution(ptr);
+ return server_add(ptr, _hostname, port, weight, _hostname.c_str[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET : MEMCACHED_CONNECTION_TCP);
}
memcached_return_t memcached_server_add_parsed(memcached_st *ptr,
memcpy(buffer, hostname, hostname_length);
buffer[hostname_length]= 0;
- return server_add(ptr, buffer,
+ memcached_string_t _hostname= { buffer, hostname_length };
+
+ return server_add(ptr, _hostname,
port,
weight,
MEMCACHED_CONNECTION_TCP);
libmemcached/protocol/binary_handler.h \
libmemcached/protocol/common.h \
libmemcached/response.h \
+ libmemcached/server.hpp \
libmemcached/server_instance.h \
libmemcached/string.hpp \
libmemcached/virtual_bucket.h
#include <libmemcached/common.h>
static inline void _server_init(memcached_server_st *self, memcached_st *root,
- const char *hostname, in_port_t port,
+ const memcached_string_t& hostname,
+ in_port_t port,
uint32_t weight, memcached_connection_t type)
{
self->options.is_shutting_down= false;
self->root= root;
self->limit_maxbytes= 0;
- if (hostname)
- {
- strncpy(self->hostname, hostname, NI_MAXHOST - 1);
- }
- else
- {
- self->hostname[0]= 0;
- }
+ memcpy(self->hostname, hostname.c_str, hostname.size);
+ self->hostname[hostname.size]= 0;
}
static memcached_server_st *_server_create(memcached_server_st *self, const memcached_st *memc)
self= (memcached_server_st *)libmemcached_malloc(memc, sizeof(memcached_server_st));
if (not self)
+ {
return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
+ }
self->options.is_allocated= true;
}
return self;
}
-memcached_server_st *__server_create_with(const memcached_st *memc,
+memcached_server_st *__server_create_with(memcached_st *memc,
memcached_server_write_instance_st self,
- const char *hostname, in_port_t port,
- uint32_t weight, memcached_connection_t type)
+ const memcached_string_t& hostname,
+ const in_port_t port,
+ uint32_t weight,
+ const memcached_connection_t type)
{
+ if (memcached_is_valid_servername(hostname) == false)
+ {
+ memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided"));
+ return NULL;
+ }
+
self= _server_create(self, memc);
if (not self)
{
freeaddrinfo(self->address_info);
self->address_info= NULL;
+ self->address_info_next= NULL;
}
memcached_error_free(*self);
If we do not have a valid object to clone from, we toss an error.
*/
memcached_server_st *memcached_server_clone(memcached_server_st *destination,
- const memcached_server_st *source)
+ memcached_server_st *source)
{
/* We just do a normal create if source is missing */
if (not source)
+ {
return NULL;
+ }
+ memcached_string_t hostname= { memcached_string_make_from_cstr(source->hostname) };
destination= __server_create_with(source->root, destination,
- source->hostname, source->port, source->weight,
+ hostname,
+ source->port, source->weight,
source->type);
if (not destination)
{
LIBMEMCACHED_LOCAL
memcached_server_st *memcached_server_clone(memcached_server_st *destination,
- const memcached_server_st *source);
+ memcached_server_st *source);
LIBMEMCACHED_API
memcached_server_instance_st memcached_server_get_last_disconnect(const memcached_st *ptr);
--- /dev/null
+/* 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.
+ *
+ */
+
+
+#pragma once
+
+#include <libmemcached/basic_string.h>
+
+static inline bool memcached_is_valid_servername(const memcached_string_t& arg)
+{
+ return arg.size > 0 or arg.size < NI_MAXHOST;
+}
+
+LIBMEMCACHED_LOCAL
+ memcached_server_st *__server_create_with(memcached_st *memc,
+ memcached_server_write_instance_st host,
+ const memcached_string_t& hostname,
+ const in_port_t port,
+ uint32_t weight,
+ const memcached_connection_t type);
port= MEMCACHED_DEFAULT_PORT;
}
+
/* Increment count for hosts */
count= 1;
if (ptr != NULL)
return NULL;
}
+ memcached_string_t _hostname= { memcached_string_make_from_cstr(hostname) };
/* @todo Check return type */
- if (not __server_create_with(NULL, &new_host_list[count-1], hostname, port, weight, port ? MEMCACHED_CONNECTION_TCP : MEMCACHED_CONNECTION_UNIX_SOCKET))
+ if (not __server_create_with(NULL, &new_host_list[count-1], _hostname, port, weight, port ? MEMCACHED_CONNECTION_TCP : MEMCACHED_CONNECTION_UNIX_SOCKET))
{
*error= memcached_set_errno(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
return NULL;
tests/pool.h \
tests/print.h \
tests/replication.h \
+ tests/server_add.h \
tests/string.h \
tests/virtual_buckets.h
tests/pool.cc \
tests/print.cc \
tests/replication.cc \
+ tests/server_add.cc \
tests/virtual_buckets.cc
tests_testapp_SOURCES+= clients/generator.cc clients/execute.cc
tests_testapp_DEPENDENCIES= \
#define SMALL_STRING_LEN 1024
#include <libtest/test.hpp>
+
+#include "tests/basic.h"
+#include "tests/debug.h"
#include "tests/deprecated.h"
-#include "tests/parser.h"
+#include "tests/error_conditions.h"
#include "tests/ketama.h"
-#include "tests/pool.h"
#include "tests/namespace.h"
-#include "tests/replication.h"
-#include "tests/debug.h"
-#include "tests/basic.h"
-#include "tests/error_conditions.h"
+#include "tests/parser.h"
+#include "tests/pool.h"
#include "tests/print.h"
+#include "tests/replication.h"
+#include "tests/server_add.h"
#include "tests/virtual_buckets.h"
using namespace libtest;
static test_return_t set_memory_alloc(memcached_st *memc)
{
- test_compare(MEMCACHED_FAILURE,
+ test_compare(MEMCACHED_INVALID_ARGUMENTS,
memcached_set_memory_allocators(memc, NULL, my_free,
my_realloc, my_calloc, NULL));
{0, 0, (test_callback_fn*)0}
};
+test_st memcached_server_add_tests[] ={
+ {"memcached_server_add(\"\")", false, (test_callback_fn*)memcached_server_add_empty_test },
+ {"memcached_server_add(NULL)", false, (test_callback_fn*)memcached_server_add_null_test },
+ {0, 0, (test_callback_fn*)0}
+};
+
test_st namespace_tests[] ={
{"basic tests", true, (test_callback_fn*)selection_of_namespace_tests },
{"increment", true, (test_callback_fn*)memcached_increment_namespace },
{"basic", 0, 0, basic_tests},
{"hsieh_availability", 0, 0, hsieh_availability},
{"murmur_availability", 0, 0, murmur_availability},
+ {"memcached_server_add", 0, 0, memcached_server_add_tests},
{"block", 0, 0, tests},
{"binary", (test_callback_fn*)pre_binary, 0, tests},
{"nonblock", (test_callback_fn*)pre_nonblock, 0, tests},
--- /dev/null
+/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ *
+ * Libmemcached Client and Server
+ *
+ * Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ * All rights reserved.
+ *
+ * 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.
+ *
+ */
+
+#include <config.h>
+#include <libtest/test.hpp>
+
+using namespace libtest;
+
+#include <libmemcached/memcached.h>
+
+#include <tests/server_add.h>
+
+test_return_t memcached_server_add_null_test(memcached_st*)
+{
+ memcached_st *memc= memcached_create(NULL);
+
+ test_compare(MEMCACHED_SUCCESS, memcached_server_add(memc, NULL, 0));
+
+ memcached_free(memc);
+
+ return TEST_SUCCESS;
+}
+
+test_return_t memcached_server_add_empty_test(memcached_st*)
+{
+ memcached_st *memc= memcached_create(NULL);
+
+ test_compare(MEMCACHED_SUCCESS, memcached_server_add(memc, "", 0));
+
+ memcached_free(memc);
+
+ return TEST_SUCCESS;
+}
--- /dev/null
+/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ *
+ * Libmemcached Client and Server
+ *
+ * Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ * All rights reserved.
+ *
+ * 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.
+ *
+ */
+
+#pragma once
+
+test_return_t memcached_server_add_null_test(memcached_st*);
+test_return_t memcached_server_add_empty_test(memcached_st*);