s3:smbd: rename has_ctdb_public_ip to has_cluster_movable_ip
[Samba.git] / ctdb / tcp / tcp_init.c
blob97ebe1d887a7c47535f7ea22e7317e991f3fb477
1 /*
2 ctdb over TCP
4 Copyright (C) Andrew Tridgell 2006
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "replace.h"
21 #include "system/network.h"
22 #include "system/filesys.h"
24 #include <talloc.h>
25 #include <tevent.h>
27 #include "lib/util/time.h"
28 #include "lib/util/debug.h"
30 #include "ctdb_private.h"
32 #include "common/common.h"
33 #include "common/logging.h"
35 #include "ctdb_tcp.h"
37 static int tnode_destructor(struct ctdb_tcp_node *tnode)
39 // struct ctdb_node *node = talloc_find_parent_bytype(tnode, struct ctdb_node);
41 if (tnode->out_fd != -1) {
42 close(tnode->out_fd);
43 tnode->out_fd = -1;
46 return 0;
50 initialise tcp portion of a ctdb node
52 static int ctdb_tcp_add_node(struct ctdb_node *node)
54 struct ctdb_tcp_node *tnode;
55 tnode = talloc_zero(node, struct ctdb_tcp_node);
56 CTDB_NO_MEMORY(node->ctdb, tnode);
58 tnode->out_fd = -1;
59 tnode->ctdb = node->ctdb;
61 node->transport_data = tnode;
62 talloc_set_destructor(tnode, tnode_destructor);
64 return 0;
68 initialise transport structures
70 static int ctdb_tcp_initialise(struct ctdb_context *ctdb)
72 unsigned int i;
74 /* listen on our own address */
75 if (ctdb_tcp_listen(ctdb) != 0) {
76 DEBUG(DEBUG_CRIT, (__location__ " Failed to start listening on the CTDB socket\n"));
77 exit(1);
80 for (i=0; i < ctdb->num_nodes; i++) {
81 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
82 continue;
84 if (ctdb_tcp_add_node(ctdb->nodes[i]) != 0) {
85 DEBUG(DEBUG_CRIT, ("methods->add_node failed at %d\n", i));
86 return -1;
90 return 0;
94 start the protocol going
96 static int ctdb_tcp_connect_node(struct ctdb_node *node)
98 struct ctdb_context *ctdb = node->ctdb;
99 struct ctdb_tcp_node *tnode = talloc_get_type(
100 node->transport_data, struct ctdb_tcp_node);
102 /* startup connection to the other server - will happen on
103 next event loop */
104 if (!ctdb_same_address(ctdb->address, &node->address)) {
105 tnode->connect_te = tevent_add_timer(ctdb->ev, tnode,
106 timeval_zero(),
107 ctdb_tcp_node_connect,
108 node);
111 return 0;
115 shutdown and try to restart a connection to a node after it has been
116 disconnected
118 static void ctdb_tcp_restart(struct ctdb_node *node)
120 struct ctdb_tcp_node *tnode = talloc_get_type(
121 node->transport_data, struct ctdb_tcp_node);
123 DEBUG(DEBUG_NOTICE,("Tearing down connection to dead node :%d\n", node->pnn));
124 ctdb_tcp_stop_incoming(node);
125 ctdb_tcp_stop_outgoing(node);
127 tnode->connect_te = tevent_add_timer(node->ctdb->ev, tnode,
128 timeval_zero(),
129 ctdb_tcp_node_connect, node);
134 shutdown the transport
136 static void ctdb_tcp_shutdown(struct ctdb_context *ctdb)
138 uint32_t i;
140 TALLOC_FREE(ctdb->transport_data);
142 for (i=0; i<ctdb->num_nodes; i++) {
143 TALLOC_FREE(ctdb->nodes[i]->transport_data);
148 start the transport
150 static int ctdb_tcp_start(struct ctdb_context *ctdb)
152 unsigned int i;
154 for (i=0; i < ctdb->num_nodes; i++) {
155 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
156 continue;
158 ctdb_tcp_connect_node(ctdb->nodes[i]);
161 return 0;
166 transport packet allocator - allows transport to control memory for packets
168 static void *ctdb_tcp_allocate_pkt(TALLOC_CTX *mem_ctx, size_t size)
170 /* tcp transport needs to round to 8 byte alignment to ensure
171 that we can use a length header and 64 bit elements in
172 structures */
173 size = (size+(CTDB_TCP_ALIGNMENT-1)) & ~(CTDB_TCP_ALIGNMENT-1);
174 return talloc_size(mem_ctx, size);
178 static const struct ctdb_methods ctdb_tcp_methods = {
179 .initialise = ctdb_tcp_initialise,
180 .start = ctdb_tcp_start,
181 .queue_pkt = ctdb_tcp_queue_pkt,
182 .add_node = ctdb_tcp_add_node,
183 .connect_node = ctdb_tcp_connect_node,
184 .allocate_pkt = ctdb_tcp_allocate_pkt,
185 .shutdown = ctdb_tcp_shutdown,
186 .restart = ctdb_tcp_restart,
189 static int tcp_ctcp_destructor(struct ctdb_tcp *ctcp)
191 ctcp->ctdb->transport_data = NULL;
192 ctcp->ctdb->methods = NULL;
194 return 0;
199 initialise tcp portion of ctdb
201 int ctdb_tcp_init(struct ctdb_context *ctdb)
203 struct ctdb_tcp *ctcp;
204 ctcp = talloc_zero(ctdb, struct ctdb_tcp);
205 CTDB_NO_MEMORY(ctdb, ctcp);
207 ctcp->listen_fd = -1;
208 ctcp->ctdb = ctdb;
209 ctdb->transport_data = ctcp;
210 ctdb->methods = &ctdb_tcp_methods;
212 talloc_set_destructor(ctcp, tcp_ctcp_destructor);
213 return 0;