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