ctdb-tcp: Remove a use of ctdb_addr_to_str()
[samba4-gss.git] / ctdb / tcp / tcp_connect.c
blob1ad3fa74d55d3a4f7da892b45e4e2d2f33762076
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"
37 #include "common/path.h"
39 #include "protocol/protocol_util.h"
41 #include "ctdb_tcp.h"
44 stop any outgoing connection (established or pending) to a node
46 void ctdb_tcp_stop_outgoing(struct ctdb_node *node)
48 struct ctdb_tcp_node *tnode = talloc_get_type(
49 node->transport_data, struct ctdb_tcp_node);
51 TALLOC_FREE(tnode->out_queue);
52 TALLOC_FREE(tnode->connect_te);
53 TALLOC_FREE(tnode->connect_fde);
54 if (tnode->out_fd != -1) {
55 close(tnode->out_fd);
56 tnode->out_fd = -1;
61 stop incoming connection to a node
63 void ctdb_tcp_stop_incoming(struct ctdb_node *node)
65 struct ctdb_tcp_node *tnode = talloc_get_type(
66 node->transport_data, struct ctdb_tcp_node);
68 TALLOC_FREE(tnode->in_queue);
72 called when a complete packet has come in - should not happen on this socket
73 unless the other side closes the connection with RST or FIN
75 void ctdb_tcp_tnode_cb(uint8_t *data, size_t cnt, void *private_data)
77 struct ctdb_node *node = talloc_get_type(private_data, struct ctdb_node);
79 node->ctdb->upcalls->node_dead(node);
81 TALLOC_FREE(data);
85 called when socket becomes writeable on connect
87 static void ctdb_node_connect_write(struct tevent_context *ev,
88 struct tevent_fd *fde,
89 uint16_t flags, void *private_data)
91 struct ctdb_node *node = talloc_get_type(private_data,
92 struct ctdb_node);
93 struct ctdb_tcp_node *tnode = talloc_get_type(node->transport_data,
94 struct ctdb_tcp_node);
95 struct ctdb_context *ctdb = node->ctdb;
96 int error = 0;
97 socklen_t len = sizeof(error);
98 int one = 1;
99 int ret;
101 TALLOC_FREE(tnode->connect_te);
103 ret = getsockopt(tnode->out_fd, SOL_SOCKET, SO_ERROR, &error, &len);
104 if (ret != 0 || error != 0) {
105 ctdb_tcp_stop_outgoing(node);
106 tnode->connect_te = tevent_add_timer(ctdb->ev, tnode,
107 timeval_current_ofs(1, 0),
108 ctdb_tcp_node_connect, node);
109 return;
112 TALLOC_FREE(tnode->connect_fde);
114 ret = setsockopt(tnode->out_fd,
115 IPPROTO_TCP,
116 TCP_NODELAY,
117 (char *)&one,
118 sizeof(one));
119 if (ret == -1) {
120 DBG_WARNING("Failed to set TCP_NODELAY on fd - %s\n",
121 strerror(errno));
123 ret = setsockopt(tnode->out_fd,
124 SOL_SOCKET,
125 SO_KEEPALIVE,(char *)&one,
126 sizeof(one));
127 if (ret == -1) {
128 DBG_WARNING("Failed to set KEEPALIVE on fd - %s\n",
129 strerror(errno));
132 tnode->out_queue = ctdb_queue_setup(node->ctdb,
133 tnode,
134 tnode->out_fd,
135 CTDB_TCP_ALIGNMENT,
136 ctdb_tcp_tnode_cb,
137 node,
138 "to-node-%s",
139 node->name);
140 if (tnode->out_queue == NULL) {
141 DBG_ERR("Failed to set up outgoing queue\n");
142 ctdb_tcp_stop_outgoing(node);
143 tnode->connect_te = tevent_add_timer(ctdb->ev,
144 tnode,
145 timeval_current_ofs(1, 0),
146 ctdb_tcp_node_connect,
147 node);
148 return;
151 /* the queue subsystem now owns this fd */
152 tnode->out_fd = -1;
155 * Mark the node to which this connection has been established
156 * as connected, but only if the corresponding listening
157 * socket is also connected
159 if (tnode->in_queue != NULL) {
160 node->ctdb->upcalls->node_connected(node);
165 static void ctdb_tcp_node_connect_timeout(struct tevent_context *ev,
166 struct tevent_timer *te,
167 struct timeval t,
168 void *private_data);
171 called when we should try and establish a tcp connection to a node
173 static void ctdb_tcp_start_outgoing(struct ctdb_node *node)
175 struct ctdb_tcp_node *tnode = talloc_get_type(node->transport_data,
176 struct ctdb_tcp_node);
177 struct ctdb_context *ctdb = node->ctdb;
178 ctdb_sock_addr sock_in;
179 int sockin_size;
180 int sockout_size;
181 ctdb_sock_addr sock_out;
182 int ret;
184 sock_out = node->address;
186 tnode->out_fd = socket(sock_out.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
187 if (tnode->out_fd == -1) {
188 DBG_ERR("Failed to create socket\n");
189 goto failed;
192 ret = set_blocking(tnode->out_fd, false);
193 if (ret != 0) {
194 DBG_ERR("Failed to set socket non-blocking (%s)\n",
195 strerror(errno));
196 goto failed;
199 set_close_on_exec(tnode->out_fd);
201 DBG_DEBUG("Created TCP SOCKET FD:%d\n", tnode->out_fd);
203 /* Bind our side of the socketpair to the same address we use to listen
204 * on incoming CTDB traffic.
205 * We must specify this address to make sure that the address we expose to
206 * the remote side is actually routable in case CTDB traffic will run on
207 * a dedicated non-routeable network.
209 sock_in = *ctdb->address;
211 /* AIX libs check to see if the socket address and length
212 arguments are consistent with each other on calls like
213 connect(). Can not get by with just sizeof(sock_in),
214 need sizeof(sock_in.ip).
216 switch (sock_in.sa.sa_family) {
217 case AF_INET:
218 sock_in.ip.sin_port = 0 /* Any port */;
219 sockin_size = sizeof(sock_in.ip);
220 sockout_size = sizeof(sock_out.ip);
221 break;
222 case AF_INET6:
223 sock_in.ip6.sin6_port = 0 /* Any port */;
224 sockin_size = sizeof(sock_in.ip6);
225 sockout_size = sizeof(sock_out.ip6);
226 break;
227 default:
228 DBG_ERR("Unknown address family %u\n", sock_in.sa.sa_family);
229 /* Can't happen to due to address parsing restrictions */
230 goto failed;
233 ret = bind(tnode->out_fd, (struct sockaddr *)&sock_in, sockin_size);
234 if (ret == -1) {
235 DBG_ERR("Failed to bind socket (%s)\n", strerror(errno));
236 goto failed;
239 ret = connect(tnode->out_fd,
240 (struct sockaddr *)&sock_out,
241 sockout_size);
242 if (ret != 0 && errno != EINPROGRESS) {
243 goto failed;
246 /* non-blocking connect - wait for write event */
247 tnode->connect_fde = tevent_add_fd(node->ctdb->ev,
248 tnode,
249 tnode->out_fd,
250 TEVENT_FD_WRITE|TEVENT_FD_READ,
251 ctdb_node_connect_write,
252 node);
254 /* don't give it long to connect - retry in one second. This ensures
255 that we find a node is up quickly (tcp normally backs off a syn reply
256 delay by quite a lot) */
257 tnode->connect_te = tevent_add_timer(ctdb->ev,
258 tnode,
259 timeval_current_ofs(1, 0),
260 ctdb_tcp_node_connect_timeout,
261 node);
263 return;
265 failed:
266 ctdb_tcp_stop_outgoing(node);
267 tnode->connect_te = tevent_add_timer(ctdb->ev,
268 tnode,
269 timeval_current_ofs(1, 0),
270 ctdb_tcp_node_connect,
271 node);
274 void ctdb_tcp_node_connect(struct tevent_context *ev,
275 struct tevent_timer *te,
276 struct timeval t,
277 void *private_data)
279 struct ctdb_node *node = talloc_get_type_abort(private_data,
280 struct ctdb_node);
282 ctdb_tcp_start_outgoing(node);
285 static void ctdb_tcp_node_connect_timeout(struct tevent_context *ev,
286 struct tevent_timer *te,
287 struct timeval t,
288 void *private_data)
290 struct ctdb_node *node = talloc_get_type_abort(private_data,
291 struct ctdb_node);
293 ctdb_tcp_stop_outgoing(node);
294 ctdb_tcp_start_outgoing(node);
298 called when we get contacted by another node
299 currently makes no attempt to check if the connection is really from a ctdb
300 node in our cluster
302 static void ctdb_listen_event(struct tevent_context *ev, struct tevent_fd *fde,
303 uint16_t flags, void *private_data)
305 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
306 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->transport_data,
307 struct ctdb_tcp);
308 ctdb_sock_addr addr;
309 socklen_t len;
310 int fd;
311 struct ctdb_node *node;
312 struct ctdb_tcp_node *tnode;
313 int one = 1;
314 int ret;
316 memset(&addr, 0, sizeof(addr));
317 len = sizeof(addr);
318 fd = accept(ctcp->listen_fd, (struct sockaddr *)&addr, &len);
319 if (fd == -1) return;
320 smb_set_close_on_exec(fd);
322 node = ctdb_ip_to_node(ctdb, &addr);
323 if (node == NULL) {
324 char *t = ctdb_sock_addr_to_string(ctcp, &addr, true);
325 if (t == NULL) {
326 DBG_ERR("Refused connection from unparsable node\n");
327 goto failed;
330 D_ERR("Refused connection from unknown node %s\n", t);
331 talloc_free(t);
332 goto failed;
335 tnode = talloc_get_type_abort(node->transport_data,
336 struct ctdb_tcp_node);
337 if (tnode == NULL) {
338 /* This can't happen - see ctdb_tcp_initialise() */
339 DBG_ERR("INTERNAL ERROR setting up connection from node %s\n",
340 node->name);
341 goto failed;
344 if (tnode->in_queue != NULL) {
345 DBG_ERR("Incoming queue active, rejecting connection from %s\n",
346 node->name);
347 goto failed;
350 ret = set_blocking(fd, false);
351 if (ret != 0) {
352 DBG_ERR("Failed to set socket non-blocking (%s)\n",
353 strerror(errno));
354 goto failed;
357 set_close_on_exec(fd);
359 DBG_DEBUG("Created SOCKET FD:%d to incoming ctdb connection\n", fd);
361 ret = setsockopt(fd,
362 SOL_SOCKET,
363 SO_KEEPALIVE,
364 (char *)&one,
365 sizeof(one));
366 if (ret == -1) {
367 DBG_WARNING("Failed to set KEEPALIVE on fd - %s\n",
368 strerror(errno));
371 tnode->in_queue = ctdb_queue_setup(ctdb,
372 tnode,
374 CTDB_TCP_ALIGNMENT,
375 ctdb_tcp_read_cb,
376 node,
377 "ctdbd-%s",
378 node->name);
379 if (tnode->in_queue == NULL) {
380 DBG_ERR("Failed to set up incoming queue\n");
381 goto failed;
385 * Mark the connecting node as connected, but only if the
386 * corresponding outbound connected is also up
388 if (tnode->out_queue != NULL) {
389 node->ctdb->upcalls->node_connected(node);
392 return;
394 failed:
395 close(fd);
398 static int ctdb_tcp_listen_addr(struct ctdb_context *ctdb,
399 ctdb_sock_addr *addr,
400 bool strict)
402 struct ctdb_tcp *ctcp = talloc_get_type_abort(
403 ctdb->transport_data, struct ctdb_tcp);
404 ctdb_sock_addr sock;
405 int sock_size;
406 int one = 1;
407 struct tevent_fd *fde = NULL;
408 int ret;
410 sock = *addr;
411 ctcp->listen_fd = -1;
413 switch (sock.sa.sa_family) {
414 case AF_INET:
415 sock_size = sizeof(sock.ip);
416 break;
417 case AF_INET6:
418 sock_size = sizeof(sock.ip6);
419 break;
420 default:
421 DBG_ERR("Unknown family %u\n", sock.sa.sa_family);
422 goto failed;
425 ctcp->listen_fd = socket(sock.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
426 if (ctcp->listen_fd == -1) {
427 DBG_ERR("Socket failed - %s (%d)\n", strerror(errno), errno);
428 goto failed;
431 set_close_on_exec(ctcp->listen_fd);
433 ret = setsockopt(ctcp->listen_fd,
434 SOL_SOCKET,
435 SO_REUSEADDR,
436 (char *)&one,
437 sizeof(one));
438 if (ret == -1) {
439 DBG_WARNING("Failed to set REUSEADDR on fd - %s (%d)\n",
440 strerror(errno),
441 errno);
444 ret =bind(ctcp->listen_fd, (struct sockaddr * )&sock, sock_size);
445 if (ret != 0) {
446 if (strict || errno != EADDRNOTAVAIL) {
447 DBG_ERR("Failed to bind() to socket - %s (%d)\n",
448 strerror(errno),
449 errno);
450 } else {
451 DBG_DEBUG("Failed to bind() to socket - %s (%d)\n",
452 strerror(errno),
453 errno);
455 goto failed;
458 ret = listen(ctcp->listen_fd, 10);
459 if (ret == -1) {
460 DBG_ERR("Failed to listen() on socket - %s (%d)\n",
461 strerror(errno),
462 errno);
463 goto failed;
466 fde = tevent_add_fd(ctdb->ev,
467 ctcp,
468 ctcp->listen_fd,
469 TEVENT_FD_READ,
470 ctdb_listen_event,
471 ctdb);
472 tevent_fd_set_auto_close(fde);
474 return 0;
476 failed:
477 if (ctcp->listen_fd != -1) {
478 close(ctcp->listen_fd);
479 ctcp->listen_fd = -1;
481 return -1;
485 automatically find which address to listen on
487 static int ctdb_tcp_listen_automatic(struct ctdb_context *ctdb)
489 int lock_fd;
490 unsigned int i;
491 char *lock_path = NULL;
492 struct flock lock;
493 int ret;
496 * If there are no nodes, then it won't be possible to find
497 * the first one. Log a failure and short circuit the whole
498 * process.
500 if (ctdb->num_nodes == 0) {
501 DEBUG(DEBUG_CRIT,("No nodes available to attempt bind to - is the nodes file empty?\n"));
502 return -1;
506 * In order to ensure that we don't get two nodes with the
507 * same address, we must make the bind() and listen() calls
508 * atomic. The SO_REUSEADDR setsockopt only prevents double
509 * binds if the first socket is in LISTEN state.
511 lock_path = path_rundir_append(ctdb, ".socket_lock");
512 if (lock_path == NULL) {
513 DBG_ERR("Memory allocation error\n");
514 return -1;
516 lock_fd = open(lock_path, O_RDWR|O_CREAT, 0666);
517 if (lock_fd == -1) {
518 DBG_ERR("Unable to open %s\n", lock_path);
519 talloc_free(lock_path);
520 return -1;
523 lock.l_type = F_WRLCK;
524 lock.l_whence = SEEK_SET;
525 lock.l_start = 0;
526 lock.l_len = 1;
527 lock.l_pid = 0;
529 if (fcntl(lock_fd, F_SETLKW, &lock) != 0) {
530 DBG_ERR("Unable to lock %s\n", lock_path);
531 close(lock_fd);
532 talloc_free(lock_path);
533 return -1;
535 talloc_free(lock_path);
537 for (i=0; i < ctdb->num_nodes; i++) {
538 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
539 continue;
542 ret = ctdb_tcp_listen_addr(ctdb,
543 &ctdb->nodes[i]->address,
544 false);
545 if (ret == 0) {
546 break;
550 if (i == ctdb->num_nodes) {
551 D_ERR("Unable to bind to any node address - giving up\n");
552 return -1;
555 ctdb->address = talloc_memdup(ctdb,
556 &ctdb->nodes[i]->address,
557 sizeof(ctdb_sock_addr));
558 if (ctdb->address == NULL) {
559 DBG_ERR("Memory allocation error\n");
560 return -1;
563 ctdb->name = talloc_strdup(ctdb, ctdb->nodes[i]->name);
564 if (ctdb->name == NULL) {
565 DBG_ERR("Memory allocation error\n");
566 return -1;
569 D_INFO("ctdb chose network address %s\n", ctdb->name);
570 return 0;
575 listen on our own address
577 int ctdb_tcp_listen(struct ctdb_context *ctdb)
579 int ret;
581 /* we can either auto-bind to the first available address, or we can
582 use a specified address */
583 if (!ctdb->address) {
584 ret = ctdb_tcp_listen_automatic(ctdb);
585 return ret;
588 ret = ctdb_tcp_listen_addr(ctdb, ctdb->address, true);
589 return ret;