wscript: separate embedded_heimdal from system_heimdal
[Samba.git] / ctdb / tcp / tcp_init.c
blobb6083666e18a591b3e58c8b720d374250bbd33d2
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->fd != -1) {
42 close(tnode->fd);
43 tnode->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->fd = -1;
59 node->private_data = tnode;
60 talloc_set_destructor(tnode, tnode_destructor);
62 tnode->out_queue = ctdb_queue_setup(node->ctdb, node, tnode->fd, CTDB_TCP_ALIGNMENT,
63 ctdb_tcp_tnode_cb, node, "to-node-%s", node->name);
65 return 0;
69 initialise transport structures
71 static int ctdb_tcp_initialise(struct ctdb_context *ctdb)
73 int i;
75 /* listen on our own address */
76 if (ctdb_tcp_listen(ctdb) != 0) {
77 DEBUG(DEBUG_CRIT, (__location__ " Failed to start listening on the CTDB socket\n"));
78 exit(1);
81 for (i=0; i < ctdb->num_nodes; i++) {
82 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
83 continue;
85 if (ctdb_tcp_add_node(ctdb->nodes[i]) != 0) {
86 DEBUG(DEBUG_CRIT, ("methods->add_node failed at %d\n", i));
87 return -1;
91 return 0;
95 start the protocol going
97 static int ctdb_tcp_connect_node(struct ctdb_node *node)
99 struct ctdb_context *ctdb = node->ctdb;
100 struct ctdb_tcp_node *tnode = talloc_get_type(
101 node->private_data, struct ctdb_tcp_node);
103 /* startup connection to the other server - will happen on
104 next event loop */
105 if (!ctdb_same_address(ctdb->address, &node->address)) {
106 tnode->connect_te = tevent_add_timer(ctdb->ev, tnode,
107 timeval_zero(),
108 ctdb_tcp_node_connect,
109 node);
112 return 0;
116 shutdown and try to restart a connection to a node after it has been
117 disconnected
119 static void ctdb_tcp_restart(struct ctdb_node *node)
121 struct ctdb_tcp_node *tnode = talloc_get_type(
122 node->private_data, struct ctdb_tcp_node);
124 DEBUG(DEBUG_NOTICE,("Tearing down connection to dead node :%d\n", node->pnn));
126 ctdb_tcp_stop_connection(node);
128 tnode->connect_te = tevent_add_timer(node->ctdb->ev, tnode,
129 timeval_zero(),
130 ctdb_tcp_node_connect, node);
135 shutdown the transport
137 static void ctdb_tcp_shutdown(struct ctdb_context *ctdb)
139 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
140 struct ctdb_tcp);
141 talloc_free(ctcp);
142 ctdb->private_data = NULL;
146 start the transport
148 static int ctdb_tcp_start(struct ctdb_context *ctdb)
150 int i;
152 for (i=0; i < ctdb->num_nodes; i++) {
153 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
154 continue;
156 ctdb_tcp_connect_node(ctdb->nodes[i]);
159 return 0;
164 transport packet allocator - allows transport to control memory for packets
166 static void *ctdb_tcp_allocate_pkt(TALLOC_CTX *mem_ctx, size_t size)
168 /* tcp transport needs to round to 8 byte alignment to ensure
169 that we can use a length header and 64 bit elements in
170 structures */
171 size = (size+(CTDB_TCP_ALIGNMENT-1)) & ~(CTDB_TCP_ALIGNMENT-1);
172 return talloc_size(mem_ctx, size);
176 static const struct ctdb_methods ctdb_tcp_methods = {
177 .initialise = ctdb_tcp_initialise,
178 .start = ctdb_tcp_start,
179 .queue_pkt = ctdb_tcp_queue_pkt,
180 .add_node = ctdb_tcp_add_node,
181 .connect_node = ctdb_tcp_connect_node,
182 .allocate_pkt = ctdb_tcp_allocate_pkt,
183 .shutdown = ctdb_tcp_shutdown,
184 .restart = ctdb_tcp_restart,
187 static int tcp_ctcp_destructor(struct ctdb_tcp *ctcp)
189 ctcp->ctdb->private_data = NULL;
190 ctcp->ctdb->methods = NULL;
192 return 0;
197 initialise tcp portion of ctdb
199 int ctdb_tcp_init(struct ctdb_context *ctdb)
201 struct ctdb_tcp *ctcp;
202 ctcp = talloc_zero(ctdb, struct ctdb_tcp);
203 CTDB_NO_MEMORY(ctdb, ctcp);
205 ctcp->listen_fd = -1;
206 ctcp->ctdb = ctdb;
207 ctdb->private_data = ctcp;
208 ctdb->methods = &ctdb_tcp_methods;
210 talloc_set_destructor(ctcp, tcp_ctcp_destructor);
211 return 0;