5 #include <sys/socket.h>
7 #include <netinet/tcp.h>
9 #include <netinet/in.h>
11 static memcached_return
set_hostinfo(memcached_server_st
*server
)
14 struct addrinfo hints
;
16 char str_port
[NI_MAXSERV
];
18 sprintf(str_port
, "%u", server
->port
);
20 memset(&hints
, 0, sizeof(hints
));
21 hints
.ai_family
= AF_INET
;
22 hints
.ai_socktype
= SOCK_STREAM
;
25 e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &ai
);
28 WATCHPOINT_STRING(server
->hostname
);
29 WATCHPOINT_STRING(gai_strerror(e
));
30 return MEMCACHED_HOST_LOOKUP_FAILURE
;
33 if (server
->address_info
)
34 freeaddrinfo(server
->address_info
);
35 server
->address_info
= ai
;
37 return MEMCACHED_SUCCESS
;
40 static memcached_return
unix_socket_connect(memcached_st
*ptr
, unsigned int server_key
)
42 struct sockaddr_un servAddr
;
45 if (ptr
->hosts
[server_key
].fd
== -1)
47 if ((ptr
->hosts
[server_key
].fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
49 ptr
->cached_errno
= errno
;
50 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
53 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
54 servAddr
.sun_family
= AF_UNIX
;
55 strcpy(servAddr
.sun_path
, ptr
->hosts
[server_key
].hostname
); /* Copy filename */
57 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
60 if (connect(ptr
->hosts
[server_key
].fd
,
61 (struct sockaddr
*)&servAddr
,
62 sizeof(servAddr
)) < 0)
65 /* We are spinning waiting on connect */
70 case EISCONN
: /* We were spinning waiting on connect */
73 WATCHPOINT_ERRNO(errno
);
74 ptr
->cached_errno
= errno
;
75 return MEMCACHED_ERRNO
;
80 return MEMCACHED_SUCCESS
;
83 static memcached_return
udp_connect(memcached_st
*ptr
, unsigned int server_key
)
85 if (ptr
->hosts
[server_key
].fd
== -1)
87 /* Old connection junk still is in the structure */
88 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].stack_responses
== 0);
91 If we have not allocated the hosts object.
92 Or if the cache has not been set.
94 if (ptr
->hosts
[server_key
].sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
95 (!(ptr
->flags
& MEM_USE_CACHE_LOOKUPS
)))
99 rc
= set_hostinfo(&ptr
->hosts
[server_key
]);
100 if (rc
!= MEMCACHED_SUCCESS
)
103 ptr
->hosts
[server_key
].sockaddr_inited
= MEMCACHED_ALLOCATED
;
106 /* Create the socket */
107 if ((ptr
->hosts
[server_key
].fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
109 ptr
->cached_errno
= errno
;
110 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
114 return MEMCACHED_SUCCESS
;
117 static memcached_return
tcp_connect(memcached_st
*ptr
, unsigned int server_key
)
119 if (ptr
->hosts
[server_key
].fd
== -1)
121 /* Old connection junk still is in the structure */
122 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].stack_responses
== 0);
123 struct addrinfo
*use
;
125 if (ptr
->hosts
[server_key
].sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
126 (!(ptr
->flags
& MEM_USE_CACHE_LOOKUPS
)))
130 rc
= set_hostinfo(&ptr
->hosts
[server_key
]);
131 if (rc
!= MEMCACHED_SUCCESS
)
133 ptr
->hosts
[server_key
].sockaddr_inited
= MEMCACHED_ALLOCATED
;
135 use
= ptr
->hosts
[server_key
].address_info
;
137 /* Create the socket */
138 if ((ptr
->hosts
[server_key
].fd
= socket(use
->ai_family
,
140 use
->ai_protocol
)) < 0)
142 ptr
->cached_errno
= errno
;
143 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
146 /* For the moment, not getting a nonblocking mode will note be fatal */
147 if (ptr
->flags
& MEM_NO_BLOCK
)
151 flags
= fcntl(ptr
->hosts
[server_key
].fd
, F_GETFL
, 0);
153 (void)fcntl(ptr
->hosts
[server_key
].fd
, F_SETFL
, flags
| O_NONBLOCK
);
156 if (ptr
->flags
& MEM_TCP_NODELAY
)
160 setsockopt(ptr
->hosts
[server_key
].fd
, IPPROTO_TCP
, TCP_NODELAY
,
161 &flag
, (socklen_t
)sizeof(int));
166 setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_SNDBUF
,
167 &ptr
->send_size
, (socklen_t
)sizeof(int));
172 setsockopt(ptr
->hosts
[server_key
].fd
, SOL_SOCKET
, SO_SNDBUF
,
173 &ptr
->recv_size
, (socklen_t
)sizeof(int));
176 /* connect to server */
178 if (connect(ptr
->hosts
[server_key
].fd
,
180 use
->ai_addrlen
) < 0)
183 /* We are spinning waiting on connect */
188 case EISCONN
: /* We were spinning waiting on connect */
191 ptr
->cached_errno
= errno
;
192 WATCHPOINT_ERRNO(ptr
->cached_errno
);
193 return MEMCACHED_ERRNO
;
197 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].stack_responses
== 0);
200 return MEMCACHED_SUCCESS
;
204 memcached_return
memcached_connect(memcached_st
*ptr
, unsigned int server_key
)
206 memcached_return rc
= MEMCACHED_NO_SERVERS
;
207 LIBMEMCACHED_MEMCACHED_CONNECT_START();
209 if (ptr
->connected
== ptr
->number_of_hosts
&& ptr
->number_of_hosts
)
210 return MEMCACHED_SUCCESS
;
212 if (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
213 return MEMCACHED_NO_SERVERS
;
215 /* We need to clean up the multi startup piece */
216 switch (ptr
->hosts
[server_key
].type
)
218 case MEMCACHED_CONNECTION_UNKNOWN
:
219 WATCHPOINT_ASSERT(0);
220 rc
= MEMCACHED_NOT_SUPPORTED
;
222 case MEMCACHED_CONNECTION_UDP
:
223 rc
= udp_connect(ptr
, server_key
);
225 case MEMCACHED_CONNECTION_TCP
:
226 rc
= tcp_connect(ptr
, server_key
);
228 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
229 rc
= unix_socket_connect(ptr
, server_key
);
233 if (rc
!= MEMCACHED_SUCCESS
)
234 WATCHPOINT_ERROR(rc
);
236 LIBMEMCACHED_MEMCACHED_CONNECT_END();