Merged tridge's branch.
[samba.git] / 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 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   start the protocol going
31 */
32 int ctdb_tcp_start(struct ctdb_context *ctdb)
33 {
34         int i;
35
36         /* listen on our own address */
37         if (ctdb_tcp_listen(ctdb) != 0) return -1;
38
39         /* startup connections to the other servers - will happen on
40            next event loop */
41         for (i=0;i<ctdb->num_nodes;i++) {
42                 struct ctdb_node *node = *(ctdb->nodes + i);
43                 if (!(ctdb->flags & CTDB_FLAG_SELF_CONNECT) &&
44                     ctdb_same_address(&ctdb->address, &node->address)) continue;
45                 event_add_timed(ctdb->ev, node, timeval_zero(), 
46                                 ctdb_tcp_node_connect, node);
47         }
48
49         return 0;
50 }
51
52
53 /*
54   initialise tcp portion of a ctdb node 
55 */
56 int ctdb_tcp_add_node(struct ctdb_node *node)
57 {
58         struct ctdb_tcp_node *tnode;
59         tnode = talloc_zero(node, struct ctdb_tcp_node);
60         CTDB_NO_MEMORY(node->ctdb, tnode);
61
62         tnode->fd = -1;
63         node->private = tnode;
64         return 0;
65 }
66
67
68 /*
69   transport packet allocator - allows transport to control memory for packets
70 */
71 void *ctdb_tcp_allocate_pkt(struct ctdb_context *ctdb, size_t size)
72 {
73         /* tcp transport needs to round to 8 byte alignment to ensure
74            that we can use a length header and 64 bit elements in
75            structures */
76         size = (size+(CTDB_TCP_ALIGNMENT-1)) & ~(CTDB_TCP_ALIGNMENT-1);
77         return talloc_size(ctdb, size);
78 }
79
80
81 static const struct ctdb_methods ctdb_tcp_methods = {
82         .start     = ctdb_tcp_start,
83         .add_node  = ctdb_tcp_add_node,
84         .queue_pkt = ctdb_tcp_queue_pkt,
85         .allocate_pkt = ctdb_tcp_allocate_pkt
86 };
87
88 /*
89   initialise tcp portion of ctdb 
90 */
91 int ctdb_tcp_init(struct ctdb_context *ctdb)
92 {
93         struct ctdb_tcp *ctcp;
94         ctcp = talloc_zero(ctdb, struct ctdb_tcp);
95         CTDB_NO_MEMORY(ctdb, ctcp);
96
97         ctcp->listen_fd = -1;
98         ctdb->private = ctcp;
99         ctdb->methods = &ctdb_tcp_methods;
100         return 0;
101 }
102