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)
177 if (ptr
->server_failure_counter
>= ptr
->root
->server_failure_limit
)
179 memcached_server_remove(ptr
);
182 /* Old connection junk still is in the structure */
183 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
185 if (ptr
->sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
186 (!(ptr
->root
->flags
& MEM_USE_CACHE_LOOKUPS
)))
190 rc
= set_hostinfo(ptr
);
191 if (rc
!= MEMCACHED_SUCCESS
)
193 ptr
->sockaddr_inited
= MEMCACHED_ALLOCATED
;
196 use
= ptr
->address_info
;
197 /* Create the socket */
200 if ((ptr
->fd
= socket(use
->ai_family
,
202 use
->ai_protocol
)) < 0)
204 ptr
->cached_errno
= errno
;
205 WATCHPOINT_ERRNO(errno
);
206 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
209 (void)set_socket_options(ptr
);
211 /* connect to server */
215 use
->ai_addrlen
) < 0)
218 /* We are spinning waiting on connect */
222 struct pollfd fds
[1];
225 memset(&fds
, 0, sizeof(struct pollfd
));
227 fds
[0].events
= POLLOUT
| POLLERR
;
228 error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
234 else if (error
!= 1 || fds
[0].revents
& POLLERR
)
236 ptr
->cached_errno
= errno
;
237 WATCHPOINT_ERRNO(ptr
->cached_errno
);
238 WATCHPOINT_NUMBER(ptr
->root
->connect_timeout
);
241 if (ptr
->address_info
)
243 freeaddrinfo(ptr
->address_info
);
244 ptr
->address_info
= NULL
;
247 if (ptr
->root
->retry_timeout
)
249 struct timeval next_time
;
251 gettimeofday(&next_time
, NULL
);
252 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
254 ptr
->server_failure_counter
+= 1;
255 return MEMCACHED_ERRNO
;
260 /* We are spinning waiting on connect */
263 case EISCONN
: /* We were spinning waiting on connect */
267 ptr
->cached_errno
= errno
;
270 if (ptr
->root
->retry_timeout
)
272 struct timeval next_time
;
274 gettimeofday(&next_time
, NULL
);
275 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
281 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
282 ptr
->server_failure_counter
= 0;
283 return MEMCACHED_SUCCESS
;
290 ptr
->server_failure_counter
+= 1;
291 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
294 ptr
->server_failure_counter
= 0;
295 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
299 memcached_return
memcached_connect(memcached_server_st
*ptr
)
301 memcached_return rc
= MEMCACHED_NO_SERVERS
;
302 LIBMEMCACHED_MEMCACHED_CONNECT_START();
304 if (ptr
->root
->retry_timeout
)
306 struct timeval next_time
;
308 gettimeofday(&next_time
, NULL
);
309 if (next_time
.tv_sec
< ptr
->next_retry
)
310 return MEMCACHED_TIMEOUT
;
312 /* We need to clean up the multi startup piece */
315 case MEMCACHED_CONNECTION_UNKNOWN
:
316 WATCHPOINT_ASSERT(0);
317 rc
= MEMCACHED_NOT_SUPPORTED
;
319 case MEMCACHED_CONNECTION_UDP
:
320 case MEMCACHED_CONNECTION_TCP
:
321 rc
= network_connect(ptr
);
323 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
324 rc
= unix_socket_connect(ptr
);
327 WATCHPOINT_ASSERT(0);
330 LIBMEMCACHED_MEMCACHED_CONNECT_END();