3 memcached_return
memcached_server_add(memcached_st
*ptr
, char *hostname
, unsigned int port
)
5 memcached_host_st
*host_ptr
, *prev_ptr
;
8 port
= MEMCACHED_DEFAULT_PORT
;
11 hostname
= "localhost";
15 for (host_ptr
= ptr
->hosts
; host_ptr
; host_ptr
= host_ptr
->next
)
17 host_ptr
= (memcached_host_st
*)malloc(sizeof(memcached_host_st
));
19 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
20 prev_ptr
->next
= host_ptr
;
25 host_ptr
= (memcached_host_st
*)malloc(sizeof(memcached_host_st
));
27 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
30 memset(host_ptr
, 0, sizeof(memcached_host_st
));
31 host_ptr
->hostname
= (char *)malloc(sizeof(char) * strlen(hostname
));
33 if (!host_ptr
->hostname
)
36 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
38 memcpy(host_ptr
->hostname
, hostname
, strlen(hostname
));
41 return MEMCACHED_SUCCESS
;
44 memcached_return
memcached_connect(memcached_st
*ptr
)
46 struct sockaddr_in localAddr
, servAddr
;
48 memcached_host_st
*host_ptr
;
51 return MEMCACHED_SUCCESS
;
56 rc
= memcached_server_add(ptr
, NULL
, 0);
58 if (rc
!= MEMCACHED_SUCCESS
)
63 for (host_ptr
= ptr
->hosts
; host_ptr
; host_ptr
= host_ptr
->next
)
65 if ((h
= gethostbyname(host_ptr
->hostname
)) == NULL
)
67 fprintf(stderr
, "unknown host '%s'\n", host_ptr
->hostname
);
68 return MEMCACHED_HOST_LOCKUP_FAILURE
;
71 servAddr
.sin_family
= h
->h_addrtype
;
72 memcpy((char *) &servAddr
.sin_addr
.s_addr
, h
->h_addr_list
[0], h
->h_length
);
73 servAddr
.sin_port
= htons(host_ptr
->port
);
75 /* Create the socket */
76 if ((ptr
->fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
78 fprintf(stderr
, "cannot open socket");
79 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
83 /* bind any port number */
84 localAddr
.sin_family
= AF_INET
;
85 localAddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
86 localAddr
.sin_port
= htons(0);
88 if (bind(ptr
->fd
, (struct sockaddr
*) &localAddr
, sizeof(localAddr
)) < 0)
90 fprintf(stderr
, "cannot bind port TCP %u\n", host_ptr
->port
);
91 return(MEMCACHED_CONNECTION_BIND_FAILURE
);
94 /* connect to server */
95 if (connect(ptr
->fd
, (struct sockaddr
*) &servAddr
, sizeof(servAddr
)) < 0)
97 fprintf(stderr
, "cannot connect to host '%s' (%u) (error: %s)\n", host_ptr
->hostname
,
100 return MEMCACHED_HOST_LOCKUP_FAILURE
;
106 return MEMCACHED_SUCCESS
;