Fix for bad name servers.
[awesomized/libmemcached] / libmemcached / connect.c
1 #include "common.h"
2 #include <netdb.h>
3 #include <poll.h>
4 #include <sys/time.h>
5 #include <time.h>
6
7 static memcached_return_t set_hostinfo(memcached_server_st *server)
8 {
9 struct addrinfo *ai;
10 struct addrinfo hints;
11 char str_port[NI_MAXSERV];
12 uint32_t counter= 5;
13
14 snprintf(str_port, NI_MAXSERV, "%u", (uint32_t)server->port);
15
16 memset(&hints, 0, sizeof(hints));
17
18 // hints.ai_family= AF_INET;
19 if (server->type == MEMCACHED_CONNECTION_UDP)
20 {
21 hints.ai_protocol= IPPROTO_UDP;
22 hints.ai_socktype= SOCK_DGRAM;
23 }
24 else
25 {
26 hints.ai_socktype= SOCK_STREAM;
27 hints.ai_protocol= IPPROTO_TCP;
28 }
29
30 while (--counter)
31 {
32 int e= getaddrinfo(server->hostname, str_port, &hints, &ai);
33
34 if (e == 0)
35 {
36 break;
37 }
38 else if (e == EAI_AGAIN)
39 {
40 struct timespec dream, rem;
41
42 dream.tv_nsec= 1000;
43 dream.tv_sec= 0;
44
45 nanosleep(&dream, &rem);
46
47 continue;
48 }
49 else
50 {
51 WATCHPOINT_STRING(server->hostname);
52 WATCHPOINT_STRING(gai_strerror(e));
53 return MEMCACHED_HOST_LOOKUP_FAILURE;
54 }
55 }
56
57 if (server->address_info)
58 {
59 freeaddrinfo(server->address_info);
60 server->address_info= NULL;
61 }
62 server->address_info= ai;
63
64 return MEMCACHED_SUCCESS;
65 }
66
67 static memcached_return_t set_socket_options(memcached_server_st *ptr)
68 {
69 WATCHPOINT_ASSERT(ptr->fd != -1);
70
71 if (ptr->type == MEMCACHED_CONNECTION_UDP)
72 return MEMCACHED_SUCCESS;
73
74 #ifdef HAVE_SNDTIMEO
75 if (ptr->root->snd_timeout)
76 {
77 int error;
78 struct timeval waittime;
79
80 waittime.tv_sec= 0;
81 waittime.tv_usec= ptr->root->snd_timeout;
82
83 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDTIMEO,
84 &waittime, (socklen_t)sizeof(struct timeval));
85 WATCHPOINT_ASSERT(error == 0);
86 if (error)
87 return MEMCACHED_FAILURE;
88 }
89 #endif
90
91 #ifdef HAVE_RCVTIMEO
92 if (ptr->root->rcv_timeout)
93 {
94 int error;
95 struct timeval waittime;
96
97 waittime.tv_sec= 0;
98 waittime.tv_usec= ptr->root->rcv_timeout;
99
100 error= setsockopt(ptr->fd, SOL_SOCKET, SO_RCVTIMEO,
101 &waittime, (socklen_t)sizeof(struct timeval));
102 WATCHPOINT_ASSERT(error == 0);
103 if (error)
104 return MEMCACHED_FAILURE;
105 }
106 #endif
107
108 if (ptr->root->flags.no_block)
109 {
110 int error;
111 struct linger linger;
112
113 linger.l_onoff= 1;
114 linger.l_linger= 0; /* By default on close() just drop the socket */
115 error= setsockopt(ptr->fd, SOL_SOCKET, SO_LINGER,
116 &linger, (socklen_t)sizeof(struct linger));
117 WATCHPOINT_ASSERT(error == 0);
118 if (error)
119 return MEMCACHED_FAILURE;
120 }
121
122 if (ptr->root->flags.tcp_nodelay)
123 {
124 int flag= 1;
125 int error;
126
127 error= setsockopt(ptr->fd, IPPROTO_TCP, TCP_NODELAY,
128 &flag, (socklen_t)sizeof(int));
129 WATCHPOINT_ASSERT(error == 0);
130 if (error)
131 return MEMCACHED_FAILURE;
132 }
133
134 if (ptr->root->flags.tcp_keepalive)
135 {
136 int flag= 1;
137 int error;
138
139 error= setsockopt(ptr->fd, SOL_SOCKET, SO_KEEPALIVE,
140 &flag, (socklen_t)sizeof(int));
141 WATCHPOINT_ASSERT(error == 0);
142 if (error)
143 return MEMCACHED_FAILURE;
144 }
145
146 #ifdef TCP_KEEPIDLE
147 if (ptr->root->tcp_keepidle > 0)
148 {
149 int error;
150
151 error= setsockopt(ptr->fd, IPPROTO_TCP, TCP_KEEPIDLE,
152 &ptr->root->tcp_keepidle, (socklen_t)sizeof(int));
153 WATCHPOINT_ASSERT(error == 0);
154 if (error)
155 return MEMCACHED_FAILURE;
156 }
157 #endif
158
159 if (ptr->root->send_size > 0)
160 {
161 int error;
162
163 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDBUF,
164 &ptr->root->send_size, (socklen_t)sizeof(int));
165 WATCHPOINT_ASSERT(error == 0);
166 if (error)
167 return MEMCACHED_FAILURE;
168 }
169
170 if (ptr->root->recv_size > 0)
171 {
172 int error;
173
174 error= setsockopt(ptr->fd, SOL_SOCKET, SO_RCVBUF,
175 &ptr->root->recv_size, (socklen_t)sizeof(int));
176 WATCHPOINT_ASSERT(error == 0);
177 if (error)
178 return MEMCACHED_FAILURE;
179 }
180
181 /* libmemcached will always use nonblocking IO to avoid write deadlocks */
182 int flags;
183
184 do
185 flags= fcntl(ptr->fd, F_GETFL, 0);
186 while (flags == -1 && (errno == EINTR || errno == EAGAIN));
187
188 unlikely (flags == -1)
189 {
190 return MEMCACHED_CONNECTION_FAILURE;
191 }
192 else if ((flags & O_NONBLOCK) == 0)
193 {
194 int rval;
195
196 do
197 rval= fcntl(ptr->fd, F_SETFL, flags | O_NONBLOCK);
198 while (rval == -1 && (errno == EINTR || errno == EAGAIN));
199
200 unlikely (rval == -1)
201 {
202 return MEMCACHED_CONNECTION_FAILURE;
203 }
204 }
205
206 return MEMCACHED_SUCCESS;
207 }
208
209 static memcached_return_t unix_socket_connect(memcached_server_st *ptr)
210 {
211 struct sockaddr_un servAddr;
212
213 if (ptr->fd == -1)
214 {
215 if ((ptr->fd= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
216 {
217 ptr->cached_errno= errno;
218 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
219 }
220
221 memset(&servAddr, 0, sizeof (struct sockaddr_un));
222 servAddr.sun_family= AF_UNIX;
223 strcpy(servAddr.sun_path, ptr->hostname); /* Copy filename */
224
225 test_connect:
226 if (connect(ptr->fd,
227 (struct sockaddr *)&servAddr,
228 sizeof(servAddr)) < 0)
229 {
230 switch (errno)
231 {
232 case EINPROGRESS:
233 case EALREADY:
234 case EINTR:
235 goto test_connect;
236 case EISCONN: /* We were spinning waiting on connect */
237 break;
238 default:
239 WATCHPOINT_ERRNO(errno);
240 ptr->cached_errno= errno;
241 return MEMCACHED_ERRNO;
242 }
243 }
244 }
245
246 WATCHPOINT_ASSERT(ptr->fd != -1);
247 return MEMCACHED_SUCCESS;
248 }
249
250 static memcached_return_t network_connect(memcached_server_st *ptr)
251 {
252 if (ptr->fd == -1)
253 {
254 struct addrinfo *use;
255
256 WATCHPOINT_ASSERT(ptr->cursor_active == 0);
257
258 if (! ptr->options.sockaddr_inited ||
259 (!(ptr->root->flags.use_cache_lookups)))
260 {
261 memcached_return_t rc;
262
263 rc= set_hostinfo(ptr);
264 if (rc != MEMCACHED_SUCCESS)
265 return rc;
266 ptr->options.sockaddr_inited= true;
267 }
268
269 use= ptr->address_info;
270 /* Create the socket */
271 while (use != NULL)
272 {
273 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
274 if (ptr->type == MEMCACHED_CONNECTION_UDP && use->ai_family != AF_INET)
275 {
276 use= use->ai_next;
277 continue;
278 }
279
280 if ((ptr->fd= socket(use->ai_family,
281 use->ai_socktype,
282 use->ai_protocol)) < 0)
283 {
284 ptr->cached_errno= errno;
285 WATCHPOINT_ERRNO(errno);
286 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
287 }
288
289 (void)set_socket_options(ptr);
290
291 /* connect to server */
292 if ((connect(ptr->fd, use->ai_addr, use->ai_addrlen) == -1))
293 {
294 ptr->cached_errno= errno;
295 if (errno == EINPROGRESS || /* nonblocking mode - first return, */
296 errno == EALREADY) /* nonblocking mode - subsequent returns */
297 {
298 struct pollfd fds[1];
299 fds[0].fd = ptr->fd;
300 fds[0].events = POLLOUT;
301
302 int timeout= ptr->root->connect_timeout;
303 if (ptr->root->flags.no_block == false)
304 timeout= -1;
305
306 size_t loop_max= 5;
307 while (--loop_max)
308 {
309 int error= poll(fds, 1, timeout);
310
311 switch (error)
312 {
313 case 1:
314 loop_max= 1;
315 break;
316 case 0:
317 continue;
318 // A real error occurred and we need to completely bail
319 default:
320 WATCHPOINT_ERRNO(errno);
321 switch (errno)
322 {
323 #ifdef TARGET_OS_LINUX
324 case ERESTART:
325 #endif
326 case EINTR:
327 continue;
328 default:
329 if (fds[0].revents & POLLERR)
330 {
331 int err;
332 socklen_t len= sizeof (err);
333 (void)getsockopt(ptr->fd, SOL_SOCKET, SO_ERROR, &err, &len);
334 ptr->cached_errno= (err == 0) ? errno : err;
335 }
336
337 (void)close(ptr->fd);
338 ptr->fd= -1;
339
340 break;
341 }
342 }
343 }
344 }
345 else if (errno == EISCONN) /* we are connected :-) */
346 {
347 break;
348 }
349 else if (errno != EINTR)
350 {
351 (void)close(ptr->fd);
352 ptr->fd= -1;
353 break;
354 }
355 }
356
357 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
358 if (ptr->fd != -1 && ptr->root->sasl.callbacks != NULL)
359 {
360 memcached_return rc= memcached_sasl_authenticate_connection(ptr);
361 if (rc != MEMCACHED_SUCCESS)
362 {
363 (void)close(ptr->fd);
364 ptr->fd= -1;
365
366 return rc;
367 }
368 }
369 #endif
370
371
372 if (ptr->fd != -1)
373 {
374 return MEMCACHED_SUCCESS;
375 }
376 use = use->ai_next;
377 }
378 }
379
380 if (ptr->fd == -1)
381 {
382 WATCHPOINT_STRING("Never got a good file descriptor");
383
384 /* Failed to connect. schedule next retry */
385 if (ptr->root->retry_timeout)
386 {
387 struct timeval next_time;
388
389 if (gettimeofday(&next_time, NULL) == 0)
390 ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
391 }
392
393 if (ptr->cached_errno == 0)
394 return MEMCACHED_TIMEOUT;
395
396 return MEMCACHED_ERRNO; /* The last error should be from connect() */
397 }
398
399 return MEMCACHED_SUCCESS; /* The last error should be from connect() */
400 }
401
402 void set_last_disconnected_host(memcached_server_write_instance_st ptr)
403 {
404 // const_cast
405 memcached_st *root= (memcached_st *)ptr->root;
406
407 #if 0
408 WATCHPOINT_STRING(ptr->hostname);
409 WATCHPOINT_NUMBER(ptr->port);
410 WATCHPOINT_ERRNO(ptr->cached_errno);
411 #endif
412 if (root->last_disconnected_server)
413 memcached_server_free(root->last_disconnected_server);
414 root->last_disconnected_server= memcached_server_clone(NULL, ptr);
415 }
416
417 memcached_return_t memcached_connect(memcached_server_write_instance_st ptr)
418 {
419 memcached_return_t rc= MEMCACHED_NO_SERVERS;
420 LIBMEMCACHED_MEMCACHED_CONNECT_START();
421
422 /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */
423 WATCHPOINT_ASSERT(ptr->root);
424 if (ptr->root->retry_timeout && ptr->next_retry)
425 {
426 struct timeval curr_time;
427
428 gettimeofday(&curr_time, NULL);
429
430 // We should optimize this to remove the allocation if the server was
431 // the last server to die
432 if (ptr->next_retry > curr_time.tv_sec)
433 {
434 set_last_disconnected_host(ptr);
435
436 return MEMCACHED_SERVER_MARKED_DEAD;
437 }
438 }
439
440 // If we are over the counter failure, we just fail. Reject host only
441 // works if you have a set number of failures.
442 if (ptr->root->server_failure_limit && ptr->server_failure_counter >= ptr->root->server_failure_limit)
443 {
444 set_last_disconnected_host(ptr);
445
446 // @todo fix this by fixing behavior to no longer make use of
447 // memcached_st
448 if (_is_auto_eject_host(ptr->root))
449 {
450 run_distribution((memcached_st *)ptr->root);
451 }
452
453 return MEMCACHED_SERVER_MARKED_DEAD;
454 }
455
456 /* We need to clean up the multi startup piece */
457 switch (ptr->type)
458 {
459 case MEMCACHED_CONNECTION_UNKNOWN:
460 WATCHPOINT_ASSERT(0);
461 rc= MEMCACHED_NOT_SUPPORTED;
462 break;
463 case MEMCACHED_CONNECTION_UDP:
464 case MEMCACHED_CONNECTION_TCP:
465 rc= network_connect(ptr);
466 break;
467 case MEMCACHED_CONNECTION_UNIX_SOCKET:
468 rc= unix_socket_connect(ptr);
469 break;
470 case MEMCACHED_CONNECTION_MAX:
471 default:
472 WATCHPOINT_ASSERT(0);
473 }
474
475 if (rc == MEMCACHED_SUCCESS)
476 {
477 ptr->server_failure_counter= 0;
478 ptr->next_retry= 0;
479 }
480 else
481 {
482 ptr->server_failure_counter++;
483
484 set_last_disconnected_host(ptr);
485 }
486
487 LIBMEMCACHED_MEMCACHED_CONNECT_END();
488
489 return rc;
490 }