r23798: updated old Temple Place FSF addresses to new URL
[gd/samba/.git] / source4 / cluster / ctdb / tcp / tcp_init.c
1 /* 
2    ctdb over TCP
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 3 of the License, or (at your option) any later version.
10
11    This library 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 GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "lib/tdb/include/tdb.h"
22 #include "lib/events/events.h"
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "../include/ctdb_private.h"
26 #include "ctdb_tcp.h"
27
28 /*
29   start the protocol going
30 */
31 static int ctdb_tcp_start(struct ctdb_context *ctdb)
32 {
33         int i;
34
35         /* listen on our own address */
36         if (ctdb_tcp_listen(ctdb) != 0) return -1;
37
38         /* startup connections to the other servers - will happen on
39            next event loop */
40         for (i=0;i<ctdb->num_nodes;i++) {
41                 struct ctdb_node *node = *(ctdb->nodes + i);
42                 if (!(ctdb->flags & CTDB_FLAG_SELF_CONNECT) &&
43                     ctdb_same_address(&ctdb->address, &node->address)) continue;
44                 event_add_timed(ctdb->ev, node, timeval_zero(), 
45                                 ctdb_tcp_node_connect, node);
46         }
47
48         return 0;
49 }
50
51
52 /*
53   initialise tcp portion of a ctdb node 
54 */
55 static int ctdb_tcp_add_node(struct ctdb_node *node)
56 {
57         struct ctdb_tcp_node *tnode;
58         tnode = talloc_zero(node, struct ctdb_tcp_node);
59         CTDB_NO_MEMORY(node->ctdb, tnode);
60
61         tnode->fd = -1;
62         node->private_data = tnode;
63
64         tnode->queue = ctdb_queue_setup(node->ctdb, node, tnode->fd, CTDB_TCP_ALIGNMENT,
65                                         ctdb_tcp_tnode_cb, node);
66         
67         return 0;
68 }
69
70
71 /*
72   transport packet allocator - allows transport to control memory for packets
73 */
74 static void *ctdb_tcp_allocate_pkt(TALLOC_CTX *mem_ctx, size_t size)
75 {
76         /* tcp transport needs to round to 8 byte alignment to ensure
77            that we can use a length header and 64 bit elements in
78            structures */
79         size = (size+(CTDB_TCP_ALIGNMENT-1)) & ~(CTDB_TCP_ALIGNMENT-1);
80         return talloc_size(mem_ctx, size);
81 }
82
83
84 static const struct ctdb_methods ctdb_tcp_methods = {
85         .start     = ctdb_tcp_start,
86         .add_node  = ctdb_tcp_add_node,
87         .queue_pkt = ctdb_tcp_queue_pkt,
88         .allocate_pkt = ctdb_tcp_allocate_pkt
89 };
90
91 /*
92   initialise tcp portion of ctdb 
93 */
94 int ctdb_tcp_init(struct ctdb_context *ctdb)
95 {
96         struct ctdb_tcp *ctcp;
97         ctcp = talloc_zero(ctdb, struct ctdb_tcp);
98         CTDB_NO_MEMORY(ctdb, ctcp);
99
100         ctcp->listen_fd = -1;
101         ctdb->private_data = ctcp;
102         ctdb->methods = &ctdb_tcp_methods;
103         return 0;
104 }
105