3 static memcached_return
set_hostinfo(memcached_server_st
*server
)
8 char str_port
[NI_MAXSERV
];
10 sprintf(str_port
, "%u", server
->port
);
12 memset(&hints
, 0, sizeof(hints
));
14 hints
.ai_family
= AF_INET
;
15 if (server
->type
== MEMCACHED_CONNECTION_UDP
)
17 hints
.ai_protocol
= IPPROTO_UDP
;
18 hints
.ai_socktype
= SOCK_DGRAM
;
22 hints
.ai_socktype
= SOCK_STREAM
;
23 hints
.ai_protocol
= IPPROTO_TCP
;
26 e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &ai
);
29 WATCHPOINT_STRING(server
->hostname
);
30 WATCHPOINT_STRING(gai_strerror(e
));
31 return MEMCACHED_HOST_LOOKUP_FAILURE
;
34 if (server
->address_info
)
35 freeaddrinfo(server
->address_info
);
36 server
->address_info
= ai
;
38 return MEMCACHED_SUCCESS
;
41 static memcached_return
unix_socket_connect(memcached_server_st
*ptr
)
43 struct sockaddr_un servAddr
;
48 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
50 ptr
->cached_errno
= errno
;
51 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
54 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
55 servAddr
.sun_family
= AF_UNIX
;
56 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
58 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
62 (struct sockaddr
*)&servAddr
,
63 sizeof(servAddr
)) < 0)
66 /* We are spinning waiting on connect */
71 case EISCONN
: /* We were spinning waiting on connect */
74 WATCHPOINT_ERRNO(errno
);
75 ptr
->cached_errno
= errno
;
76 return MEMCACHED_ERRNO
;
80 return MEMCACHED_SUCCESS
;
83 static memcached_return
network_connect(memcached_server_st
*ptr
)
89 /* Old connection junk still is in the structure */
90 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
92 if (ptr
->sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
93 (!(ptr
->root
->flags
& MEM_USE_CACHE_LOOKUPS
)))
97 rc
= set_hostinfo(ptr
);
98 if (rc
!= MEMCACHED_SUCCESS
)
100 ptr
->sockaddr_inited
= MEMCACHED_ALLOCATED
;
102 use
= ptr
->address_info
;
104 /* Create the socket */
105 if ((ptr
->fd
= socket(use
->ai_family
,
107 use
->ai_protocol
)) < 0)
109 ptr
->cached_errno
= errno
;
110 WATCHPOINT_ERRNO(errno
);
111 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
114 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
115 return MEMCACHED_SUCCESS
;
117 if (ptr
->root
->flags
& MEM_NO_BLOCK
)
120 struct linger linger
;
121 struct timeval waittime
;
127 linger
.l_linger
= MEMCACHED_DEFAULT_TIMEOUT
;
128 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
129 &linger
, (socklen_t
)sizeof(struct linger
));
130 WATCHPOINT_ASSERT(error
== 0);
132 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
133 &waittime
, (socklen_t
)sizeof(struct timeval
));
134 WATCHPOINT_ASSERT(error
== 0);
136 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
137 &waittime
, (socklen_t
)sizeof(struct timeval
));
138 WATCHPOINT_ASSERT(error
== 0);
141 if (ptr
->root
->flags
& MEM_TCP_NODELAY
)
146 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
147 &flag
, (socklen_t
)sizeof(int));
148 WATCHPOINT_ASSERT(error
== 0);
151 if (ptr
->root
->send_size
)
155 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
156 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
157 WATCHPOINT_ASSERT(error
== 0);
160 if (ptr
->root
->recv_size
)
164 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
165 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
166 WATCHPOINT_ASSERT(error
== 0);
169 /* For the moment, not getting a nonblocking mode will not be fatal */
170 if (ptr
->root
->flags
& MEM_NO_BLOCK
)
174 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
177 (void)fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
182 /* connect to server */
186 use
->ai_addrlen
) < 0)
189 /* We are spinning waiting on connect */
194 case EISCONN
: /* We were spinning waiting on connect */
197 ptr
->cached_errno
= errno
;
198 WATCHPOINT_ERRNO(ptr
->cached_errno
);
201 return MEMCACHED_ERRNO
;
205 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
208 return MEMCACHED_SUCCESS
;
212 memcached_return
memcached_connect(memcached_server_st
*ptr
)
214 memcached_return rc
= MEMCACHED_NO_SERVERS
;
215 LIBMEMCACHED_MEMCACHED_CONNECT_START();
217 /* We need to clean up the multi startup piece */
220 case MEMCACHED_CONNECTION_UNKNOWN
:
221 WATCHPOINT_ASSERT(0);
222 rc
= MEMCACHED_NOT_SUPPORTED
;
224 case MEMCACHED_CONNECTION_UDP
:
225 case MEMCACHED_CONNECTION_TCP
:
226 rc
= network_connect(ptr
);
228 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
229 rc
= unix_socket_connect(ptr
);
232 WATCHPOINT_ASSERT(0);
235 if (rc
!= MEMCACHED_SUCCESS
)
236 WATCHPOINT_ERROR(rc
);
238 LIBMEMCACHED_MEMCACHED_CONNECT_END();