3 memcached_return
memcached_server_add(memcached_st
*ptr
, char *hostname
, unsigned int port
)
6 port
= MEMCACHED_DEFAULT_PORT
;
12 ptr
->hosts
= (memcached_host_st
*)realloc(ptr
->hosts
, sizeof(memcached_host_st
) * (size_t)ptr
->number_of_hosts
);
13 ptr
->hosts
[ptr
->number_of_hosts
].hostname
=
14 (char *)malloc(sizeof(char) * strlen(hostname
));
15 memcpy(ptr
->hosts
[ptr
->number_of_hosts
].hostname
, hostname
, strlen(hostname
));
16 ptr
->hosts
[ptr
->number_of_hosts
].port
= port
;
17 ptr
->hosts
[ptr
->number_of_hosts
].fd
= -1;
18 ptr
->number_of_hosts
++;
20 return MEMCACHED_SUCCESS
;
23 memcached_return
memcached_connect(memcached_st
*ptr
)
26 struct sockaddr_in localAddr
, servAddr
;
28 memcached_host_st
*host_ptr
;
31 return MEMCACHED_SUCCESS
;
36 rc
= memcached_server_add(ptr
, NULL
, 0);
38 if (rc
!= MEMCACHED_SUCCESS
)
43 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
45 if ((h
= gethostbyname(ptr
->hosts
[x
].hostname
)) == NULL
)
47 fprintf(stderr
, "unknown host '%s'\n", ptr
->hosts
[x
].hostname
);
48 return MEMCACHED_HOST_LOCKUP_FAILURE
;
51 servAddr
.sin_family
= h
->h_addrtype
;
52 memcpy((char *) &servAddr
.sin_addr
.s_addr
, h
->h_addr_list
[0], h
->h_length
);
53 servAddr
.sin_port
= htons(ptr
->hosts
[x
].port
);
55 /* Create the socket */
56 if ((ptr
->hosts
[0].fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
58 fprintf(stderr
, "cannot open socket");
59 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
63 /* bind any port number */
64 localAddr
.sin_family
= AF_INET
;
65 localAddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
66 localAddr
.sin_port
= htons(0);
68 if (bind(ptr
->hosts
[0].fd
, (struct sockaddr
*) &localAddr
, sizeof(localAddr
)) < 0)
70 fprintf(stderr
, "cannot bind port TCP %u\n", ptr
->hosts
[x
].port
);
71 return(MEMCACHED_CONNECTION_BIND_FAILURE
);
74 /* connect to server */
75 if (connect(ptr
->hosts
[0].fd
, (struct sockaddr
*) &servAddr
, sizeof(servAddr
)) < 0)
77 fprintf(stderr
, "cannot connect to host '%s' (%u) (error: %s)\n", ptr
->hosts
[x
].hostname
,
80 return MEMCACHED_HOST_LOCKUP_FAILURE
;
86 return MEMCACHED_SUCCESS
;