Bugfix for memcached_connect() so that it will not always start up servers.
[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].sockaddr_inited == MEMCACHED_NOT_ALLOCATED ||
127 (!(ptr->flags & MEM_USE_CACHE_LOOKUPS)))
128 {
129 memcached_return rc;
130
131 rc= set_hostinfo(&ptr->hosts[server_key]);
132 if (rc != MEMCACHED_SUCCESS)
133 return rc;
134 ptr->hosts[server_key].sockaddr_inited= MEMCACHED_ALLOCATED;
135 }
136
137 /* Create the socket */
138 if ((ptr->hosts[server_key].fd= socket(AF_INET, SOCK_STREAM, 0)) < 0)
139 {
140 ptr->cached_errno= errno;
141 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
142 }
143
144 /* For the moment, not getting a nonblocking mode will note be fatal */
145 if (ptr->flags & MEM_NO_BLOCK)
146 {
147 int flags;
148
149 flags= fcntl(ptr->hosts[server_key].fd, F_GETFL, 0);
150 if (flags != -1)
151 (void)fcntl(ptr->hosts[server_key].fd, F_SETFL, flags | O_NONBLOCK);
152 }
153
154 if (ptr->flags & MEM_TCP_NODELAY)
155 {
156 int flag= 1;
157
158 setsockopt(ptr->hosts[server_key].fd, IPPROTO_TCP, TCP_NODELAY,
159 &flag, (socklen_t)sizeof(int));
160 }
161
162 if (ptr->send_size)
163 {
164 setsockopt(ptr->hosts[server_key].fd, SOL_SOCKET, SO_SNDBUF,
165 &ptr->send_size, (socklen_t)sizeof(int));
166 }
167
168 if (ptr->recv_size)
169 {
170 setsockopt(ptr->hosts[server_key].fd, SOL_SOCKET, SO_SNDBUF,
171 &ptr->recv_size, (socklen_t)sizeof(int));
172 }
173
174 /* connect to server */
175 test_connect:
176 if (connect(ptr->hosts[server_key].fd,
177 (struct sockaddr *)&ptr->hosts[server_key].servAddr,
178 sizeof(struct sockaddr)) < 0)
179 {
180 switch (errno) {
181 /* We are spinning waiting on connect */
182 case EALREADY:
183 case EINPROGRESS:
184 case EINTR:
185 goto test_connect;
186 case EISCONN: /* We were spinning waiting on connect */
187 break;
188 default:
189 ptr->cached_errno= errno;
190 return MEMCACHED_ERRNO;
191 }
192 ptr->connected++;
193 }
194 WATCHPOINT_ASSERT(ptr->hosts[server_key].stack_responses == 0);
195 }
196
197 return MEMCACHED_SUCCESS;
198 }
199
200
201 memcached_return memcached_connect(memcached_st *ptr, unsigned int server_key)
202 {
203 memcached_return rc= MEMCACHED_NO_SERVERS;
204 LIBMEMCACHED_MEMCACHED_CONNECT_START();
205
206 if (ptr->connected == ptr->number_of_hosts && ptr->number_of_hosts)
207 return MEMCACHED_SUCCESS;
208
209 if (ptr->hosts == NULL || ptr->number_of_hosts == 0)
210 return MEMCACHED_NO_SERVERS;
211
212 /* We need to clean up the multi startup piece */
213 switch (ptr->hosts[server_key].type)
214 {
215 case MEMCACHED_CONNECTION_UNKNOWN:
216 WATCHPOINT_ASSERT(0);
217 rc= MEMCACHED_NOT_SUPPORTED;
218 break;
219 case MEMCACHED_CONNECTION_UDP:
220 rc= udp_connect(ptr, server_key);
221 break;
222 case MEMCACHED_CONNECTION_TCP:
223 rc= tcp_connect(ptr, server_key);
224 break;
225 case MEMCACHED_CONNECTION_UNIX_SOCKET:
226 rc= unix_socket_connect(ptr, server_key);
227 break;
228 }
229
230 if (rc != MEMCACHED_SUCCESS)
231 WATCHPOINT_ERROR(rc);
232
233 LIBMEMCACHED_MEMCACHED_CONNECT_END();
234
235 return rc;
236 }