Wrapping up fixed for .24 release
[awesomized/libmemcached] / libmemcached / memcached_connect.c
1 #include "common.h"
2 #include <netdb.h>
3 #include <poll.h>
4 #include <sys/time.h>
5
6 static memcached_return set_hostinfo(memcached_server_st *server)
7 {
8 struct addrinfo *ai;
9 struct addrinfo hints;
10 int e;
11 char str_port[NI_MAXSERV];
12
13 sprintf(str_port, "%u", server->port);
14
15 memset(&hints, 0, sizeof(hints));
16
17 // hints.ai_family= AF_INET;
18 if (server->type == MEMCACHED_CONNECTION_UDP)
19 {
20 hints.ai_protocol= IPPROTO_UDP;
21 hints.ai_socktype= SOCK_DGRAM;
22 }
23 else
24 {
25 hints.ai_socktype= SOCK_STREAM;
26 hints.ai_protocol= IPPROTO_TCP;
27 }
28
29 e= getaddrinfo(server->hostname, str_port, &hints, &ai);
30 if (e != 0)
31 {
32 WATCHPOINT_STRING(server->hostname);
33 WATCHPOINT_STRING(gai_strerror(e));
34 return MEMCACHED_HOST_LOOKUP_FAILURE;
35 }
36
37 if (server->address_info)
38 {
39 freeaddrinfo(server->address_info);
40 server->address_info= NULL;
41 }
42 server->address_info= ai;
43
44 return MEMCACHED_SUCCESS;
45 }
46
47 static memcached_return set_socket_options(memcached_server_st *ptr)
48 {
49 if (ptr->type == MEMCACHED_CONNECTION_UDP)
50 return MEMCACHED_SUCCESS;
51
52 if (ptr->root->snd_timeout)
53 {
54 int error;
55 struct timeval waittime;
56
57 waittime.tv_sec= 0;
58 waittime.tv_usec= ptr->root->snd_timeout;
59
60 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDTIMEO,
61 &waittime, (socklen_t)sizeof(struct timeval));
62 WATCHPOINT_ASSERT(error == 0);
63 }
64
65 if (ptr->root->rcv_timeout)
66 {
67 int error;
68 struct timeval waittime;
69
70 waittime.tv_sec= 0;
71 waittime.tv_usec= ptr->root->rcv_timeout;
72
73 error= setsockopt(ptr->fd, SOL_SOCKET, SO_RCVTIMEO,
74 &waittime, (socklen_t)sizeof(struct timeval));
75 WATCHPOINT_ASSERT(error == 0);
76 }
77
78 {
79 int error;
80 struct linger linger;
81
82 linger.l_onoff= 1;
83 linger.l_linger= MEMCACHED_DEFAULT_TIMEOUT;
84 error= setsockopt(ptr->fd, SOL_SOCKET, SO_LINGER,
85 &linger, (socklen_t)sizeof(struct linger));
86 WATCHPOINT_ASSERT(error == 0);
87 }
88
89 if (ptr->root->flags & MEM_TCP_NODELAY)
90 {
91 int flag= 1;
92 int error;
93
94 error= setsockopt(ptr->fd, IPPROTO_TCP, TCP_NODELAY,
95 &flag, (socklen_t)sizeof(int));
96 WATCHPOINT_ASSERT(error == 0);
97 }
98
99 if (ptr->root->send_size)
100 {
101 int error;
102
103 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDBUF,
104 &ptr->root->send_size, (socklen_t)sizeof(int));
105 WATCHPOINT_ASSERT(error == 0);
106 }
107
108 if (ptr->root->recv_size)
109 {
110 int error;
111
112 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDBUF,
113 &ptr->root->recv_size, (socklen_t)sizeof(int));
114 WATCHPOINT_ASSERT(error == 0);
115 }
116
117 /* For the moment, not getting a nonblocking mode will not be fatal */
118 if (ptr->root->flags & MEM_NO_BLOCK)
119 {
120 int flags;
121
122 flags= fcntl(ptr->fd, F_GETFL, 0);
123 unlikely (flags != -1)
124 {
125 (void)fcntl(ptr->fd, F_SETFL, flags | O_NONBLOCK);
126 }
127 }
128
129 return MEMCACHED_SUCCESS;
130 }
131
132 static memcached_return unix_socket_connect(memcached_server_st *ptr)
133 {
134 struct sockaddr_un servAddr;
135 socklen_t addrlen;
136
137 if (ptr->fd == -1)
138 {
139 if ((ptr->fd= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
140 {
141 ptr->cached_errno= errno;
142 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
143 }
144
145 memset(&servAddr, 0, sizeof (struct sockaddr_un));
146 servAddr.sun_family= AF_UNIX;
147 strcpy(servAddr.sun_path, ptr->hostname); /* Copy filename */
148
149 addrlen= strlen(servAddr.sun_path) + sizeof(servAddr.sun_family);
150
151 test_connect:
152 if (connect(ptr->fd,
153 (struct sockaddr *)&servAddr,
154 sizeof(servAddr)) < 0)
155 {
156 switch (errno) {
157 case EINPROGRESS:
158 case EALREADY:
159 case EINTR:
160 goto test_connect;
161 case EISCONN: /* We were spinning waiting on connect */
162 break;
163 default:
164 WATCHPOINT_ERRNO(errno);
165 ptr->cached_errno= errno;
166 return MEMCACHED_ERRNO;
167 }
168 }
169 }
170 return MEMCACHED_SUCCESS;
171 }
172
173 static memcached_return network_connect(memcached_server_st *ptr)
174 {
175 if (ptr->fd == -1)
176 {
177 struct addrinfo *use;
178
179 if (ptr->root->server_failure_limit != 0)
180 {
181 if (ptr->server_failure_counter >= ptr->root->server_failure_limit)
182 {
183 memcached_server_remove(ptr);
184 return MEMCACHED_FAILURE;
185 }
186 }
187 /* Old connection junk still is in the structure */
188 WATCHPOINT_ASSERT(ptr->cursor_active == 0);
189
190 if (ptr->sockaddr_inited == MEMCACHED_NOT_ALLOCATED ||
191 (!(ptr->root->flags & MEM_USE_CACHE_LOOKUPS)))
192 {
193 memcached_return rc;
194
195 rc= set_hostinfo(ptr);
196 if (rc != MEMCACHED_SUCCESS)
197 return rc;
198 ptr->sockaddr_inited= MEMCACHED_ALLOCATED;
199 }
200
201 use= ptr->address_info;
202 /* Create the socket */
203 while (use != NULL)
204 {
205 if ((ptr->fd= socket(use->ai_family,
206 use->ai_socktype,
207 use->ai_protocol)) < 0)
208 {
209 ptr->cached_errno= errno;
210 WATCHPOINT_ERRNO(errno);
211 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
212 }
213
214 (void)set_socket_options(ptr);
215
216 /* connect to server */
217 test_connect:
218 if (connect(ptr->fd,
219 use->ai_addr,
220 use->ai_addrlen) < 0)
221 {
222 switch (errno) {
223 /* We are spinning waiting on connect */
224 case EALREADY:
225 case EINPROGRESS:
226 {
227 struct pollfd fds[1];
228 int error;
229
230 memset(&fds, 0, sizeof(struct pollfd));
231 fds[0].fd= ptr->fd;
232 fds[0].events= POLLOUT | POLLERR;
233 error= poll(fds, 1, ptr->root->connect_timeout);
234
235 if (error == 0)
236 {
237 goto handle_retry;
238 }
239 else if (error != 1 || fds[0].revents & POLLERR)
240 {
241 ptr->cached_errno= errno;
242 WATCHPOINT_ERRNO(ptr->cached_errno);
243 WATCHPOINT_NUMBER(ptr->root->connect_timeout);
244 close(ptr->fd);
245 ptr->fd= -1;
246 if (ptr->address_info)
247 {
248 freeaddrinfo(ptr->address_info);
249 ptr->address_info= NULL;
250 }
251
252 if (ptr->root->retry_timeout)
253 {
254 struct timeval next_time;
255
256 gettimeofday(&next_time, NULL);
257 ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
258 }
259 ptr->server_failure_counter+= 1;
260 return MEMCACHED_ERRNO;
261 }
262
263 break;
264 }
265 /* We are spinning waiting on connect */
266 case EINTR:
267 goto test_connect;
268 case EISCONN: /* We were spinning waiting on connect */
269 break;
270 default:
271 handle_retry:
272 ptr->cached_errno= errno;
273 close(ptr->fd);
274 ptr->fd= -1;
275 if (ptr->root->retry_timeout)
276 {
277 struct timeval next_time;
278
279 gettimeofday(&next_time, NULL);
280 ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
281 }
282 }
283 }
284 else
285 {
286 WATCHPOINT_ASSERT(ptr->cursor_active == 0);
287 ptr->server_failure_counter= 0;
288 return MEMCACHED_SUCCESS;
289 }
290 use = use->ai_next;
291 }
292 }
293
294 if (ptr->fd == -1) {
295 ptr->server_failure_counter+= 1;
296 return MEMCACHED_ERRNO; /* The last error should be from connect() */
297 }
298
299 ptr->server_failure_counter= 0;
300 return MEMCACHED_SUCCESS; /* The last error should be from connect() */
301 }
302
303
304 memcached_return memcached_connect(memcached_server_st *ptr)
305 {
306 memcached_return rc= MEMCACHED_NO_SERVERS;
307 LIBMEMCACHED_MEMCACHED_CONNECT_START();
308
309 if (ptr->root->retry_timeout)
310 {
311 struct timeval next_time;
312
313 gettimeofday(&next_time, NULL);
314 if (next_time.tv_sec < ptr->next_retry)
315 return MEMCACHED_TIMEOUT;
316 }
317 /* We need to clean up the multi startup piece */
318 switch (ptr->type)
319 {
320 case MEMCACHED_CONNECTION_UNKNOWN:
321 WATCHPOINT_ASSERT(0);
322 rc= MEMCACHED_NOT_SUPPORTED;
323 break;
324 case MEMCACHED_CONNECTION_UDP:
325 case MEMCACHED_CONNECTION_TCP:
326 rc= network_connect(ptr);
327 break;
328 case MEMCACHED_CONNECTION_UNIX_SOCKET:
329 rc= unix_socket_connect(ptr);
330 break;
331 default:
332 WATCHPOINT_ASSERT(0);
333 }
334
335 LIBMEMCACHED_MEMCACHED_CONNECT_END();
336
337 return rc;
338 }