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
;
55 if (ptr
->root
->snd_timeout
)
58 struct timeval waittime
;
61 waittime
.tv_usec
= ptr
->root
->snd_timeout
;
63 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
64 &waittime
, (socklen_t
)sizeof(struct timeval
));
65 WATCHPOINT_ASSERT(error
== 0);
68 if (ptr
->root
->rcv_timeout
)
71 struct timeval waittime
;
74 waittime
.tv_usec
= ptr
->root
->rcv_timeout
;
76 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
77 &waittime
, (socklen_t
)sizeof(struct timeval
));
78 WATCHPOINT_ASSERT(error
== 0);
87 linger
.l_linger
= MEMCACHED_DEFAULT_TIMEOUT
;
88 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
89 &linger
, (socklen_t
)sizeof(struct linger
));
90 WATCHPOINT_ASSERT(error
== 0);
93 if (ptr
->root
->flags
& MEM_TCP_NODELAY
)
98 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
99 &flag
, (socklen_t
)sizeof(int));
100 WATCHPOINT_ASSERT(error
== 0);
103 if (ptr
->root
->send_size
)
107 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
108 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
109 WATCHPOINT_ASSERT(error
== 0);
112 if (ptr
->root
->recv_size
)
116 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
117 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
118 WATCHPOINT_ASSERT(error
== 0);
121 /* For the moment, not getting a nonblocking mode will not be fatal */
122 if (ptr
->root
->flags
& MEM_NO_BLOCK
)
126 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
127 unlikely (flags
!= -1)
129 (void)fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
133 return MEMCACHED_SUCCESS
;
136 static memcached_return
unix_socket_connect(memcached_server_st
*ptr
)
138 struct sockaddr_un servAddr
;
143 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
145 ptr
->cached_errno
= errno
;
146 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
149 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
150 servAddr
.sun_family
= AF_UNIX
;
151 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
153 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
157 (struct sockaddr
*)&servAddr
,
158 sizeof(servAddr
)) < 0)
165 case EISCONN
: /* We were spinning waiting on connect */
168 WATCHPOINT_ERRNO(errno
);
169 ptr
->cached_errno
= errno
;
170 return MEMCACHED_ERRNO
;
175 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
176 return MEMCACHED_SUCCESS
;
179 static memcached_return
network_connect(memcached_server_st
*ptr
)
183 struct addrinfo
*use
;
185 if (ptr
->root
->server_failure_limit
!= 0)
187 if (ptr
->server_failure_counter
>= ptr
->root
->server_failure_limit
)
189 memcached_server_remove(ptr
);
190 return MEMCACHED_FAILURE
;
194 if (ptr
->sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
195 (!(ptr
->root
->flags
& MEM_USE_CACHE_LOOKUPS
)))
199 rc
= set_hostinfo(ptr
);
200 if (rc
!= MEMCACHED_SUCCESS
)
202 ptr
->sockaddr_inited
= MEMCACHED_ALLOCATED
;
205 use
= ptr
->address_info
;
206 /* Create the socket */
209 if ((ptr
->fd
= socket(use
->ai_family
,
211 use
->ai_protocol
)) < 0)
213 ptr
->cached_errno
= errno
;
214 WATCHPOINT_ERRNO(errno
);
215 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
218 (void)set_socket_options(ptr
);
220 /* connect to server */
224 use
->ai_addrlen
) < 0)
227 /* We are spinning waiting on connect */
231 struct pollfd fds
[1];
234 memset(&fds
, 0, sizeof(struct pollfd
));
236 fds
[0].events
= POLLOUT
| POLLERR
;
237 error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
243 else if (error
!= 1 && fds
[0].revents
& POLLERR
)
245 ptr
->cached_errno
= errno
;
246 WATCHPOINT_ERRNO(ptr
->cached_errno
);
247 WATCHPOINT_NUMBER(ptr
->root
->connect_timeout
);
248 memcached_quit_server(ptr
, 1);
250 if (ptr
->root
->retry_timeout
)
252 struct timeval next_time
;
254 gettimeofday(&next_time
, NULL
);
255 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
257 ptr
->server_failure_counter
+= 1;
259 return MEMCACHED_ERRNO
;
264 /* We are spinning waiting on connect */
267 case EISCONN
: /* We were spinning waiting on connect */
271 ptr
->cached_errno
= errno
;
274 if (ptr
->root
->retry_timeout
)
276 struct timeval next_time
;
278 gettimeofday(&next_time
, NULL
);
279 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
285 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
286 ptr
->server_failure_counter
= 0;
287 return MEMCACHED_SUCCESS
;
294 ptr
->server_failure_counter
+= 1;
295 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
298 ptr
->server_failure_counter
= 0;
299 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
303 memcached_return
memcached_connect(memcached_server_st
*ptr
)
305 memcached_return rc
= MEMCACHED_NO_SERVERS
;
306 LIBMEMCACHED_MEMCACHED_CONNECT_START();
308 if (ptr
->root
->retry_timeout
)
310 struct timeval next_time
;
312 gettimeofday(&next_time
, NULL
);
313 if (next_time
.tv_sec
< ptr
->next_retry
)
314 return MEMCACHED_TIMEOUT
;
316 /* We need to clean up the multi startup piece */
319 case MEMCACHED_CONNECTION_UNKNOWN
:
320 WATCHPOINT_ASSERT(0);
321 rc
= MEMCACHED_NOT_SUPPORTED
;
323 case MEMCACHED_CONNECTION_UDP
:
324 case MEMCACHED_CONNECTION_TCP
:
325 rc
= network_connect(ptr
);
327 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
328 rc
= unix_socket_connect(ptr
);
331 WATCHPOINT_ASSERT(0);
334 LIBMEMCACHED_MEMCACHED_CONNECT_END();