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