Set recv/send sockket sizes.
[awesomized/libmemcached] / lib / memcached_connect.c
1 #include "common.h"
2
3 #include <fcntl.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netinet/tcp.h>
7 #include <netdb.h>
8
9 memcached_return memcached_real_connect(memcached_st *ptr, unsigned int server_key)
10 {
11 struct sockaddr_in localAddr, servAddr;
12 struct hostent *h;
13
14 if (ptr->hosts[server_key].fd == -1)
15 {
16 /* Old connection junk still is in the structure */
17 assert(ptr->hosts[server_key].stack_responses == 0);
18
19 if ((h= gethostbyname(ptr->hosts[server_key].hostname)) == NULL)
20 {
21 ptr->my_errno= h_errno;
22 return MEMCACHED_HOST_LOCKUP_FAILURE;
23 }
24
25 servAddr.sin_family= h->h_addrtype;
26 memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
27 servAddr.sin_port = htons(ptr->hosts[server_key].port);
28
29 /* Create the socket */
30 if ((ptr->hosts[server_key].fd= socket(AF_INET, SOCK_STREAM, 0)) < 0)
31 {
32 ptr->my_errno= errno;
33 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
34 }
35
36 /* bind any port number */
37 localAddr.sin_family = AF_INET;
38 localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
39 localAddr.sin_port = htons(0);
40
41 /* For the moment, not getting a nonblocking mode will note be fatal */
42 if (ptr->flags & MEM_NO_BLOCK)
43 {
44 int flags;
45
46 flags= fcntl(ptr->hosts[server_key].fd, F_GETFL, 0);
47 if (flags != -1)
48 (void)fcntl(ptr->hosts[server_key].fd, F_SETFL, flags | O_NONBLOCK);
49 }
50
51 if (ptr->flags & MEM_TCP_NODELAY)
52 {
53 int flag= 1;
54
55 setsockopt(ptr->hosts[server_key].fd, IPPROTO_TCP, TCP_NODELAY,
56 &flag, (socklen_t)sizeof(int));
57 }
58
59 if (ptr->send_size)
60 {
61 setsockopt(ptr->hosts[server_key].fd, SOL_SOCKET, SO_SNDBUF,
62 &ptr->send_size, (socklen_t)sizeof(int));
63 }
64
65 if (ptr->recv_size)
66 {
67 setsockopt(ptr->hosts[server_key].fd, SOL_SOCKET, SO_SNDBUF,
68 &ptr->recv_size, (socklen_t)sizeof(int));
69 }
70
71 /* connect to server */
72 test_connect:
73 if (connect(ptr->hosts[server_key].fd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
74 {
75 switch (errno) {
76 /* We are spinning waiting on connect */
77 case EALREADY:
78 case EINPROGRESS:
79 case EINTR:
80 goto test_connect;
81 case EISCONN: /* We were spinning waiting on connect */
82 break;
83 default:
84 ptr->my_errno= errno;
85 return MEMCACHED_ERRNO;
86 }
87 ptr->connected++;
88 }
89 assert(ptr->hosts[server_key].stack_responses == 0);
90 }
91
92 return MEMCACHED_SUCCESS;
93 }
94
95
96 memcached_return memcached_connect(memcached_st *ptr, unsigned int server_key)
97 {
98 memcached_return rc;
99 LIBMEMCACHED_MEMCACHED_CONNECT_START();
100
101 if (ptr->connected == ptr->number_of_hosts)
102 return MEMCACHED_SUCCESS;
103
104 if (!ptr->hosts)
105 return MEMCACHED_NO_SERVERS;
106
107 /* We need to clean up the multi startup piece */
108 if (server_key)
109 rc= memcached_real_connect(ptr, server_key);
110 else
111 {
112 unsigned int x;
113
114 for (x= 0; x < ptr->number_of_hosts; x++)
115 rc= memcached_real_connect(ptr, x);
116 }
117 LIBMEMCACHED_MEMCACHED_CONNECT_END();
118
119 return rc;
120 }