ctdb-tests: Avoid ShellCheck warnings
[Samba.git] / ctdb / tcp / tcp_connect.c
blob6f5862af5998ea708966f26cac22561da1dd2b5e
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 "replace.h"
22 #include "system/network.h"
23 #include "system/filesys.h"
25 #include <talloc.h>
26 #include <tevent.h>
28 #include "lib/util/debug.h"
29 #include "lib/util/time.h"
30 #include "lib/util/blocking.h"
32 #include "ctdb_private.h"
34 #include "common/system.h"
35 #include "common/common.h"
36 #include "common/logging.h"
38 #include "ctdb_tcp.h"
41 stop any outgoing connection (established or pending) to a node
43 void ctdb_tcp_stop_outgoing(struct ctdb_node *node)
45 struct ctdb_tcp_node *tnode = talloc_get_type(
46 node->transport_data, struct ctdb_tcp_node);
48 TALLOC_FREE(tnode->out_queue);
49 TALLOC_FREE(tnode->connect_te);
50 TALLOC_FREE(tnode->connect_fde);
51 if (tnode->out_fd != -1) {
52 close(tnode->out_fd);
53 tnode->out_fd = -1;
58 stop incoming connection to a node
60 void ctdb_tcp_stop_incoming(struct ctdb_node *node)
62 struct ctdb_tcp_node *tnode = talloc_get_type(
63 node->transport_data, struct ctdb_tcp_node);
65 TALLOC_FREE(tnode->in_queue);
69 called when a complete packet has come in - should not happen on this socket
70 unless the other side closes the connection with RST or FIN
72 void ctdb_tcp_tnode_cb(uint8_t *data, size_t cnt, void *private_data)
74 struct ctdb_node *node = talloc_get_type(private_data, struct ctdb_node);
76 node->ctdb->upcalls->node_dead(node);
78 TALLOC_FREE(data);
82 called when socket becomes writeable on connect
84 static void ctdb_node_connect_write(struct tevent_context *ev,
85 struct tevent_fd *fde,
86 uint16_t flags, void *private_data)
88 struct ctdb_node *node = talloc_get_type(private_data,
89 struct ctdb_node);
90 struct ctdb_tcp_node *tnode = talloc_get_type(node->transport_data,
91 struct ctdb_tcp_node);
92 struct ctdb_context *ctdb = node->ctdb;
93 int error = 0;
94 socklen_t len = sizeof(error);
95 int one = 1;
96 int ret;
98 TALLOC_FREE(tnode->connect_te);
100 ret = getsockopt(tnode->out_fd, SOL_SOCKET, SO_ERROR, &error, &len);
101 if (ret != 0 || error != 0) {
102 ctdb_tcp_stop_outgoing(node);
103 tnode->connect_te = tevent_add_timer(ctdb->ev, tnode,
104 timeval_current_ofs(1, 0),
105 ctdb_tcp_node_connect, node);
106 return;
109 TALLOC_FREE(tnode->connect_fde);
111 ret = setsockopt(tnode->out_fd,
112 IPPROTO_TCP,
113 TCP_NODELAY,
114 (char *)&one,
115 sizeof(one));
116 if (ret == -1) {
117 DBG_WARNING("Failed to set TCP_NODELAY on fd - %s\n",
118 strerror(errno));
120 ret = setsockopt(tnode->out_fd,
121 SOL_SOCKET,
122 SO_KEEPALIVE,(char *)&one,
123 sizeof(one));
124 if (ret == -1) {
125 DBG_WARNING("Failed to set KEEPALIVE on fd - %s\n",
126 strerror(errno));
129 tnode->out_queue = ctdb_queue_setup(node->ctdb,
130 tnode,
131 tnode->out_fd,
132 CTDB_TCP_ALIGNMENT,
133 ctdb_tcp_tnode_cb,
134 node,
135 "to-node-%s",
136 node->name);
137 if (tnode->out_queue == NULL) {
138 DBG_ERR("Failed to set up outgoing queue\n");
139 ctdb_tcp_stop_outgoing(node);
140 tnode->connect_te = tevent_add_timer(ctdb->ev,
141 tnode,
142 timeval_current_ofs(1, 0),
143 ctdb_tcp_node_connect,
144 node);
145 return;
148 /* the queue subsystem now owns this fd */
149 tnode->out_fd = -1;
152 * Mark the node to which this connection has been established
153 * as connected, but only if the corresponding listening
154 * socket is also connected
156 if (tnode->in_queue != NULL) {
157 node->ctdb->upcalls->node_connected(node);
162 static void ctdb_tcp_node_connect_timeout(struct tevent_context *ev,
163 struct tevent_timer *te,
164 struct timeval t,
165 void *private_data);
168 called when we should try and establish a tcp connection to a node
170 static void ctdb_tcp_start_outgoing(struct ctdb_node *node)
172 struct ctdb_tcp_node *tnode = talloc_get_type(node->transport_data,
173 struct ctdb_tcp_node);
174 struct ctdb_context *ctdb = node->ctdb;
175 ctdb_sock_addr sock_in;
176 int sockin_size;
177 int sockout_size;
178 ctdb_sock_addr sock_out;
179 int ret;
181 sock_out = node->address;
183 tnode->out_fd = socket(sock_out.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
184 if (tnode->out_fd == -1) {
185 DBG_ERR("Failed to create socket\n");
186 goto failed;
189 ret = set_blocking(tnode->out_fd, false);
190 if (ret != 0) {
191 DBG_ERR("Failed to set socket non-blocking (%s)\n",
192 strerror(errno));
193 goto failed;
196 set_close_on_exec(tnode->out_fd);
198 DBG_DEBUG("Created TCP SOCKET FD:%d\n", tnode->out_fd);
200 /* Bind our side of the socketpair to the same address we use to listen
201 * on incoming CTDB traffic.
202 * We must specify this address to make sure that the address we expose to
203 * the remote side is actually routable in case CTDB traffic will run on
204 * a dedicated non-routeable network.
206 sock_in = *ctdb->address;
208 /* AIX libs check to see if the socket address and length
209 arguments are consistent with each other on calls like
210 connect(). Can not get by with just sizeof(sock_in),
211 need sizeof(sock_in.ip).
213 switch (sock_in.sa.sa_family) {
214 case AF_INET:
215 sock_in.ip.sin_port = 0 /* Any port */;
216 sockin_size = sizeof(sock_in.ip);
217 sockout_size = sizeof(sock_out.ip);
218 break;
219 case AF_INET6:
220 sock_in.ip6.sin6_port = 0 /* Any port */;
221 sockin_size = sizeof(sock_in.ip6);
222 sockout_size = sizeof(sock_out.ip6);
223 break;
224 default:
225 DBG_ERR("Unknown address family %u\n", sock_in.sa.sa_family);
226 /* Can't happen to due to address parsing restrictions */
227 goto failed;
230 ret = bind(tnode->out_fd, (struct sockaddr *)&sock_in, sockin_size);
231 if (ret == -1) {
232 DBG_ERR("Failed to bind socket (%s)\n", strerror(errno));
233 goto failed;
236 ret = connect(tnode->out_fd,
237 (struct sockaddr *)&sock_out,
238 sockout_size);
239 if (ret != 0 && errno != EINPROGRESS) {
240 goto failed;
243 /* non-blocking connect - wait for write event */
244 tnode->connect_fde = tevent_add_fd(node->ctdb->ev,
245 tnode,
246 tnode->out_fd,
247 TEVENT_FD_WRITE|TEVENT_FD_READ,
248 ctdb_node_connect_write,
249 node);
251 /* don't give it long to connect - retry in one second. This ensures
252 that we find a node is up quickly (tcp normally backs off a syn reply
253 delay by quite a lot) */
254 tnode->connect_te = tevent_add_timer(ctdb->ev,
255 tnode,
256 timeval_current_ofs(1, 0),
257 ctdb_tcp_node_connect_timeout,
258 node);
260 return;
262 failed:
263 ctdb_tcp_stop_outgoing(node);
264 tnode->connect_te = tevent_add_timer(ctdb->ev,
265 tnode,
266 timeval_current_ofs(1, 0),
267 ctdb_tcp_node_connect,
268 node);
271 void ctdb_tcp_node_connect(struct tevent_context *ev,
272 struct tevent_timer *te,
273 struct timeval t,
274 void *private_data)
276 struct ctdb_node *node = talloc_get_type_abort(private_data,
277 struct ctdb_node);
279 ctdb_tcp_start_outgoing(node);
282 static void ctdb_tcp_node_connect_timeout(struct tevent_context *ev,
283 struct tevent_timer *te,
284 struct timeval t,
285 void *private_data)
287 struct ctdb_node *node = talloc_get_type_abort(private_data,
288 struct ctdb_node);
290 ctdb_tcp_stop_outgoing(node);
291 ctdb_tcp_start_outgoing(node);
295 called when we get contacted by another node
296 currently makes no attempt to check if the connection is really from a ctdb
297 node in our cluster
299 static void ctdb_listen_event(struct tevent_context *ev, struct tevent_fd *fde,
300 uint16_t flags, void *private_data)
302 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
303 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->transport_data,
304 struct ctdb_tcp);
305 ctdb_sock_addr addr;
306 socklen_t len;
307 int fd;
308 struct ctdb_node *node;
309 struct ctdb_tcp_node *tnode;
310 int one = 1;
311 int ret;
313 memset(&addr, 0, sizeof(addr));
314 len = sizeof(addr);
315 fd = accept(ctcp->listen_fd, (struct sockaddr *)&addr, &len);
316 if (fd == -1) return;
317 smb_set_close_on_exec(fd);
319 node = ctdb_ip_to_node(ctdb, &addr);
320 if (node == NULL) {
321 D_ERR("Refused connection from unknown node %s\n",
322 ctdb_addr_to_str(&addr));
323 close(fd);
324 return;
327 tnode = talloc_get_type_abort(node->transport_data,
328 struct ctdb_tcp_node);
329 if (tnode == NULL) {
330 /* This can't happen - see ctdb_tcp_initialise() */
331 DBG_ERR("INTERNAL ERROR setting up connection from node %s\n",
332 ctdb_addr_to_str(&addr));
333 close(fd);
334 return;
337 if (tnode->in_queue != NULL) {
338 DBG_ERR("Incoming queue active, rejecting connection from %s\n",
339 ctdb_addr_to_str(&addr));
340 close(fd);
341 return;
344 ret = set_blocking(fd, false);
345 if (ret != 0) {
346 DBG_ERR("Failed to set socket non-blocking (%s)\n",
347 strerror(errno));
348 close(fd);
349 return;
352 set_close_on_exec(fd);
354 DBG_DEBUG("Created SOCKET FD:%d to incoming ctdb connection\n", fd);
356 ret = setsockopt(fd,
357 SOL_SOCKET,
358 SO_KEEPALIVE,
359 (char *)&one,
360 sizeof(one));
361 if (ret == -1) {
362 DBG_WARNING("Failed to set KEEPALIVE on fd - %s\n",
363 strerror(errno));
366 tnode->in_queue = ctdb_queue_setup(ctdb,
367 tnode,
369 CTDB_TCP_ALIGNMENT,
370 ctdb_tcp_read_cb,
371 node,
372 "ctdbd-%s",
373 ctdb_addr_to_str(&addr));
374 if (tnode->in_queue == NULL) {
375 DBG_ERR("Failed to set up incoming queue\n");
376 close(fd);
377 return;
381 * Mark the connecting node as connected, but only if the
382 * corresponding outbound connected is also up
384 if (tnode->out_queue != NULL) {
385 node->ctdb->upcalls->node_connected(node);
391 automatically find which address to listen on
393 static int ctdb_tcp_listen_automatic(struct ctdb_context *ctdb)
395 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->transport_data,
396 struct ctdb_tcp);
397 ctdb_sock_addr sock;
398 int lock_fd;
399 unsigned int i;
400 const char *lock_path = CTDB_RUNDIR "/.socket_lock";
401 struct flock lock;
402 int one = 1;
403 int sock_size;
404 struct tevent_fd *fde;
406 /* If there are no nodes, then it won't be possible to find
407 * the first one. Log a failure and short circuit the whole
408 * process.
410 if (ctdb->num_nodes == 0) {
411 DEBUG(DEBUG_CRIT,("No nodes available to attempt bind to - is the nodes file empty?\n"));
412 return -1;
415 /* in order to ensure that we don't get two nodes with the
416 same address, we must make the bind() and listen() calls
417 atomic. The SO_REUSEADDR setsockopt only prevents double
418 binds if the first socket is in LISTEN state */
419 lock_fd = open(lock_path, O_RDWR|O_CREAT, 0666);
420 if (lock_fd == -1) {
421 DEBUG(DEBUG_CRIT,("Unable to open %s\n", lock_path));
422 return -1;
425 lock.l_type = F_WRLCK;
426 lock.l_whence = SEEK_SET;
427 lock.l_start = 0;
428 lock.l_len = 1;
429 lock.l_pid = 0;
431 if (fcntl(lock_fd, F_SETLKW, &lock) != 0) {
432 DEBUG(DEBUG_CRIT,("Unable to lock %s\n", lock_path));
433 close(lock_fd);
434 return -1;
437 for (i=0; i < ctdb->num_nodes; i++) {
438 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
439 continue;
441 sock = ctdb->nodes[i]->address;
443 switch (sock.sa.sa_family) {
444 case AF_INET:
445 sock_size = sizeof(sock.ip);
446 break;
447 case AF_INET6:
448 sock_size = sizeof(sock.ip6);
449 break;
450 default:
451 DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
452 sock.sa.sa_family));
453 continue;
456 ctcp->listen_fd = socket(sock.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
457 if (ctcp->listen_fd == -1) {
458 ctdb_set_error(ctdb, "socket failed\n");
459 continue;
462 set_close_on_exec(ctcp->listen_fd);
464 if (setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,
465 (char *)&one,sizeof(one)) == -1) {
466 DEBUG(DEBUG_WARNING, ("Failed to set REUSEADDR on fd - %s\n",
467 strerror(errno)));
470 if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sock_size) == 0) {
471 break;
474 if (errno == EADDRNOTAVAIL) {
475 DEBUG(DEBUG_DEBUG,(__location__ " Failed to bind() to socket. %s(%d)\n",
476 strerror(errno), errno));
477 } else {
478 DEBUG(DEBUG_ERR,(__location__ " Failed to bind() to socket. %s(%d)\n",
479 strerror(errno), errno));
482 close(ctcp->listen_fd);
483 ctcp->listen_fd = -1;
486 if (i == ctdb->num_nodes) {
487 DEBUG(DEBUG_CRIT,("Unable to bind to any of the node addresses - giving up\n"));
488 goto failed;
490 ctdb->address = talloc_memdup(ctdb,
491 &ctdb->nodes[i]->address,
492 sizeof(ctdb_sock_addr));
493 if (ctdb->address == NULL) {
494 ctdb_set_error(ctdb, "Out of memory at %s:%d",
495 __FILE__, __LINE__);
496 goto failed;
499 ctdb->name = talloc_asprintf(ctdb, "%s:%u",
500 ctdb_addr_to_str(ctdb->address),
501 ctdb_addr_to_port(ctdb->address));
502 if (ctdb->name == NULL) {
503 ctdb_set_error(ctdb, "Out of memory at %s:%d",
504 __FILE__, __LINE__);
505 goto failed;
507 DEBUG(DEBUG_INFO,("ctdb chose network address %s\n", ctdb->name));
509 if (listen(ctcp->listen_fd, 10) == -1) {
510 goto failed;
513 fde = tevent_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, TEVENT_FD_READ,
514 ctdb_listen_event, ctdb);
515 tevent_fd_set_auto_close(fde);
517 close(lock_fd);
519 return 0;
521 failed:
522 close(lock_fd);
523 if (ctcp->listen_fd != -1) {
524 close(ctcp->listen_fd);
525 ctcp->listen_fd = -1;
527 return -1;
532 listen on our own address
534 int ctdb_tcp_listen(struct ctdb_context *ctdb)
536 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->transport_data,
537 struct ctdb_tcp);
538 ctdb_sock_addr sock;
539 int sock_size;
540 int one = 1;
541 struct tevent_fd *fde;
543 /* we can either auto-bind to the first available address, or we can
544 use a specified address */
545 if (!ctdb->address) {
546 return ctdb_tcp_listen_automatic(ctdb);
549 sock = *ctdb->address;
551 switch (sock.sa.sa_family) {
552 case AF_INET:
553 sock_size = sizeof(sock.ip);
554 break;
555 case AF_INET6:
556 sock_size = sizeof(sock.ip6);
557 break;
558 default:
559 DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
560 sock.sa.sa_family));
561 goto failed;
564 ctcp->listen_fd = socket(sock.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
565 if (ctcp->listen_fd == -1) {
566 ctdb_set_error(ctdb, "socket failed\n");
567 return -1;
570 set_close_on_exec(ctcp->listen_fd);
572 if (setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one)) == -1) {
573 DEBUG(DEBUG_WARNING, ("Failed to set REUSEADDR on fd - %s\n",
574 strerror(errno)));
577 if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sock_size) != 0) {
578 DEBUG(DEBUG_ERR,(__location__ " Failed to bind() to socket. %s(%d)\n", strerror(errno), errno));
579 goto failed;
582 if (listen(ctcp->listen_fd, 10) == -1) {
583 goto failed;
586 fde = tevent_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, TEVENT_FD_READ,
587 ctdb_listen_event, ctdb);
588 tevent_fd_set_auto_close(fde);
590 return 0;
592 failed:
593 if (ctcp->listen_fd != -1) {
594 close(ctcp->listen_fd);
596 ctcp->listen_fd = -1;
597 return -1;