5 #include <sys/socket.h>
7 #include <netinet/tcp.h>
10 static memcached_return
set_hostinfo(memcached_server_st
*server
)
13 #ifdef HAVE_GETHOSTBYNAME_R
14 struct hostent h_static
;
15 char buffer
[SMALL_STRING_LEN
];
18 if (gethostbyname_r(server
->hostname
,
19 &h_static
, buffer
, SMALL_STRING_LEN
,
22 WATCHPOINT_STRING(server
->hostname
);
23 WATCHPOINT_STRING(hstrerror(tmp_error
));
24 return MEMCACHED_HOST_LOOKUP_FAILURE
;
27 if ((h
= gethostbyname(server
->hostname
)) == NULL
)
29 WATCHPOINT_STRING(server
->hostname
);
30 WATCHPOINT_STRING(hstrerror(h_errno
));
31 return MEMCACHED_HOST_LOOKUP_FAILURE
;
35 server
->servAddr
.sin_family
= h
->h_addrtype
;
36 memcpy((char *) &server
->servAddr
.sin_addr
.s_addr
, h
->h_addr_list
[0], h
->h_length
);
38 server
->servAddr
.sin_port
= htons(server
->port
);
40 return MEMCACHED_SUCCESS
;
43 static memcached_return
unix_socket_connect(memcached_st
*ptr
, unsigned int server_key
)
45 struct sockaddr_un servAddr
;
48 if (ptr
->hosts
[server_key
].fd
== -1)
50 if ((ptr
->hosts
[server_key
].fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
52 ptr
->cached_errno
= errno
;
53 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
56 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
57 servAddr
.sun_family
= AF_UNIX
;
58 strcpy(servAddr
.sun_path
, ptr
->hosts
[server_key
].hostname
); /* Copy filename */
60 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
63 if (connect(ptr
->hosts
[server_key
].fd
,
64 (struct sockaddr
*)&servAddr
,
65 sizeof(servAddr
)) < 0)
68 /* We are spinning waiting on connect */
73 case EISCONN
: /* We were spinning waiting on connect */
76 ptr
->cached_errno
= errno
;
77 return MEMCACHED_ERRNO
;
82 return MEMCACHED_SUCCESS
;
85 static memcached_return
udp_connect(memcached_st
*ptr
, unsigned int server_key
)
87 if (ptr
->hosts
[server_key
].fd
== -1)
89 /* Old connection junk still is in the structure */
90 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].stack_responses
== 0);
93 If we have not allocated the hosts object.
94 Or if the cache has not been set.
96 if (ptr
->hosts
[server_key
].sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
97 (!(ptr
->flags
& MEM_USE_CACHE_LOOKUPS
)))
101 rc
= set_hostinfo(&ptr
->hosts
[server_key
]);
102 if (rc
!= MEMCACHED_SUCCESS
)
105 ptr
->hosts
[server_key
].sockaddr_inited
= MEMCACHED_ALLOCATED
;
108 /* Create the socket */
109 if ((ptr
->hosts
[server_key
].fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
111 ptr
->cached_errno
= errno
;
112 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
116 return MEMCACHED_SUCCESS
;
119 static memcached_return
tcp_connect(memcached_st
*ptr
, unsigned int server_key
)
121 if (ptr
->hosts
[server_key
].fd
== -1)
123 /* Old connection junk still is in the structure */
124 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].stack_responses
== 0);
126 if (ptr
->hosts
[server_key
].sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
127 (!(ptr
->flags
& MEM_USE_CACHE_LOOKUPS
)))
131 rc
= set_hostinfo(&ptr
->hosts
[server_key
]);
132 if (rc
!= MEMCACHED_SUCCESS
)
134 ptr
->hosts
[server_key
].sockaddr_inited
= MEMCACHED_ALLOCATED
;
137 /* Create the socket */
138 if ((ptr
->hosts
[server_key
].fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
140 ptr
->cached_errno
= errno
;
141 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
144 /* For the moment, not getting a nonblocking mode will note be fatal */
145 if (ptr
->flags
& MEM_NO_BLOCK
)
149 flags
= fcntl(ptr
->hosts
[server_key
].fd
, F_GETFL
, 0);
151 (void)fcntl(ptr
->hosts
[server_key
].fd
, F_SETFL
, flags
| O_NONBLOCK
);
154 if (ptr
->flags
& MEM_TCP_NODELAY
)
158 setsockopt(ptr
->hosts
[server_key
].fd
, IPPROTO_TCP
, TCP_NODELAY
,
159 &flag
, (socklen_t
)sizeof(int));
164 setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_SNDBUF
,
165 &ptr
->send_size
, (socklen_t
)sizeof(int));
170 setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_SNDBUF
,
171 &ptr
->recv_size
, (socklen_t
)sizeof(int));
174 /* connect to server */
176 if (connect(ptr
->hosts
[server_key
].fd
,
177 (struct sockaddr
*)&ptr
->hosts
[server_key
].servAddr
,
178 sizeof(struct sockaddr
)) < 0)
181 /* We are spinning waiting on connect */
186 case EISCONN
: /* We were spinning waiting on connect */
189 ptr
->cached_errno
= errno
;
190 return MEMCACHED_ERRNO
;
194 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].stack_responses
== 0);
197 return MEMCACHED_SUCCESS
;
201 memcached_return
memcached_connect(memcached_st
*ptr
, unsigned int server_key
)
203 memcached_return rc
= MEMCACHED_NO_SERVERS
;
204 LIBMEMCACHED_MEMCACHED_CONNECT_START();
206 if (ptr
->connected
== ptr
->number_of_hosts
&& ptr
->number_of_hosts
)
207 return MEMCACHED_SUCCESS
;
209 if (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
210 return MEMCACHED_NO_SERVERS
;
212 /* We need to clean up the multi startup piece */
213 switch (ptr
->hosts
[server_key
].type
)
215 case MEMCACHED_CONNECTION_UNKNOWN
:
216 WATCHPOINT_ASSERT(0);
217 rc
= MEMCACHED_NOT_SUPPORTED
;
219 case MEMCACHED_CONNECTION_UDP
:
220 rc
= udp_connect(ptr
, server_key
);
222 case MEMCACHED_CONNECTION_TCP
:
223 rc
= tcp_connect(ptr
, server_key
);
225 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
226 rc
= unix_socket_connect(ptr
, server_key
);
230 if (rc
!= MEMCACHED_SUCCESS
)
231 WATCHPOINT_ERROR(rc
);
233 LIBMEMCACHED_MEMCACHED_CONNECT_END();