s4-auth: Make sure error_string is correctly initialized
[Samba.git] / ctdb / tcp / tcp_connect.c
blobb106f22c86fa51dae72428e03696f3a8881fd29c
1 /*
2 ctdb over TCP
4 Copyright (C) Andrew Tridgell 2006
5 Copyright (C) Ronnie Sahlberg 2008
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "tdb.h"
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "../include/ctdb_private.h"
26 #include "ctdb_tcp.h"
29 stop any connecting (established or pending) to a node
31 void ctdb_tcp_stop_connection(struct ctdb_node *node)
33 struct ctdb_tcp_node *tnode = talloc_get_type(
34 node->private_data, struct ctdb_tcp_node);
36 ctdb_queue_set_fd(tnode->out_queue, -1);
37 talloc_free(tnode->connect_te);
38 talloc_free(tnode->connect_fde);
39 tnode->connect_fde = NULL;
40 tnode->connect_te = NULL;
41 if (tnode->fd != -1) {
42 close(tnode->fd);
43 tnode->fd = -1;
49 called when a complete packet has come in - should not happen on this socket
50 unless the other side closes the connection with RST or FIN
52 void ctdb_tcp_tnode_cb(uint8_t *data, size_t cnt, void *private_data)
54 struct ctdb_node *node = talloc_get_type(private_data, struct ctdb_node);
55 struct ctdb_tcp_node *tnode = talloc_get_type(
56 node->private_data, struct ctdb_tcp_node);
58 if (data == NULL) {
59 node->ctdb->upcalls->node_dead(node);
62 ctdb_tcp_stop_connection(node);
63 tnode->connect_te = event_add_timed(node->ctdb->ev, tnode,
64 timeval_current_ofs(3, 0),
65 ctdb_tcp_node_connect, node);
69 called when socket becomes writeable on connect
71 static void ctdb_node_connect_write(struct event_context *ev, struct fd_event *fde,
72 uint16_t flags, void *private_data)
74 struct ctdb_node *node = talloc_get_type(private_data,
75 struct ctdb_node);
76 struct ctdb_tcp_node *tnode = talloc_get_type(node->private_data,
77 struct ctdb_tcp_node);
78 struct ctdb_context *ctdb = node->ctdb;
79 int error = 0;
80 socklen_t len = sizeof(error);
81 int one = 1;
83 talloc_free(tnode->connect_te);
84 tnode->connect_te = NULL;
86 if (getsockopt(tnode->fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0 ||
87 error != 0) {
88 ctdb_tcp_stop_connection(node);
89 tnode->connect_te = event_add_timed(ctdb->ev, tnode,
90 timeval_current_ofs(1, 0),
91 ctdb_tcp_node_connect, node);
92 return;
95 talloc_free(tnode->connect_fde);
96 tnode->connect_fde = NULL;
98 if (setsockopt(tnode->fd,IPPROTO_TCP,TCP_NODELAY,(char *)&one,sizeof(one)) == -1) {
99 DEBUG(DEBUG_WARNING, ("Failed to set TCP_NODELAY on fd - %s\n",
100 strerror(errno)));
102 if (setsockopt(tnode->fd,SOL_SOCKET,SO_KEEPALIVE,(char *)&one,sizeof(one)) == -1) {
103 DEBUG(DEBUG_WARNING, ("Failed to set KEEPALIVE on fd - %s\n",
104 strerror(errno)));
107 ctdb_queue_set_fd(tnode->out_queue, tnode->fd);
109 /* the queue subsystem now owns this fd */
110 tnode->fd = -1;
115 called when we should try and establish a tcp connection to a node
117 void ctdb_tcp_node_connect(struct event_context *ev, struct timed_event *te,
118 struct timeval t, void *private_data)
120 struct ctdb_node *node = talloc_get_type(private_data,
121 struct ctdb_node);
122 struct ctdb_tcp_node *tnode = talloc_get_type(node->private_data,
123 struct ctdb_tcp_node);
124 struct ctdb_context *ctdb = node->ctdb;
125 ctdb_sock_addr sock_in;
126 int sockin_size;
127 int sockout_size;
128 ctdb_sock_addr sock_out;
130 ctdb_tcp_stop_connection(node);
132 sock_out = node->address;
134 tnode->fd = socket(sock_out.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
135 if (tnode->fd == -1) {
136 DEBUG(DEBUG_ERR, (__location__ "Failed to create socket\n"));
137 return;
139 set_nonblocking(tnode->fd);
140 set_close_on_exec(tnode->fd);
142 DEBUG(DEBUG_DEBUG, (__location__ " Created TCP SOCKET FD:%d\n", tnode->fd));
144 /* Bind our side of the socketpair to the same address we use to listen
145 * on incoming CTDB traffic.
146 * We must specify this address to make sure that the address we expose to
147 * the remote side is actually routable in case CTDB traffic will run on
148 * a dedicated non-routeable network.
150 sock_in = *ctdb->address;
152 /* AIX libs check to see if the socket address and length
153 arguments are consistent with each other on calls like
154 connect(). Can not get by with just sizeof(sock_in),
155 need sizeof(sock_in.ip).
157 switch (sock_in.sa.sa_family) {
158 case AF_INET:
159 sock_in.ip.sin_port = 0 /* Any port */;
160 sockin_size = sizeof(sock_in.ip);
161 sockout_size = sizeof(sock_out.ip);
162 break;
163 case AF_INET6:
164 sock_in.ip6.sin6_port = 0 /* Any port */;
165 sockin_size = sizeof(sock_in.ip6);
166 sockout_size = sizeof(sock_out.ip6);
167 break;
168 default:
169 DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
170 sock_in.sa.sa_family));
171 close(tnode->fd);
172 return;
175 if (bind(tnode->fd, (struct sockaddr *)&sock_in, sockin_size) == -1) {
176 DEBUG(DEBUG_ERR, (__location__ "Failed to bind socket %s(%d)\n",
177 strerror(errno), errno));
178 close(tnode->fd);
179 return;
182 if (connect(tnode->fd, (struct sockaddr *)&sock_out, sockout_size) != 0 &&
183 errno != EINPROGRESS) {
184 ctdb_tcp_stop_connection(node);
185 tnode->connect_te = event_add_timed(ctdb->ev, tnode,
186 timeval_current_ofs(1, 0),
187 ctdb_tcp_node_connect, node);
188 return;
191 /* non-blocking connect - wait for write event */
192 tnode->connect_fde = event_add_fd(node->ctdb->ev, tnode, tnode->fd,
193 EVENT_FD_WRITE|EVENT_FD_READ,
194 ctdb_node_connect_write, node);
196 /* don't give it long to connect - retry in one second. This ensures
197 that we find a node is up quickly (tcp normally backs off a syn reply
198 delay by quite a lot) */
199 tnode->connect_te = event_add_timed(ctdb->ev, tnode, timeval_current_ofs(1, 0),
200 ctdb_tcp_node_connect, node);
204 called when we get contacted by another node
205 currently makes no attempt to check if the connection is really from a ctdb
206 node in our cluster
208 static void ctdb_listen_event(struct event_context *ev, struct fd_event *fde,
209 uint16_t flags, void *private_data)
211 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
212 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data, struct ctdb_tcp);
213 ctdb_sock_addr addr;
214 socklen_t len;
215 int fd, nodeid;
216 struct ctdb_incoming *in;
217 int one = 1;
219 memset(&addr, 0, sizeof(addr));
220 len = sizeof(addr);
221 fd = accept(ctcp->listen_fd, (struct sockaddr *)&addr, &len);
222 if (fd == -1) return;
224 nodeid = ctdb_ip_to_nodeid(ctdb, &addr);
226 if (nodeid == -1) {
227 DEBUG(DEBUG_ERR, ("Refused connection from unknown node %s\n", ctdb_addr_to_str(&addr)));
228 close(fd);
229 return;
232 in = talloc_zero(ctcp, struct ctdb_incoming);
233 in->fd = fd;
234 in->ctdb = ctdb;
236 set_nonblocking(in->fd);
237 set_close_on_exec(in->fd);
239 DEBUG(DEBUG_DEBUG, (__location__ " Created SOCKET FD:%d to incoming ctdb connection\n", fd));
241 if (setsockopt(in->fd,SOL_SOCKET,SO_KEEPALIVE,(char *)&one,sizeof(one)) == -1) {
242 DEBUG(DEBUG_WARNING, ("Failed to set KEEPALIVE on fd - %s\n",
243 strerror(errno)));
246 in->queue = ctdb_queue_setup(ctdb, in, in->fd, CTDB_TCP_ALIGNMENT,
247 ctdb_tcp_read_cb, in, "ctdbd-%s", ctdb_addr_to_str(&addr));
252 automatically find which address to listen on
254 static int ctdb_tcp_listen_automatic(struct ctdb_context *ctdb)
256 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
257 struct ctdb_tcp);
258 ctdb_sock_addr sock;
259 int lock_fd, i;
260 const char *lock_path = CTDB_RUNDIR "/.socket_lock";
261 struct flock lock;
262 int one = 1;
263 int sock_size;
264 struct tevent_fd *fde;
266 /* If there are no nodes, then it won't be possible to find
267 * the first one. Log a failure and short circuit the whole
268 * process.
270 if (ctdb->num_nodes == 0) {
271 DEBUG(DEBUG_CRIT,("No nodes available to attempt bind to - is the nodes file empty?\n"));
272 return -1;
275 /* in order to ensure that we don't get two nodes with the
276 same adddress, we must make the bind() and listen() calls
277 atomic. The SO_REUSEADDR setsockopt only prevents double
278 binds if the first socket is in LISTEN state */
279 lock_fd = open(lock_path, O_RDWR|O_CREAT, 0666);
280 if (lock_fd == -1) {
281 DEBUG(DEBUG_CRIT,("Unable to open %s\n", lock_path));
282 return -1;
285 lock.l_type = F_WRLCK;
286 lock.l_whence = SEEK_SET;
287 lock.l_start = 0;
288 lock.l_len = 1;
289 lock.l_pid = 0;
291 if (fcntl(lock_fd, F_SETLKW, &lock) != 0) {
292 DEBUG(DEBUG_CRIT,("Unable to lock %s\n", lock_path));
293 close(lock_fd);
294 return -1;
297 for (i=0; i < ctdb->num_nodes; i++) {
298 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
299 continue;
301 sock = ctdb->nodes[i]->address;
303 switch (sock.sa.sa_family) {
304 case AF_INET:
305 sock_size = sizeof(sock.ip);
306 break;
307 case AF_INET6:
308 sock_size = sizeof(sock.ip6);
309 break;
310 default:
311 DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
312 sock.sa.sa_family));
313 continue;
316 ctcp->listen_fd = socket(sock.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
317 if (ctcp->listen_fd == -1) {
318 ctdb_set_error(ctdb, "socket failed\n");
319 continue;
322 set_close_on_exec(ctcp->listen_fd);
324 if (setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,
325 (char *)&one,sizeof(one)) == -1) {
326 DEBUG(DEBUG_WARNING, ("Failed to set REUSEADDR on fd - %s\n",
327 strerror(errno)));
330 if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sock_size) == 0) {
331 break;
334 if (errno == EADDRNOTAVAIL) {
335 DEBUG(DEBUG_DEBUG,(__location__ " Failed to bind() to socket. %s(%d)\n",
336 strerror(errno), errno));
337 } else {
338 DEBUG(DEBUG_ERR,(__location__ " Failed to bind() to socket. %s(%d)\n",
339 strerror(errno), errno));
343 if (i == ctdb->num_nodes) {
344 DEBUG(DEBUG_CRIT,("Unable to bind to any of the node addresses - giving up\n"));
345 goto failed;
347 ctdb->address = talloc_memdup(ctdb,
348 &ctdb->nodes[i]->address,
349 sizeof(ctdb_sock_addr));
350 if (ctdb->address == NULL) {
351 ctdb_set_error(ctdb, "Out of memory at %s:%d",
352 __FILE__, __LINE__);
353 goto failed;
356 ctdb->name = talloc_asprintf(ctdb, "%s:%u",
357 ctdb_addr_to_str(ctdb->address),
358 ctdb_addr_to_port(ctdb->address));
359 if (ctdb->name == NULL) {
360 ctdb_set_error(ctdb, "Out of memory at %s:%d",
361 __FILE__, __LINE__);
362 goto failed;
364 DEBUG(DEBUG_INFO,("ctdb chose network address %s\n", ctdb->name));
366 if (listen(ctcp->listen_fd, 10) == -1) {
367 goto failed;
370 fde = event_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, EVENT_FD_READ,
371 ctdb_listen_event, ctdb);
372 tevent_fd_set_auto_close(fde);
374 close(lock_fd);
376 return 0;
378 failed:
379 close(lock_fd);
380 if (ctcp->listen_fd != -1) {
381 close(ctcp->listen_fd);
382 ctcp->listen_fd = -1;
384 return -1;
389 listen on our own address
391 int ctdb_tcp_listen(struct ctdb_context *ctdb)
393 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
394 struct ctdb_tcp);
395 ctdb_sock_addr sock;
396 int sock_size;
397 int one = 1;
398 struct tevent_fd *fde;
400 /* we can either auto-bind to the first available address, or we can
401 use a specified address */
402 if (!ctdb->address) {
403 return ctdb_tcp_listen_automatic(ctdb);
406 sock = *ctdb->address;
408 switch (sock.sa.sa_family) {
409 case AF_INET:
410 sock_size = sizeof(sock.ip);
411 break;
412 case AF_INET6:
413 sock_size = sizeof(sock.ip6);
414 break;
415 default:
416 DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
417 sock.sa.sa_family));
418 goto failed;
421 ctcp->listen_fd = socket(sock.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
422 if (ctcp->listen_fd == -1) {
423 ctdb_set_error(ctdb, "socket failed\n");
424 return -1;
427 set_close_on_exec(ctcp->listen_fd);
429 if (setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one)) == -1) {
430 DEBUG(DEBUG_WARNING, ("Failed to set REUSEADDR on fd - %s\n",
431 strerror(errno)));
434 if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sock_size) != 0) {
435 DEBUG(DEBUG_ERR,(__location__ " Failed to bind() to socket. %s(%d)\n", strerror(errno), errno));
436 goto failed;
439 if (listen(ctcp->listen_fd, 10) == -1) {
440 goto failed;
443 fde = event_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, EVENT_FD_READ,
444 ctdb_listen_event, ctdb);
445 tevent_fd_set_auto_close(fde);
447 return 0;
449 failed:
450 if (ctcp->listen_fd != -1) {
451 close(ctcp->listen_fd);
453 ctcp->listen_fd = -1;
454 return -1;