Fix for memory leak when parsing servers.
[awesomized/libmemcached] / libmemcached / server.c
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
17 static inline 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
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
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
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 if (self->root)
119 {
120 libmemcached_free(self->root, self);
121 }
122 else
123 {
124 free(self);
125 }
126 }
127 else
128 {
129 self->options.is_initialized= false;
130 }
131 }
132
133 /*
134 If we do not have a valid object to clone from, we toss an error.
135 */
136 memcached_server_st *memcached_server_clone(memcached_server_st *destination,
137 const memcached_server_st *source)
138 {
139 /* We just do a normal create if source is missing */
140 if (source == NULL)
141 return NULL;
142
143 destination= memcached_server_create_with(source->root, destination,
144 source->hostname, source->port, source->weight,
145 source->type);
146 if (destination != NULL)
147 {
148 destination->cached_errno= source->cached_errno;
149
150 if (source->cached_server_error)
151 destination->cached_server_error= strdup(source->cached_server_error);
152 }
153
154 return destination;
155
156 }
157
158 memcached_return_t memcached_server_cursor(const memcached_st *ptr,
159 const memcached_server_fn *callback,
160 void *context,
161 uint32_t number_of_callbacks)
162 {
163 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
164 {
165 memcached_server_instance_st instance=
166 memcached_server_instance_by_position(ptr, x);
167
168 for (uint32_t y= 0; y < number_of_callbacks; y++)
169 {
170 unsigned int iferror;
171
172 iferror= (*callback[y])(ptr, instance, context);
173
174 if (iferror)
175 continue;
176 }
177 }
178
179 return MEMCACHED_SUCCESS;
180 }
181
182 memcached_return_t memcached_server_execute(memcached_st *ptr,
183 memcached_server_execute_fn callback,
184 void *context)
185 {
186 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
187 {
188 memcached_server_write_instance_st instance=
189 memcached_server_instance_fetch(ptr, x);
190
191 unsigned int iferror;
192
193 iferror= (*callback)(ptr, instance, context);
194
195 if (iferror)
196 continue;
197 }
198
199 return MEMCACHED_SUCCESS;
200 }
201
202 memcached_server_instance_st memcached_server_by_key(const memcached_st *ptr,
203 const char *key,
204 size_t key_length,
205 memcached_return_t *error)
206 {
207 uint32_t server_key;
208 memcached_server_instance_st instance;
209
210 *error= memcached_validate_key_length(key_length,
211 ptr->flags.binary_protocol);
212 unlikely (*error != MEMCACHED_SUCCESS)
213 return NULL;
214
215 unlikely (memcached_server_count(ptr) == 0)
216 {
217 *error= MEMCACHED_NO_SERVERS;
218 return NULL;
219 }
220
221 if (ptr->flags.verify_key && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
222 {
223 *error= MEMCACHED_BAD_KEY_PROVIDED;
224 return NULL;
225 }
226
227 server_key= memcached_generate_hash(ptr, key, key_length);
228 instance= memcached_server_instance_by_position(ptr, server_key);
229
230 return instance;
231
232 }
233
234 void memcached_server_error_reset(memcached_server_st *ptr)
235 {
236 ptr->cached_server_error[0]= 0;
237 }
238
239 memcached_server_instance_st memcached_server_get_last_disconnect(const memcached_st *ptr)
240 {
241 return ptr->last_disconnected_server;
242 }
243
244 void memcached_server_list_free(memcached_server_list_st self)
245 {
246 if (self == NULL)
247 return;
248
249 const memcached_st *root= self->root;
250
251 for (uint32_t x= 0; x < memcached_server_list_count(self); x++)
252 {
253 if (self[x].address_info)
254 {
255 freeaddrinfo(self[x].address_info);
256 self[x].address_info= NULL;
257 }
258 }
259
260 if (root)
261 {
262 libmemcached_free(root, self);
263 }
264 else
265 {
266 free(self);
267 }
268 }
269
270 uint32_t memcached_servers_set_count(memcached_server_st *servers, uint32_t count)
271 {
272 return servers->number_of_hosts= count;
273 }
274
275 uint32_t memcached_server_count(const memcached_st *self)
276 {
277 return self->number_of_hosts;
278 }
279
280 const char *memcached_server_name(memcached_server_instance_st self)
281 {
282 return self->hostname;
283 }
284
285 in_port_t memcached_server_port(memcached_server_instance_st self)
286 {
287 return self->port;
288 }
289
290 uint32_t memcached_server_response_count(memcached_server_instance_st self)
291 {
292 return self->cursor_active;
293 }
294
295 const char *memcached_server_error(memcached_server_instance_st ptr)
296 {
297 return ptr
298 ? ptr->cached_server_error
299 : NULL;
300 }
301