5 static memcached_return
set_hostinfo(memcached_server_st
*server
)
10 char str_port
[NI_MAXSERV
];
12 sprintf(str_port
, "%u", server
->port
);
14 memset(&hints
, 0, sizeof(hints
));
16 hints
.ai_family
= AF_INET
;
17 if (server
->type
== MEMCACHED_CONNECTION_UDP
)
19 hints
.ai_protocol
= IPPROTO_UDP
;
20 hints
.ai_socktype
= SOCK_DGRAM
;
24 hints
.ai_socktype
= SOCK_STREAM
;
25 hints
.ai_protocol
= IPPROTO_TCP
;
28 e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &ai
);
31 WATCHPOINT_STRING(server
->hostname
);
32 WATCHPOINT_STRING(gai_strerror(e
));
33 return MEMCACHED_HOST_LOOKUP_FAILURE
;
36 if (server
->address_info
)
37 freeaddrinfo(server
->address_info
);
38 server
->address_info
= ai
;
40 return MEMCACHED_SUCCESS
;
43 static memcached_return
set_socket_options(memcached_server_st
*ptr
)
45 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
46 return MEMCACHED_SUCCESS
;
48 if (ptr
->root
->snd_timeout
)
51 struct timeval waittime
;
54 waittime
.tv_usec
= ptr
->root
->snd_timeout
;
56 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
57 &waittime
, (socklen_t
)sizeof(struct timeval
));
58 WATCHPOINT_ASSERT(error
== 0);
61 if (ptr
->root
->rcv_timeout
)
64 struct timeval waittime
;
67 waittime
.tv_usec
= ptr
->root
->rcv_timeout
;
69 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
70 &waittime
, (socklen_t
)sizeof(struct timeval
));
71 WATCHPOINT_ASSERT(error
== 0);
79 linger
.l_linger
= MEMCACHED_DEFAULT_TIMEOUT
;
80 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
81 &linger
, (socklen_t
)sizeof(struct linger
));
82 WATCHPOINT_ASSERT(error
== 0);
85 if (ptr
->root
->flags
& MEM_TCP_NODELAY
)
90 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
91 &flag
, (socklen_t
)sizeof(int));
92 WATCHPOINT_ASSERT(error
== 0);
95 if (ptr
->root
->send_size
)
99 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
100 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
101 WATCHPOINT_ASSERT(error
== 0);
104 if (ptr
->root
->recv_size
)
108 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
109 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
110 WATCHPOINT_ASSERT(error
== 0);
113 /* For the moment, not getting a nonblocking mode will not be fatal */
114 if (ptr
->root
->flags
& MEM_NO_BLOCK
)
118 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
119 unlikely (flags
!= -1)
121 (void)fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
125 return MEMCACHED_SUCCESS
;
128 static memcached_return
unix_socket_connect(memcached_server_st
*ptr
)
130 struct sockaddr_un servAddr
;
135 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
137 ptr
->cached_errno
= errno
;
138 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
141 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
142 servAddr
.sun_family
= AF_UNIX
;
143 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
145 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
149 (struct sockaddr
*)&servAddr
,
150 sizeof(servAddr
)) < 0)
157 case EISCONN
: /* We were spinning waiting on connect */
160 WATCHPOINT_ERRNO(errno
);
161 ptr
->cached_errno
= errno
;
162 return MEMCACHED_ERRNO
;
166 return MEMCACHED_SUCCESS
;
169 static memcached_return
network_connect(memcached_server_st
*ptr
)
173 struct addrinfo
*use
;
175 if(ptr
->root
->server_failure_limit
!= 0) {
176 if(ptr
->server_failure_counter
>= ptr
->root
->server_failure_limit
) {
177 memcached_server_remove(ptr
);
180 /* Old connection junk still is in the structure */
181 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
183 if (ptr
->sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
184 (!(ptr
->root
->flags
& MEM_USE_CACHE_LOOKUPS
)))
188 rc
= set_hostinfo(ptr
);
189 if (rc
!= MEMCACHED_SUCCESS
)
191 ptr
->sockaddr_inited
= MEMCACHED_ALLOCATED
;
194 use
= ptr
->address_info
;
195 /* Create the socket */
198 if ((ptr
->fd
= socket(use
->ai_family
,
200 use
->ai_protocol
)) < 0)
202 ptr
->cached_errno
= errno
;
203 WATCHPOINT_ERRNO(errno
);
204 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
207 (void)set_socket_options(ptr
);
209 /* connect to server */
213 use
->ai_addrlen
) < 0)
216 /* We are spinning waiting on connect */
220 struct pollfd fds
[1];
223 memset(&fds
, 0, sizeof(struct pollfd
));
225 fds
[0].events
= POLLOUT
| POLLERR
;
226 error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
232 else if (error
!= 1 || fds
[0].revents
& POLLERR
)
234 ptr
->cached_errno
= errno
;
235 WATCHPOINT_ERRNO(ptr
->cached_errno
);
236 WATCHPOINT_NUMBER(ptr
->root
->connect_timeout
);
239 if (ptr
->address_info
)
241 freeaddrinfo(ptr
->address_info
);
242 ptr
->address_info
= NULL
;
245 if (ptr
->root
->retry_timeout
)
247 struct timeval next_time
;
249 gettimeofday(&next_time
, NULL
);
250 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
252 ptr
->server_failure_counter
+= 1;
253 return MEMCACHED_ERRNO
;
258 /* We are spinning waiting on connect */
261 case EISCONN
: /* We were spinning waiting on connect */
265 ptr
->cached_errno
= errno
;
268 if (ptr
->root
->retry_timeout
)
270 struct timeval next_time
;
272 gettimeofday(&next_time
, NULL
);
273 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
279 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
280 ptr
->server_failure_counter
= 0;
281 return MEMCACHED_SUCCESS
;
288 ptr
->server_failure_counter
+= 1;
289 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
292 ptr
->server_failure_counter
= 0;
293 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
297 memcached_return
memcached_connect(memcached_server_st
*ptr
)
299 memcached_return rc
= MEMCACHED_NO_SERVERS
;
300 LIBMEMCACHED_MEMCACHED_CONNECT_START();
302 if (ptr
->root
->retry_timeout
)
304 struct timeval next_time
;
306 gettimeofday(&next_time
, NULL
);
307 if (next_time
.tv_sec
< ptr
->next_retry
)
308 return MEMCACHED_TIMEOUT
;
310 /* We need to clean up the multi startup piece */
313 case MEMCACHED_CONNECTION_UNKNOWN
:
314 WATCHPOINT_ASSERT(0);
315 rc
= MEMCACHED_NOT_SUPPORTED
;
317 case MEMCACHED_CONNECTION_UDP
:
318 case MEMCACHED_CONNECTION_TCP
:
319 rc
= network_connect(ptr
);
321 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
322 rc
= unix_socket_connect(ptr
);
325 WATCHPOINT_ASSERT(0);
328 LIBMEMCACHED_MEMCACHED_CONNECT_END();