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
)
52 struct timeval waittime
;
58 linger
.l_linger
= MEMCACHED_DEFAULT_TIMEOUT
;
59 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
60 &linger
, (socklen_t
)sizeof(struct linger
));
61 WATCHPOINT_ASSERT(error
== 0);
63 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
64 &waittime
, (socklen_t
)sizeof(struct timeval
));
65 WATCHPOINT_ASSERT(error
== 0);
67 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
68 &waittime
, (socklen_t
)sizeof(struct timeval
));
69 WATCHPOINT_ASSERT(error
== 0);
72 if (ptr
->root
->flags
& MEM_TCP_NODELAY
)
77 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
78 &flag
, (socklen_t
)sizeof(int));
79 WATCHPOINT_ASSERT(error
== 0);
82 if (ptr
->root
->send_size
)
86 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
87 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
88 WATCHPOINT_ASSERT(error
== 0);
91 if (ptr
->root
->recv_size
)
95 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
96 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
97 WATCHPOINT_ASSERT(error
== 0);
100 /* For the moment, not getting a nonblocking mode will not be fatal */
101 if (ptr
->root
->flags
& MEM_NO_BLOCK
)
105 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
106 unlikely (flags
!= -1)
108 (void)fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
112 return MEMCACHED_SUCCESS
;
115 static memcached_return
unix_socket_connect(memcached_server_st
*ptr
)
117 struct sockaddr_un servAddr
;
122 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
124 ptr
->cached_errno
= errno
;
125 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
128 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
129 servAddr
.sun_family
= AF_UNIX
;
130 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
132 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
136 (struct sockaddr
*)&servAddr
,
137 sizeof(servAddr
)) < 0)
144 case EISCONN
: /* We were spinning waiting on connect */
147 WATCHPOINT_ERRNO(errno
);
148 ptr
->cached_errno
= errno
;
149 return MEMCACHED_ERRNO
;
153 return MEMCACHED_SUCCESS
;
156 static memcached_return
network_connect(memcached_server_st
*ptr
)
160 struct addrinfo
*use
;
162 /* Old connection junk still is in the structure */
163 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
165 if (ptr
->sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
166 (!(ptr
->root
->flags
& MEM_USE_CACHE_LOOKUPS
)))
170 rc
= set_hostinfo(ptr
);
171 if (rc
!= MEMCACHED_SUCCESS
)
173 ptr
->sockaddr_inited
= MEMCACHED_ALLOCATED
;
176 use
= ptr
->address_info
;
177 /* Create the socket */
180 if ((ptr
->fd
= socket(use
->ai_family
,
182 use
->ai_protocol
)) < 0)
184 ptr
->cached_errno
= errno
;
185 WATCHPOINT_ERRNO(errno
);
186 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
189 (void)set_socket_options(ptr
);
191 /* connect to server */
195 use
->ai_addrlen
) < 0)
198 /* We are spinning waiting on connect */
202 struct pollfd fds
[1];
205 memset(&fds
, 0, sizeof(struct pollfd
));
207 fds
[0].events
= POLLOUT
| POLLERR
;
208 error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
212 ptr
->cached_errno
= errno
;
213 WATCHPOINT_ERRNO(ptr
->cached_errno
);
214 WATCHPOINT_NUMBER(ptr
->root
->connect_timeout
);
217 return MEMCACHED_ERRNO
;
222 /* We are spinning waiting on connect */
225 case EISCONN
: /* We were spinning waiting on connect */
228 ptr
->cached_errno
= errno
;
229 WATCHPOINT_ERRNO(ptr
->cached_errno
);
232 if (ptr
->root
->retry_timeout
)
234 struct timeval next_time
;
236 gettimeofday(&next_time
, NULL
);
237 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
243 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
244 return MEMCACHED_SUCCESS
;
251 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
253 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
257 memcached_return
memcached_connect(memcached_server_st
*ptr
)
259 memcached_return rc
= MEMCACHED_NO_SERVERS
;
260 LIBMEMCACHED_MEMCACHED_CONNECT_START();
262 if (ptr
->root
->retry_timeout
)
264 struct timeval next_time
;
266 gettimeofday(&next_time
, NULL
);
267 if (next_time
.tv_sec
< ptr
->next_retry
)
268 return MEMCACHED_TIMEOUT
;
270 /* We need to clean up the multi startup piece */
273 case MEMCACHED_CONNECTION_UNKNOWN
:
274 WATCHPOINT_ASSERT(0);
275 rc
= MEMCACHED_NOT_SUPPORTED
;
277 case MEMCACHED_CONNECTION_UDP
:
278 case MEMCACHED_CONNECTION_TCP
:
279 rc
= network_connect(ptr
);
281 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
282 rc
= unix_socket_connect(ptr
);
285 WATCHPOINT_ASSERT(0);
288 if (rc
!= MEMCACHED_SUCCESS
)
289 WATCHPOINT_ERROR(rc
);
291 LIBMEMCACHED_MEMCACHED_CONNECT_END();