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);
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)
138 /* We are spinning waiting on connect */
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 */
202 case EISCONN
: /* We were spinning waiting on connect */
205 ptr
->cached_errno
= errno
;
206 WATCHPOINT_ERRNO(ptr
->cached_errno
);
213 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
214 return MEMCACHED_SUCCESS
;
221 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
223 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
227 memcached_return
memcached_connect(memcached_server_st
*ptr
)
229 memcached_return rc
= MEMCACHED_NO_SERVERS
;
230 LIBMEMCACHED_MEMCACHED_CONNECT_START();
232 /* We need to clean up the multi startup piece */
235 case MEMCACHED_CONNECTION_UNKNOWN
:
236 WATCHPOINT_ASSERT(0);
237 rc
= MEMCACHED_NOT_SUPPORTED
;
239 case MEMCACHED_CONNECTION_UDP
:
240 case MEMCACHED_CONNECTION_TCP
:
241 rc
= network_connect(ptr
);
243 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
244 rc
= unix_socket_connect(ptr
);
247 WATCHPOINT_ASSERT(0);
250 if (rc
!= MEMCACHED_SUCCESS
)
251 WATCHPOINT_ERROR(rc
);
253 LIBMEMCACHED_MEMCACHED_CONNECT_END();