f066c52661d25a585585d60fa5496aad08cc2cf9
[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 WATCHPOINT_ASSERT(ptr->fd != -1);
50
51 if (ptr->type == MEMCACHED_CONNECTION_UDP)
52 return MEMCACHED_SUCCESS;
53
54 #ifdef HAVE_SNDTIMEO
55 if (ptr->root->snd_timeout)
56 {
57 int error;
58 struct timeval waittime;
59
60 waittime.tv_sec= 0;
61 waittime.tv_usec= ptr->root->snd_timeout;
62
63 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDTIMEO,
64 &waittime, (socklen_t)sizeof(struct timeval));
65 WATCHPOINT_ASSERT(error == 0);
66 }
67 #endif
68
69 #ifdef HAVE_RCVTIMEO
70 if (ptr->root->rcv_timeout)
71 {
72 int error;
73 struct timeval waittime;
74
75 waittime.tv_sec= 0;
76 waittime.tv_usec= ptr->root->rcv_timeout;
77
78 error= setsockopt(ptr->fd, SOL_SOCKET, SO_RCVTIMEO,
79 &waittime, (socklen_t)sizeof(struct timeval));
80 WATCHPOINT_ASSERT(error == 0);
81 }
82 #endif
83
84 {
85 int error;
86 struct linger linger;
87
88 linger.l_onoff= 1;
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);
93 }
94
95 if (ptr->root->flags & MEM_TCP_NODELAY)
96 {
97 int flag= 1;
98 int error;
99
100 error= setsockopt(ptr->fd, IPPROTO_TCP, TCP_NODELAY,
101 &flag, (socklen_t)sizeof(int));
102 WATCHPOINT_ASSERT(error == 0);
103 }
104
105 if (ptr->root->send_size)
106 {
107 int error;
108
109 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDBUF,
110 &ptr->root->send_size, (socklen_t)sizeof(int));
111 WATCHPOINT_ASSERT(error == 0);
112 }
113
114 if (ptr->root->recv_size)
115 {
116 int error;
117
118 error= setsockopt(ptr->fd, SOL_SOCKET, SO_RCVBUF,
119 &ptr->root->recv_size, (socklen_t)sizeof(int));
120 WATCHPOINT_ASSERT(error == 0);
121 }
122
123 /* For the moment, not getting a nonblocking mode will not be fatal */
124 if (ptr->root->flags & MEM_NO_BLOCK)
125 {
126 int flags;
127
128 flags= fcntl(ptr->fd, F_GETFL, 0);
129 unlikely (flags != -1)
130 {
131 (void)fcntl(ptr->fd, F_SETFL, flags | O_NONBLOCK);
132 }
133 }
134
135 return MEMCACHED_SUCCESS;
136 }
137
138 static memcached_return unix_socket_connect(memcached_server_st *ptr)
139 {
140 struct sockaddr_un servAddr;
141 socklen_t addrlen;
142
143 if (ptr->fd == -1)
144 {
145 if ((ptr->fd= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
146 {
147 ptr->cached_errno= errno;
148 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
149 }
150
151 memset(&servAddr, 0, sizeof (struct sockaddr_un));
152 servAddr.sun_family= AF_UNIX;
153 strcpy(servAddr.sun_path, ptr->hostname); /* Copy filename */
154
155 addrlen= strlen(servAddr.sun_path) + sizeof(servAddr.sun_family);
156
157 test_connect:
158 if (connect(ptr->fd,
159 (struct sockaddr *)&servAddr,
160 sizeof(servAddr)) < 0)
161 {
162 switch (errno) {
163 case EINPROGRESS:
164 case EALREADY:
165 case EINTR:
166 goto test_connect;
167 case EISCONN: /* We were spinning waiting on connect */
168 break;
169 default:
170 WATCHPOINT_ERRNO(errno);
171 ptr->cached_errno= errno;
172 return MEMCACHED_ERRNO;
173 }
174 }
175 }
176
177 WATCHPOINT_ASSERT(ptr->fd != -1);
178 return MEMCACHED_SUCCESS;
179 }
180
181 static memcached_return network_connect(memcached_server_st *ptr)
182 {
183 if (ptr->fd == -1)
184 {
185 struct addrinfo *use;
186
187 if (ptr->root->server_failure_limit != 0)
188 {
189 if (ptr->server_failure_counter >= ptr->root->server_failure_limit)
190 {
191 memcached_server_remove(ptr);
192 return MEMCACHED_FAILURE;
193 }
194 }
195
196 if (ptr->sockaddr_inited ||
197 (!(ptr->root->flags & MEM_USE_CACHE_LOOKUPS)))
198 {
199 memcached_return rc;
200
201 rc= set_hostinfo(ptr);
202 if (rc != MEMCACHED_SUCCESS)
203 return rc;
204 ptr->sockaddr_inited= true;
205 }
206
207 use= ptr->address_info;
208 /* Create the socket */
209 while (use != NULL)
210 {
211 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
212 if (ptr->type == MEMCACHED_CONNECTION_UDP && use->ai_family != AF_INET) {
213 use= use->ai_next;
214 continue;
215 }
216
217 if ((ptr->fd= socket(use->ai_family,
218 use->ai_socktype,
219 use->ai_protocol)) < 0)
220 {
221 ptr->cached_errno= errno;
222 WATCHPOINT_ERRNO(errno);
223 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
224 }
225
226 (void)set_socket_options(ptr);
227
228 int flags;
229 if (ptr->root->connect_timeout)
230 {
231 flags= fcntl(ptr->fd, F_GETFL, 0);
232 if (flags != -1 && !(flags & O_NONBLOCK))
233 (void)fcntl(ptr->fd, F_SETFL, flags | O_NONBLOCK);
234 }
235
236 /* connect to server */
237 while (ptr->fd != -1 &&
238 connect(ptr->fd, use->ai_addr, use->ai_addrlen) < 0)
239 {
240 ptr->cached_errno= errno;
241 if (errno == EINPROGRESS || /* nonblocking mode - first return, */
242 errno == EALREADY) /* nonblocking mode - subsequent returns */
243 {
244 struct pollfd fds[1] = { [0].fd = ptr->fd, [0].events = POLLOUT };
245 int error= poll(fds, 1, ptr->root->connect_timeout);
246
247 if (error != 1 || fds[0].revents & POLLERR)
248 {
249 if (fds[0].revents & POLLERR)
250 {
251 int err;
252 int len = sizeof (err);
253 (void)getsockopt(ptr->fd, SOL_SOCKET, SO_ERROR, &err, &len);
254 ptr->cached_errno= errno;
255 }
256
257 (void)close(ptr->fd);
258 ptr->fd= -1;
259 }
260 }
261 else if (errno == EISCONN) /* we are connected :-) */
262 {
263 break;
264 }
265 else if (errno != EINTR)
266 {
267 (void)close(ptr->fd);
268 ptr->fd= -1;
269 break;
270 }
271 }
272
273 if (ptr->fd != -1)
274 {
275 /* restore flags */
276 if (ptr->root->connect_timeout && (flags & O_NONBLOCK) == 0)
277 (void)fcntl(ptr->fd, F_SETFL, flags & ~O_NONBLOCK);
278
279 WATCHPOINT_ASSERT(ptr->cursor_active == 0);
280 ptr->server_failure_counter= 0;
281 return MEMCACHED_SUCCESS;
282 }
283 use = use->ai_next;
284 }
285 }
286
287 if (ptr->fd == -1)
288 {
289 /* Failed to connect. schedule next retry */
290 if (ptr->root->retry_timeout)
291 {
292 struct timeval next_time;
293
294 if (gettimeofday(&next_time, NULL) == 0)
295 ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
296 }
297 ptr->server_failure_counter+= 1;
298 return MEMCACHED_ERRNO; /* The last error should be from connect() */
299 }
300
301 ptr->server_failure_counter= 0;
302 return MEMCACHED_SUCCESS; /* The last error should be from connect() */
303 }
304
305
306 memcached_return memcached_connect(memcached_server_st *ptr)
307 {
308 memcached_return rc= MEMCACHED_NO_SERVERS;
309 LIBMEMCACHED_MEMCACHED_CONNECT_START();
310
311 if (ptr->root->retry_timeout)
312 {
313 struct timeval next_time;
314
315 gettimeofday(&next_time, NULL);
316 if (next_time.tv_sec < ptr->next_retry)
317 return MEMCACHED_TIMEOUT;
318 }
319 /* We need to clean up the multi startup piece */
320 switch (ptr->type)
321 {
322 case MEMCACHED_CONNECTION_UNKNOWN:
323 WATCHPOINT_ASSERT(0);
324 rc= MEMCACHED_NOT_SUPPORTED;
325 break;
326 case MEMCACHED_CONNECTION_UDP:
327 case MEMCACHED_CONNECTION_TCP:
328 rc= network_connect(ptr);
329 break;
330 case MEMCACHED_CONNECTION_UNIX_SOCKET:
331 rc= unix_socket_connect(ptr);
332 break;
333 default:
334 WATCHPOINT_ASSERT(0);
335 }
336
337 LIBMEMCACHED_MEMCACHED_CONNECT_END();
338
339 return rc;
340 }