125d7b0ab1b4adc842492284a3d11b67d755a1f3
[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
109 {
110 int set = 1;
111 int error= setsockopt(ptr->fd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
112
113 // This is not considered a fatal error
114 if (error == -1)
115 {
116 WATCHPOINT_ERRNO(errno);
117 perror("setsockopt(SO_NOSIGPIPE)");
118 }
119 }
120
121 if (ptr->root->flags.no_block)
122 {
123 int error;
124 struct linger linger;
125
126 linger.l_onoff= 1;
127 linger.l_linger= 0; /* By default on close() just drop the socket */
128 error= setsockopt(ptr->fd, SOL_SOCKET, SO_LINGER,
129 &linger, (socklen_t)sizeof(struct linger));
130 WATCHPOINT_ASSERT(error == 0);
131 if (error)
132 return MEMCACHED_FAILURE;
133 }
134
135 if (ptr->root->flags.tcp_nodelay)
136 {
137 int flag= 1;
138 int error;
139
140 error= setsockopt(ptr->fd, IPPROTO_TCP, TCP_NODELAY,
141 &flag, (socklen_t)sizeof(int));
142 WATCHPOINT_ASSERT(error == 0);
143 if (error)
144 return MEMCACHED_FAILURE;
145 }
146
147 if (ptr->root->flags.tcp_keepalive)
148 {
149 int flag= 1;
150 int error;
151
152 error= setsockopt(ptr->fd, SOL_SOCKET, SO_KEEPALIVE,
153 &flag, (socklen_t)sizeof(int));
154 WATCHPOINT_ASSERT(error == 0);
155 if (error)
156 return MEMCACHED_FAILURE;
157 }
158
159 #ifdef TCP_KEEPIDLE
160 if (ptr->root->tcp_keepidle > 0)
161 {
162 int error;
163
164 error= setsockopt(ptr->fd, IPPROTO_TCP, TCP_KEEPIDLE,
165 &ptr->root->tcp_keepidle, (socklen_t)sizeof(int));
166 WATCHPOINT_ASSERT(error == 0);
167 if (error)
168 return MEMCACHED_FAILURE;
169 }
170 #endif
171
172 if (ptr->root->send_size > 0)
173 {
174 int error;
175
176 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDBUF,
177 &ptr->root->send_size, (socklen_t)sizeof(int));
178 WATCHPOINT_ASSERT(error == 0);
179 if (error)
180 return MEMCACHED_FAILURE;
181 }
182
183 if (ptr->root->recv_size > 0)
184 {
185 int error;
186
187 error= setsockopt(ptr->fd, SOL_SOCKET, SO_RCVBUF,
188 &ptr->root->recv_size, (socklen_t)sizeof(int));
189 WATCHPOINT_ASSERT(error == 0);
190 if (error)
191 return MEMCACHED_FAILURE;
192 }
193
194 /* libmemcached will always use nonblocking IO to avoid write deadlocks */
195 int flags;
196
197 do
198 flags= fcntl(ptr->fd, F_GETFL, 0);
199 while (flags == -1 && (errno == EINTR || errno == EAGAIN));
200
201 unlikely (flags == -1)
202 {
203 return MEMCACHED_CONNECTION_FAILURE;
204 }
205 else if ((flags & O_NONBLOCK) == 0)
206 {
207 int rval;
208
209 do
210 rval= fcntl(ptr->fd, F_SETFL, flags | O_NONBLOCK);
211 while (rval == -1 && (errno == EINTR || errno == EAGAIN));
212
213 unlikely (rval == -1)
214 {
215 return MEMCACHED_CONNECTION_FAILURE;
216 }
217 }
218
219 return MEMCACHED_SUCCESS;
220 }
221
222 static memcached_return_t unix_socket_connect(memcached_server_st *ptr)
223 {
224 struct sockaddr_un servAddr;
225
226 if (ptr->fd == -1)
227 {
228 if ((ptr->fd= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
229 {
230 ptr->cached_errno= errno;
231 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
232 }
233
234 memset(&servAddr, 0, sizeof (struct sockaddr_un));
235 servAddr.sun_family= AF_UNIX;
236 strcpy(servAddr.sun_path, ptr->hostname); /* Copy filename */
237
238 test_connect:
239 if (connect(ptr->fd,
240 (struct sockaddr *)&servAddr,
241 sizeof(servAddr)) < 0)
242 {
243 switch (errno)
244 {
245 case EINPROGRESS:
246 case EALREADY:
247 case EINTR:
248 goto test_connect;
249 case EISCONN: /* We were spinning waiting on connect */
250 break;
251 default:
252 WATCHPOINT_ERRNO(errno);
253 ptr->cached_errno= errno;
254 return MEMCACHED_ERRNO;
255 }
256 }
257 }
258
259 WATCHPOINT_ASSERT(ptr->fd != -1);
260
261 return MEMCACHED_SUCCESS;
262 }
263
264 static memcached_return_t network_connect(memcached_server_st *ptr)
265 {
266 if (ptr->fd == -1)
267 {
268 struct addrinfo *use;
269
270 WATCHPOINT_ASSERT(ptr->cursor_active == 0);
271
272 if (! ptr->options.sockaddr_inited ||
273 (!(ptr->root->flags.use_cache_lookups)))
274 {
275 memcached_return_t rc;
276
277 rc= set_hostinfo(ptr);
278 if (rc != MEMCACHED_SUCCESS)
279 return rc;
280 ptr->options.sockaddr_inited= true;
281 }
282
283 use= ptr->address_info;
284 /* Create the socket */
285 while (use != NULL)
286 {
287 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
288 if (ptr->type == MEMCACHED_CONNECTION_UDP && use->ai_family != AF_INET)
289 {
290 use= use->ai_next;
291 continue;
292 }
293
294 if ((ptr->fd= socket(use->ai_family,
295 use->ai_socktype,
296 use->ai_protocol)) < 0)
297 {
298 ptr->cached_errno= errno;
299 WATCHPOINT_ERRNO(errno);
300 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
301 }
302
303 (void)set_socket_options(ptr);
304
305 /* connect to server */
306 if ((connect(ptr->fd, use->ai_addr, use->ai_addrlen) == -1))
307 {
308 ptr->cached_errno= errno;
309 if (errno == EINPROGRESS || /* nonblocking mode - first return, */
310 errno == EALREADY) /* nonblocking mode - subsequent returns */
311 {
312 struct pollfd fds[1];
313 fds[0].fd = ptr->fd;
314 fds[0].events = POLLOUT;
315
316 int timeout= ptr->root->connect_timeout;
317 if (ptr->root->flags.no_block == false)
318 timeout= -1;
319
320 size_t loop_max= 5;
321 while (--loop_max)
322 {
323 int error= poll(fds, 1, timeout);
324
325 switch (error)
326 {
327 case 1:
328 loop_max= 1;
329 break;
330 case 0:
331 continue;
332 // A real error occurred and we need to completely bail
333 default:
334 WATCHPOINT_ERRNO(errno);
335 switch (errno)
336 {
337 #ifdef TARGET_OS_LINUX
338 case ERESTART:
339 #endif
340 case EINTR:
341 continue;
342 default:
343 if (fds[0].revents & POLLERR)
344 {
345 int err;
346 socklen_t len= sizeof (err);
347 (void)getsockopt(ptr->fd, SOL_SOCKET, SO_ERROR, &err, &len);
348 ptr->cached_errno= (err == 0) ? errno : err;
349 }
350
351 (void)close(ptr->fd);
352 ptr->fd= -1;
353
354 break;
355 }
356 }
357 }
358 }
359 else if (errno == EISCONN) /* we are connected :-) */
360 {
361 break;
362 }
363 else if (errno != EINTR)
364 {
365 (void)close(ptr->fd);
366 ptr->fd= -1;
367 break;
368 }
369 }
370
371 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
372 if (ptr->fd != -1 && ptr->root->sasl.callbacks != NULL)
373 {
374 memcached_return rc= memcached_sasl_authenticate_connection(ptr);
375 if (rc != MEMCACHED_SUCCESS)
376 {
377 (void)close(ptr->fd);
378 ptr->fd= -1;
379
380 return rc;
381 }
382 }
383 #endif
384
385
386 if (ptr->fd != -1)
387 {
388 return MEMCACHED_SUCCESS;
389 }
390 use = use->ai_next;
391 }
392 }
393
394 if (ptr->fd == -1)
395 {
396 WATCHPOINT_STRING("Never got a good file descriptor");
397
398 /* Failed to connect. schedule next retry */
399 if (ptr->root->retry_timeout)
400 {
401 struct timeval next_time;
402
403 if (gettimeofday(&next_time, NULL) == 0)
404 ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
405 }
406
407 if (ptr->cached_errno == 0)
408 return MEMCACHED_TIMEOUT;
409
410 return MEMCACHED_ERRNO; /* The last error should be from connect() */
411 }
412
413 return MEMCACHED_SUCCESS; /* The last error should be from connect() */
414 }
415
416 void set_last_disconnected_host(memcached_server_write_instance_st ptr)
417 {
418 // const_cast
419 memcached_st *root= (memcached_st *)ptr->root;
420
421 #if 0
422 WATCHPOINT_STRING(ptr->hostname);
423 WATCHPOINT_NUMBER(ptr->port);
424 WATCHPOINT_ERRNO(ptr->cached_errno);
425 #endif
426 if (root->last_disconnected_server)
427 memcached_server_free(root->last_disconnected_server);
428 root->last_disconnected_server= memcached_server_clone(NULL, ptr);
429 }
430
431 memcached_return_t memcached_connect(memcached_server_write_instance_st ptr)
432 {
433 memcached_return_t rc= MEMCACHED_NO_SERVERS;
434 LIBMEMCACHED_MEMCACHED_CONNECT_START();
435
436 /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */
437 WATCHPOINT_ASSERT(ptr->root);
438 if (ptr->root->retry_timeout && ptr->next_retry)
439 {
440 struct timeval curr_time;
441
442 gettimeofday(&curr_time, NULL);
443
444 // We should optimize this to remove the allocation if the server was
445 // the last server to die
446 if (ptr->next_retry > curr_time.tv_sec)
447 {
448 set_last_disconnected_host(ptr);
449
450 return MEMCACHED_SERVER_MARKED_DEAD;
451 }
452 }
453
454 // If we are over the counter failure, we just fail. Reject host only
455 // works if you have a set number of failures.
456 if (ptr->root->server_failure_limit && ptr->server_failure_counter >= ptr->root->server_failure_limit)
457 {
458 set_last_disconnected_host(ptr);
459
460 // @todo fix this by fixing behavior to no longer make use of
461 // memcached_st
462 if (_is_auto_eject_host(ptr->root))
463 {
464 run_distribution((memcached_st *)ptr->root);
465 }
466
467 return MEMCACHED_SERVER_MARKED_DEAD;
468 }
469
470 /* We need to clean up the multi startup piece */
471 switch (ptr->type)
472 {
473 case MEMCACHED_CONNECTION_UNKNOWN:
474 WATCHPOINT_ASSERT(0);
475 rc= MEMCACHED_NOT_SUPPORTED;
476 break;
477 case MEMCACHED_CONNECTION_UDP:
478 case MEMCACHED_CONNECTION_TCP:
479 rc= network_connect(ptr);
480 break;
481 case MEMCACHED_CONNECTION_UNIX_SOCKET:
482 rc= unix_socket_connect(ptr);
483 break;
484 case MEMCACHED_CONNECTION_MAX:
485 default:
486 WATCHPOINT_ASSERT(0);
487 }
488
489 if (rc == MEMCACHED_SUCCESS)
490 {
491 ptr->server_failure_counter= 0;
492 ptr->next_retry= 0;
493 }
494 else
495 {
496 ptr->server_failure_counter++;
497
498 set_last_disconnected_host(ptr);
499 }
500
501 LIBMEMCACHED_MEMCACHED_CONNECT_END();
502
503 return rc;
504 }