we need to listen at transport initialise stage to find our own node number
[sahlberg/ctdb.git] / 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 2 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, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "includes.h"
22 #include "lib/tdb/include/tdb.h"
23 #include "lib/events/events.h"
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #include "../include/ctdb_private.h"
27 #include "ctdb_tcp.h"
28
29
30 /*
31   initialise tcp portion of a ctdb node 
32 */
33 static int ctdb_tcp_add_node(struct ctdb_node *node)
34 {
35         struct ctdb_tcp_node *tnode;
36         tnode = talloc_zero(node, struct ctdb_tcp_node);
37         CTDB_NO_MEMORY(node->ctdb, tnode);
38
39         tnode->fd = -1;
40         node->private_data = tnode;
41
42         tnode->queue = ctdb_queue_setup(node->ctdb, node, tnode->fd, CTDB_TCP_ALIGNMENT,
43                                         ctdb_tcp_tnode_cb, node);
44         
45         return 0;
46 }
47
48 /*
49   initialise transport structures
50 */
51 static int ctdb_tcp_initialise(struct ctdb_context *ctdb)
52 {
53         int i;
54
55         /* listen on our own address */
56         if (ctdb_tcp_listen(ctdb) != 0) return -1;
57
58         for (i=0; i<ctdb->num_nodes; i++) {
59                 if (ctdb_tcp_add_node(ctdb->nodes[i]) != 0) {
60                         DEBUG(0, ("methods->add_node failed at %d\n", i));
61                         return -1;
62                 }
63         }
64         
65         return 0;
66 }
67
68 /*
69   start the protocol going
70 */
71 static int ctdb_tcp_start(struct ctdb_context *ctdb)
72 {
73         int i;
74
75         /* startup connections to the other servers - will happen on
76            next event loop */
77         for (i=0;i<ctdb->num_nodes;i++) {
78                 struct ctdb_node *node = *(ctdb->nodes + i);
79                 if (!(ctdb->flags & CTDB_FLAG_SELF_CONNECT) &&
80                     ctdb_same_address(&ctdb->address, &node->address)) continue;
81                 event_add_timed(ctdb->ev, node, timeval_zero(), 
82                                 ctdb_tcp_node_connect, node);
83         }
84
85         return 0;
86 }
87
88
89 /*
90   transport packet allocator - allows transport to control memory for packets
91 */
92 static void *ctdb_tcp_allocate_pkt(TALLOC_CTX *mem_ctx, size_t size)
93 {
94         /* tcp transport needs to round to 8 byte alignment to ensure
95            that we can use a length header and 64 bit elements in
96            structures */
97         size = (size+(CTDB_TCP_ALIGNMENT-1)) & ~(CTDB_TCP_ALIGNMENT-1);
98         return talloc_size(mem_ctx, size);
99 }
100
101
102 static const struct ctdb_methods ctdb_tcp_methods = {
103         .initialise   = ctdb_tcp_initialise,
104         .start        = ctdb_tcp_start,
105         .queue_pkt    = ctdb_tcp_queue_pkt,
106         .add_node     = ctdb_tcp_add_node,
107         .allocate_pkt = ctdb_tcp_allocate_pkt
108 };
109
110 /*
111   initialise tcp portion of ctdb 
112 */
113 int ctdb_tcp_init(struct ctdb_context *ctdb)
114 {
115         struct ctdb_tcp *ctcp;
116         ctcp = talloc_zero(ctdb, struct ctdb_tcp);
117         CTDB_NO_MEMORY(ctdb, ctcp);
118
119         ctcp->listen_fd = -1;
120         ctdb->private_data = ctcp;
121         ctdb->methods = &ctdb_tcp_methods;
122         return 0;
123 }
124