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