Removed recent regression issue with close()
[awesomized/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_family= AF_INET;
14 hints.ai_socktype= SOCK_STREAM;
15 hints.ai_protocol= 0;
16
17 e= getaddrinfo(server->hostname, str_port, &hints, &ai);
18 if (e != 0)
19 {
20 WATCHPOINT_STRING(server->hostname);
21 WATCHPOINT_STRING(gai_strerror(e));
22 return MEMCACHED_HOST_LOOKUP_FAILURE;
23 }
24
25 if (server->address_info)
26 freeaddrinfo(server->address_info);
27 server->address_info= ai;
28
29 return MEMCACHED_SUCCESS;
30 }
31
32 static memcached_return unix_socket_connect(memcached_st *ptr, unsigned int server_key)
33 {
34 struct sockaddr_un servAddr;
35 socklen_t addrlen;
36
37 if (ptr->hosts[server_key].fd == -1)
38 {
39 if ((ptr->hosts[server_key].fd= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
40 {
41 ptr->cached_errno= errno;
42 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
43 }
44
45 memset(&servAddr, 0, sizeof (struct sockaddr_un));
46 servAddr.sun_family= AF_UNIX;
47 strcpy(servAddr.sun_path, ptr->hosts[server_key].hostname); /* Copy filename */
48
49 addrlen= strlen(servAddr.sun_path) + sizeof(servAddr.sun_family);
50
51 test_connect:
52 if (connect(ptr->hosts[server_key].fd,
53 (struct sockaddr *)&servAddr,
54 sizeof(servAddr)) < 0)
55 {
56 switch (errno) {
57 /* We are spinning waiting on connect */
58 case EALREADY:
59 case EINPROGRESS:
60 case EINTR:
61 goto test_connect;
62 case EISCONN: /* We were spinning waiting on connect */
63 break;
64 default:
65 WATCHPOINT_ERRNO(errno);
66 ptr->cached_errno= errno;
67 return MEMCACHED_ERRNO;
68 }
69 ptr->connected++;
70 }
71 }
72 return MEMCACHED_SUCCESS;
73 }
74
75 static memcached_return udp_connect(memcached_st *ptr, unsigned int server_key)
76 {
77 if (ptr->hosts[server_key].fd == -1)
78 {
79 /* Old connection junk still is in the structure */
80 WATCHPOINT_ASSERT(ptr->hosts[server_key].stack_responses == 0);
81
82 /*
83 If we have not allocated the hosts object.
84 Or if the cache has not been set.
85 */
86 if (ptr->hosts[server_key].sockaddr_inited == MEMCACHED_NOT_ALLOCATED ||
87 (!(ptr->flags & MEM_USE_CACHE_LOOKUPS)))
88 {
89 memcached_return rc;
90
91 rc= set_hostinfo(&ptr->hosts[server_key]);
92 if (rc != MEMCACHED_SUCCESS)
93 return rc;
94
95 ptr->hosts[server_key].sockaddr_inited= MEMCACHED_ALLOCATED;
96 }
97
98 /* Create the socket */
99 if ((ptr->hosts[server_key].fd= socket(AF_INET, SOCK_DGRAM, 0)) < 0)
100 {
101 ptr->cached_errno= errno;
102 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
103 }
104 }
105
106 return MEMCACHED_SUCCESS;
107 }
108
109 static memcached_return tcp_connect(memcached_st *ptr, unsigned int server_key)
110 {
111 if (ptr->hosts[server_key].fd == -1)
112 {
113 /* Old connection junk still is in the structure */
114 WATCHPOINT_ASSERT(ptr->hosts[server_key].stack_responses == 0);
115 struct addrinfo *use;
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 /* For the moment, not getting a nonblocking mode will not be fatal */
140 if (ptr->flags & MEM_NO_BLOCK)
141 {
142 int flags;
143
144 flags= fcntl(ptr->hosts[server_key].fd, F_GETFL, 0);
145 if (flags != -1)
146 {
147 (void)fcntl(ptr->hosts[server_key].fd, F_SETFL, flags | O_NONBLOCK);
148
149 flags= 1;
150 setsockopt(ptr->hosts[server_key].fd, IPPROTO_TCP, SO_LINGER,
151 &flags, (socklen_t)sizeof(int));
152 }
153 }
154
155 if (ptr->flags & MEM_TCP_NODELAY)
156 {
157 int flag= 1;
158
159 setsockopt(ptr->hosts[server_key].fd, IPPROTO_TCP, TCP_NODELAY,
160 &flag, (socklen_t)sizeof(int));
161 }
162
163 if (ptr->send_size)
164 {
165 setsockopt(ptr->hosts[server_key].fd, SOL_SOCKET, SO_SNDBUF,
166 &ptr->send_size, (socklen_t)sizeof(int));
167 }
168
169 if (ptr->recv_size)
170 {
171 setsockopt(ptr->hosts[server_key].fd, SOL_SOCKET, SO_SNDBUF,
172 &ptr->recv_size, (socklen_t)sizeof(int));
173 }
174
175 /* connect to server */
176 test_connect:
177 if (connect(ptr->hosts[server_key].fd,
178 use->ai_addr,
179 use->ai_addrlen) < 0)
180 {
181 switch (errno) {
182 /* We are spinning waiting on connect */
183 case EALREADY:
184 case EINPROGRESS:
185 case EINTR:
186 goto test_connect;
187 case EISCONN: /* We were spinning waiting on connect */
188 break;
189 default:
190 ptr->cached_errno= errno;
191 WATCHPOINT_ERRNO(ptr->cached_errno);
192 return MEMCACHED_ERRNO;
193 }
194 ptr->connected++;
195 }
196 WATCHPOINT_ASSERT(ptr->hosts[server_key].stack_responses == 0);
197 }
198
199 return MEMCACHED_SUCCESS;
200 }
201
202
203 memcached_return memcached_connect(memcached_st *ptr, unsigned int server_key)
204 {
205 memcached_return rc= MEMCACHED_NO_SERVERS;
206 LIBMEMCACHED_MEMCACHED_CONNECT_START();
207
208 if (ptr->connected == ptr->number_of_hosts && ptr->number_of_hosts)
209 return MEMCACHED_SUCCESS;
210
211 if (ptr->hosts == NULL || ptr->number_of_hosts == 0)
212 return MEMCACHED_NO_SERVERS;
213
214 /* We need to clean up the multi startup piece */
215 switch (ptr->hosts[server_key].type)
216 {
217 case MEMCACHED_CONNECTION_UNKNOWN:
218 WATCHPOINT_ASSERT(0);
219 rc= MEMCACHED_NOT_SUPPORTED;
220 break;
221 case MEMCACHED_CONNECTION_UDP:
222 rc= udp_connect(ptr, server_key);
223 break;
224 case MEMCACHED_CONNECTION_TCP:
225 rc= tcp_connect(ptr, server_key);
226 break;
227 case MEMCACHED_CONNECTION_UNIX_SOCKET:
228 rc= unix_socket_connect(ptr, server_key);
229 break;
230 }
231
232 if (rc != MEMCACHED_SUCCESS)
233 WATCHPOINT_ERROR(rc);
234
235 LIBMEMCACHED_MEMCACHED_CONNECT_END();
236
237 return rc;
238 }