summaryrefslogtreecommitdiff
path: root/source4/cluster/ctdb/tcp
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2007-07-10 08:06:51 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 14:59:20 -0500
commit6504900f1f52927adab3489b8d04b6644ceaee7d (patch)
tree893cadba231a529f320046429767cedf8b0eec30 /source4/cluster/ctdb/tcp
parentd4e2d157cbce5a1cdc75efb5ced75d9608a0f7df (diff)
downloadsamba-6504900f1f52927adab3489b8d04b6644ceaee7d.tar.gz
samba-6504900f1f52927adab3489b8d04b6644ceaee7d.tar.bz2
samba-6504900f1f52927adab3489b8d04b6644ceaee7d.zip
r23806: update Samba4 with the latest ctdb code.
This doesn't get the ctdb code fully working in Samba4, it just gets it building and not breaking non-clustered use of Samba. It will take a bit longer to update some of the calling ctdb_cluster.c code to make it work correctly in Samba4. Note also that Samba4 now only links to the client portion of ctdb. For the moment I am leaving the ctdbd as a separate daemon, which you install separately from http://ctdb.samba.org/. (This used to be commit b196077cbb55cbecad87065133c2d67198e31066)
Diffstat (limited to 'source4/cluster/ctdb/tcp')
-rw-r--r--source4/cluster/ctdb/tcp/ctdb_tcp.h26
-rw-r--r--source4/cluster/ctdb/tcp/tcp_connect.c247
-rw-r--r--source4/cluster/ctdb/tcp/tcp_init.c103
-rw-r--r--source4/cluster/ctdb/tcp/tcp_io.c65
4 files changed, 305 insertions, 136 deletions
diff --git a/source4/cluster/ctdb/tcp/ctdb_tcp.h b/source4/cluster/ctdb/tcp/ctdb_tcp.h
index 6fbe145900..3a1285f47c 100644
--- a/source4/cluster/ctdb/tcp/ctdb_tcp.h
+++ b/source4/cluster/ctdb/tcp/ctdb_tcp.h
@@ -3,18 +3,18 @@
Copyright (C) Andrew Tridgell 2006
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 3 of the License, or (at your option) any later version.
-
- This library is distributed in the hope that it will be useful,
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
@@ -37,7 +37,9 @@ struct ctdb_incoming {
*/
struct ctdb_tcp_node {
int fd;
- struct ctdb_queue *queue;
+ struct ctdb_queue *out_queue;
+ struct fd_event *connect_fde;
+ struct timed_event *connect_te;
};
diff --git a/source4/cluster/ctdb/tcp/tcp_connect.c b/source4/cluster/ctdb/tcp/tcp_connect.c
index c0a0da5d47..2f828e5717 100644
--- a/source4/cluster/ctdb/tcp/tcp_connect.c
+++ b/source4/cluster/ctdb/tcp/tcp_connect.c
@@ -3,18 +3,18 @@
Copyright (C) Andrew Tridgell 2006
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 3 of the License, or (at your option) any later version.
-
- This library is distributed in the hope that it will be useful,
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
@@ -25,14 +25,6 @@
#include "../include/ctdb_private.h"
#include "ctdb_tcp.h"
-static void set_nonblocking(int fd)
-{
- unsigned v;
- v = fcntl(fd, F_GETFL, 0);
- fcntl(fd, F_SETFL, v | O_NONBLOCK);
-}
-
-
/*
called when a complete packet has come in - should not happen on this socket
*/
@@ -48,10 +40,9 @@ void ctdb_tcp_tnode_cb(uint8_t *data, size_t cnt, void *private_data)
/* start a new connect cycle to try to re-establish the
link */
- close(tnode->fd);
- ctdb_queue_set_fd(tnode->queue, -1);
+ ctdb_queue_set_fd(tnode->out_queue, -1);
tnode->fd = -1;
- event_add_timed(node->ctdb->ev, node, timeval_zero(),
+ event_add_timed(node->ctdb->ev, tnode, timeval_zero(),
ctdb_tcp_node_connect, node);
}
@@ -70,12 +61,15 @@ static void ctdb_node_connect_write(struct event_context *ev, struct fd_event *f
socklen_t len = sizeof(error);
int one = 1;
+ talloc_free(tnode->connect_te);
+ tnode->connect_te = NULL;
+
if (getsockopt(tnode->fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0 ||
error != 0) {
talloc_free(fde);
close(tnode->fd);
tnode->fd = -1;
- event_add_timed(ctdb->ev, node, timeval_current_ofs(1, 0),
+ event_add_timed(ctdb->ev, tnode, timeval_current_ofs(1, 0),
ctdb_tcp_node_connect, node);
return;
}
@@ -83,8 +77,9 @@ static void ctdb_node_connect_write(struct event_context *ev, struct fd_event *f
talloc_free(fde);
setsockopt(tnode->fd,IPPROTO_TCP,TCP_NODELAY,(char *)&one,sizeof(one));
+ setsockopt(tnode->fd,SOL_SOCKET,SO_KEEPALIVE,(char *)&one,sizeof(one));
- ctdb_queue_set_fd(tnode->queue, tnode->fd);
+ ctdb_queue_set_fd(tnode->out_queue, tnode->fd);
/* tell the ctdb layer we are connected */
node->ctdb->upcalls->node_connected(node);
@@ -120,10 +115,22 @@ void ctdb_tcp_node_connect(struct event_context *ev, struct timed_event *te,
struct sockaddr_in sock_in;
struct sockaddr_in sock_out;
+ if (tnode->fd != -1) {
+ talloc_free(tnode->connect_fde);
+ tnode->connect_fde = NULL;
+ close(tnode->fd);
+ tnode->fd = -1;
+ }
+
tnode->fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
set_nonblocking(tnode->fd);
+ set_close_on_exec(tnode->fd);
+ ZERO_STRUCT(sock_out);
+#ifdef HAVE_SOCK_SIN_LEN
+ sock_out.sin_len = sizeof(sock_out);
+#endif
if (ctdb_tcp_get_address(ctdb, node->address.address, &sock_out.sin_addr) != 0) {
return;
}
@@ -137,6 +144,10 @@ void ctdb_tcp_node_connect(struct event_context *ev, struct timed_event *te,
* the remote side is actually routable in case CTDB traffic will run on
* a dedicated non-routeable network.
*/
+ ZERO_STRUCT(sock_in);
+#ifdef HAVE_SOCK_SIN_LEN
+ sock_in.sin_len = sizeof(sock_in);
+#endif
if (ctdb_tcp_get_address(ctdb, ctdb->address.address, &sock_in.sin_addr) != 0) {
return;
}
@@ -148,24 +159,22 @@ void ctdb_tcp_node_connect(struct event_context *ev, struct timed_event *te,
errno != EINPROGRESS) {
/* try again once a second */
close(tnode->fd);
- event_add_timed(ctdb->ev, node, timeval_current_ofs(1, 0),
+ tnode->fd = -1;
+ event_add_timed(ctdb->ev, tnode, timeval_current_ofs(1, 0),
ctdb_tcp_node_connect, node);
return;
}
/* non-blocking connect - wait for write event */
- event_add_fd(node->ctdb->ev, node, tnode->fd, EVENT_FD_WRITE|EVENT_FD_READ,
- ctdb_node_connect_write, node);
-}
-
-/*
- destroy a ctdb_incoming structure
-*/
-static int ctdb_incoming_destructor(struct ctdb_incoming *in)
-{
- close(in->fd);
- in->fd = -1;
- return 0;
+ tnode->connect_fde = event_add_fd(node->ctdb->ev, tnode, tnode->fd,
+ EVENT_FD_WRITE|EVENT_FD_READ,
+ ctdb_node_connect_write, node);
+
+ /* don't give it long to connect - retry in one second. This ensures
+ that we find a node is up quickly (tcp normally backs off a syn reply
+ delay by quite a lot) */
+ tnode->connect_te = event_add_timed(ctdb->ev, tnode, timeval_current_ofs(1, 0),
+ ctdb_tcp_node_connect, node);
}
/*
@@ -176,30 +185,129 @@ static int ctdb_incoming_destructor(struct ctdb_incoming *in)
static void ctdb_listen_event(struct event_context *ev, struct fd_event *fde,
uint16_t flags, void *private_data)
{
- struct ctdb_context *ctdb;
- struct ctdb_tcp *ctcp;
+ struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
+ struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data, struct ctdb_tcp);
struct sockaddr_in addr;
socklen_t len;
- int fd;
+ int fd, nodeid;
struct ctdb_incoming *in;
+ int one = 1;
+ const char *incoming_node;
- ctdb = talloc_get_type(private_data, struct ctdb_context);
- ctcp = talloc_get_type(ctdb->private_data, struct ctdb_tcp);
memset(&addr, 0, sizeof(addr));
len = sizeof(addr);
fd = accept(ctcp->listen_fd, (struct sockaddr *)&addr, &len);
if (fd == -1) return;
- in = talloc_zero(ctdb, struct ctdb_incoming);
+ incoming_node = inet_ntoa(addr.sin_addr);
+ for (nodeid=0;nodeid<ctdb->num_nodes;nodeid++) {
+ if (!strcmp(incoming_node, ctdb->nodes[nodeid]->address.address)) {
+ DEBUG(0, ("Incoming connection from node:%d %s\n",nodeid,incoming_node));
+ break;
+ }
+ }
+ if (nodeid>=ctdb->num_nodes) {
+ DEBUG(0, ("Refused connection from unknown node %s\n", incoming_node));
+ close(fd);
+ return;
+ }
+
+ in = talloc_zero(ctcp, struct ctdb_incoming);
in->fd = fd;
in->ctdb = ctdb;
set_nonblocking(in->fd);
+ set_close_on_exec(in->fd);
+
+ setsockopt(in->fd,SOL_SOCKET,SO_KEEPALIVE,(char *)&one,sizeof(one));
in->queue = ctdb_queue_setup(ctdb, in, in->fd, CTDB_TCP_ALIGNMENT,
ctdb_tcp_read_cb, in);
+}
+
+
+/*
+ automatically find which address to listen on
+*/
+static int ctdb_tcp_listen_automatic(struct ctdb_context *ctdb)
+{
+ struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
+ struct ctdb_tcp);
+ struct sockaddr_in sock;
+ int lock_fd, i;
+ const char *lock_path = "/tmp/.ctdb_socket_lock";
+ struct flock lock;
+
+ /* in order to ensure that we don't get two nodes with the
+ same adddress, we must make the bind() and listen() calls
+ atomic. The SO_REUSEADDR setsockopt only prevents double
+ binds if the first socket is in LISTEN state */
+ lock_fd = open(lock_path, O_RDWR|O_CREAT, 0666);
+ if (lock_fd == -1) {
+ DEBUG(0,("Unable to open %s\n", lock_path));
+ return -1;
+ }
+
+ lock.l_type = F_WRLCK;
+ lock.l_whence = SEEK_SET;
+ lock.l_start = 0;
+ lock.l_len = 1;
+ lock.l_pid = 0;
+
+ if (fcntl(lock_fd, F_SETLKW, &lock) != 0) {
+ DEBUG(0,("Unable to lock %s\n", lock_path));
+ close(lock_fd);
+ return -1;
+ }
+
+ for (i=0;i<ctdb->num_nodes;i++) {
+ ZERO_STRUCT(sock);
+#ifdef HAVE_SOCK_SIN_LEN
+ sock.sin_len = sizeof(sock);
+#endif
+ sock.sin_port = htons(ctdb->nodes[i]->address.port);
+ sock.sin_family = PF_INET;
+ if (ctdb_tcp_get_address(ctdb, ctdb->nodes[i]->address.address,
+ &sock.sin_addr) != 0) {
+ continue;
+ }
+
+ if (bind(ctcp->listen_fd, (struct sockaddr * )&sock,
+ sizeof(sock)) == 0) {
+ break;
+ }
+ }
+
+ if (i == ctdb->num_nodes) {
+ DEBUG(0,("Unable to bind to any of the node addresses - giving up\n"));
+ goto failed;
+ }
+ ctdb->address = ctdb->nodes[i]->address;
+ ctdb->name = talloc_asprintf(ctdb, "%s:%u",
+ ctdb->address.address,
+ ctdb->address.port);
+ ctdb->vnn = ctdb->nodes[i]->vnn;
+ ctdb->nodes[i]->flags &= ~NODE_FLAGS_DISCONNECTED;
+ DEBUG(1,("ctdb chose network address %s:%u vnn %u\n",
+ ctdb->address.address,
+ ctdb->address.port,
+ ctdb->vnn));
+
+ if (listen(ctcp->listen_fd, 10) == -1) {
+ goto failed;
+ }
+
+ event_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
+ ctdb_listen_event, ctdb);
- talloc_set_destructor(in, ctdb_incoming_destructor);
+ close(lock_fd);
+ return 0;
+
+failed:
+ close(lock_fd);
+ close(ctcp->listen_fd);
+ ctcp->listen_fd = -1;
+ return -1;
}
@@ -213,37 +321,50 @@ int ctdb_tcp_listen(struct ctdb_context *ctdb)
struct sockaddr_in sock;
int one = 1;
- sock.sin_port = htons(ctdb->address.port);
- sock.sin_family = PF_INET;
- if (ctdb_tcp_get_address(ctdb, ctdb->address.address, &sock.sin_addr) != 0) {
+ ctcp->listen_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (ctcp->listen_fd == -1) {
+ ctdb_set_error(ctdb, "socket failed\n");
return -1;
}
- ctcp->listen_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (ctcp->listen_fd == -1) {
- ctdb_set_error(ctdb, "socket failed\n");
- return -1;
- }
+ set_close_on_exec(ctcp->listen_fd);
setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
- if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sizeof(sock)) != 0) {
- ctdb_set_error(ctdb, "bind failed\n");
- close(ctcp->listen_fd);
- ctcp->listen_fd = -1;
- return -1;
- }
+ /* we can either auto-bind to the first available address, or we can
+ use a specified address */
+ if (!ctdb->address.address) {
+ return ctdb_tcp_listen_automatic(ctdb);
+ }
+
+ ZERO_STRUCT(sock);
+#ifdef HAVE_SOCK_SIN_LEN
+ sock.sin_len = sizeof(sock);
+#endif
+ sock.sin_port = htons(ctdb->address.port);
+ sock.sin_family = PF_INET;
+
+ if (ctdb_tcp_get_address(ctdb, ctdb->address.address,
+ &sock.sin_addr) != 0) {
+ goto failed;
+ }
+
+ if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sizeof(sock)) != 0) {
+ goto failed;
+ }
if (listen(ctcp->listen_fd, 10) == -1) {
- ctdb_set_error(ctdb, "listen failed\n");
- close(ctcp->listen_fd);
- ctcp->listen_fd = -1;
- return -1;
+ goto failed;
}
- event_add_fd(ctdb->ev, ctdb, ctcp->listen_fd, EVENT_FD_READ,
+ event_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
ctdb_listen_event, ctdb);
return 0;
+
+failed:
+ close(ctcp->listen_fd);
+ ctcp->listen_fd = -1;
+ return -1;
}
diff --git a/source4/cluster/ctdb/tcp/tcp_init.c b/source4/cluster/ctdb/tcp/tcp_init.c
index b4b7910446..f5d4e4c1d6 100644
--- a/source4/cluster/ctdb/tcp/tcp_init.c
+++ b/source4/cluster/ctdb/tcp/tcp_init.c
@@ -3,18 +3,18 @@
Copyright (C) Andrew Tridgell 2006
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 3 of the License, or (at your option) any later version.
-
- This library is distributed in the hope that it will be useful,
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
@@ -25,24 +25,64 @@
#include "../include/ctdb_private.h"
#include "ctdb_tcp.h"
+
/*
- start the protocol going
+ initialise tcp portion of a ctdb node
*/
-static int ctdb_tcp_start(struct ctdb_context *ctdb)
+static int ctdb_tcp_add_node(struct ctdb_node *node)
+{
+ struct ctdb_tcp *ctcp = talloc_get_type(node->ctdb->private_data,
+ struct ctdb_tcp);
+ struct ctdb_tcp_node *tnode;
+ tnode = talloc_zero(ctcp, struct ctdb_tcp_node);
+ CTDB_NO_MEMORY(node->ctdb, tnode);
+
+ tnode->fd = -1;
+ node->private_data = tnode;
+
+ tnode->out_queue = ctdb_queue_setup(node->ctdb, ctcp, tnode->fd, CTDB_TCP_ALIGNMENT,
+ ctdb_tcp_tnode_cb, node);
+
+ return 0;
+}
+
+/*
+ initialise transport structures
+*/
+static int ctdb_tcp_initialise(struct ctdb_context *ctdb)
{
int i;
/* listen on our own address */
if (ctdb_tcp_listen(ctdb) != 0) return -1;
+ for (i=0; i<ctdb->num_nodes; i++) {
+ if (ctdb_tcp_add_node(ctdb->nodes[i]) != 0) {
+ DEBUG(0, ("methods->add_node failed at %d\n", i));
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+/*
+ start the protocol going
+*/
+static int ctdb_tcp_start(struct ctdb_context *ctdb)
+{
+ int i;
+
/* startup connections to the other servers - will happen on
next event loop */
for (i=0;i<ctdb->num_nodes;i++) {
struct ctdb_node *node = *(ctdb->nodes + i);
- if (!(ctdb->flags & CTDB_FLAG_SELF_CONNECT) &&
- ctdb_same_address(&ctdb->address, &node->address)) continue;
- event_add_timed(ctdb->ev, node, timeval_zero(),
- ctdb_tcp_node_connect, node);
+ struct ctdb_tcp_node *tnode = talloc_get_type(
+ node->private_data, struct ctdb_tcp_node);
+ if (!ctdb_same_address(&ctdb->address, &node->address)) {
+ event_add_timed(ctdb->ev, tnode, timeval_zero(),
+ ctdb_tcp_node_connect, node);
+ }
}
return 0;
@@ -50,21 +90,14 @@ static int ctdb_tcp_start(struct ctdb_context *ctdb)
/*
- initialise tcp portion of a ctdb node
+ shutdown the transport
*/
-static int ctdb_tcp_add_node(struct ctdb_node *node)
+static void ctdb_tcp_shutdown(struct ctdb_context *ctdb)
{
- struct ctdb_tcp_node *tnode;
- tnode = talloc_zero(node, struct ctdb_tcp_node);
- CTDB_NO_MEMORY(node->ctdb, tnode);
-
- tnode->fd = -1;
- node->private_data = tnode;
-
- tnode->queue = ctdb_queue_setup(node->ctdb, node, tnode->fd, CTDB_TCP_ALIGNMENT,
- ctdb_tcp_tnode_cb, node);
-
- return 0;
+ struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
+ struct ctdb_tcp);
+ talloc_free(ctcp);
+ ctdb->private_data = NULL;
}
@@ -82,10 +115,12 @@ static void *ctdb_tcp_allocate_pkt(TALLOC_CTX *mem_ctx, size_t size)
static const struct ctdb_methods ctdb_tcp_methods = {
- .start = ctdb_tcp_start,
- .add_node = ctdb_tcp_add_node,
- .queue_pkt = ctdb_tcp_queue_pkt,
- .allocate_pkt = ctdb_tcp_allocate_pkt
+ .initialise = ctdb_tcp_initialise,
+ .start = ctdb_tcp_start,
+ .queue_pkt = ctdb_tcp_queue_pkt,
+ .add_node = ctdb_tcp_add_node,
+ .allocate_pkt = ctdb_tcp_allocate_pkt,
+ .shutdown = ctdb_tcp_shutdown,
};
/*
diff --git a/source4/cluster/ctdb/tcp/tcp_io.c b/source4/cluster/ctdb/tcp/tcp_io.c
index 49d5132a8a..c10afb3425 100644
--- a/source4/cluster/ctdb/tcp/tcp_io.c
+++ b/source4/cluster/ctdb/tcp/tcp_io.c
@@ -3,18 +3,18 @@
Copyright (C) Andrew Tridgell 2006
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 3 of the License, or (at your option) any later version.
-
- This library is distributed in the hope that it will be useful,
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
@@ -33,38 +33,49 @@
void ctdb_tcp_read_cb(uint8_t *data, size_t cnt, void *args)
{
struct ctdb_incoming *in = talloc_get_type(args, struct ctdb_incoming);
- struct ctdb_req_header *hdr;
+ struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
if (data == NULL) {
/* incoming socket has died */
- talloc_free(in);
- return;
+ goto failed;
}
if (cnt < sizeof(*hdr)) {
- ctdb_set_error(in->ctdb, "Bad packet length %d\n", cnt);
- return;
+ DEBUG(0,(__location__ " Bad packet length %u\n", (unsigned)cnt));
+ goto failed;
+ }
+
+ if (cnt & (CTDB_TCP_ALIGNMENT-1)) {
+ DEBUG(0,(__location__ " Length 0x%x not multiple of alignment\n",
+ (unsigned)cnt));
+ goto failed;
}
- hdr = (struct ctdb_req_header *)data;
+
+
if (cnt != hdr->length) {
- ctdb_set_error(in->ctdb, "Bad header length %d expected %d\n",
- hdr->length, cnt);
- return;
+ DEBUG(0,(__location__ " Bad header length %u expected %u\n",
+ (unsigned)hdr->length, (unsigned)cnt));
+ goto failed;
}
if (hdr->ctdb_magic != CTDB_MAGIC) {
- ctdb_set_error(in->ctdb, "Non CTDB packet rejected\n");
- return;
+ DEBUG(0,(__location__ " Non CTDB packet 0x%x rejected\n",
+ hdr->ctdb_magic));
+ goto failed;
}
if (hdr->ctdb_version != CTDB_VERSION) {
- ctdb_set_error(in->ctdb, "Bad CTDB version 0x%x rejected\n", hdr->ctdb_version);
- return;
+ DEBUG(0, (__location__ " Bad CTDB version 0x%x rejected\n",
+ hdr->ctdb_version));
+ goto failed;
}
- /* most common case - we got a whole packet in one go
- tell the ctdb layer above that we have a packet */
+ /* tell the ctdb layer above that we have a packet */
in->ctdb->upcalls->recv_pkt(in->ctdb, data, cnt);
+ return;
+
+failed:
+ talloc_free(in);
}
/*
@@ -74,5 +85,5 @@ int ctdb_tcp_queue_pkt(struct ctdb_node *node, uint8_t *data, uint32_t length)
{
struct ctdb_tcp_node *tnode = talloc_get_type(node->private_data,
struct ctdb_tcp_node);
- return ctdb_queue_send(tnode->queue, data, length);
+ return ctdb_queue_send(tnode->out_queue, data, length);
}