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