1 /* LibMemcached
2  * Copyright (C) 2006-2009 Brian Aker
3  * All rights reserved.
4  *
5  * Use and distribution licensed under the BSD license.  See
6  * the COPYING file in the parent directory for full text.
7  *
8  * Summary: String structure used for libmemcached.
9  *
10  */
11 
12 /*
13   This is a partial implementation for fetching/creating memcached_server_st objects.
14 */
15 #include "common.h"
16 
_server_init(memcached_server_st *self, const memcached_st *root, const char *hostname, in_port_t port, uint32_t weight, memcached_connection_t type)17 static void _server_init(memcached_server_st *self, const memcached_st *root,
18                                 const char *hostname, in_port_t port,
19                                 uint32_t weight, memcached_connection_t type)
20 {
21   self->options.sockaddr_inited= false;
22   self->options.is_shutting_down= false;
23   self->number_of_hosts= 0;
24   self->cursor_active= 0;
25   self->port= port;
26   self->cached_errno= 0;
27   self->fd= -1;
28   self->io_bytes_sent= 0;
29   self->server_failure_counter= 0;
30   self->weight= weight;
31   self->state.is_corked= false;
32   self->state.is_dead= false;
33   WATCHPOINT_SET(self->io_wait_count.read= 0);
34   WATCHPOINT_SET(self->io_wait_count.write= 0);
35   self->major_version= UINT8_MAX;
36   self->micro_version= UINT8_MAX;
37   self->minor_version= UINT8_MAX;
38   self->type= type;
39   self->read_ptr= self->read_buffer;
40   self->cached_server_error= NULL;
41   self->read_buffer_length= 0;
42   self->read_data_length= 0;
43   self->write_buffer_offset= 0;
44   self->address_info= NULL;
45 
46   if (root)
47   {
48     self->next_retry= root->retry_timeout;
49   }
50   else
51   {
52     self->next_retry= 0;
53   }
54 
55   self->root= root;
56   self->limit_maxbytes= 0;
57   if (hostname == NULL)
58     self->hostname[0]= 0;
59   else
60     strncpy(self->hostname, hostname, NI_MAXHOST - 1);
61 }
62 
_server_create(memcached_server_st *self, const memcached_st *memc)63 static memcached_server_st *_server_create(memcached_server_st *self, const memcached_st *memc)
64 {
65   if (self == NULL)
66   {
67    self= (memcached_server_st *)libmemcached_malloc(memc, sizeof(memcached_server_st));
68 
69     if (! self)
70       return NULL; /*  MEMCACHED_MEMORY_ALLOCATION_FAILURE */
71 
72     self->options.is_allocated= true;
73   }
74   else
75   {
76     self->options.is_allocated= false;
77   }
78 
79   self->options.is_initialized= true;
80 
81   return self;
82 }
83 
memcached_server_create_with(const memcached_st *memc, memcached_server_write_instance_st self, const char *hostname, in_port_t port, uint32_t weight, memcached_connection_t type)84 memcached_server_st *memcached_server_create_with(const memcached_st *memc,
85                                                   memcached_server_write_instance_st self,
86                                                   const char *hostname, in_port_t port,
87                                                   uint32_t weight, memcached_connection_t type)
88 {
89   self= _server_create(self, memc);
90 
91   if (self == NULL)
92     return NULL;
93 
94   _server_init(self, memc, hostname, port, weight, type);
95 
96 
97   if (type == MEMCACHED_CONNECTION_UDP)
98   {
99     self->write_buffer_offset= UDP_DATAGRAM_HEADER_LENGTH;
100     memcached_io_init_udp_header(self, 0);
101   }
102 
103   return self;
104 }
105 
memcached_server_free(memcached_server_st *self)106 void memcached_server_free(memcached_server_st *self)
107 {
108   memcached_quit_server(self, false);
109 
110   if (self->cached_server_error)
111     free(self->cached_server_error);
112 
113   if (self->address_info)
114     freeaddrinfo(self->address_info);
115 
116   if (memcached_is_allocated(self))
117   {
118     libmemcached_free(self->root, self);
119   }
120   else
121   {
122     self->options.is_initialized= false;
123   }
124 }
125 
126 /*
127   If we do not have a valid object to clone from, we toss an error.
128 */
memcached_server_clone(memcached_server_st *destination, const memcached_server_st *source)129 memcached_server_st *memcached_server_clone(memcached_server_st *destination,
130                                             const memcached_server_st *source)
131 {
132   /* We just do a normal create if source is missing */
133   if (source == NULL)
134     return NULL;
135 
136   destination= memcached_server_create_with(source->root, destination,
137                                             source->hostname, source->port, source->weight,
138                                             source->type);
139   if (destination != NULL)
140   {
141     destination->cached_errno= source->cached_errno;
142 
143     if (source->cached_server_error)
144       destination->cached_server_error= strdup(source->cached_server_error);
145   }
146 
147   return destination;
148 
149 }
150 
memcached_server_cursor(const memcached_st *ptr, const memcached_server_fn *callback, void *context, uint32_t number_of_callbacks)151 memcached_return_t memcached_server_cursor(const memcached_st *ptr,
152                                            const memcached_server_fn *callback,
153                                            void *context,
154                                            uint32_t number_of_callbacks)
155 {
156   uint32_t x, y;
157   for (x= 0; x < memcached_server_count(ptr); x++)
158   {
159     memcached_server_instance_st instance=
160       memcached_server_instance_by_position(ptr, x);
161 
162     for (y= 0; y < number_of_callbacks; y++)
163     {
164       unsigned int iferror;
165 
166       iferror= (*callback[y])(ptr, instance, context);
167 
168       if (iferror)
169         continue;
170     }
171   }
172 
173   return MEMCACHED_SUCCESS;
174 }
175 
memcached_server_execute(memcached_st *ptr, memcached_server_execute_fn callback, void *context)176 memcached_return_t memcached_server_execute(memcached_st *ptr,
177                                             memcached_server_execute_fn callback,
178                                             void *context)
179 {
180    uint32_t x;
181   for (x= 0; x < memcached_server_count(ptr); x++)
182   {
183     memcached_server_write_instance_st instance=
184       memcached_server_instance_fetch(ptr, x);
185 
186     unsigned int iferror;
187 
188     iferror= (*callback)(ptr, instance, context);
189 
190     if (iferror)
191       continue;
192   }
193 
194   return MEMCACHED_SUCCESS;
195 }
196 
memcached_server_by_key(const memcached_st *ptr, const char *key, size_t key_length, memcached_return_t *error)197 memcached_server_instance_st memcached_server_by_key(const memcached_st *ptr,
198                                                      const char *key,
199                                                      size_t key_length,
200                                                      memcached_return_t *error)
201 {
202   uint32_t server_key;
203   memcached_server_instance_st instance;
204 
205   *error= memcached_validate_key_length(key_length,
206                                         ptr->flags.binary_protocol);
207   unlikely (*error != MEMCACHED_SUCCESS)
208     return NULL;
209 
210   unlikely (memcached_server_count(ptr) == 0)
211   {
212     *error= MEMCACHED_NO_SERVERS;
213     return NULL;
214   }
215 
216   if (ptr->flags.verify_key && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
217   {
218     *error= MEMCACHED_BAD_KEY_PROVIDED;
219     return NULL;
220   }
221 
222   server_key= memcached_generate_hash(ptr, key, key_length);
223   instance= memcached_server_instance_by_position(ptr, server_key);
224 
225   return instance;
226 
227 }
228 
memcached_server_error_reset(memcached_server_st *ptr)229 void memcached_server_error_reset(memcached_server_st *ptr)
230 {
231   ptr->cached_server_error[0]= 0;
232 }
233 
memcached_server_get_last_disconnect(const memcached_st *ptr)234 memcached_server_instance_st memcached_server_get_last_disconnect(const memcached_st *ptr)
235 {
236   return ptr->last_disconnected_server;
237 }
238 
memcached_server_list_free(memcached_server_list_st self)239 void memcached_server_list_free(memcached_server_list_st self)
240 {
241    const memcached_st *root;
242    uint32_t x;
243 
244   if (self == NULL)
245     return;
246 
247   root= self->root;
248 
249   for (x= 0; x < memcached_server_list_count(self); x++)
250   {
251     if (self[x].address_info)
252     {
253       freeaddrinfo(self[x].address_info);
254       self[x].address_info= NULL;
255     }
256   }
257 
258   if (root)
259   {
260     libmemcached_free(root, self);
261   }
262   else
263   {
264     free(self);
265   }
266 }
267 
memcached_servers_set_count(memcached_server_st *servers, uint32_t count)268 uint32_t memcached_servers_set_count(memcached_server_st *servers, uint32_t count)
269 {
270   return servers->number_of_hosts= count;
271 }
272 
memcached_server_count(const memcached_st *self)273 uint32_t memcached_server_count(const memcached_st *self)
274 {
275   return self->number_of_hosts;
276 }
277 
memcached_server_name(memcached_server_instance_st self)278 const char *memcached_server_name(memcached_server_instance_st self)
279 {
280   return self->hostname;
281 }
282 
memcached_server_port(memcached_server_instance_st self)283 in_port_t memcached_server_port(memcached_server_instance_st self)
284 {
285   return self->port;
286 }
287 
memcached_server_response_count(memcached_server_instance_st self)288 uint32_t memcached_server_response_count(memcached_server_instance_st self)
289 {
290   return self->cursor_active;
291 }
292 
memcached_server_error(memcached_server_instance_st ptr)293 const char *memcached_server_error(memcached_server_instance_st ptr)
294 {
295   return ptr
296     ?  ptr->cached_server_error
297     : NULL;
298 }
299