New version update
[m6w6/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 != 1)
211 {
212 ptr->cached_errno= errno;
213 WATCHPOINT_ERRNO(ptr->cached_errno);
214 WATCHPOINT_NUMBER(ptr->root->connect_timeout);
215 close(ptr->fd);
216 ptr->fd= -1;
217 return MEMCACHED_ERRNO;
218 }
219
220 break;
221 }
222 /* We are spinning waiting on connect */
223 case EINTR:
224 goto test_connect;
225 case EISCONN: /* We were spinning waiting on connect */
226 break;
227 default:
228 ptr->cached_errno= errno;
229 WATCHPOINT_ERRNO(ptr->cached_errno);
230 close(ptr->fd);
231 ptr->fd= -1;
232 if (ptr->root->retry_timeout)
233 {
234 struct timeval next_time;
235
236 gettimeofday(&next_time, NULL);
237 ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
238 }
239 }
240 }
241 else
242 {
243 WATCHPOINT_ASSERT(ptr->cursor_active == 0);
244 return MEMCACHED_SUCCESS;
245 }
246 use = use->ai_next;
247 }
248 }
249
250 if (ptr->fd == -1)
251 return MEMCACHED_ERRNO; /* The last error should be from connect() */
252
253 return MEMCACHED_SUCCESS; /* The last error should be from connect() */
254 }
255
256
257 memcached_return memcached_connect(memcached_server_st *ptr)
258 {
259 memcached_return rc= MEMCACHED_NO_SERVERS;
260 LIBMEMCACHED_MEMCACHED_CONNECT_START();
261
262 if (ptr->root->retry_timeout)
263 {
264 struct timeval next_time;
265
266 gettimeofday(&next_time, NULL);
267 if (next_time.tv_sec < ptr->next_retry)
268 return MEMCACHED_TIMEOUT;
269 }
270 /* We need to clean up the multi startup piece */
271 switch (ptr->type)
272 {
273 case MEMCACHED_CONNECTION_UNKNOWN:
274 WATCHPOINT_ASSERT(0);
275 rc= MEMCACHED_NOT_SUPPORTED;
276 break;
277 case MEMCACHED_CONNECTION_UDP:
278 case MEMCACHED_CONNECTION_TCP:
279 rc= network_connect(ptr);
280 break;
281 case MEMCACHED_CONNECTION_UNIX_SOCKET:
282 rc= unix_socket_connect(ptr);
283 break;
284 default:
285 WATCHPOINT_ASSERT(0);
286 }
287
288 if (rc != MEMCACHED_SUCCESS)
289 WATCHPOINT_ERROR(rc);
290
291 LIBMEMCACHED_MEMCACHED_CONNECT_END();
292
293 return rc;
294 }