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