6 static memcached_return
set_hostinfo(memcached_server_st
*server
)
11 char str_port
[NI_MAXSERV
];
13 sprintf(str_port
, "%u", server
->port
);
15 memset(&hints
, 0, sizeof(hints
));
17 // hints.ai_family= AF_INET;
18 if (server
->type
== MEMCACHED_CONNECTION_UDP
)
20 hints
.ai_protocol
= IPPROTO_UDP
;
21 hints
.ai_socktype
= SOCK_DGRAM
;
25 hints
.ai_socktype
= SOCK_STREAM
;
26 hints
.ai_protocol
= IPPROTO_TCP
;
29 e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &ai
);
32 WATCHPOINT_STRING(server
->hostname
);
33 WATCHPOINT_STRING(gai_strerror(e
));
34 return MEMCACHED_HOST_LOOKUP_FAILURE
;
37 if (server
->address_info
)
39 freeaddrinfo(server
->address_info
);
40 server
->address_info
= NULL
;
42 server
->address_info
= ai
;
44 return MEMCACHED_SUCCESS
;
47 static memcached_return
set_socket_options(memcached_server_st
*ptr
)
49 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
51 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
52 return MEMCACHED_SUCCESS
;
54 if (ptr
->root
->snd_timeout
)
57 struct timeval waittime
;
60 waittime
.tv_usec
= ptr
->root
->snd_timeout
;
62 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
63 &waittime
, (socklen_t
)sizeof(struct timeval
));
64 WATCHPOINT_ASSERT(error
== 0);
67 if (ptr
->root
->rcv_timeout
)
70 struct timeval waittime
;
73 waittime
.tv_usec
= ptr
->root
->rcv_timeout
;
75 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
76 &waittime
, (socklen_t
)sizeof(struct timeval
));
77 WATCHPOINT_ASSERT(error
== 0);
85 linger
.l_linger
= MEMCACHED_DEFAULT_TIMEOUT
;
86 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
87 &linger
, (socklen_t
)sizeof(struct linger
));
88 WATCHPOINT_ASSERT(error
== 0);
91 if (ptr
->root
->flags
& MEM_TCP_NODELAY
)
96 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
97 &flag
, (socklen_t
)sizeof(int));
98 WATCHPOINT_ASSERT(error
== 0);
101 if (ptr
->root
->send_size
)
105 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
106 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
107 WATCHPOINT_ASSERT(error
== 0);
110 if (ptr
->root
->recv_size
)
114 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
115 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
116 WATCHPOINT_ASSERT(error
== 0);
119 /* For the moment, not getting a nonblocking mode will not be fatal */
120 if (ptr
->root
->flags
& MEM_NO_BLOCK
)
124 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
125 unlikely (flags
!= -1)
127 (void)fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
131 return MEMCACHED_SUCCESS
;
134 static memcached_return
unix_socket_connect(memcached_server_st
*ptr
)
136 struct sockaddr_un servAddr
;
141 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
143 ptr
->cached_errno
= errno
;
144 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
147 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
148 servAddr
.sun_family
= AF_UNIX
;
149 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
151 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
155 (struct sockaddr
*)&servAddr
,
156 sizeof(servAddr
)) < 0)
163 case EISCONN
: /* We were spinning waiting on connect */
166 WATCHPOINT_ERRNO(errno
);
167 ptr
->cached_errno
= errno
;
168 return MEMCACHED_ERRNO
;
173 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
174 return MEMCACHED_SUCCESS
;
177 static memcached_return
network_connect(memcached_server_st
*ptr
)
181 struct addrinfo
*use
;
183 if (ptr
->root
->server_failure_limit
!= 0)
185 if (ptr
->server_failure_counter
>= ptr
->root
->server_failure_limit
)
187 memcached_server_remove(ptr
);
188 return MEMCACHED_FAILURE
;
192 if (ptr
->sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
193 (!(ptr
->root
->flags
& MEM_USE_CACHE_LOOKUPS
)))
197 rc
= set_hostinfo(ptr
);
198 if (rc
!= MEMCACHED_SUCCESS
)
200 ptr
->sockaddr_inited
= MEMCACHED_ALLOCATED
;
203 use
= ptr
->address_info
;
204 /* Create the socket */
207 if ((ptr
->fd
= socket(use
->ai_family
,
209 use
->ai_protocol
)) < 0)
211 ptr
->cached_errno
= errno
;
212 WATCHPOINT_ERRNO(errno
);
213 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
216 (void)set_socket_options(ptr
);
218 /* connect to server */
222 use
->ai_addrlen
) < 0)
225 /* We are spinning waiting on connect */
229 struct pollfd fds
[1];
232 memset(&fds
, 0, sizeof(struct pollfd
));
234 fds
[0].events
= POLLOUT
| POLLERR
;
235 error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
241 else if (error
!= 1 || fds
[0].revents
& POLLERR
)
243 ptr
->cached_errno
= errno
;
244 WATCHPOINT_ERRNO(ptr
->cached_errno
);
245 WATCHPOINT_NUMBER(ptr
->root
->connect_timeout
);
248 if (ptr
->address_info
)
250 freeaddrinfo(ptr
->address_info
);
251 ptr
->address_info
= NULL
;
254 if (ptr
->root
->retry_timeout
)
256 struct timeval next_time
;
258 gettimeofday(&next_time
, NULL
);
259 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
261 ptr
->server_failure_counter
+= 1;
262 return MEMCACHED_ERRNO
;
267 /* We are spinning waiting on connect */
270 case EISCONN
: /* We were spinning waiting on connect */
274 ptr
->cached_errno
= errno
;
277 if (ptr
->root
->retry_timeout
)
279 struct timeval next_time
;
281 gettimeofday(&next_time
, NULL
);
282 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
288 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
289 ptr
->server_failure_counter
= 0;
290 return MEMCACHED_SUCCESS
;
297 ptr
->server_failure_counter
+= 1;
298 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
301 ptr
->server_failure_counter
= 0;
302 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
306 memcached_return
memcached_connect(memcached_server_st
*ptr
)
308 memcached_return rc
= MEMCACHED_NO_SERVERS
;
309 LIBMEMCACHED_MEMCACHED_CONNECT_START();
311 if (ptr
->root
->retry_timeout
)
313 struct timeval next_time
;
315 gettimeofday(&next_time
, NULL
);
316 if (next_time
.tv_sec
< ptr
->next_retry
)
317 return MEMCACHED_TIMEOUT
;
319 /* We need to clean up the multi startup piece */
322 case MEMCACHED_CONNECTION_UNKNOWN
:
323 WATCHPOINT_ASSERT(0);
324 rc
= MEMCACHED_NOT_SUPPORTED
;
326 case MEMCACHED_CONNECTION_UDP
:
327 case MEMCACHED_CONNECTION_TCP
:
328 rc
= network_connect(ptr
);
330 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
331 rc
= unix_socket_connect(ptr
);
334 WATCHPOINT_ASSERT(0);
337 LIBMEMCACHED_MEMCACHED_CONNECT_END();