4 static memcached_return
set_hostinfo(memcached_server_st
*server
)
9 char str_port
[NI_MAXSERV
];
11 sprintf(str_port
, "%u", server
->port
);
13 memset(&hints
, 0, sizeof(hints
));
15 hints
.ai_family
= AF_INET
;
16 if (server
->type
== MEMCACHED_CONNECTION_UDP
)
18 hints
.ai_protocol
= IPPROTO_UDP
;
19 hints
.ai_socktype
= SOCK_DGRAM
;
23 hints
.ai_socktype
= SOCK_STREAM
;
24 hints
.ai_protocol
= IPPROTO_TCP
;
27 e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &ai
);
30 WATCHPOINT_STRING(server
->hostname
);
31 WATCHPOINT_STRING(gai_strerror(e
));
32 return MEMCACHED_HOST_LOOKUP_FAILURE
;
35 if (server
->address_info
)
36 freeaddrinfo(server
->address_info
);
37 server
->address_info
= ai
;
39 return MEMCACHED_SUCCESS
;
42 static memcached_return
set_socket_options(memcached_server_st
*ptr
)
44 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
45 return MEMCACHED_SUCCESS
;
47 if (ptr
->root
->flags
& MEM_NO_BLOCK
)
51 struct timeval waittime
;
57 linger
.l_linger
= MEMCACHED_DEFAULT_TIMEOUT
;
58 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
59 &linger
, (socklen_t
)sizeof(struct linger
));
60 WATCHPOINT_ASSERT(error
== 0);
62 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
63 &waittime
, (socklen_t
)sizeof(struct timeval
));
64 WATCHPOINT_ASSERT(error
== 0);
66 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
67 &waittime
, (socklen_t
)sizeof(struct timeval
));
68 WATCHPOINT_ASSERT(error
== 0);
71 if (ptr
->root
->flags
& MEM_TCP_NODELAY
)
76 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
77 &flag
, (socklen_t
)sizeof(int));
78 WATCHPOINT_ASSERT(error
== 0);
81 if (ptr
->root
->send_size
)
85 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
86 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
87 WATCHPOINT_ASSERT(error
== 0);
90 if (ptr
->root
->recv_size
)
94 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
95 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
96 WATCHPOINT_ASSERT(error
== 0);
99 /* For the moment, not getting a nonblocking mode will not be fatal */
100 if (ptr
->root
->flags
& MEM_NO_BLOCK
)
104 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
105 unlikely (flags
!= -1)
107 (void)fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
111 return MEMCACHED_SUCCESS
;
114 static memcached_return
unix_socket_connect(memcached_server_st
*ptr
)
116 struct sockaddr_un servAddr
;
121 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
123 ptr
->cached_errno
= errno
;
124 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
127 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
128 servAddr
.sun_family
= AF_UNIX
;
129 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
131 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
135 (struct sockaddr
*)&servAddr
,
136 sizeof(servAddr
)) < 0)
143 case EISCONN
: /* We were spinning waiting on connect */
146 WATCHPOINT_ERRNO(errno
);
147 ptr
->cached_errno
= errno
;
148 return MEMCACHED_ERRNO
;
152 return MEMCACHED_SUCCESS
;
155 static memcached_return
network_connect(memcached_server_st
*ptr
)
159 struct addrinfo
*use
;
161 /* Old connection junk still is in the structure */
162 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
164 if (ptr
->sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
165 (!(ptr
->root
->flags
& MEM_USE_CACHE_LOOKUPS
)))
169 rc
= set_hostinfo(ptr
);
170 if (rc
!= MEMCACHED_SUCCESS
)
172 ptr
->sockaddr_inited
= MEMCACHED_ALLOCATED
;
175 use
= ptr
->address_info
;
176 /* Create the socket */
179 if ((ptr
->fd
= socket(use
->ai_family
,
181 use
->ai_protocol
)) < 0)
183 ptr
->cached_errno
= errno
;
184 WATCHPOINT_ERRNO(errno
);
185 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
188 (void)set_socket_options(ptr
);
190 /* connect to server */
194 use
->ai_addrlen
) < 0)
197 /* We are spinning waiting on connect */
201 struct pollfd fds
[1];
204 memset(&fds
, 0, sizeof(struct pollfd
));
206 fds
[0].events
= POLLOUT
| POLLERR
;
207 error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
211 ptr
->cached_errno
= errno
;
212 WATCHPOINT_ERRNO(ptr
->cached_errno
);
215 return MEMCACHED_ERRNO
;
220 /* We are spinning waiting on connect */
223 case EISCONN
: /* We were spinning waiting on connect */
226 ptr
->cached_errno
= errno
;
227 WATCHPOINT_ERRNO(ptr
->cached_errno
);
234 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
235 return MEMCACHED_SUCCESS
;
242 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
244 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
248 memcached_return
memcached_connect(memcached_server_st
*ptr
)
250 memcached_return rc
= MEMCACHED_NO_SERVERS
;
251 LIBMEMCACHED_MEMCACHED_CONNECT_START();
253 /* We need to clean up the multi startup piece */
256 case MEMCACHED_CONNECTION_UNKNOWN
:
257 WATCHPOINT_ASSERT(0);
258 rc
= MEMCACHED_NOT_SUPPORTED
;
260 case MEMCACHED_CONNECTION_UDP
:
261 case MEMCACHED_CONNECTION_TCP
:
262 rc
= network_connect(ptr
);
264 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
265 rc
= unix_socket_connect(ptr
);
268 WATCHPOINT_ASSERT(0);
271 if (rc
!= MEMCACHED_SUCCESS
)
272 WATCHPOINT_ERROR(rc
);
274 LIBMEMCACHED_MEMCACHED_CONNECT_END();