6 static memcached_return
set_hostinfo(memcached_server_st
*server
)
11 char str_port
[NI_MAXSERV
];
13 sprintf(str_port
, "%u", server
->port
);
15 memset(&hints
, 0, sizeof(hints
));
17 // hints.ai_family= AF_INET;
18 if (server
->type
== MEMCACHED_CONNECTION_UDP
)
20 hints
.ai_protocol
= IPPROTO_UDP
;
21 hints
.ai_socktype
= SOCK_DGRAM
;
25 hints
.ai_socktype
= SOCK_STREAM
;
26 hints
.ai_protocol
= IPPROTO_TCP
;
29 e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &ai
);
32 WATCHPOINT_STRING(server
->hostname
);
33 WATCHPOINT_STRING(gai_strerror(e
));
34 return MEMCACHED_HOST_LOOKUP_FAILURE
;
37 if (server
->address_info
)
39 freeaddrinfo(server
->address_info
);
40 server
->address_info
= NULL
;
42 server
->address_info
= ai
;
44 return MEMCACHED_SUCCESS
;
47 static memcached_return
set_socket_options(memcached_server_st
*ptr
)
49 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
51 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
52 return MEMCACHED_SUCCESS
;
55 if (ptr
->root
->snd_timeout
)
58 struct timeval waittime
;
61 waittime
.tv_usec
= ptr
->root
->snd_timeout
;
63 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
64 &waittime
, (socklen_t
)sizeof(struct timeval
));
65 WATCHPOINT_ASSERT(error
== 0);
70 if (ptr
->root
->rcv_timeout
)
73 struct timeval waittime
;
76 waittime
.tv_usec
= ptr
->root
->rcv_timeout
;
78 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
79 &waittime
, (socklen_t
)sizeof(struct timeval
));
80 WATCHPOINT_ASSERT(error
== 0);
89 linger
.l_linger
= MEMCACHED_DEFAULT_TIMEOUT
;
90 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
91 &linger
, (socklen_t
)sizeof(struct linger
));
92 WATCHPOINT_ASSERT(error
== 0);
95 if (ptr
->root
->flags
& MEM_TCP_NODELAY
)
100 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
101 &flag
, (socklen_t
)sizeof(int));
102 WATCHPOINT_ASSERT(error
== 0);
105 if (ptr
->root
->send_size
)
109 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
110 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
111 WATCHPOINT_ASSERT(error
== 0);
114 if (ptr
->root
->recv_size
)
118 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVBUF
,
119 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
120 WATCHPOINT_ASSERT(error
== 0);
123 /* For the moment, not getting a nonblocking mode will not be fatal */
124 if (ptr
->root
->flags
& MEM_NO_BLOCK
)
128 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
129 unlikely (flags
!= -1)
131 (void)fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
135 return MEMCACHED_SUCCESS
;
138 static memcached_return
unix_socket_connect(memcached_server_st
*ptr
)
140 struct sockaddr_un servAddr
;
145 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
147 ptr
->cached_errno
= errno
;
148 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
151 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
152 servAddr
.sun_family
= AF_UNIX
;
153 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
155 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
159 (struct sockaddr
*)&servAddr
,
160 sizeof(servAddr
)) < 0)
167 case EISCONN
: /* We were spinning waiting on connect */
170 WATCHPOINT_ERRNO(errno
);
171 ptr
->cached_errno
= errno
;
172 return MEMCACHED_ERRNO
;
177 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
178 return MEMCACHED_SUCCESS
;
181 static memcached_return
network_connect(memcached_server_st
*ptr
)
185 struct addrinfo
*use
;
187 if (ptr
->root
->server_failure_limit
!= 0)
189 if (ptr
->server_failure_counter
>= ptr
->root
->server_failure_limit
)
191 memcached_server_remove(ptr
);
192 return MEMCACHED_FAILURE
;
196 if (ptr
->sockaddr_inited
||
197 (!(ptr
->root
->flags
& MEM_USE_CACHE_LOOKUPS
)))
201 rc
= set_hostinfo(ptr
);
202 if (rc
!= MEMCACHED_SUCCESS
)
204 ptr
->sockaddr_inited
= true;
207 use
= ptr
->address_info
;
208 /* Create the socket */
211 if ((ptr
->fd
= socket(use
->ai_family
,
213 use
->ai_protocol
)) < 0)
215 ptr
->cached_errno
= errno
;
216 WATCHPOINT_ERRNO(errno
);
217 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
220 (void)set_socket_options(ptr
);
222 /* connect to server */
226 use
->ai_addrlen
) < 0)
229 /* We are spinning waiting on connect */
233 struct pollfd fds
[1];
236 memset(&fds
, 0, sizeof(struct pollfd
));
238 fds
[0].events
= POLLOUT
| POLLERR
;
239 error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
245 else if (error
!= 1 && fds
[0].revents
& POLLERR
)
247 ptr
->cached_errno
= errno
;
248 WATCHPOINT_ERRNO(ptr
->cached_errno
);
249 WATCHPOINT_NUMBER(ptr
->root
->connect_timeout
);
250 memcached_quit_server(ptr
, 1);
252 if (ptr
->root
->retry_timeout
)
254 struct timeval next_time
;
256 gettimeofday(&next_time
, NULL
);
257 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
259 ptr
->server_failure_counter
+= 1;
261 return MEMCACHED_ERRNO
;
266 /* We are spinning waiting on connect */
269 case EISCONN
: /* We were spinning waiting on connect */
273 ptr
->cached_errno
= errno
;
276 if (ptr
->root
->retry_timeout
)
278 struct timeval next_time
;
280 gettimeofday(&next_time
, NULL
);
281 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
287 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
288 ptr
->server_failure_counter
= 0;
289 return MEMCACHED_SUCCESS
;
296 ptr
->server_failure_counter
+= 1;
297 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
300 ptr
->server_failure_counter
= 0;
301 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
305 memcached_return
memcached_connect(memcached_server_st
*ptr
)
307 memcached_return rc
= MEMCACHED_NO_SERVERS
;
308 LIBMEMCACHED_MEMCACHED_CONNECT_START();
310 if (ptr
->root
->retry_timeout
)
312 struct timeval next_time
;
314 gettimeofday(&next_time
, NULL
);
315 if (next_time
.tv_sec
< ptr
->next_retry
)
316 return MEMCACHED_TIMEOUT
;
318 /* We need to clean up the multi startup piece */
321 case MEMCACHED_CONNECTION_UNKNOWN
:
322 WATCHPOINT_ASSERT(0);
323 rc
= MEMCACHED_NOT_SUPPORTED
;
325 case MEMCACHED_CONNECTION_UDP
:
326 case MEMCACHED_CONNECTION_TCP
:
327 rc
= network_connect(ptr
);
329 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
330 rc
= unix_socket_connect(ptr
);
333 WATCHPOINT_ASSERT(0);
336 LIBMEMCACHED_MEMCACHED_CONNECT_END();