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