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 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
50 return MEMCACHED_SUCCESS
;
52 if (ptr
->root
->snd_timeout
)
55 struct timeval waittime
;
58 waittime
.tv_usec
= ptr
->root
->snd_timeout
;
60 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
61 &waittime
, (socklen_t
)sizeof(struct timeval
));
62 WATCHPOINT_ASSERT(error
== 0);
65 if (ptr
->root
->rcv_timeout
)
68 struct timeval waittime
;
71 waittime
.tv_usec
= ptr
->root
->rcv_timeout
;
73 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
74 &waittime
, (socklen_t
)sizeof(struct timeval
));
75 WATCHPOINT_ASSERT(error
== 0);
83 linger
.l_linger
= MEMCACHED_DEFAULT_TIMEOUT
;
84 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
85 &linger
, (socklen_t
)sizeof(struct linger
));
86 WATCHPOINT_ASSERT(error
== 0);
89 if (ptr
->root
->flags
& MEM_TCP_NODELAY
)
94 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
95 &flag
, (socklen_t
)sizeof(int));
96 WATCHPOINT_ASSERT(error
== 0);
99 if (ptr
->root
->send_size
)
103 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
104 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
105 WATCHPOINT_ASSERT(error
== 0);
108 if (ptr
->root
->recv_size
)
112 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
113 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
114 WATCHPOINT_ASSERT(error
== 0);
117 /* For the moment, not getting a nonblocking mode will not be fatal */
118 if (ptr
->root
->flags
& MEM_NO_BLOCK
)
122 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
123 unlikely (flags
!= -1)
125 (void)fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
129 return MEMCACHED_SUCCESS
;
132 static memcached_return
unix_socket_connect(memcached_server_st
*ptr
)
134 struct sockaddr_un servAddr
;
139 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
141 ptr
->cached_errno
= errno
;
142 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
145 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
146 servAddr
.sun_family
= AF_UNIX
;
147 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
149 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
153 (struct sockaddr
*)&servAddr
,
154 sizeof(servAddr
)) < 0)
161 case EISCONN
: /* We were spinning waiting on connect */
164 WATCHPOINT_ERRNO(errno
);
165 ptr
->cached_errno
= errno
;
166 return MEMCACHED_ERRNO
;
170 return MEMCACHED_SUCCESS
;
173 static memcached_return
network_connect(memcached_server_st
*ptr
)
177 struct addrinfo
*use
;
179 if (ptr
->root
->server_failure_limit
!= 0)
181 if (ptr
->server_failure_counter
>= ptr
->root
->server_failure_limit
)
183 memcached_server_remove(ptr
);
184 return MEMCACHED_FAILURE
;
188 if (ptr
->sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
189 (!(ptr
->root
->flags
& MEM_USE_CACHE_LOOKUPS
)))
193 rc
= set_hostinfo(ptr
);
194 if (rc
!= MEMCACHED_SUCCESS
)
196 ptr
->sockaddr_inited
= MEMCACHED_ALLOCATED
;
199 use
= ptr
->address_info
;
200 /* Create the socket */
203 if ((ptr
->fd
= socket(use
->ai_family
,
205 use
->ai_protocol
)) < 0)
207 ptr
->cached_errno
= errno
;
208 WATCHPOINT_ERRNO(errno
);
209 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
212 (void)set_socket_options(ptr
);
214 /* connect to server */
218 use
->ai_addrlen
) < 0)
221 /* We are spinning waiting on connect */
225 struct pollfd fds
[1];
228 memset(&fds
, 0, sizeof(struct pollfd
));
230 fds
[0].events
= POLLOUT
| POLLERR
;
231 error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
237 else if (error
!= 1 || fds
[0].revents
& POLLERR
)
239 ptr
->cached_errno
= errno
;
240 WATCHPOINT_ERRNO(ptr
->cached_errno
);
241 WATCHPOINT_NUMBER(ptr
->root
->connect_timeout
);
244 if (ptr
->address_info
)
246 freeaddrinfo(ptr
->address_info
);
247 ptr
->address_info
= NULL
;
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;
258 return MEMCACHED_ERRNO
;
263 /* We are spinning waiting on connect */
266 case EISCONN
: /* We were spinning waiting on connect */
270 ptr
->cached_errno
= errno
;
273 if (ptr
->root
->retry_timeout
)
275 struct timeval next_time
;
277 gettimeofday(&next_time
, NULL
);
278 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
284 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
285 ptr
->server_failure_counter
= 0;
286 return MEMCACHED_SUCCESS
;
293 ptr
->server_failure_counter
+= 1;
294 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
297 ptr
->server_failure_counter
= 0;
298 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
302 memcached_return
memcached_connect(memcached_server_st
*ptr
)
304 memcached_return rc
= MEMCACHED_NO_SERVERS
;
305 LIBMEMCACHED_MEMCACHED_CONNECT_START();
307 if (ptr
->root
->retry_timeout
)
309 struct timeval next_time
;
311 gettimeofday(&next_time
, NULL
);
312 if (next_time
.tv_sec
< ptr
->next_retry
)
313 return MEMCACHED_TIMEOUT
;
315 /* We need to clean up the multi startup piece */
318 case MEMCACHED_CONNECTION_UNKNOWN
:
319 WATCHPOINT_ASSERT(0);
320 rc
= MEMCACHED_NOT_SUPPORTED
;
322 case MEMCACHED_CONNECTION_UDP
:
323 case MEMCACHED_CONNECTION_TCP
:
324 rc
= network_connect(ptr
);
326 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
327 rc
= unix_socket_connect(ptr
);
330 WATCHPOINT_ASSERT(0);
333 LIBMEMCACHED_MEMCACHED_CONNECT_END();