Added support for UNIX sockets.
[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 <sys/un.h>
7 #include <netinet/tcp.h>
8 #include <netdb.h>
9
10 static memcached_return unix_socket_connect(memcached_st *ptr, unsigned int server_key)
11 {
12 struct sockaddr_un servAddr;
13 socklen_t addrlen;
14
15 if (ptr->hosts[server_key].fd == -1)
16 {
17 if ((ptr->hosts[server_key].fd= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
18 {
19 ptr->my_errno= errno;
20 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
21 }
22
23 memset(&servAddr, 0, sizeof (struct sockaddr_un));
24 servAddr.sun_family= AF_UNIX;
25 strcpy(servAddr.sun_path, ptr->hosts[server_key].hostname);
26
27 addrlen= strlen(servAddr.sun_path) + sizeof(servAddr.sun_family);
28
29 test_connect:
30 if (connect(ptr->hosts[server_key].fd, (struct sockaddr_un *)&servAddr, sizeof(servAddr)) < 0)
31 {
32 switch (errno) {
33 /* We are spinning waiting on connect */
34 case EALREADY:
35 case EINPROGRESS:
36 case EINTR:
37 goto test_connect;
38 case EISCONN: /* We were spinning waiting on connect */
39 break;
40 default:
41 ptr->my_errno= errno;
42 return MEMCACHED_ERRNO;
43 }
44 ptr->connected++;
45 }
46 }
47 return MEMCACHED_SUCCESS;
48 }
49
50 static memcached_return tcp_connect(memcached_st *ptr, unsigned int server_key)
51 {
52 struct sockaddr_in servAddr;
53 struct hostent *h;
54
55 if (ptr->hosts[server_key].fd == -1)
56 {
57 /* Old connection junk still is in the structure */
58 WATCHPOINT_ASSERT(ptr->hosts[server_key].stack_responses == 0);
59
60 if ((h= gethostbyname(ptr->hosts[server_key].hostname)) == NULL)
61 {
62 ptr->my_errno= h_errno;
63 return MEMCACHED_HOST_LOCKUP_FAILURE;
64 }
65
66 servAddr.sin_family= h->h_addrtype;
67 memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
68 servAddr.sin_port = htons(ptr->hosts[server_key].port);
69
70 /* Create the socket */
71 if ((ptr->hosts[server_key].fd= socket(AF_INET, SOCK_STREAM, 0)) < 0)
72 {
73 ptr->my_errno= errno;
74 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
75 }
76
77 /* For the moment, not getting a nonblocking mode will note be fatal */
78 if (ptr->flags & MEM_NO_BLOCK)
79 {
80 int flags;
81
82 flags= fcntl(ptr->hosts[server_key].fd, F_GETFL, 0);
83 if (flags != -1)
84 (void)fcntl(ptr->hosts[server_key].fd, F_SETFL, flags | O_NONBLOCK);
85 }
86
87 if (ptr->flags & MEM_TCP_NODELAY)
88 {
89 int flag= 1;
90
91 setsockopt(ptr->hosts[server_key].fd, IPPROTO_TCP, TCP_NODELAY,
92 &flag, (socklen_t)sizeof(int));
93 }
94
95 if (ptr->send_size)
96 {
97 setsockopt(ptr->hosts[server_key].fd, SOL_SOCKET, SO_SNDBUF,
98 &ptr->send_size, (socklen_t)sizeof(int));
99 }
100
101 if (ptr->recv_size)
102 {
103 setsockopt(ptr->hosts[server_key].fd, SOL_SOCKET, SO_SNDBUF,
104 &ptr->recv_size, (socklen_t)sizeof(int));
105 }
106
107 /* connect to server */
108 test_connect:
109 if (connect(ptr->hosts[server_key].fd, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0)
110 {
111 switch (errno) {
112 /* We are spinning waiting on connect */
113 case EALREADY:
114 case EINPROGRESS:
115 case EINTR:
116 goto test_connect;
117 case EISCONN: /* We were spinning waiting on connect */
118 break;
119 default:
120 ptr->my_errno= errno;
121 return MEMCACHED_ERRNO;
122 }
123 ptr->connected++;
124 }
125 WATCHPOINT_ASSERT(ptr->hosts[server_key].stack_responses == 0);
126 }
127
128 return MEMCACHED_SUCCESS;
129 }
130
131
132 memcached_return memcached_connect(memcached_st *ptr, unsigned int server_key)
133 {
134 memcached_return rc= MEMCACHED_NO_SERVERS;
135 LIBMEMCACHED_MEMCACHED_CONNECT_START();
136
137 if (ptr->connected == ptr->number_of_hosts)
138 return MEMCACHED_SUCCESS;
139
140 if (!ptr->hosts)
141 return MEMCACHED_NO_SERVERS;
142
143 /* We need to clean up the multi startup piece */
144 if (server_key)
145 rc= tcp_connect(ptr, server_key);
146 else
147 {
148 unsigned int x;
149
150 for (x= 0; x < ptr->number_of_hosts; x++)
151 {
152 memcached_return possible_rc;
153
154 possible_rc= MEMCACHED_NOT_SUPPORTED; /* Remove warning */
155
156 switch (ptr->hosts[x].type)
157 {
158 case MEMCACHED_CONNECTION_UNKNOWN:
159 case MEMCACHED_CONNECTION_UDP:
160 WATCHPOINT_ASSERT(0);
161 possible_rc= MEMCACHED_NOT_SUPPORTED;
162 break;
163 case MEMCACHED_CONNECTION_TCP:
164 possible_rc= tcp_connect(ptr, x);
165 break;
166 case MEMCACHED_CONNECTION_UNIX_SOCKET:
167 possible_rc= unix_socket_connect(ptr, x);
168 break;
169 }
170 rc= MEMCACHED_SUCCESS;
171
172 if (possible_rc != MEMCACHED_SUCCESS)
173 rc= MEMCACHED_SOME_ERRORS;
174 }
175 }
176 LIBMEMCACHED_MEMCACHED_CONNECT_END();
177
178 return rc;
179 }