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
));
13 hints
.ai_family
= AF_INET
;
14 hints
.ai_socktype
= SOCK_STREAM
;
17 e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &ai
);
20 WATCHPOINT_STRING(server
->hostname
);
21 WATCHPOINT_STRING(gai_strerror(e
));
22 return MEMCACHED_HOST_LOOKUP_FAILURE
;
25 if (server
->address_info
)
26 freeaddrinfo(server
->address_info
);
27 server
->address_info
= ai
;
29 return MEMCACHED_SUCCESS
;
32 static memcached_return
unix_socket_connect(memcached_st
*ptr
, unsigned int server_key
)
34 struct sockaddr_un servAddr
;
37 if (ptr
->hosts
[server_key
].fd
== -1)
39 if ((ptr
->hosts
[server_key
].fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
41 ptr
->cached_errno
= errno
;
42 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
45 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
46 servAddr
.sun_family
= AF_UNIX
;
47 strcpy(servAddr
.sun_path
, ptr
->hosts
[server_key
].hostname
); /* Copy filename */
49 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
52 if (connect(ptr
->hosts
[server_key
].fd
,
53 (struct sockaddr
*)&servAddr
,
54 sizeof(servAddr
)) < 0)
57 /* We are spinning waiting on connect */
62 case EISCONN
: /* We were spinning waiting on connect */
65 WATCHPOINT_ERRNO(errno
);
66 ptr
->cached_errno
= errno
;
67 return MEMCACHED_ERRNO
;
72 return MEMCACHED_SUCCESS
;
75 static memcached_return
udp_connect(memcached_st
*ptr
, unsigned int server_key
)
77 if (ptr
->hosts
[server_key
].fd
== -1)
79 /* Old connection junk still is in the structure */
80 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].cursor_active
== 0);
83 If we have not allocated the hosts object.
84 Or if the cache has not been set.
86 if (ptr
->hosts
[server_key
].sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
87 (!(ptr
->flags
& MEM_USE_CACHE_LOOKUPS
)))
91 rc
= set_hostinfo(&ptr
->hosts
[server_key
]);
92 if (rc
!= MEMCACHED_SUCCESS
)
95 ptr
->hosts
[server_key
].sockaddr_inited
= MEMCACHED_ALLOCATED
;
98 /* Create the socket */
99 if ((ptr
->hosts
[server_key
].fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
101 ptr
->cached_errno
= errno
;
102 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
106 return MEMCACHED_SUCCESS
;
109 static memcached_return
tcp_connect(memcached_st
*ptr
, unsigned int server_key
)
111 if (ptr
->hosts
[server_key
].fd
== -1)
113 struct addrinfo
*use
;
115 /* Old connection junk still is in the structure */
116 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].cursor_active
== 0);
118 if (ptr
->hosts
[server_key
].sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
119 (!(ptr
->flags
& MEM_USE_CACHE_LOOKUPS
)))
123 rc
= set_hostinfo(&ptr
->hosts
[server_key
]);
124 if (rc
!= MEMCACHED_SUCCESS
)
126 ptr
->hosts
[server_key
].sockaddr_inited
= MEMCACHED_ALLOCATED
;
128 use
= ptr
->hosts
[server_key
].address_info
;
130 /* Create the socket */
131 if ((ptr
->hosts
[server_key
].fd
= socket(use
->ai_family
,
133 use
->ai_protocol
)) < 0)
135 ptr
->cached_errno
= errno
;
136 WATCHPOINT_ERRNO(errno
);
137 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
140 if (ptr
->flags
& MEM_NO_BLOCK
)
143 struct linger linger
;
144 struct timeval waittime
;
150 linger
.l_linger
= MEMCACHED_DEFAULT_TIMEOUT
;
151 error
= setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_LINGER
,
152 &linger
, (socklen_t
)sizeof(struct linger
));
153 WATCHPOINT_ASSERT(error
== 0);
155 error
= setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_SNDTIMEO
,
156 &waittime
, (socklen_t
)sizeof(struct timeval
));
157 WATCHPOINT_ASSERT(error
== 0);
159 error
= setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_RCVTIMEO
,
160 &waittime
, (socklen_t
)sizeof(struct timeval
));
161 WATCHPOINT_ASSERT(error
== 0);
164 if (ptr
->flags
& MEM_TCP_NODELAY
)
169 error
= setsockopt(ptr
->hosts
[server_key
].fd
, IPPROTO_TCP
, TCP_NODELAY
,
170 &flag
, (socklen_t
)sizeof(int));
171 WATCHPOINT_ASSERT(error
== 0);
178 error
= setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_SNDBUF
,
179 &ptr
->send_size
, (socklen_t
)sizeof(int));
180 WATCHPOINT_ASSERT(error
== 0);
187 error
= setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_SNDBUF
,
188 &ptr
->recv_size
, (socklen_t
)sizeof(int));
189 WATCHPOINT_ASSERT(error
== 0);
192 /* For the moment, not getting a nonblocking mode will not be fatal */
193 if (ptr
->flags
& MEM_NO_BLOCK
)
197 flags
= fcntl(ptr
->hosts
[server_key
].fd
, F_GETFL
, 0);
200 (void)fcntl(ptr
->hosts
[server_key
].fd
, F_SETFL
, flags
| O_NONBLOCK
);
205 /* connect to server */
207 if (connect(ptr
->hosts
[server_key
].fd
,
209 use
->ai_addrlen
) < 0)
212 /* We are spinning waiting on connect */
217 case EISCONN
: /* We were spinning waiting on connect */
220 ptr
->cached_errno
= errno
;
221 WATCHPOINT_ERRNO(ptr
->cached_errno
);
222 return MEMCACHED_ERRNO
;
227 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].cursor_active
== 0);
230 return MEMCACHED_SUCCESS
;
234 memcached_return
memcached_connect(memcached_st
*ptr
, unsigned int server_key
)
236 memcached_return rc
= MEMCACHED_NO_SERVERS
;
237 LIBMEMCACHED_MEMCACHED_CONNECT_START();
239 if (ptr
->connected
== ptr
->number_of_hosts
&& ptr
->number_of_hosts
)
240 return MEMCACHED_SUCCESS
;
242 if (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
243 return MEMCACHED_NO_SERVERS
;
245 /* We need to clean up the multi startup piece */
246 switch (ptr
->hosts
[server_key
].type
)
248 case MEMCACHED_CONNECTION_UNKNOWN
:
249 WATCHPOINT_ASSERT(0);
250 rc
= MEMCACHED_NOT_SUPPORTED
;
252 case MEMCACHED_CONNECTION_UDP
:
253 rc
= udp_connect(ptr
, server_key
);
255 case MEMCACHED_CONNECTION_TCP
:
256 rc
= tcp_connect(ptr
, server_key
);
258 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
259 rc
= unix_socket_connect(ptr
, server_key
);
263 if (rc
!= MEMCACHED_SUCCESS
)
264 WATCHPOINT_ERROR(rc
);
266 LIBMEMCACHED_MEMCACHED_CONNECT_END();