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
->flags
& MEM_NO_BLOCK
)
51 struct timeval waittime
;
56 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
57 &waittime
, (socklen_t
)sizeof(struct timeval
));
58 WATCHPOINT_ASSERT(error
== 0);
60 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
61 &waittime
, (socklen_t
)sizeof(struct timeval
));
62 WATCHPOINT_ASSERT(error
== 0);
70 linger
.l_linger
= MEMCACHED_DEFAULT_TIMEOUT
;
71 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
72 &linger
, (socklen_t
)sizeof(struct linger
));
73 WATCHPOINT_ASSERT(error
== 0);
76 if (ptr
->root
->flags
& MEM_TCP_NODELAY
)
81 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
82 &flag
, (socklen_t
)sizeof(int));
83 WATCHPOINT_ASSERT(error
== 0);
86 if (ptr
->root
->send_size
)
90 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
91 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
92 WATCHPOINT_ASSERT(error
== 0);
95 if (ptr
->root
->recv_size
)
99 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
100 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
101 WATCHPOINT_ASSERT(error
== 0);
104 /* For the moment, not getting a nonblocking mode will not be fatal */
105 if (ptr
->root
->flags
& MEM_NO_BLOCK
)
109 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
110 unlikely (flags
!= -1)
112 (void)fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
116 return MEMCACHED_SUCCESS
;
119 static memcached_return
unix_socket_connect(memcached_server_st
*ptr
)
121 struct sockaddr_un servAddr
;
126 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
128 ptr
->cached_errno
= errno
;
129 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
132 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
133 servAddr
.sun_family
= AF_UNIX
;
134 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
136 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
140 (struct sockaddr
*)&servAddr
,
141 sizeof(servAddr
)) < 0)
148 case EISCONN
: /* We were spinning waiting on connect */
151 WATCHPOINT_ERRNO(errno
);
152 ptr
->cached_errno
= errno
;
153 return MEMCACHED_ERRNO
;
157 return MEMCACHED_SUCCESS
;
160 static memcached_return
network_connect(memcached_server_st
*ptr
)
164 struct addrinfo
*use
;
166 /* Old connection junk still is in the structure */
167 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
169 if (ptr
->sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
170 (!(ptr
->root
->flags
& MEM_USE_CACHE_LOOKUPS
)))
174 rc
= set_hostinfo(ptr
);
175 if (rc
!= MEMCACHED_SUCCESS
)
177 ptr
->sockaddr_inited
= MEMCACHED_ALLOCATED
;
180 use
= ptr
->address_info
;
181 /* Create the socket */
184 if ((ptr
->fd
= socket(use
->ai_family
,
186 use
->ai_protocol
)) < 0)
188 ptr
->cached_errno
= errno
;
189 WATCHPOINT_ERRNO(errno
);
190 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
193 (void)set_socket_options(ptr
);
195 /* connect to server */
199 use
->ai_addrlen
) < 0)
202 /* We are spinning waiting on connect */
206 struct pollfd fds
[1];
209 memset(&fds
, 0, sizeof(struct pollfd
));
211 fds
[0].events
= POLLOUT
| POLLERR
;
212 error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
220 ptr
->cached_errno
= errno
;
221 WATCHPOINT_ERRNO(ptr
->cached_errno
);
222 WATCHPOINT_NUMBER(ptr
->root
->connect_timeout
);
225 if (ptr
->address_info
)
227 freeaddrinfo(ptr
->address_info
);
228 ptr
->address_info
= NULL
;
231 return MEMCACHED_ERRNO
;
236 /* We are spinning waiting on connect */
239 case EISCONN
: /* We were spinning waiting on connect */
243 ptr
->cached_errno
= errno
;
246 if (ptr
->root
->retry_timeout
)
248 struct timeval next_time
;
250 gettimeofday(&next_time
, NULL
);
251 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
257 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
258 return MEMCACHED_SUCCESS
;
265 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
267 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
271 memcached_return
memcached_connect(memcached_server_st
*ptr
)
273 memcached_return rc
= MEMCACHED_NO_SERVERS
;
274 LIBMEMCACHED_MEMCACHED_CONNECT_START();
276 if (ptr
->root
->retry_timeout
)
278 struct timeval next_time
;
280 gettimeofday(&next_time
, NULL
);
281 if (next_time
.tv_sec
< ptr
->next_retry
)
282 return MEMCACHED_TIMEOUT
;
284 /* We need to clean up the multi startup piece */
287 case MEMCACHED_CONNECTION_UNKNOWN
:
288 WATCHPOINT_ASSERT(0);
289 rc
= MEMCACHED_NOT_SUPPORTED
;
291 case MEMCACHED_CONNECTION_UDP
:
292 case MEMCACHED_CONNECTION_TCP
:
293 rc
= network_connect(ptr
);
295 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
296 rc
= unix_socket_connect(ptr
);
299 WATCHPOINT_ASSERT(0);
302 LIBMEMCACHED_MEMCACHED_CONNECT_END();