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