5 memcached_return
memcached_connect(memcached_st
*ptr
)
8 struct sockaddr_in localAddr
, servAddr
;
11 LIBMEMCACHED_MEMCACHED_CONNECT_START();
13 if (ptr
->connected
== ptr
->number_of_hosts
)
14 return MEMCACHED_SUCCESS
;
17 return MEMCACHED_NO_SERVERS
;
19 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
21 if (ptr
->hosts
[x
].fd
== -1)
25 if ((h
= gethostbyname(ptr
->hosts
[x
].hostname
)) == NULL
)
26 return MEMCACHED_HOST_LOCKUP_FAILURE
;
28 servAddr
.sin_family
= h
->h_addrtype
;
29 memcpy((char *) &servAddr
.sin_addr
.s_addr
, h
->h_addr_list
[0], h
->h_length
);
30 servAddr
.sin_port
= htons(ptr
->hosts
[x
].port
);
32 /* Create the socket */
33 if ((ptr
->hosts
[0].fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
36 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
40 /* bind any port number */
41 localAddr
.sin_family
= AF_INET
;
42 localAddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
43 localAddr
.sin_port
= htons(0);
45 /* For the moment, not getting a nonblocking mode will note be fatal */
46 if (ptr
->flags
& MEM_NO_BLOCK
)
48 flags
= fcntl(ptr
->hosts
[0].fd
, F_GETFL
, 0);
50 (void)fcntl(ptr
->hosts
[0].fd
, F_SETFL
, flags
| O_NONBLOCK
);
53 /* connect to server */
55 if (connect(ptr
->hosts
[0].fd
, (struct sockaddr
*) &servAddr
, sizeof(servAddr
)) < 0)
58 /* We are spinning waiting on connect */
62 case EISCONN
: /* We were spinning waiting on connect */
66 return MEMCACHED_HOST_LOCKUP_FAILURE
;
73 LIBMEMCACHED_MEMCACHED_CONNECT_END();
75 return MEMCACHED_SUCCESS
;