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 WATCHPOINT_STRING(server
->hostname
);
17 WATCHPOINT_STRING(hstrerror(h_errno
));
18 return MEMCACHED_HOST_LOOKUP_FAILURE
;
21 server
->servAddr
.sin_family
= h
->h_addrtype
;
22 memcpy((char *) &server
->servAddr
.sin_addr
.s_addr
, h
->h_addr_list
[0], h
->h_length
);
23 server
->servAddr
.sin_port
= htons(server
->port
);
25 return MEMCACHED_SUCCESS
;
28 static memcached_return
unix_socket_connect(memcached_st
*ptr
, unsigned int server_key
)
30 struct sockaddr_un servAddr
;
33 if (ptr
->hosts
[server_key
].fd
== -1)
35 if ((ptr
->hosts
[server_key
].fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
37 ptr
->cached_errno
= errno
;
38 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
41 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
42 servAddr
.sun_family
= AF_UNIX
;
43 strcpy(servAddr
.sun_path
, ptr
->hosts
[server_key
].hostname
); /* Copy filename */
45 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
48 if (connect(ptr
->hosts
[server_key
].fd
,
49 (struct sockaddr
*)&servAddr
,
50 sizeof(servAddr
)) < 0)
53 /* We are spinning waiting on connect */
58 case EISCONN
: /* We were spinning waiting on connect */
61 ptr
->cached_errno
= errno
;
62 return MEMCACHED_ERRNO
;
67 return MEMCACHED_SUCCESS
;
70 static memcached_return
udp_connect(memcached_st
*ptr
, unsigned int server_key
)
72 if (ptr
->hosts
[server_key
].fd
== -1)
74 /* Old connection junk still is in the structure */
75 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].stack_responses
== 0);
78 If we have not allocated the hosts object.
79 Or if the cache has not been set.
81 if (ptr
->hosts
[server_key
].sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
82 (!(ptr
->flags
& MEM_USE_CACHE_LOOKUPS
)))
86 rc
= set_hostinfo(&ptr
->hosts
[server_key
]);
87 if (rc
!= MEMCACHED_SUCCESS
)
90 ptr
->hosts
[server_key
].sockaddr_inited
= MEMCACHED_ALLOCATED
;
93 /* Create the socket */
94 if ((ptr
->hosts
[server_key
].fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
96 ptr
->cached_errno
= errno
;
97 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
101 return MEMCACHED_SUCCESS
;
104 static memcached_return
tcp_connect(memcached_st
*ptr
, unsigned int server_key
)
106 if (ptr
->hosts
[server_key
].fd
== -1)
108 /* Old connection junk still is in the structure */
109 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].stack_responses
== 0);
111 if (ptr
->hosts
[server_key
].servAddr
.sin_family
== 0)
115 rc
= set_hostinfo(&ptr
->hosts
[server_key
]);
116 if (rc
!= MEMCACHED_SUCCESS
)
120 /* Create the socket */
121 if ((ptr
->hosts
[server_key
].fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
123 ptr
->cached_errno
= errno
;
124 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
127 /* For the moment, not getting a nonblocking mode will note be fatal */
128 if (ptr
->flags
& MEM_NO_BLOCK
)
132 flags
= fcntl(ptr
->hosts
[server_key
].fd
, F_GETFL
, 0);
134 (void)fcntl(ptr
->hosts
[server_key
].fd
, F_SETFL
, flags
| O_NONBLOCK
);
137 if (ptr
->flags
& MEM_TCP_NODELAY
)
141 setsockopt(ptr
->hosts
[server_key
].fd
, IPPROTO_TCP
, TCP_NODELAY
,
142 &flag
, (socklen_t
)sizeof(int));
147 setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_SNDBUF
,
148 &ptr
->send_size
, (socklen_t
)sizeof(int));
153 setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_SNDBUF
,
154 &ptr
->recv_size
, (socklen_t
)sizeof(int));
157 /* connect to server */
159 if (connect(ptr
->hosts
[server_key
].fd
,
160 (struct sockaddr
*)&ptr
->hosts
[server_key
].servAddr
,
161 sizeof(struct sockaddr
)) < 0)
164 /* We are spinning waiting on connect */
169 case EISCONN
: /* We were spinning waiting on connect */
172 ptr
->cached_errno
= errno
;
173 return MEMCACHED_ERRNO
;
177 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].stack_responses
== 0);
180 return MEMCACHED_SUCCESS
;
184 memcached_return
memcached_connect(memcached_st
*ptr
, unsigned int server_key
)
186 memcached_return rc
= MEMCACHED_NO_SERVERS
;
187 LIBMEMCACHED_MEMCACHED_CONNECT_START();
189 if (ptr
->connected
== ptr
->number_of_hosts
&& ptr
->number_of_hosts
)
190 return MEMCACHED_SUCCESS
;
192 if (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
193 return MEMCACHED_NO_SERVERS
;
195 /* We need to clean up the multi startup piece */
198 rc
= tcp_connect(ptr
, server_key
);
199 switch (ptr
->hosts
[server_key
].type
)
201 case MEMCACHED_CONNECTION_UNKNOWN
:
202 WATCHPOINT_ASSERT(0);
203 rc
= MEMCACHED_NOT_SUPPORTED
;
205 case MEMCACHED_CONNECTION_UDP
:
206 rc
= udp_connect(ptr
, server_key
);
208 case MEMCACHED_CONNECTION_TCP
:
209 rc
= tcp_connect(ptr
, server_key
);
211 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
212 rc
= unix_socket_connect(ptr
, server_key
);
216 if (rc
!= MEMCACHED_SUCCESS
)
217 WATCHPOINT_ERROR(rc
);
223 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
225 memcached_return possible_rc
;
227 possible_rc
= MEMCACHED_NOT_SUPPORTED
; /* Remove warning */
229 switch (ptr
->hosts
[x
].type
)
231 case MEMCACHED_CONNECTION_UNKNOWN
:
232 WATCHPOINT_ASSERT(0);
233 possible_rc
= MEMCACHED_NOT_SUPPORTED
;
235 case MEMCACHED_CONNECTION_UDP
:
236 possible_rc
= udp_connect(ptr
, x
);
238 case MEMCACHED_CONNECTION_TCP
:
239 possible_rc
= tcp_connect(ptr
, x
);
241 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
242 possible_rc
= unix_socket_connect(ptr
, x
);
245 rc
= MEMCACHED_SUCCESS
;
247 if (possible_rc
!= MEMCACHED_SUCCESS
)
249 WATCHPOINT_ERROR(possible_rc
);
250 rc
= MEMCACHED_SOME_ERRORS
;
254 LIBMEMCACHED_MEMCACHED_CONNECT_END();