Fixed typo: SO_RCVBUF instead of SO_SNDBUF
[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 if ((ptr->fd= socket(use->ai_family,
212 use->ai_socktype,
213 use->ai_protocol)) < 0)
214 {
215 ptr->cached_errno= errno;
216 WATCHPOINT_ERRNO(errno);
217 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
218 }
219
220 (void)set_socket_options(ptr);
221
222 /* connect to server */
223 test_connect:
224 if (connect(ptr->fd,
225 use->ai_addr,
226 use->ai_addrlen) < 0)
227 {
228 switch (errno) {
229 /* We are spinning waiting on connect */
230 case EALREADY:
231 case EINPROGRESS:
232 {
233 struct pollfd fds[1];
234 int error;
235
236 memset(&fds, 0, sizeof(struct pollfd));
237 fds[0].fd= ptr->fd;
238 fds[0].events= POLLOUT | POLLERR;
239 error= poll(fds, 1, ptr->root->connect_timeout);
240
241 if (error == 0)
242 {
243 goto handle_retry;
244 }
245 else if (error != 1 && fds[0].revents & POLLERR)
246 {
247 ptr->cached_errno= errno;
248 WATCHPOINT_ERRNO(ptr->cached_errno);
249 WATCHPOINT_NUMBER(ptr->root->connect_timeout);
250 memcached_quit_server(ptr, 1);
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
261 return MEMCACHED_ERRNO;
262 }
263
264 break;
265 }
266 /* We are spinning waiting on connect */
267 case EINTR:
268 goto test_connect;
269 case EISCONN: /* We were spinning waiting on connect */
270 break;
271 default:
272 handle_retry:
273 ptr->cached_errno= errno;
274 close(ptr->fd);
275 ptr->fd= -1;
276 if (ptr->root->retry_timeout)
277 {
278 struct timeval next_time;
279
280 gettimeofday(&next_time, NULL);
281 ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
282 }
283 }
284 }
285 else
286 {
287 WATCHPOINT_ASSERT(ptr->cursor_active == 0);
288 ptr->server_failure_counter= 0;
289 return MEMCACHED_SUCCESS;
290 }
291 use = use->ai_next;
292 }
293 }
294
295 if (ptr->fd == -1) {
296 ptr->server_failure_counter+= 1;
297 return MEMCACHED_ERRNO; /* The last error should be from connect() */
298 }
299
300 ptr->server_failure_counter= 0;
301 return MEMCACHED_SUCCESS; /* The last error should be from connect() */
302 }
303
304
305 memcached_return memcached_connect(memcached_server_st *ptr)
306 {
307 memcached_return rc= MEMCACHED_NO_SERVERS;
308 LIBMEMCACHED_MEMCACHED_CONNECT_START();
309
310 if (ptr->root->retry_timeout)
311 {
312 struct timeval next_time;
313
314 gettimeofday(&next_time, NULL);
315 if (next_time.tv_sec < ptr->next_retry)
316 return MEMCACHED_TIMEOUT;
317 }
318 /* We need to clean up the multi startup piece */
319 switch (ptr->type)
320 {
321 case MEMCACHED_CONNECTION_UNKNOWN:
322 WATCHPOINT_ASSERT(0);
323 rc= MEMCACHED_NOT_SUPPORTED;
324 break;
325 case MEMCACHED_CONNECTION_UDP:
326 case MEMCACHED_CONNECTION_TCP:
327 rc= network_connect(ptr);
328 break;
329 case MEMCACHED_CONNECTION_UNIX_SOCKET:
330 rc= unix_socket_connect(ptr);
331 break;
332 default:
333 WATCHPOINT_ASSERT(0);
334 }
335
336 LIBMEMCACHED_MEMCACHED_CONNECT_END();
337
338 return rc;
339 }