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
) * (ptr
->number_of_hosts
+1));
13 memset(ptr
->hosts
, 0, sizeof(memcached_host_st
) * (size_t)ptr
->number_of_hosts
);
14 ptr
->hosts
[ptr
->number_of_hosts
].hostname
=
15 (char *)malloc(sizeof(char) * (strlen(hostname
)+1));
16 memset(ptr
->hosts
[ptr
->number_of_hosts
].hostname
, 0, strlen(hostname
)+1);
17 memcpy(ptr
->hosts
[ptr
->number_of_hosts
].hostname
, hostname
, strlen(hostname
));
18 ptr
->hosts
[ptr
->number_of_hosts
].port
= port
;
19 ptr
->hosts
[ptr
->number_of_hosts
].fd
= -1;
20 ptr
->number_of_hosts
++;
22 return MEMCACHED_SUCCESS
;
25 memcached_return
memcached_connect(memcached_st
*ptr
)
28 struct sockaddr_in localAddr
, servAddr
;
30 memcached_host_st
*host_ptr
;
33 return MEMCACHED_SUCCESS
;
38 rc
= memcached_server_add(ptr
, NULL
, 0);
40 if (rc
!= MEMCACHED_SUCCESS
)
45 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
47 if ((h
= gethostbyname(ptr
->hosts
[x
].hostname
)) == NULL
)
49 fprintf(stderr
, "unknown host '%s'\n", ptr
->hosts
[x
].hostname
);
50 return MEMCACHED_HOST_LOCKUP_FAILURE
;
53 servAddr
.sin_family
= h
->h_addrtype
;
54 memcpy((char *) &servAddr
.sin_addr
.s_addr
, h
->h_addr_list
[0], h
->h_length
);
55 servAddr
.sin_port
= htons(ptr
->hosts
[x
].port
);
57 /* Create the socket */
58 if ((ptr
->hosts
[0].fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
60 fprintf(stderr
, "cannot open socket");
61 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
65 /* bind any port number */
66 localAddr
.sin_family
= AF_INET
;
67 localAddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
68 localAddr
.sin_port
= htons(0);
70 if (bind(ptr
->hosts
[0].fd
, (struct sockaddr
*) &localAddr
, sizeof(localAddr
)) < 0)
72 fprintf(stderr
, "cannot bind port TCP %u\n", ptr
->hosts
[x
].port
);
73 return(MEMCACHED_CONNECTION_BIND_FAILURE
);
76 /* connect to server */
77 if (connect(ptr
->hosts
[0].fd
, (struct sockaddr
*) &servAddr
, sizeof(servAddr
)) < 0)
79 fprintf(stderr
, "cannot connect to host '%s' (%u) (error: %s)\n", ptr
->hosts
[x
].hostname
,
82 return MEMCACHED_HOST_LOCKUP_FAILURE
;
88 return MEMCACHED_SUCCESS
;