3 memcached_return
memcached_server_add(memcached_st
*ptr
, char *hostname
, unsigned int port
)
5 memcached_host_st
*new_host_list
;
9 port
= MEMCACHED_DEFAULT_PORT
;
12 hostname
= "localhost";
15 new_host_list
= (memcached_host_st
*)realloc(ptr
->hosts
, sizeof(memcached_host_st
) * (ptr
->number_of_hosts
+1));
17 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
18 memset(&new_host_list
[ptr
->number_of_hosts
], 0, sizeof(memcached_host_st
));
21 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
23 ptr
->hosts
= new_host_list
;
26 (char *)malloc(sizeof(char) * (strlen(hostname
)+1));
28 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
30 memset(new_hostname
, 0, strlen(hostname
)+1);
31 memcpy(new_hostname
, hostname
, strlen(hostname
));
32 ptr
->hosts
[ptr
->number_of_hosts
].hostname
= new_hostname
;
33 ptr
->hosts
[ptr
->number_of_hosts
].port
= port
;
34 ptr
->hosts
[ptr
->number_of_hosts
].fd
= -1;
35 ptr
->number_of_hosts
++;
37 return MEMCACHED_SUCCESS
;
40 memcached_return
memcached_connect(memcached_st
*ptr
)
43 struct sockaddr_in localAddr
, servAddr
;
47 return MEMCACHED_SUCCESS
;
52 rc
= memcached_server_add(ptr
, NULL
, 0);
54 if (rc
!= MEMCACHED_SUCCESS
)
59 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
61 if ((h
= gethostbyname(ptr
->hosts
[x
].hostname
)) == NULL
)
62 return MEMCACHED_HOST_LOCKUP_FAILURE
;
64 servAddr
.sin_family
= h
->h_addrtype
;
65 memcpy((char *) &servAddr
.sin_addr
.s_addr
, h
->h_addr_list
[0], h
->h_length
);
66 servAddr
.sin_port
= htons(ptr
->hosts
[x
].port
);
68 /* Create the socket */
69 if ((ptr
->hosts
[0].fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
70 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
73 /* bind any port number */
74 localAddr
.sin_family
= AF_INET
;
75 localAddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
76 localAddr
.sin_port
= htons(0);
78 if (bind(ptr
->hosts
[0].fd
, (struct sockaddr
*) &localAddr
, sizeof(localAddr
)) < 0)
79 return(MEMCACHED_CONNECTION_BIND_FAILURE
);
81 /* connect to server */
82 if (connect(ptr
->hosts
[0].fd
, (struct sockaddr
*) &servAddr
, sizeof(servAddr
)) < 0)
83 return MEMCACHED_HOST_LOCKUP_FAILURE
;
88 return MEMCACHED_SUCCESS
;