5 #include <sys/socket.h>
7 #include <netinet/tcp.h>
10 static memcached_return
set_hostinfo(memcached_server_st
*server
)
14 if ((h
= gethostbyname(server
->hostname
)) == NULL
)
16 return MEMCACHED_HOST_LOCKUP_FAILURE
;
19 server
->servAddr
.sin_family
= h
->h_addrtype
;
20 memcpy((char *) &server
->servAddr
.sin_addr
.s_addr
, h
->h_addr_list
[0], h
->h_length
);
21 server
->servAddr
.sin_port
= htons(server
->port
);
23 return MEMCACHED_SUCCESS
;
26 static memcached_return
unix_socket_connect(memcached_st
*ptr
, unsigned int server_key
)
28 struct sockaddr_un servAddr
;
31 if (ptr
->hosts
[server_key
].fd
== -1)
33 if ((ptr
->hosts
[server_key
].fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
35 ptr
->cached_errno
= errno
;
36 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
39 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
40 servAddr
.sun_family
= AF_UNIX
;
41 strcpy(servAddr
.sun_path
, ptr
->hosts
[server_key
].hostname
); /* Copy filename */
43 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
46 if (connect(ptr
->hosts
[server_key
].fd
,
47 (struct sockaddr
*)&servAddr
,
48 sizeof(servAddr
)) < 0)
51 /* We are spinning waiting on connect */
56 case EISCONN
: /* We were spinning waiting on connect */
59 ptr
->cached_errno
= errno
;
60 return MEMCACHED_ERRNO
;
65 return MEMCACHED_SUCCESS
;
68 static memcached_return
udp_connect(memcached_st
*ptr
, unsigned int server_key
)
70 if (ptr
->hosts
[server_key
].fd
== -1)
72 /* Old connection junk still is in the structure */
73 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].stack_responses
== 0);
75 if (ptr
->hosts
[server_key
].servAddr
.sin_family
== 0)
79 rc
= set_hostinfo(&ptr
->hosts
[server_key
]);
80 if (rc
!= MEMCACHED_SUCCESS
)
84 /* Create the socket */
85 if ((ptr
->hosts
[server_key
].fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
87 ptr
->cached_errno
= errno
;
88 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
92 return MEMCACHED_SUCCESS
;
95 static memcached_return
tcp_connect(memcached_st
*ptr
, unsigned int server_key
)
97 if (ptr
->hosts
[server_key
].fd
== -1)
99 /* Old connection junk still is in the structure */
100 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].stack_responses
== 0);
102 if (ptr
->hosts
[server_key
].servAddr
.sin_family
== 0)
106 rc
= set_hostinfo(&ptr
->hosts
[server_key
]);
107 if (rc
!= MEMCACHED_SUCCESS
)
111 /* Create the socket */
112 if ((ptr
->hosts
[server_key
].fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
114 ptr
->cached_errno
= errno
;
115 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
118 /* For the moment, not getting a nonblocking mode will note be fatal */
119 if (ptr
->flags
& MEM_NO_BLOCK
)
123 flags
= fcntl(ptr
->hosts
[server_key
].fd
, F_GETFL
, 0);
125 (void)fcntl(ptr
->hosts
[server_key
].fd
, F_SETFL
, flags
| O_NONBLOCK
);
128 if (ptr
->flags
& MEM_TCP_NODELAY
)
132 setsockopt(ptr
->hosts
[server_key
].fd
, IPPROTO_TCP
, TCP_NODELAY
,
133 &flag
, (socklen_t
)sizeof(int));
138 setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_SNDBUF
,
139 &ptr
->send_size
, (socklen_t
)sizeof(int));
144 setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_SNDBUF
,
145 &ptr
->recv_size
, (socklen_t
)sizeof(int));
148 /* connect to server */
150 if (connect(ptr
->hosts
[server_key
].fd
,
151 (struct sockaddr
*)&ptr
->hosts
[server_key
].servAddr
,
152 sizeof(struct sockaddr
)) < 0)
155 /* We are spinning waiting on connect */
160 case EISCONN
: /* We were spinning waiting on connect */
163 ptr
->cached_errno
= errno
;
164 return MEMCACHED_ERRNO
;
168 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].stack_responses
== 0);
171 return MEMCACHED_SUCCESS
;
175 memcached_return
memcached_connect(memcached_st
*ptr
, unsigned int server_key
)
177 memcached_return rc
= MEMCACHED_NO_SERVERS
;
178 LIBMEMCACHED_MEMCACHED_CONNECT_START();
180 if (ptr
->connected
== ptr
->number_of_hosts
&& ptr
->number_of_hosts
)
181 return MEMCACHED_SUCCESS
;
183 if (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
184 return MEMCACHED_NO_SERVERS
;
186 /* We need to clean up the multi startup piece */
189 rc
= tcp_connect(ptr
, server_key
);
190 switch (ptr
->hosts
[server_key
].type
)
192 case MEMCACHED_CONNECTION_UNKNOWN
:
193 WATCHPOINT_ASSERT(0);
194 rc
= MEMCACHED_NOT_SUPPORTED
;
196 case MEMCACHED_CONNECTION_UDP
:
197 rc
= udp_connect(ptr
, server_key
);
199 case MEMCACHED_CONNECTION_TCP
:
200 rc
= tcp_connect(ptr
, server_key
);
202 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
203 rc
= unix_socket_connect(ptr
, server_key
);
211 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
213 memcached_return possible_rc
;
215 possible_rc
= MEMCACHED_NOT_SUPPORTED
; /* Remove warning */
217 switch (ptr
->hosts
[x
].type
)
219 case MEMCACHED_CONNECTION_UNKNOWN
:
220 WATCHPOINT_ASSERT(0);
221 possible_rc
= MEMCACHED_NOT_SUPPORTED
;
223 case MEMCACHED_CONNECTION_UDP
:
224 possible_rc
= udp_connect(ptr
, x
);
226 case MEMCACHED_CONNECTION_TCP
:
227 possible_rc
= tcp_connect(ptr
, x
);
229 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
230 possible_rc
= unix_socket_connect(ptr
, x
);
233 rc
= MEMCACHED_SUCCESS
;
235 if (possible_rc
!= MEMCACHED_SUCCESS
)
236 rc
= MEMCACHED_SOME_ERRORS
;
239 LIBMEMCACHED_MEMCACHED_CONNECT_END();