r23798: updated old Temple Place FSF addresses to new URL
[abartlet/samba.git/.git] / source4 / cluster / ctdb / tcp / tcp_io.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/events/events.h"
22 #include "lib/util/dlinklist.h"
23 #include "lib/tdb/include/tdb.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   called when a complete packet has come in
32  */
33 void ctdb_tcp_read_cb(uint8_t *data, size_t cnt, void *args)
34 {
35         struct ctdb_incoming *in = talloc_get_type(args, struct ctdb_incoming);
36         struct ctdb_req_header *hdr;
37
38         if (data == NULL) {
39                 /* incoming socket has died */
40                 talloc_free(in);
41                 return;
42         }
43
44         if (cnt < sizeof(*hdr)) {
45                 ctdb_set_error(in->ctdb, "Bad packet length %d\n", cnt);
46                 return;
47         }
48         hdr = (struct ctdb_req_header *)data;
49         if (cnt != hdr->length) {
50                 ctdb_set_error(in->ctdb, "Bad header length %d expected %d\n", 
51                                hdr->length, cnt);
52                 return;
53         }
54
55         if (hdr->ctdb_magic != CTDB_MAGIC) {
56                 ctdb_set_error(in->ctdb, "Non CTDB packet rejected\n");
57                 return;
58         }
59
60         if (hdr->ctdb_version != CTDB_VERSION) {
61                 ctdb_set_error(in->ctdb, "Bad CTDB version 0x%x rejected\n", hdr->ctdb_version);
62                 return;
63         }
64
65         /* most common case - we got a whole packet in one go
66            tell the ctdb layer above that we have a packet */
67         in->ctdb->upcalls->recv_pkt(in->ctdb, data, cnt);
68 }
69
70 /*
71   queue a packet for sending
72 */
73 int ctdb_tcp_queue_pkt(struct ctdb_node *node, uint8_t *data, uint32_t length)
74 {
75         struct ctdb_tcp_node *tnode = talloc_get_type(node->private_data,
76                                                       struct ctdb_tcp_node);
77         return ctdb_queue_send(tnode->queue, data, length);
78 }