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