2 * Copyright (C) 2006-2010 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
8 * Summary: Server IO, Not public!
12 #include <libmemcached/common.h>
17 static memcached_return_t
connect_poll(memcached_server_st
*ptr
)
21 fds
[0].events
= POLLOUT
;
26 while (--loop_max
) // Should only loop on cases of ERESTART or EINTR
28 error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
35 socklen_t len
= sizeof (err
);
36 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
38 // We check the value to see what happened wth the socket.
41 return MEMCACHED_SUCCESS
;
45 ptr
->cached_errno
= errno
;
47 return MEMCACHED_ERRNO
;
51 return MEMCACHED_TIMEOUT
;
52 default: // A real error occurred and we need to completely bail
53 WATCHPOINT_ERRNO(get_socket_errno());
54 switch (get_socket_errno())
56 #ifdef TARGET_OS_LINUX
62 if (fds
[0].revents
& POLLERR
)
65 socklen_t len
= sizeof (err
);
66 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
67 ptr
->cached_errno
= (err
== 0) ? get_socket_errno() : err
;
71 ptr
->cached_errno
= get_socket_errno();
74 (void)closesocket(ptr
->fd
);
75 ptr
->fd
= INVALID_SOCKET
;
77 return MEMCACHED_ERRNO
;
82 // This should only be possible from ERESTART or EINTR;
83 ptr
->cached_errno
= get_socket_errno();
85 return MEMCACHED_ERRNO
;
88 static memcached_return_t
set_hostinfo(memcached_server_st
*server
)
90 struct addrinfo hints
;
91 char str_port
[NI_MAXSERV
];
93 assert(! server
->address_info
); // We cover the case where a programming mistake has been made.
94 if (server
->address_info
)
96 freeaddrinfo(server
->address_info
);
97 server
->address_info
= NULL
;
98 server
->address_info_next
= NULL
;
101 int length
= snprintf(str_port
, NI_MAXSERV
, "%u", (uint32_t)server
->port
);
102 if (length
>= NI_MAXSERV
|| length
< 0)
103 return MEMCACHED_FAILURE
;
105 memset(&hints
, 0, sizeof(hints
));
108 hints
.ai_family
= AF_INET
;
110 if (server
->type
== MEMCACHED_CONNECTION_UDP
)
112 hints
.ai_protocol
= IPPROTO_UDP
;
113 hints
.ai_socktype
= SOCK_DGRAM
;
117 hints
.ai_socktype
= SOCK_STREAM
;
118 hints
.ai_protocol
= IPPROTO_TCP
;
124 int e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &server
->address_info
);
130 else if (e
== EAI_AGAIN
)
133 struct timespec dream
, rem
;
138 nanosleep(&dream
, &rem
);
144 WATCHPOINT_STRING(server
->hostname
);
145 WATCHPOINT_STRING(gai_strerror(e
));
146 return MEMCACHED_HOST_LOOKUP_FAILURE
;
150 server
->address_info_next
= server
->address_info
;
152 return MEMCACHED_SUCCESS
;
155 static inline memcached_return_t
set_socket_nonblocking(memcached_server_st
*ptr
)
159 if (ioctlsocket(ptr
->fd
, FIONBIO
, &arg
) == SOCKET_ERROR
)
161 ptr
->cached_errno
= get_socket_errno();
162 return MEMCACHED_CONNECTION_FAILURE
;
168 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
169 while (flags
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
171 unlikely (flags
== -1)
173 ptr
->cached_errno
= errno
;
174 return MEMCACHED_CONNECTION_FAILURE
;
176 else if ((flags
& O_NONBLOCK
) == 0)
181 rval
= fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
182 while (rval
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
184 unlikely (rval
== -1)
186 ptr
->cached_errno
= errno
;
187 return MEMCACHED_CONNECTION_FAILURE
;
191 return MEMCACHED_SUCCESS
;
194 static memcached_return_t
set_socket_options(memcached_server_st
*ptr
)
196 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
198 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
199 return MEMCACHED_SUCCESS
;
202 if (ptr
->root
->snd_timeout
)
205 struct timeval waittime
;
208 waittime
.tv_usec
= ptr
->root
->snd_timeout
;
210 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
211 &waittime
, (socklen_t
)sizeof(struct timeval
));
212 WATCHPOINT_ASSERT(error
== 0);
214 return MEMCACHED_FAILURE
;
219 if (ptr
->root
->rcv_timeout
)
222 struct timeval waittime
;
225 waittime
.tv_usec
= ptr
->root
->rcv_timeout
;
227 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
228 &waittime
, (socklen_t
)sizeof(struct timeval
));
229 WATCHPOINT_ASSERT(error
== 0);
231 return MEMCACHED_FAILURE
;
236 #if defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
239 int error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (void *)&set
, sizeof(int));
241 // This is not considered a fatal error
244 WATCHPOINT_ERRNO(get_socket_errno());
245 perror("setsockopt(SO_NOSIGPIPE)");
250 if (ptr
->root
->flags
.no_block
)
253 struct linger linger
;
256 linger
.l_linger
= 0; /* By default on close() just drop the socket */
257 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
258 &linger
, (socklen_t
)sizeof(struct linger
));
259 WATCHPOINT_ASSERT(error
== 0);
261 return MEMCACHED_FAILURE
;
264 if (ptr
->root
->flags
.tcp_nodelay
)
269 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
270 &flag
, (socklen_t
)sizeof(int));
271 WATCHPOINT_ASSERT(error
== 0);
273 return MEMCACHED_FAILURE
;
276 if (ptr
->root
->flags
.tcp_keepalive
)
281 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_KEEPALIVE
,
282 &flag
, (socklen_t
)sizeof(int));
283 WATCHPOINT_ASSERT(error
== 0);
285 return MEMCACHED_FAILURE
;
289 if (ptr
->root
->tcp_keepidle
> 0)
293 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_KEEPIDLE
,
294 &ptr
->root
->tcp_keepidle
, (socklen_t
)sizeof(int));
295 WATCHPOINT_ASSERT(error
== 0);
297 return MEMCACHED_FAILURE
;
301 if (ptr
->root
->send_size
> 0)
305 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
306 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
307 WATCHPOINT_ASSERT(error
== 0);
309 return MEMCACHED_FAILURE
;
312 if (ptr
->root
->recv_size
> 0)
316 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVBUF
,
317 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
318 WATCHPOINT_ASSERT(error
== 0);
320 return MEMCACHED_FAILURE
;
324 /* libmemcached will always use nonblocking IO to avoid write deadlocks */
325 return set_socket_nonblocking(ptr
);
328 static memcached_return_t
unix_socket_connect(memcached_server_st
*ptr
)
331 struct sockaddr_un servAddr
;
333 WATCHPOINT_ASSERT(ptr
->fd
== -1);
335 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
337 ptr
->cached_errno
= errno
;
338 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
341 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
342 servAddr
.sun_family
= AF_UNIX
;
343 strncpy(servAddr
.sun_path
, ptr
->hostname
, sizeof(servAddr
.sun_path
)); /* Copy filename */
347 (struct sockaddr
*)&servAddr
,
348 sizeof(servAddr
)) < 0)
356 case EISCONN
: /* We were spinning waiting on connect */
359 WATCHPOINT_ERRNO(errno
);
360 ptr
->cached_errno
= errno
;
361 return MEMCACHED_ERRNO
;
365 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
367 return MEMCACHED_SUCCESS
;
370 return MEMCACHED_NOT_SUPPORTED
;
374 static memcached_return_t
network_connect(memcached_server_st
*ptr
)
376 bool timeout_error_occured
= false;
378 WATCHPOINT_ASSERT(ptr
->fd
== INVALID_SOCKET
);
379 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
381 if (! ptr
->address_info
)
383 memcached_return_t rc
= set_hostinfo(ptr
);
384 if (rc
!= MEMCACHED_SUCCESS
)
388 /* Create the socket */
389 while (ptr
->address_info_next
&& ptr
->fd
== INVALID_SOCKET
)
391 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
392 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& ptr
->address_info_next
->ai_family
!= AF_INET
)
394 ptr
->address_info_next
= ptr
->address_info_next
->ai_next
;
398 if ((ptr
->fd
= socket(ptr
->address_info_next
->ai_family
,
399 ptr
->address_info_next
->ai_socktype
,
400 ptr
->address_info_next
->ai_protocol
)) < 0)
402 ptr
->cached_errno
= get_socket_errno();
403 WATCHPOINT_ERRNO(get_socket_errno());
404 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
407 (void)set_socket_options(ptr
);
409 /* connect to server */
410 if ((connect(ptr
->fd
, ptr
->address_info_next
->ai_addr
, ptr
->address_info_next
->ai_addrlen
) != SOCKET_ERROR
))
415 /* An error occurred */
416 ptr
->cached_errno
= get_socket_errno();
417 switch (ptr
->cached_errno
)
420 case EINPROGRESS
: // nonblocking mode - first return
421 case EALREADY
: // nonblocking mode - subsequent returns
423 memcached_return_t rc
;
424 rc
= connect_poll(ptr
);
426 if (rc
== MEMCACHED_TIMEOUT
)
427 timeout_error_occured
= true;
429 if (rc
== MEMCACHED_SUCCESS
)
433 case EISCONN
: // we are connected :-)
436 case EINTR
: // Special case, we retry ai_addr
437 (void)closesocket(ptr
->fd
);
438 ptr
->fd
= INVALID_SOCKET
;
442 (void)closesocket(ptr
->fd
);
443 ptr
->fd
= INVALID_SOCKET
;
444 ptr
->address_info_next
= ptr
->address_info_next
->ai_next
;
449 if (ptr
->fd
== INVALID_SOCKET
)
451 WATCHPOINT_STRING("Never got a good file descriptor");
453 /* Failed to connect. schedule next retry */
454 if (ptr
->root
->retry_timeout
)
456 struct timeval next_time
;
458 if (gettimeofday(&next_time
, NULL
) == 0)
459 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
462 if (timeout_error_occured
)
463 return MEMCACHED_TIMEOUT
;
465 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
468 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
471 void set_last_disconnected_host(memcached_server_write_instance_st ptr
)
474 memcached_st
*root
= (memcached_st
*)ptr
->root
;
477 WATCHPOINT_STRING(ptr
->hostname
);
478 WATCHPOINT_NUMBER(ptr
->port
);
479 WATCHPOINT_ERRNO(ptr
->cached_errno
);
481 if (root
->last_disconnected_server
)
482 memcached_server_free(root
->last_disconnected_server
);
483 root
->last_disconnected_server
= memcached_server_clone(NULL
, ptr
);
486 memcached_return_t
memcached_connect(memcached_server_write_instance_st ptr
)
488 memcached_return_t rc
= MEMCACHED_NO_SERVERS
;
490 if (ptr
->fd
!= INVALID_SOCKET
)
491 return MEMCACHED_SUCCESS
;
493 LIBMEMCACHED_MEMCACHED_CONNECT_START();
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
)
499 struct timeval curr_time
;
501 gettimeofday(&curr_time
, NULL
);
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
)
507 set_last_disconnected_host(ptr
);
509 return MEMCACHED_SERVER_MARKED_DEAD
;
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
)
517 set_last_disconnected_host(ptr
);
519 // @todo fix this by fixing behavior to no longer make use of
521 if (_is_auto_eject_host(ptr
->root
))
523 run_distribution((memcached_st
*)ptr
->root
);
526 return MEMCACHED_SERVER_MARKED_DEAD
;
529 /* We need to clean up the multi startup piece */
532 case MEMCACHED_CONNECTION_UNKNOWN
:
533 WATCHPOINT_ASSERT(0);
534 rc
= MEMCACHED_NOT_SUPPORTED
;
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
.callbacks
)
542 rc
= memcached_sasl_authenticate_connection(ptr
);
543 if (rc
!= MEMCACHED_SUCCESS
)
545 (void)closesocket(ptr
->fd
);
546 ptr
->fd
= INVALID_SOCKET
;
551 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
552 rc
= unix_socket_connect(ptr
);
554 case MEMCACHED_CONNECTION_MAX
:
556 WATCHPOINT_ASSERT(0);
559 if (rc
== MEMCACHED_SUCCESS
)
561 ptr
->server_failure_counter
= 0;
566 ptr
->server_failure_counter
++;
568 set_last_disconnected_host(ptr
);
571 LIBMEMCACHED_MEMCACHED_CONNECT_END();