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_socktype
= SOCK_STREAM
;
16 e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &ai
);
19 WATCHPOINT_STRING(server
->hostname
);
20 WATCHPOINT_STRING(gai_strerror(e
));
21 return MEMCACHED_HOST_LOOKUP_FAILURE
;
24 if (server
->address_info
)
25 freeaddrinfo(server
->address_info
);
26 server
->address_info
= ai
;
28 return MEMCACHED_SUCCESS
;
31 static memcached_return
unix_socket_connect(memcached_st
*ptr
, unsigned int server_key
)
33 struct sockaddr_un servAddr
;
36 if (ptr
->hosts
[server_key
].fd
== -1)
38 if ((ptr
->hosts
[server_key
].fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
40 ptr
->cached_errno
= errno
;
41 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
44 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
45 servAddr
.sun_family
= AF_UNIX
;
46 strcpy(servAddr
.sun_path
, ptr
->hosts
[server_key
].hostname
); /* Copy filename */
48 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
51 if (connect(ptr
->hosts
[server_key
].fd
,
52 (struct sockaddr
*)&servAddr
,
53 sizeof(servAddr
)) < 0)
56 /* We are spinning waiting on connect */
61 case EISCONN
: /* We were spinning waiting on connect */
64 WATCHPOINT_ERRNO(errno
);
65 ptr
->cached_errno
= errno
;
66 return MEMCACHED_ERRNO
;
71 return MEMCACHED_SUCCESS
;
74 static memcached_return
udp_connect(memcached_st
*ptr
, unsigned int server_key
)
76 if (ptr
->hosts
[server_key
].fd
== -1)
78 /* Old connection junk still is in the structure */
79 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].cursor_active
== 0);
82 If we have not allocated the hosts object.
83 Or if the cache has not been set.
85 if (ptr
->hosts
[server_key
].sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
86 (!(ptr
->flags
& MEM_USE_CACHE_LOOKUPS
)))
90 rc
= set_hostinfo(&ptr
->hosts
[server_key
]);
91 if (rc
!= MEMCACHED_SUCCESS
)
94 ptr
->hosts
[server_key
].sockaddr_inited
= MEMCACHED_ALLOCATED
;
97 /* Create the socket */
98 if ((ptr
->hosts
[server_key
].fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
100 ptr
->cached_errno
= errno
;
101 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
105 return MEMCACHED_SUCCESS
;
108 static memcached_return
tcp_connect(memcached_st
*ptr
, unsigned int server_key
)
110 if (ptr
->hosts
[server_key
].fd
== -1)
112 struct addrinfo
*use
;
114 /* Old connection junk still is in the structure */
115 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].cursor_active
== 0);
117 if (ptr
->hosts
[server_key
].sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
118 (!(ptr
->flags
& MEM_USE_CACHE_LOOKUPS
)))
122 rc
= set_hostinfo(&ptr
->hosts
[server_key
]);
123 if (rc
!= MEMCACHED_SUCCESS
)
125 ptr
->hosts
[server_key
].sockaddr_inited
= MEMCACHED_ALLOCATED
;
127 use
= ptr
->hosts
[server_key
].address_info
;
129 /* Create the socket */
130 if ((ptr
->hosts
[server_key
].fd
= socket(use
->ai_family
,
132 use
->ai_protocol
)) < 0)
134 ptr
->cached_errno
= errno
;
135 WATCHPOINT_ERRNO(errno
);
136 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
139 if (ptr
->flags
& MEM_NO_BLOCK
)
142 struct linger linger
;
143 struct timeval waittime
;
149 linger
.l_linger
= MEMCACHED_DEFAULT_TIMEOUT
;
150 error
= setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_LINGER
,
151 &linger
, (socklen_t
)sizeof(struct linger
));
152 WATCHPOINT_ASSERT(error
== 0);
154 error
= setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_SNDTIMEO
,
155 &waittime
, (socklen_t
)sizeof(struct timeval
));
156 WATCHPOINT_ASSERT(error
== 0);
158 error
= setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_RCVTIMEO
,
159 &waittime
, (socklen_t
)sizeof(struct timeval
));
160 WATCHPOINT_ASSERT(error
== 0);
163 if (ptr
->flags
& MEM_TCP_NODELAY
)
168 error
= setsockopt(ptr
->hosts
[server_key
].fd
, IPPROTO_TCP
, TCP_NODELAY
,
169 &flag
, (socklen_t
)sizeof(int));
170 WATCHPOINT_ASSERT(error
== 0);
177 error
= setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_SNDBUF
,
178 &ptr
->send_size
, (socklen_t
)sizeof(int));
179 WATCHPOINT_ASSERT(error
== 0);
186 error
= setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_SNDBUF
,
187 &ptr
->recv_size
, (socklen_t
)sizeof(int));
188 WATCHPOINT_ASSERT(error
== 0);
191 /* For the moment, not getting a nonblocking mode will not be fatal */
192 if (ptr
->flags
& MEM_NO_BLOCK
)
196 flags
= fcntl(ptr
->hosts
[server_key
].fd
, F_GETFL
, 0);
199 (void)fcntl(ptr
->hosts
[server_key
].fd
, F_SETFL
, flags
| O_NONBLOCK
);
204 /* connect to server */
206 if (connect(ptr
->hosts
[server_key
].fd
,
208 use
->ai_addrlen
) < 0)
211 /* We are spinning waiting on connect */
216 case EISCONN
: /* We were spinning waiting on connect */
219 ptr
->cached_errno
= errno
;
220 WATCHPOINT_ERRNO(ptr
->cached_errno
);
221 close(ptr
->hosts
[server_key
].fd
);
222 ptr
->hosts
[server_key
].fd
= -1;
223 return MEMCACHED_ERRNO
;
228 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].cursor_active
== 0);
231 return MEMCACHED_SUCCESS
;
235 memcached_return
memcached_connect(memcached_st
*ptr
, unsigned int server_key
)
237 memcached_return rc
= MEMCACHED_NO_SERVERS
;
238 LIBMEMCACHED_MEMCACHED_CONNECT_START();
240 if (ptr
->connected
== ptr
->number_of_hosts
&& ptr
->number_of_hosts
)
241 return MEMCACHED_SUCCESS
;
243 if (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
244 return MEMCACHED_NO_SERVERS
;
246 /* We need to clean up the multi startup piece */
247 switch (ptr
->hosts
[server_key
].type
)
249 case MEMCACHED_CONNECTION_UNKNOWN
:
250 WATCHPOINT_ASSERT(0);
251 rc
= MEMCACHED_NOT_SUPPORTED
;
253 case MEMCACHED_CONNECTION_UDP
:
254 rc
= udp_connect(ptr
, server_key
);
256 case MEMCACHED_CONNECTION_TCP
:
257 rc
= tcp_connect(ptr
, server_key
);
259 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
260 rc
= unix_socket_connect(ptr
, server_key
);
264 if (rc
!= MEMCACHED_SUCCESS
)
265 WATCHPOINT_ERROR(rc
);
267 LIBMEMCACHED_MEMCACHED_CONNECT_END();