5 #include <sys/socket.h>
7 #include <netinet/tcp.h>
10 static memcached_return
unix_socket_connect(memcached_st
*ptr
, unsigned int server_key
)
12 struct sockaddr_un servAddr
;
15 if (ptr
->hosts
[server_key
].fd
== -1)
17 if ((ptr
->hosts
[server_key
].fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
19 ptr
->cached_errno
= errno
;
20 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
23 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
24 servAddr
.sun_family
= AF_UNIX
;
25 strcpy(servAddr
.sun_path
, ptr
->hosts
[server_key
].hostname
);
27 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
30 if (connect(ptr
->hosts
[server_key
].fd
, (struct sockaddr
*)&servAddr
, sizeof(servAddr
)) < 0)
33 /* We are spinning waiting on connect */
38 case EISCONN
: /* We were spinning waiting on connect */
41 ptr
->cached_errno
= errno
;
42 return MEMCACHED_ERRNO
;
47 return MEMCACHED_SUCCESS
;
50 static memcached_return
tcp_connect(memcached_st
*ptr
, unsigned int server_key
)
52 struct sockaddr_in servAddr
;
55 if (ptr
->hosts
[server_key
].fd
== -1)
57 /* Old connection junk still is in the structure */
58 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].stack_responses
== 0);
60 if ((h
= gethostbyname(ptr
->hosts
[server_key
].hostname
)) == NULL
)
62 ptr
->cached_errno
= h_errno
;
63 return MEMCACHED_HOST_LOCKUP_FAILURE
;
66 servAddr
.sin_family
= h
->h_addrtype
;
67 memcpy((char *) &servAddr
.sin_addr
.s_addr
, h
->h_addr_list
[0], h
->h_length
);
68 servAddr
.sin_port
= htons(ptr
->hosts
[server_key
].port
);
70 /* Create the socket */
71 if ((ptr
->hosts
[server_key
].fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
73 ptr
->cached_errno
= errno
;
74 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
77 /* For the moment, not getting a nonblocking mode will note be fatal */
78 if (ptr
->flags
& MEM_NO_BLOCK
)
82 flags
= fcntl(ptr
->hosts
[server_key
].fd
, F_GETFL
, 0);
84 (void)fcntl(ptr
->hosts
[server_key
].fd
, F_SETFL
, flags
| O_NONBLOCK
);
87 if (ptr
->flags
& MEM_TCP_NODELAY
)
91 setsockopt(ptr
->hosts
[server_key
].fd
, IPPROTO_TCP
, TCP_NODELAY
,
92 &flag
, (socklen_t
)sizeof(int));
97 setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_SNDBUF
,
98 &ptr
->send_size
, (socklen_t
)sizeof(int));
103 setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_SNDBUF
,
104 &ptr
->recv_size
, (socklen_t
)sizeof(int));
107 /* connect to server */
109 if (connect(ptr
->hosts
[server_key
].fd
, (struct sockaddr
*)&servAddr
, sizeof(servAddr
)) < 0)
112 /* We are spinning waiting on connect */
117 case EISCONN
: /* We were spinning waiting on connect */
120 ptr
->cached_errno
= errno
;
121 return MEMCACHED_ERRNO
;
125 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].stack_responses
== 0);
128 return MEMCACHED_SUCCESS
;
132 memcached_return
memcached_connect(memcached_st
*ptr
, unsigned int server_key
)
134 memcached_return rc
= MEMCACHED_NO_SERVERS
;
135 LIBMEMCACHED_MEMCACHED_CONNECT_START();
137 if (ptr
->connected
== ptr
->number_of_hosts
&& ptr
->number_of_hosts
)
138 return MEMCACHED_SUCCESS
;
140 if (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
141 return MEMCACHED_NO_SERVERS
;
143 /* We need to clean up the multi startup piece */
145 rc
= tcp_connect(ptr
, server_key
);
150 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
152 memcached_return possible_rc
;
154 possible_rc
= MEMCACHED_NOT_SUPPORTED
; /* Remove warning */
156 switch (ptr
->hosts
[x
].type
)
158 case MEMCACHED_CONNECTION_UNKNOWN
:
159 case MEMCACHED_CONNECTION_UDP
:
160 WATCHPOINT_ASSERT(0);
161 possible_rc
= MEMCACHED_NOT_SUPPORTED
;
163 case MEMCACHED_CONNECTION_TCP
:
164 possible_rc
= tcp_connect(ptr
, x
);
166 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
167 possible_rc
= unix_socket_connect(ptr
, x
);
170 rc
= MEMCACHED_SUCCESS
;
172 if (possible_rc
!= MEMCACHED_SUCCESS
)
173 rc
= MEMCACHED_SOME_ERRORS
;
176 LIBMEMCACHED_MEMCACHED_CONNECT_END();