3 static memcached_return
set_hostinfo(memcached_server_st
*server
)
8 char str_port
[NI_MAXSERV
];
10 sprintf(str_port
, "%u", server
->port
);
12 memset(&hints
, 0, sizeof(hints
));
14 hints
.ai_family
= AF_INET
;
15 if (server
->type
== MEMCACHED_CONNECTION_UDP
)
17 hints
.ai_protocol
= IPPROTO_UDP
;
18 hints
.ai_socktype
= SOCK_DGRAM
;
22 hints
.ai_socktype
= SOCK_STREAM
;
23 hints
.ai_protocol
= IPPROTO_TCP
;
26 e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &ai
);
29 WATCHPOINT_STRING(server
->hostname
);
30 WATCHPOINT_STRING(gai_strerror(e
));
31 return MEMCACHED_HOST_LOOKUP_FAILURE
;
34 if (server
->address_info
)
35 freeaddrinfo(server
->address_info
);
36 server
->address_info
= ai
;
38 return MEMCACHED_SUCCESS
;
41 static memcached_return
set_socket_options(memcached_server_st
*ptr
)
43 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
44 return MEMCACHED_SUCCESS
;
46 if (ptr
->root
->flags
& MEM_NO_BLOCK
)
50 struct timeval waittime
;
56 linger
.l_linger
= MEMCACHED_DEFAULT_TIMEOUT
;
57 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
58 &linger
, (socklen_t
)sizeof(struct linger
));
59 WATCHPOINT_ASSERT(error
== 0);
61 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
62 &waittime
, (socklen_t
)sizeof(struct timeval
));
63 WATCHPOINT_ASSERT(error
== 0);
65 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
66 &waittime
, (socklen_t
)sizeof(struct timeval
));
67 WATCHPOINT_ASSERT(error
== 0);
70 if (ptr
->root
->flags
& MEM_TCP_NODELAY
)
75 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
76 &flag
, (socklen_t
)sizeof(int));
77 WATCHPOINT_ASSERT(error
== 0);
80 if (ptr
->root
->send_size
)
84 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
85 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
86 WATCHPOINT_ASSERT(error
== 0);
89 if (ptr
->root
->recv_size
)
93 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
94 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
95 WATCHPOINT_ASSERT(error
== 0);
98 /* For the moment, not getting a nonblocking mode will not be fatal */
99 if (ptr
->root
->flags
& MEM_NO_BLOCK
)
103 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
104 unlikely (flags
!= -1)
106 (void)fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
110 return MEMCACHED_SUCCESS
;
113 static memcached_return
unix_socket_connect(memcached_server_st
*ptr
)
115 struct sockaddr_un servAddr
;
120 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
122 ptr
->cached_errno
= errno
;
123 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
126 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
127 servAddr
.sun_family
= AF_UNIX
;
128 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
130 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
134 (struct sockaddr
*)&servAddr
,
135 sizeof(servAddr
)) < 0)
140 struct timeval tm
= { ptr
->root
->connect_timeout
, 0 };
141 socklen_t len
= sizeof(int);
146 FD_SET(ptr
->fd
, &wset
);
148 select(ptr
->fd
+1, NULL
, &wset
, NULL
, &tm
);
149 if (FD_ISSET(ptr
->fd
, &wset
) != 0)
151 if (getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &value
, &len
) == 0)
170 ptr
->cached_errno
= errno
;
171 WATCHPOINT_ERRNO(ptr
->cached_errno
);
174 return MEMCACHED_ERRNO
;
179 /* We are spinning waiting on connect */
183 case EISCONN
: /* We were spinning waiting on connect */
186 WATCHPOINT_ERRNO(errno
);
187 ptr
->cached_errno
= errno
;
188 return MEMCACHED_ERRNO
;
192 return MEMCACHED_SUCCESS
;
195 static memcached_return
network_connect(memcached_server_st
*ptr
)
199 struct addrinfo
*use
;
201 /* Old connection junk still is in the structure */
202 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
204 if (ptr
->sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
205 (!(ptr
->root
->flags
& MEM_USE_CACHE_LOOKUPS
)))
209 rc
= set_hostinfo(ptr
);
210 if (rc
!= MEMCACHED_SUCCESS
)
212 ptr
->sockaddr_inited
= MEMCACHED_ALLOCATED
;
215 use
= ptr
->address_info
;
216 /* Create the socket */
219 if ((ptr
->fd
= socket(use
->ai_family
,
221 use
->ai_protocol
)) < 0)
223 ptr
->cached_errno
= errno
;
224 WATCHPOINT_ERRNO(errno
);
225 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
228 (void)set_socket_options(ptr
);
230 /* connect to server */
234 use
->ai_addrlen
) < 0)
237 /* We are spinning waiting on connect */
242 case EISCONN
: /* We were spinning waiting on connect */
245 ptr
->cached_errno
= errno
;
246 WATCHPOINT_ERRNO(ptr
->cached_errno
);
253 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
254 return MEMCACHED_SUCCESS
;
261 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
263 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
267 memcached_return
memcached_connect(memcached_server_st
*ptr
)
269 memcached_return rc
= MEMCACHED_NO_SERVERS
;
270 LIBMEMCACHED_MEMCACHED_CONNECT_START();
272 /* We need to clean up the multi startup piece */
275 case MEMCACHED_CONNECTION_UNKNOWN
:
276 WATCHPOINT_ASSERT(0);
277 rc
= MEMCACHED_NOT_SUPPORTED
;
279 case MEMCACHED_CONNECTION_UDP
:
280 case MEMCACHED_CONNECTION_TCP
:
281 rc
= network_connect(ptr
);
283 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
284 rc
= unix_socket_connect(ptr
);
287 WATCHPOINT_ASSERT(0);
290 if (rc
!= MEMCACHED_SUCCESS
)
291 WATCHPOINT_ERROR(rc
);
293 LIBMEMCACHED_MEMCACHED_CONNECT_END();