paranoid checks for bad packets in tcp layer. Close the socket if it gets a bad packet
[obnox/samba/samba-obnox.git] / 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 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/events/events.h"
23 #include "lib/util/dlinklist.h"
24 #include "lib/tdb/include/tdb.h"
25 #include "system/network.h"
26 #include "system/filesys.h"
27 #include "../include/ctdb_private.h"
28 #include "ctdb_tcp.h"
29
30
31 /*
32   called when a complete packet has come in
33  */
34 void ctdb_tcp_read_cb(uint8_t *data, size_t cnt, void *args)
35 {
36         struct ctdb_incoming *in = talloc_get_type(args, struct ctdb_incoming);
37         struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
38
39         if (data == NULL) {
40                 /* incoming socket has died */
41                 goto failed;
42         }
43
44         if (cnt < sizeof(*hdr)) {
45                 DEBUG(0,(__location__ " Bad packet length %u\n", (unsigned)cnt));
46                 goto failed;
47         }
48
49         if (cnt & (CTDB_TCP_ALIGNMENT-1)) {
50                 DEBUG(0,(__location__ " Length 0x%x not multiple of alignment\n", cnt));
51                 goto failed;
52         }
53
54
55         if (cnt != hdr->length) {
56                 DEBUG(0,(__location__ " Bad header length %u expected %u\n", 
57                          (unsigned)hdr->length, (unsigned)cnt));
58                 goto failed;
59         }
60
61         if (hdr->ctdb_magic != CTDB_MAGIC) {
62                 DEBUG(0,(__location__ " Non CTDB packet 0x%x rejected\n", 
63                          hdr->ctdb_magic));
64                 goto failed;
65         }
66
67         if (hdr->ctdb_version != CTDB_VERSION) {
68                 DEBUG(0, (__location__ " Bad CTDB version 0x%x rejected\n", 
69                           hdr->ctdb_version));
70                 goto failed;
71         }
72
73         /* tell the ctdb layer above that we have a packet */
74         in->ctdb->upcalls->recv_pkt(in->ctdb, data, cnt);
75         return;
76
77 failed:
78         talloc_free(in);
79 }
80
81 /*
82   queue a packet for sending
83 */
84 int ctdb_tcp_queue_pkt(struct ctdb_node *node, uint8_t *data, uint32_t length)
85 {
86         struct ctdb_tcp_node *tnode = talloc_get_type(node->private_data,
87                                                       struct ctdb_tcp_node);
88         return ctdb_queue_send(tnode->queue, data, length);
89 }