ctdb-daemon: Rename struct ctdb_client_notify_register to ctdb_notify_data_old
[obnox/samba/samba-obnox.git] / source3 / lib / ctdbd_conn.c
index 5044ddfb328cfad0ff5a701249052166fbe04e47..e67a31d5bc6becce12a345abf3af56b89614b970 100644 (file)
 #include "serverid.h"
 #include "ctdbd_conn.h"
 #include "system/select.h"
+#include "lib/util/sys_rw_data.h"
+#include "lib/util/iov_buf.h"
 
 #include "messages.h"
 
-/*
- * It is not possible to include ctdb.h and tdb_compat.h (included via
- * some other include above) without warnings. This fixes those
- * warnings.
- */
-
-#ifdef typesafe_cb
-#undef typesafe_cb
-#endif
-
-#ifdef typesafe_cb_preargs
-#undef typesafe_cb_preargs
-#endif
-
-#ifdef typesafe_cb_postargs
-#undef typesafe_cb_postargs
-#endif
-
 /* paths to these include files come from --with-ctdb= in configure */
 
-#include "ctdb.h"
 #include "ctdb_private.h"
 
+struct ctdbd_srvid_cb {
+       uint64_t srvid;
+       int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
+                 uint64_t dst_srvid,
+                 const uint8_t *msg, size_t msglen,
+                 void *private_data);
+       void *private_data;
+};
+
 struct ctdbd_connection {
+       const char *sockname;   /* Needed in ctdbd_traverse */
        struct messaging_context *msg_ctx;
        uint32_t reqid;
        uint32_t our_vnn;
        uint64_t rand_srvid;
+       struct ctdbd_srvid_cb *callbacks;
        int fd;
        struct tevent_fd *fde;
-
-       bool (*release_ip_handler)(const char *ip_addr, void *private_data);
-       void *release_ip_priv;
+       int timeout;
 };
 
 static uint32_t ctdbd_next_reqid(struct ctdbd_connection *conn)
@@ -70,11 +62,12 @@ static uint32_t ctdbd_next_reqid(struct ctdbd_connection *conn)
        return conn->reqid;
 }
 
-static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
-                             uint32_t vnn, uint32_t opcode,
-                             uint64_t srvid, uint32_t flags, TDB_DATA data,
-                             TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
-                             int *cstatus);
+static int ctdbd_control(struct ctdbd_connection *conn,
+                        uint32_t vnn, uint32_t opcode,
+                        uint64_t srvid, uint32_t flags,
+                        TDB_DATA data,
+                        TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
+                        int *cstatus);
 
 /*
  * exit on fatal communications errors with the ctdbd daemon
@@ -106,31 +99,96 @@ static void ctdb_packet_dump(struct ctdb_req_header *hdr)
 /*
  * Register a srvid with ctdbd
  */
-NTSTATUS register_with_ctdbd(struct ctdbd_connection *conn, uint64_t srvid)
+int register_with_ctdbd(struct ctdbd_connection *conn, uint64_t srvid,
+                       int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
+                                 uint64_t dst_srvid,
+                                 const uint8_t *msg, size_t msglen,
+                                 void *private_data),
+                       void *private_data)
 {
 
-       int cstatus;
-       return ctdbd_control(conn, CTDB_CURRENT_NODE,
-                            CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
-                            tdb_null, NULL, NULL, &cstatus);
+       int ret, cstatus;
+       size_t num_callbacks;
+       struct ctdbd_srvid_cb *tmp;
+
+       ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
+                           CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
+                           tdb_null, NULL, NULL, &cstatus);
+       if (ret != 0) {
+               return ret;
+       }
+
+       num_callbacks = talloc_array_length(conn->callbacks);
+
+       tmp = talloc_realloc(conn, conn->callbacks, struct ctdbd_srvid_cb,
+                            num_callbacks + 1);
+       if (tmp == NULL) {
+               return ENOMEM;
+       }
+       conn->callbacks = tmp;
+
+       conn->callbacks[num_callbacks] = (struct ctdbd_srvid_cb) {
+               .srvid = srvid, .cb = cb, .private_data = private_data
+       };
+
+       return 0;
+}
+
+static int ctdbd_msg_call_back(struct ctdbd_connection *conn,
+                              struct ctdb_req_message_old *msg)
+{
+       size_t msg_len;
+       size_t i, num_callbacks;
+
+       msg_len = msg->hdr.length;
+       if (msg_len < offsetof(struct ctdb_req_message_old, data)) {
+               DEBUG(10, ("%s: len %u too small\n", __func__,
+                          (unsigned)msg_len));
+               return 0;
+       }
+       msg_len -= offsetof(struct ctdb_req_message_old, data);
+
+       if (msg_len < msg->datalen) {
+               DEBUG(10, ("%s: msg_len=%u < msg->datalen=%u\n", __func__,
+                          (unsigned)msg_len, (unsigned)msg->datalen));
+               return 0;
+       }
+
+       num_callbacks = talloc_array_length(conn->callbacks);
+
+       for (i=0; i<num_callbacks; i++) {
+               struct ctdbd_srvid_cb *cb = &conn->callbacks[i];
+
+               if ((cb->srvid == msg->srvid) && (cb->cb != NULL)) {
+                       int ret;
+
+                       ret = cb->cb(msg->hdr.srcnode, msg->hdr.destnode,
+                                    msg->srvid, msg->data, msg->datalen,
+                                    cb->private_data);
+                       if (ret != 0) {
+                               return ret;
+                       }
+               }
+       }
+       return 0;
 }
 
 /*
  * get our vnn from the cluster
  */
-static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32_t *vnn)
+static int get_cluster_vnn(struct ctdbd_connection *conn, uint32_t *vnn)
 {
        int32_t cstatus=-1;
-       NTSTATUS status;
-       status = ctdbd_control(conn,
-                              CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
-                              tdb_null, NULL, NULL, &cstatus);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status)));
-               return status;
+       int ret;
+       ret = ctdbd_control(conn,
+                           CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
+                           tdb_null, NULL, NULL, &cstatus);
+       if (ret != 0) {
+               DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret)));
+               return ret;
        }
        *vnn = (uint32_t)cstatus;
-       return status;
+       return ret;
 }
 
 /*
@@ -139,18 +197,17 @@ static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32_t *vnn)
 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
 {
        int32_t cstatus=-1;
-       NTSTATUS status;
        TDB_DATA outdata;
-       struct ctdb_node_map *m;
+       struct ctdb_node_map_old *m;
        uint32_t failure_flags;
-       bool ret = false;
-       int i;
-
-       status = ctdbd_control(conn, CTDB_CURRENT_NODE,
-                              CTDB_CONTROL_GET_NODEMAP, 0, 0,
-                              tdb_null, talloc_tos(), &outdata, &cstatus);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status)));
+       bool ok = false;
+       int i, ret;
+
+       ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
+                           CTDB_CONTROL_GET_NODEMAP, 0, 0,
+                           tdb_null, talloc_tos(), &outdata, &cstatus);
+       if (ret != 0) {
+               DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret)));
                return false;
        }
        if ((cstatus != 0) || (outdata.dptr == NULL)) {
@@ -158,7 +215,7 @@ static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
                return false;
        }
 
-       m = (struct ctdb_node_map *)outdata.dptr;
+       m = (struct ctdb_node_map_old *)outdata.dptr;
 
        for (i=0; i<m->num; i++) {
                if (vnn == m->nodes[i].pnn) {
@@ -181,10 +238,10 @@ static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
                goto fail;
        }
 
-       ret = true;
+       ok = true;
 fail:
        TALLOC_FREE(outdata.dptr);
-       return ret;
+       return ok;
 }
 
 uint32_t ctdbd_vnn(const struct ctdbd_connection *conn)
@@ -192,25 +249,12 @@ uint32_t ctdbd_vnn(const struct ctdbd_connection *conn)
        return conn->our_vnn;
 }
 
-const char *lp_ctdbd_socket(void)
-{
-       const char *ret;
-
-       ret = lp__ctdbd_socket();
-       if (ret != NULL && strlen(ret) > 0) {
-               return ret;
-       }
-
-       return CTDB_SOCKET;
-}
-
 /*
  * Get us a ctdb connection
  */
 
-static int ctdbd_connect(int *pfd)
+static int ctdbd_connect(const char *sockname, int *pfd)
 {
-       const char *sockname = lp_ctdbd_socket();
        struct sockaddr_un addr = { 0, };
        int fd;
        socklen_t salen;
@@ -247,129 +291,60 @@ static int ctdbd_connect(int *pfd)
        return 0;
 }
 
-/*
- * State necessary to defer an incoming message while we are waiting for a
- * ctdb reply.
- */
-
-struct deferred_msg_state {
-       struct messaging_context *msg_ctx;
-       struct messaging_rec *rec;
-};
-
-/*
- * Timed event handler for the deferred message
- */
-
-static void deferred_message_dispatch(struct tevent_context *event_ctx,
-                                     struct tevent_timer *te,
-                                     struct timeval now,
-                                     void *private_data)
+static int ctdb_read_packet(int fd, int timeout, TALLOC_CTX *mem_ctx,
+                           struct ctdb_req_header **result)
 {
-       struct deferred_msg_state *state = talloc_get_type_abort(
-               private_data, struct deferred_msg_state);
-
-       messaging_dispatch_rec(state->msg_ctx, state->rec);
-       TALLOC_FREE(state);
-       TALLOC_FREE(te);
-}
-
-/*
- * Fetch a messaging_rec from an incoming ctdb style message
- */
-
-static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
-                                                    size_t overall_length,
-                                                    struct ctdb_req_message *msg)
-{
-       struct messaging_rec *result;
-       DATA_BLOB blob;
-       enum ndr_err_code ndr_err;
-
-       if ((overall_length < offsetof(struct ctdb_req_message, data))
-           || (overall_length
-               < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
-
-               cluster_fatal("got invalid msg length");
-       }
-
-       if (!(result = talloc(mem_ctx, struct messaging_rec))) {
-               DEBUG(0, ("talloc failed\n"));
-               return NULL;
-       }
-
-       blob = data_blob_const(msg->data, msg->datalen);
-
-       ndr_err = ndr_pull_struct_blob(
-               &blob, result, result,
-               (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
-
-       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
-                         ndr_errstr(ndr_err)));
-               TALLOC_FREE(result);
-               return NULL;
-       }
-
-       if (DEBUGLEVEL >= 11) {
-               DEBUG(11, ("ctdb_pull_messaging_rec:\n"));
-               NDR_PRINT_DEBUG(messaging_rec, result);
-       }
-
-       return result;
-}
-
-static NTSTATUS ctdb_read_packet(int fd, TALLOC_CTX *mem_ctx,
-                                struct ctdb_req_header **result)
-{
-       int timeout = lp_ctdb_timeout();
        struct ctdb_req_header *req;
        int ret, revents;
        uint32_t msglen;
-       NTSTATUS status;
-
-       if (timeout == 0) {
-               timeout = -1;
-       }
+       ssize_t nread;
 
        if (timeout != -1) {
-               ret = poll_one_fd(fd, POLLIN, timeout, &revents);
+               ret = poll_intr_one_fd(fd, POLLIN, timeout, &revents);
                if (ret == -1) {
-                       return map_nt_error_from_unix(errno);
+                       return errno;
                }
                if (ret == 0) {
-                       return NT_STATUS_IO_TIMEOUT;
+                       return ETIMEDOUT;
                }
                if (ret != 1) {
-                       return NT_STATUS_UNEXPECTED_IO_ERROR;
+                       return EIO;
                }
        }
 
-       status = read_data(fd, (char *)&msglen, sizeof(msglen));
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+       nread = read_data(fd, &msglen, sizeof(msglen));
+       if (nread == -1) {
+               return errno;
+       }
+       if (nread == 0) {
+               return EIO;
        }
 
        if (msglen < sizeof(struct ctdb_req_header)) {
-               return NT_STATUS_UNEXPECTED_IO_ERROR;
+               return EIO;
        }
 
        req = talloc_size(mem_ctx, msglen);
        if (req == NULL) {
-               return NT_STATUS_NO_MEMORY;
+               return ENOMEM;
        }
        talloc_set_name_const(req, "struct ctdb_req_header");
 
        req->length = msglen;
 
-       status = read_data(fd, ((char *)req) + sizeof(msglen),
-                          msglen - sizeof(msglen));
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+       nread = read_data(fd, ((char *)req) + sizeof(msglen),
+                         msglen - sizeof(msglen));
+       if (nread == -1) {
+               TALLOC_FREE(req);
+               return errno;
+       }
+       if (nread == 0) {
+               TALLOC_FREE(req);
+               return EIO;
        }
 
        *result = req;
-       return NT_STATUS_OK;
+       return 0;
 }
 
 /*
@@ -377,18 +352,17 @@ static NTSTATUS ctdb_read_packet(int fd, TALLOC_CTX *mem_ctx,
  * messages that might come in between.
  */
 
-static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32_t reqid,
-                             TALLOC_CTX *mem_ctx,
-                             struct ctdb_req_header **result)
+static int ctdb_read_req(struct ctdbd_connection *conn, uint32_t reqid,
+                        TALLOC_CTX *mem_ctx, struct ctdb_req_header **result)
 {
        struct ctdb_req_header *hdr;
-       NTSTATUS status;
+       int ret;
 
  next_pkt:
 
-       status = ctdb_read_packet(conn->fd, mem_ctx, &hdr);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("ctdb_read_packet failed: %s\n", nt_errstr(status)));
+       ret = ctdb_read_packet(conn->fd, conn->timeout, mem_ctx, &hdr);
+       if (ret != 0) {
+               DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret)));
                cluster_fatal("ctdbd died\n");
        }
 
@@ -396,90 +370,23 @@ static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32_t reqid,
        ctdb_packet_dump(hdr);
 
        if (hdr->operation == CTDB_REQ_MESSAGE) {
-               struct tevent_timer *evt;
-               struct deferred_msg_state *msg_state;
-               struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
+               struct ctdb_req_message_old *msg = (struct ctdb_req_message_old *)hdr;
 
                if (conn->msg_ctx == NULL) {
                        DEBUG(1, ("Got a message without having a msg ctx, "
                                  "dropping msg %llu\n",
                                  (long long unsigned)msg->srvid));
-                       goto next_pkt;
-               }
-
-               if ((conn->release_ip_handler != NULL)
-                   && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
-                       bool ret;
-
-                       /* must be dispatched immediately */
-                       DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
-                       ret = conn->release_ip_handler((const char *)msg->data,
-                                                      conn->release_ip_priv);
                        TALLOC_FREE(hdr);
-
-                       if (ret) {
-                               /*
-                                * We need to release the ip,
-                                * so return an error to the upper layers.
-                                *
-                                * We make sure we don't trigger this again.
-                                */
-                               conn->release_ip_handler = NULL;
-                               conn->release_ip_priv = NULL;
-                               return NT_STATUS_ADDRESS_CLOSED;
-                       }
                        goto next_pkt;
                }
 
-               if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
-                   || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)) {
-
-                       DEBUG(1, ("ctdb_read_req: Got %s message\n",
-                                 (msg->srvid == CTDB_SRVID_RECONFIGURE)
-                                 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
-
-                       messaging_send(conn->msg_ctx,
-                                      messaging_server_id(conn->msg_ctx),
-                                      MSG_SMB_BRL_VALIDATE, &data_blob_null);
-                       TALLOC_FREE(hdr);
-                       goto next_pkt;
-               }
-
-               msg_state = talloc(NULL, struct deferred_msg_state);
-               if (msg_state == NULL) {
-                       DEBUG(0, ("talloc failed\n"));
+               ret = ctdbd_msg_call_back(conn, msg);
+               if (ret != 0) {
                        TALLOC_FREE(hdr);
-                       goto next_pkt;
-               }
-
-               if (!(msg_state->rec = ctdb_pull_messaging_rec(
-                             msg_state, msg->hdr.length, msg))) {
-                       DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
-                       TALLOC_FREE(msg_state);
-                       TALLOC_FREE(hdr);
-                       goto next_pkt;
+                       return ret;
                }
 
                TALLOC_FREE(hdr);
-
-               msg_state->msg_ctx = conn->msg_ctx;
-
-               /*
-                * We're waiting for a call reply, but an async message has
-                * crossed. Defer dispatching to the toplevel event loop.
-                */
-               evt = tevent_add_timer(messaging_tevent_context(conn->msg_ctx),
-                                     messaging_tevent_context(conn->msg_ctx),
-                                     timeval_zero(),
-                                     deferred_message_dispatch,
-                                     msg_state);
-               if (evt == NULL) {
-                       DEBUG(0, ("event_add_timed failed\n"));
-                       TALLOC_FREE(msg_state);
-                       TALLOC_FREE(hdr);
-                       goto next_pkt;
-               }
-
                goto next_pkt;
        }
 
@@ -493,107 +400,114 @@ static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32_t reqid,
 
        *result = talloc_move(mem_ctx, &hdr);
 
-       return NT_STATUS_OK;
+       return 0;
 }
 
 static int ctdbd_connection_destructor(struct ctdbd_connection *c)
 {
-       close(c->fd);
+       TALLOC_FREE(c->fde);
+       if (c->fd != -1) {
+               close(c->fd);
+               c->fd = -1;
+       }
        return 0;
 }
 /*
  * Get us a ctdbd connection
  */
 
-static NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
-                                     struct ctdbd_connection **pconn)
+static int ctdbd_init_connection(TALLOC_CTX *mem_ctx,
+                                const char *sockname, int timeout,
+                                struct ctdbd_connection **pconn)
 {
        struct ctdbd_connection *conn;
        int ret;
-       NTSTATUS status;
 
        if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
                DEBUG(0, ("talloc failed\n"));
-               return NT_STATUS_NO_MEMORY;
+               return ENOMEM;
+       }
+
+       conn->sockname = talloc_strdup(conn, sockname);
+       if (conn->sockname == NULL) {
+               DBG_ERR("talloc failed\n");
+               ret = ENOMEM;
+               goto fail;
+       }
+
+       conn->timeout = timeout;
+
+       if (conn->timeout == 0) {
+               conn->timeout = -1;
        }
 
-       ret = ctdbd_connect(&conn->fd);
+       ret = ctdbd_connect(conn->sockname, &conn->fd);
        if (ret != 0) {
-               status = map_nt_error_from_unix(errno);
-               DEBUG(10, ("ctdbd_connect failed: %s\n", strerror(errno)));
+               DEBUG(1, ("ctdbd_connect failed: %s\n", strerror(ret)));
                goto fail;
        }
        talloc_set_destructor(conn, ctdbd_connection_destructor);
 
-       status = get_cluster_vnn(conn, &conn->our_vnn);
+       ret = get_cluster_vnn(conn, &conn->our_vnn);
 
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
+       if (ret != 0) {
+               DEBUG(10, ("get_cluster_vnn failed: %s\n", strerror(ret)));
                goto fail;
        }
 
        if (!ctdbd_working(conn, conn->our_vnn)) {
                DEBUG(2, ("Node is not working, can not connect\n"));
-               status = NT_STATUS_INTERNAL_DB_ERROR;
+               ret = EIO;
                goto fail;
        }
 
        generate_random_buffer((unsigned char *)&conn->rand_srvid,
                               sizeof(conn->rand_srvid));
 
-       status = register_with_ctdbd(conn, conn->rand_srvid);
+       ret = register_with_ctdbd(conn, conn->rand_srvid, NULL, NULL);
 
-       if (!NT_STATUS_IS_OK(status)) {
+       if (ret != 0) {
                DEBUG(5, ("Could not register random srvid: %s\n",
-                         nt_errstr(status)));
+                         strerror(ret)));
                goto fail;
        }
 
        *pconn = conn;
-       return NT_STATUS_OK;
+       return 0;
 
  fail:
        TALLOC_FREE(conn);
-       return status;
+       return ret;
 }
 
 /*
  * Get us a ctdbd connection and register us as a process
  */
 
-NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
-                                   struct ctdbd_connection **pconn)
+int ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
+                              const char *sockname, int timeout,
+                              struct ctdbd_connection **pconn)
 {
         struct ctdbd_connection *conn;
-       NTSTATUS status;
-
-       status = ctdbd_init_connection(mem_ctx, &conn);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
+       int ret;
 
-       status = register_with_ctdbd(conn, (uint64_t)getpid());
-       if (!NT_STATUS_IS_OK(status)) {
-               goto fail;
-       }
+       ret = ctdbd_init_connection(mem_ctx, sockname, timeout, &conn);
 
-       status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto fail;
+       if (ret != 0) {
+               return ret;
        }
 
-       status = register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY);
-       if (!NT_STATUS_IS_OK(status)) {
+       ret = register_with_ctdbd(conn, MSG_SRVID_SAMBA, NULL, NULL);
+       if (ret != 0) {
                goto fail;
        }
 
        *pconn = conn;
-       return NT_STATUS_OK;
+       return 0;
 
  fail:
        TALLOC_FREE(conn);
-       return status;
+       return ret;
 }
 
 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
@@ -609,72 +523,22 @@ int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
 /*
  * Packet handler to receive and handle a ctdb message
  */
-static NTSTATUS ctdb_handle_message(struct messaging_context *msg_ctx,
-                                   struct ctdbd_connection *conn,
-                                   struct ctdb_req_header *hdr)
+static int ctdb_handle_message(struct ctdbd_connection *conn,
+                              struct ctdb_req_header *hdr)
 {
-       struct ctdb_req_message *msg;
-       struct messaging_rec *msg_rec;
+       struct ctdb_req_message_old *msg;
 
        if (hdr->operation != CTDB_REQ_MESSAGE) {
                DEBUG(0, ("Received async msg of type %u, discarding\n",
                          hdr->operation));
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       msg = (struct ctdb_req_message *)hdr;
-
-       if ((conn->release_ip_handler != NULL)
-           && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
-               bool ret;
-
-               /* must be dispatched immediately */
-               DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
-               ret = conn->release_ip_handler((const char *)msg->data,
-                                              conn->release_ip_priv);
-               if (ret) {
-                       /*
-                        * We need to release the ip.
-                        *
-                        * We make sure we don't trigger this again.
-                        */
-                       conn->release_ip_handler = NULL;
-                       conn->release_ip_priv = NULL;
-               }
-               return NT_STATUS_OK;
+               return EINVAL;
        }
 
-       SMB_ASSERT(conn->msg_ctx != NULL);
+       msg = (struct ctdb_req_message_old *)hdr;
 
-       if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
-           || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)){
-               DEBUG(0,("Got cluster reconfigure message\n"));
-               /*
-                * when the cluster is reconfigured or someone of the
-                * family has passed away (SAMBA_NOTIFY), we need to
-                * clean the brl database
-                */
-               messaging_send(conn->msg_ctx,
-                              messaging_server_id(conn->msg_ctx),
-                              MSG_SMB_BRL_VALIDATE, &data_blob_null);
-
-               return NT_STATUS_OK;
-       }
-
-       /* only messages to our pid or the broadcast are valid here */
-       if (msg->srvid != getpid() && msg->srvid != MSG_SRVID_SAMBA) {
-               DEBUG(0,("Got unexpected message with srvid=%llu\n", 
-                        (unsigned long long)msg->srvid));
-               return NT_STATUS_OK;
-       }
+       ctdbd_msg_call_back(conn, msg);
 
-       msg_rec = ctdb_pull_messaging_rec(talloc_tos(), msg->hdr.length, msg);
-       if (msg_rec == NULL) {
-               DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-       messaging_dispatch_rec(conn->msg_ctx, msg_rec);
-       return NT_STATUS_OK;
+       return 0;
 }
 
 /*
@@ -683,24 +547,27 @@ static NTSTATUS ctdb_handle_message(struct messaging_context *msg_ctx,
 
 static void ctdbd_socket_handler(struct tevent_context *event_ctx,
                                 struct tevent_fd *event,
-                                uint16 flags,
+                                uint16_t flags,
                                 void *private_data)
 {
        struct ctdbd_connection *conn = talloc_get_type_abort(
                private_data, struct ctdbd_connection);
-       struct ctdb_req_header *hdr;
-       NTSTATUS status;
+       struct ctdb_req_header *hdr = NULL;
+       int ret;
 
-       status = ctdb_read_packet(conn->fd, talloc_tos(), &hdr);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("ctdb_read_packet failed: %s\n", nt_errstr(status)));
+       ret = ctdb_read_packet(conn->fd, conn->timeout, talloc_tos(), &hdr);
+       if (ret != 0) {
+               DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret)));
                cluster_fatal("ctdbd died\n");
        }
 
-       status = ctdb_handle_message(conn->msg_ctx, conn, hdr);
-       if (!NT_STATUS_IS_OK(status)) {
+       ret = ctdb_handle_message(conn, hdr);
+
+       TALLOC_FREE(hdr);
+
+       if (ret != 0) {
                DEBUG(10, ("could not handle incoming message: %s\n",
-                          nt_errstr(status)));
+                          strerror(ret)));
        }
 }
 
@@ -708,66 +575,37 @@ static void ctdbd_socket_handler(struct tevent_context *event_ctx,
  * Prepare a ctdbd connection to receive messages
  */
 
-NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
-                               struct messaging_context *msg_ctx)
+int ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
+                          struct messaging_context *msg_ctx,
+                          struct tevent_context *ev)
 {
        SMB_ASSERT(conn->msg_ctx == NULL);
        SMB_ASSERT(conn->fde == NULL);
 
-       if (!(conn->fde = tevent_add_fd(messaging_tevent_context(msg_ctx),
-                                      conn,
-                                      conn->fd,
-                                      TEVENT_FD_READ,
-                                      ctdbd_socket_handler,
-                                      conn))) {
+       conn->fde = tevent_add_fd(ev, conn, conn->fd, TEVENT_FD_READ,
+                                 ctdbd_socket_handler, conn);
+       if (conn->fde == NULL) {
                DEBUG(0, ("event_add_fd failed\n"));
-               return NT_STATUS_NO_MEMORY;
+               return ENOMEM;
        }
 
        conn->msg_ctx = msg_ctx;
 
-       return NT_STATUS_OK;
-}
-
-/*
- * Send a messaging message across a ctdbd
- */
-
-NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
-                             uint32_t dst_vnn, uint64_t dst_srvid,
-                             struct messaging_rec *msg)
-{
-       DATA_BLOB blob;
-       NTSTATUS status;
-       enum ndr_err_code ndr_err;
-
-       ndr_err = ndr_push_struct_blob(
-               &blob, talloc_tos(), msg,
-               (ndr_push_flags_fn_t)ndr_push_messaging_rec);
-
-       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
-                         ndr_errstr(ndr_err)));
-               return ndr_map_error2ntstatus(ndr_err);
-       }
-
-       status = ctdbd_messaging_send_blob(conn, dst_vnn, dst_srvid,
-                                          blob.data, blob.length);
-       TALLOC_FREE(blob.data);
-       return status;
+       return 0;
 }
 
-NTSTATUS ctdbd_messaging_send_blob(struct ctdbd_connection *conn,
-                                  uint32_t dst_vnn, uint64_t dst_srvid,
-                                  const uint8_t *buf, size_t buflen)
+int ctdbd_messaging_send_iov(struct ctdbd_connection *conn,
+                            uint32_t dst_vnn, uint64_t dst_srvid,
+                            const struct iovec *iov, int iovlen)
 {
-       struct ctdb_req_message r;
-       struct iovec iov[2];
+       struct ctdb_req_message_old r;
+       struct iovec iov2[iovlen+1];
+       size_t buflen = iov_buflen(iov, iovlen);
        ssize_t nwritten;
 
-       r.hdr.length = offsetof(struct ctdb_req_message, data) + buflen;
+       r.hdr.length = offsetof(struct ctdb_req_message_old, data) + buflen;
        r.hdr.ctdb_magic = CTDB_MAGIC;
-       r.hdr.ctdb_version = CTDB_VERSION;
+       r.hdr.ctdb_version = CTDB_PROTOCOL;
        r.hdr.generation = 1;
        r.hdr.operation  = CTDB_REQ_MESSAGE;
        r.hdr.destnode   = dst_vnn;
@@ -779,54 +617,40 @@ NTSTATUS ctdbd_messaging_send_blob(struct ctdbd_connection *conn,
        DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
        ctdb_packet_dump(&r.hdr);
 
-       iov[0].iov_base = &r;
-       iov[0].iov_len = offsetof(struct ctdb_req_message, data);
-       iov[1].iov_base = discard_const_p(uint8_t, buf);
-       iov[1].iov_len = buflen;
+       iov2[0].iov_base = &r;
+       iov2[0].iov_len = offsetof(struct ctdb_req_message_old, data);
+       memcpy(&iov2[1], iov, iovlen * sizeof(struct iovec));
 
-       nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
+       nwritten = write_data_iov(conn->fd, iov2, iovlen+1);
        if (nwritten == -1) {
                DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
                cluster_fatal("cluster dispatch daemon msg write error\n");
        }
 
-       return NT_STATUS_OK;
+       return 0;
 }
 
 /*
  * send/recv a generic ctdb control message
  */
-static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
-                             uint32_t vnn, uint32_t opcode,
-                             uint64_t srvid, uint32_t flags,
-                             TDB_DATA data,
-                             TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
-                             int *cstatus)
+static int ctdbd_control(struct ctdbd_connection *conn,
+                        uint32_t vnn, uint32_t opcode,
+                        uint64_t srvid, uint32_t flags,
+                        TDB_DATA data,
+                        TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
+                        int *cstatus)
 {
-       struct ctdb_req_control req;
+       struct ctdb_req_control_old req;
        struct ctdb_req_header *hdr;
-       struct ctdb_reply_control *reply = NULL;
-       struct ctdbd_connection *new_conn = NULL;
+       struct ctdb_reply_control_old *reply = NULL;
        struct iovec iov[2];
        ssize_t nwritten;
-       NTSTATUS status;
-
-       if (conn == NULL) {
-               status = ctdbd_init_connection(NULL, &new_conn);
-
-               if (!NT_STATUS_IS_OK(status)) {
-                       DEBUG(10, ("Could not init temp connection: %s\n",
-                                  nt_errstr(status)));
-                       goto fail;
-               }
-
-               conn = new_conn;
-       }
+       int ret;
 
        ZERO_STRUCT(req);
-       req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
+       req.hdr.length = offsetof(struct ctdb_req_control_old, data) + data.dsize;
        req.hdr.ctdb_magic   = CTDB_MAGIC;
-       req.hdr.ctdb_version = CTDB_VERSION;
+       req.hdr.ctdb_version = CTDB_PROTOCOL;
        req.hdr.operation    = CTDB_REQ_CONTROL;
        req.hdr.reqid        = ctdbd_next_reqid(conn);
        req.hdr.destnode     = vnn;
@@ -839,7 +663,7 @@ static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
        ctdb_packet_dump(&req.hdr);
 
        iov[0].iov_base = &req;
-       iov[0].iov_len = offsetof(struct ctdb_req_control, data);
+       iov[0].iov_len = offsetof(struct ctdb_req_control_old, data);
        iov[1].iov_base = data.dptr;
        iov[1].iov_len = data.dsize;
 
@@ -850,31 +674,30 @@ static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
        }
 
        if (flags & CTDB_CTRL_FLAG_NOREPLY) {
-               TALLOC_FREE(new_conn);
                if (cstatus) {
                        *cstatus = 0;
                }
-               return NT_STATUS_OK;
+               return 0;
        }
 
-       status = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
-               goto fail;
+       ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
+       if (ret != 0) {
+               DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
+               return ret;
        }
 
        if (hdr->operation != CTDB_REPLY_CONTROL) {
                DEBUG(0, ("received invalid reply\n"));
-               goto fail;
+               TALLOC_FREE(hdr);
+               return EIO;
        }
-       reply = (struct ctdb_reply_control *)hdr;
+       reply = (struct ctdb_reply_control_old *)hdr;
 
        if (outdata) {
-               if (!(outdata->dptr = (uint8 *)talloc_memdup(
+               if (!(outdata->dptr = (uint8_t *)talloc_memdup(
                              mem_ctx, reply->data, reply->datalen))) {
                        TALLOC_FREE(reply);
-                       return NT_STATUS_NO_MEMORY;
+                       return ENOMEM;
                }
                outdata->dsize = reply->datalen;
        }
@@ -882,12 +705,8 @@ static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
                (*cstatus) = reply->status;
        }
 
-       status = NT_STATUS_OK;
-
- fail:
-       TALLOC_FREE(new_conn);
        TALLOC_FREE(reply);
-       return status;
+       return ret;
 }
 
 /*
@@ -914,7 +733,6 @@ bool ctdb_processes_exist(struct ctdbd_connection *conn,
 {
        TALLOC_CTX *frame = talloc_stackframe();
        int i, num_received;
-       NTSTATUS status;
        uint32_t *reqids;
        bool result = false;
 
@@ -924,7 +742,7 @@ bool ctdb_processes_exist(struct ctdbd_connection *conn,
        }
 
        for (i=0; i<num_pids; i++) {
-               struct ctdb_req_control req;
+               struct ctdb_req_control_old req;
                pid_t pid;
                struct iovec iov[2];
                ssize_t nwritten;
@@ -944,10 +762,10 @@ bool ctdb_processes_exist(struct ctdbd_connection *conn,
                           (int)pids[i].vnn, (int)pid,
                           (int)reqids[i]));
 
-               req.hdr.length = offsetof(struct ctdb_req_control, data);
+               req.hdr.length = offsetof(struct ctdb_req_control_old, data);
                req.hdr.length += sizeof(pid);
                req.hdr.ctdb_magic   = CTDB_MAGIC;
-               req.hdr.ctdb_version = CTDB_VERSION;
+               req.hdr.ctdb_version = CTDB_PROTOCOL;
                req.hdr.operation    = CTDB_REQ_CONTROL;
                req.hdr.reqid        = reqids[i];
                req.hdr.destnode     = pids[i].vnn;
@@ -960,13 +778,12 @@ bool ctdb_processes_exist(struct ctdbd_connection *conn,
                ctdb_packet_dump(&req.hdr);
 
                iov[0].iov_base = &req;
-               iov[0].iov_len = offsetof(struct ctdb_req_control, data);
+               iov[0].iov_len = offsetof(struct ctdb_req_control_old, data);
                iov[1].iov_base = &pid;
                iov[1].iov_len = sizeof(pid);
 
                nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
                if (nwritten == -1) {
-                       status = map_nt_error_from_unix(errno);
                        DEBUG(10, ("write_data_iov failed: %s\n",
                                   strerror(errno)));
                        goto fail;
@@ -977,13 +794,14 @@ bool ctdb_processes_exist(struct ctdbd_connection *conn,
 
        while (num_received < num_pids) {
                struct ctdb_req_header *hdr;
-               struct ctdb_reply_control *reply;
+               struct ctdb_reply_control_old *reply;
                uint32_t reqid;
+               int ret;
 
-               status = ctdb_read_req(conn, 0, talloc_tos(), &hdr);
-               if (!NT_STATUS_IS_OK(status)) {
+               ret = ctdb_read_req(conn, 0, talloc_tos(), &hdr);
+               if (ret != 0) {
                        DEBUG(10, ("ctdb_read_req failed: %s\n",
-                                  nt_errstr(status)));
+                                  strerror(ret)));
                        goto fail;
                }
 
@@ -991,7 +809,7 @@ bool ctdb_processes_exist(struct ctdbd_connection *conn,
                        DEBUG(10, ("Received invalid reply\n"));
                        goto fail;
                }
-               reply = (struct ctdb_reply_control *)hdr;
+               reply = (struct ctdb_reply_control_old *)hdr;
 
                reqid = reply->hdr.reqid;
 
@@ -1018,343 +836,99 @@ fail:
        return result;
 }
 
-struct ctdb_vnn_list {
-       uint32_t vnn;
-       uint32_t reqid;
-       unsigned num_srvids;
-       unsigned num_filled;
-       uint64_t *srvids;
-       unsigned *pid_indexes;
-};
-
-/*
- * Get a list of all vnns mentioned in a list of
- * server_ids. vnn_indexes tells where in the vnns array we have to
- * place the pids.
- */
-static bool ctdb_collect_vnns(TALLOC_CTX *mem_ctx,
-                             const struct server_id *pids, unsigned num_pids,
-                             struct ctdb_vnn_list **pvnns,
-                             unsigned *pnum_vnns)
-{
-       struct ctdb_vnn_list *vnns = NULL;
-       unsigned *vnn_indexes = NULL;
-       unsigned i, num_vnns = 0;
-
-       vnn_indexes = talloc_array(mem_ctx, unsigned, num_pids);
-       if (vnn_indexes == NULL) {
-               DEBUG(1, ("talloc_array failed\n"));
-               goto fail;
-       }
-
-       for (i=0; i<num_pids; i++) {
-               unsigned j;
-               uint32_t vnn = pids[i].vnn;
-
-               for (j=0; j<num_vnns; j++) {
-                       if (vnn == vnns[j].vnn) {
-                               break;
-                       }
-               }
-               vnn_indexes[i] = j;
-
-               if (j < num_vnns) {
-                       /*
-                        * Already in the array
-                        */
-                       vnns[j].num_srvids += 1;
-                       continue;
-               }
-               vnns = talloc_realloc(mem_ctx, vnns, struct ctdb_vnn_list,
-                                     num_vnns+1);
-               if (vnns == NULL) {
-                       DEBUG(1, ("talloc_realloc failed\n"));
-                       goto fail;
-               }
-               vnns[num_vnns].vnn = vnn;
-               vnns[num_vnns].num_srvids = 1;
-               vnns[num_vnns].num_filled = 0;
-               num_vnns += 1;
-       }
-       for (i=0; i<num_vnns; i++) {
-               struct ctdb_vnn_list *vnn = &vnns[i];
-
-               vnn->srvids = talloc_array(vnns, uint64_t, vnn->num_srvids);
-               if (vnn->srvids == NULL) {
-                       DEBUG(1, ("talloc_array failed\n"));
-                       goto fail;
-               }
-               vnn->pid_indexes = talloc_array(vnns, unsigned,
-                                               vnn->num_srvids);
-               if (vnn->pid_indexes == NULL) {
-                       DEBUG(1, ("talloc_array failed\n"));
-                       goto fail;
-               }
-       }
-       for (i=0; i<num_pids; i++) {
-               struct ctdb_vnn_list *vnn = &vnns[vnn_indexes[i]];
-               vnn->srvids[vnn->num_filled] = pids[i].unique_id;
-               vnn->pid_indexes[vnn->num_filled] = i;
-               vnn->num_filled += 1;
-       }
-
-       TALLOC_FREE(vnn_indexes);
-       *pvnns = vnns;
-       *pnum_vnns = num_vnns;
-       return true;
-fail:
-       TALLOC_FREE(vnns);
-       TALLOC_FREE(vnn_indexes);
-       return false;
-}
-
-bool ctdb_serverids_exist_supported(struct ctdbd_connection *conn)
-{
-       return true;
-}
-
-bool ctdb_serverids_exist(struct ctdbd_connection *conn,
-                         const struct server_id *pids, unsigned num_pids,
-                         bool *results)
-{
-       unsigned i, num_received;
-       NTSTATUS status;
-       struct ctdb_vnn_list *vnns = NULL;
-       unsigned num_vnns;
-
-       if (!ctdb_collect_vnns(talloc_tos(), pids, num_pids,
-                              &vnns, &num_vnns)) {
-               DEBUG(1, ("ctdb_collect_vnns failed\n"));
-               goto fail;
-       }
-
-       for (i=0; i<num_vnns; i++) {
-               struct ctdb_vnn_list *vnn = &vnns[i];
-               struct ctdb_req_control req;
-               struct iovec iov[2];
-               ssize_t nwritten;
-
-               vnn->reqid = ctdbd_next_reqid(conn);
-
-               ZERO_STRUCT(req);
-
-               DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
-                          (int)vnn->vnn, (int)vnn->reqid, vnn->num_srvids));
-
-               req.hdr.length = offsetof(struct ctdb_req_control, data);
-               req.hdr.ctdb_magic   = CTDB_MAGIC;
-               req.hdr.ctdb_version = CTDB_VERSION;
-               req.hdr.operation    = CTDB_REQ_CONTROL;
-               req.hdr.reqid        = vnn->reqid;
-               req.hdr.destnode     = vnn->vnn;
-               req.opcode           = CTDB_CONTROL_CHECK_SRVIDS;
-               req.srvid            = 0;
-               req.datalen          = sizeof(uint64_t) * vnn->num_srvids;
-               req.hdr.length      += req.datalen;
-               req.flags            = 0;
-
-               DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
-               ctdb_packet_dump(&req.hdr);
-
-               iov[0].iov_base = &req;
-               iov[0].iov_len = offsetof(struct ctdb_req_control, data);
-               iov[1].iov_base = vnn->srvids;
-               iov[1].iov_len = req.datalen;
-
-               nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
-               if (nwritten == -1) {
-                       status = map_nt_error_from_unix(errno);
-                       DEBUG(10, ("write_data_iov failed: %s\n",
-                                  strerror(errno)));
-                       goto fail;
-               }
-       }
-
-       num_received = 0;
-
-       while (num_received < num_vnns) {
-               struct ctdb_req_header *hdr;
-               struct ctdb_reply_control *reply;
-               struct ctdb_vnn_list *vnn;
-               uint32_t reqid;
-               uint8_t *reply_data;
-
-               status = ctdb_read_req(conn, 0, talloc_tos(), &hdr);
-               if (!NT_STATUS_IS_OK(status)) {
-                       DEBUG(1, ("ctdb_read_req failed: %s\n",
-                                 nt_errstr(status)));
-                       goto fail;
-               }
-
-               if (hdr->operation != CTDB_REPLY_CONTROL) {
-                       DEBUG(1, ("Received invalid reply %u\n",
-                                 (unsigned)reply->hdr.operation));
-                       goto fail;
-               }
-               reply = (struct ctdb_reply_control *)hdr;
-
-               reqid = reply->hdr.reqid;
-
-               DEBUG(10, ("Received reqid %d\n", (int)reqid));
-
-               for (i=0; i<num_vnns; i++) {
-                       if (reqid == vnns[i].reqid) {
-                               break;
-                       }
-               }
-               if (i == num_vnns) {
-                       DEBUG(1, ("Received unknown reqid number %u\n",
-                                 (unsigned)reqid));
-                       goto fail;
-               }
-
-               DEBUG(10, ("Found index %u\n", i));
-
-               vnn = &vnns[i];
-
-               DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
-                          (unsigned)vnn->vnn, vnn->num_srvids,
-                          (unsigned)reply->datalen));
-
-               if (reply->datalen >= ((vnn->num_srvids+7)/8)) {
-                       /*
-                        * Got a real reply
-                        */
-                       reply_data = reply->data;
-               } else {
-                       /*
-                        * Got an error reply
-                        */
-                       DEBUG(5, ("Received short reply len %d, status %u, "
-                                 "errorlen %u\n",
-                                 (unsigned)reply->datalen,
-                                 (unsigned)reply->status,
-                                 (unsigned)reply->errorlen));
-                       dump_data(5, reply->data, reply->errorlen);
-
-                       /*
-                        * This will trigger everything set to false
-                        */
-                       reply_data = NULL;
-               }
-
-               for (i=0; i<vnn->num_srvids; i++) {
-                       int idx = vnn->pid_indexes[i];
-
-                       if (pids[i].unique_id ==
-                           SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
-                               results[idx] = true;
-                               continue;
-                       }
-                       results[idx] =
-                               (reply_data != NULL) &&
-                               ((reply_data[i/8] & (1<<(i%8))) != 0);
-               }
-
-               TALLOC_FREE(reply);
-               num_received += 1;
-       }
-
-       TALLOC_FREE(vnns);
-       return true;
-fail:
-       cluster_fatal("serverids_exist failed");
-       return false;
-}
-
 /*
  * Get a db path
  */
 char *ctdbd_dbpath(struct ctdbd_connection *conn,
                   TALLOC_CTX *mem_ctx, uint32_t db_id)
 {
-       NTSTATUS status;
+       int ret;
        TDB_DATA data;
-       int32_t cstatus;
+       TDB_DATA rdata = {0};
+       int32_t cstatus = 0;
 
        data.dptr = (uint8_t*)&db_id;
        data.dsize = sizeof(db_id);
 
-       status = ctdbd_control(conn, CTDB_CURRENT_NODE,
-                              CTDB_CONTROL_GETDBPATH, 0, 0, data, 
-                              mem_ctx, &data, &cstatus);
-       if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
-               DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
+       ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
+                           CTDB_CONTROL_GETDBPATH, 0, 0, data,
+                           mem_ctx, &rdata, &cstatus);
+       if ((ret != 0) || cstatus != 0) {
+               DEBUG(0, (__location__ " ctdb_control for getdbpath failed: %s\n",
+                         strerror(ret)));
                return NULL;
        }
 
-       return (char *)data.dptr;
+       return (char *)rdata.dptr;
 }
 
 /*
  * attach to a ctdb database
  */
-NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
-                        const char *name, uint32_t *db_id, int tdb_flags)
+int ctdbd_db_attach(struct ctdbd_connection *conn,
+                   const char *name, uint32_t *db_id, int tdb_flags)
 {
-       NTSTATUS status;
+       int ret;
        TDB_DATA data;
        int32_t cstatus;
        bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
 
        data = string_term_tdb_data(name);
 
-       status = ctdbd_control(conn, CTDB_CURRENT_NODE,
-                              persistent
-                              ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
-                              : CTDB_CONTROL_DB_ATTACH,
-                              tdb_flags, 0, data, NULL, &data, &cstatus);
-       if (!NT_STATUS_IS_OK(status)) {
+       ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
+                           persistent
+                           ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
+                           : CTDB_CONTROL_DB_ATTACH,
+                           tdb_flags, 0, data, NULL, &data, &cstatus);
+       if (ret != 0) {
                DEBUG(0, (__location__ " ctdb_control for db_attach "
-                         "failed: %s\n", nt_errstr(status)));
-               return status;
+                         "failed: %s\n", strerror(ret)));
+               return ret;
        }
 
        if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
                DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
-               return NT_STATUS_INTERNAL_ERROR;
+               return EIO;
        }
 
        *db_id = *(uint32_t *)data.dptr;
        talloc_free(data.dptr);
 
        if (!(tdb_flags & TDB_SEQNUM)) {
-               return NT_STATUS_OK;
+               return 0;
        }
 
        data.dptr = (uint8_t *)db_id;
        data.dsize = sizeof(*db_id);
 
-       status = ctdbd_control(conn, CTDB_CURRENT_NODE,
-                              CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data, 
-                              NULL, NULL, &cstatus);
-       if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
-               DEBUG(0,(__location__ " ctdb_control for enable seqnum "
-                        "failed\n"));
-               return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
-                       status;
+       ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
+                           CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
+                           NULL, NULL, &cstatus);
+       if ((ret != 0) || cstatus != 0) {
+               DEBUG(0, (__location__ " ctdb_control for enable seqnum "
+                         "failed: %s\n", strerror(ret)));
+               return (ret == 0) ? EIO : ret;
        }
 
-       return NT_STATUS_OK;
+       return 0;
 }
 
 /*
  * force the migration of a record to this node
  */
-NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id,
-                      TDB_DATA key)
+int ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id, TDB_DATA key)
 {
-       struct ctdb_req_call req;
+       struct ctdb_req_call_old req;
        struct ctdb_req_header *hdr;
        struct iovec iov[2];
        ssize_t nwritten;
-       NTSTATUS status;
+       int ret;
 
        ZERO_STRUCT(req);
 
-       req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
+       req.hdr.length = offsetof(struct ctdb_req_call_old, data) + key.dsize;
        req.hdr.ctdb_magic   = CTDB_MAGIC;
-       req.hdr.ctdb_version = CTDB_VERSION;
+       req.hdr.ctdb_version = CTDB_PROTOCOL;
        req.hdr.operation    = CTDB_REQ_CALL;
        req.hdr.reqid        = ctdbd_next_reqid(conn);
        req.flags            = CTDB_IMMEDIATE_MIGRATION;
@@ -1366,7 +940,7 @@ NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id,
        ctdb_packet_dump(&req.hdr);
 
        iov[0].iov_base = &req;
-       iov[0].iov_len = offsetof(struct ctdb_req_call, data);
+       iov[0].iov_len = offsetof(struct ctdb_req_call_old, data);
        iov[1].iov_base = key.dptr;
        iov[1].iov_len = key.dsize;
 
@@ -1376,50 +950,47 @@ NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id,
                cluster_fatal("cluster dispatch daemon msg write error\n");
        }
 
-       status = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
+       ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
+       if (ret != 0) {
+               DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
                goto fail;
        }
 
        if (hdr->operation != CTDB_REPLY_CALL) {
                DEBUG(0, ("received invalid reply\n"));
-               status = NT_STATUS_INTERNAL_ERROR;
                goto fail;
        }
 
-       status = NT_STATUS_OK;
  fail:
 
        TALLOC_FREE(hdr);
-       return status;
+       return ret;
 }
 
 /*
  * Fetch a record and parse it
  */
-NTSTATUS ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
-                    TDB_DATA key, bool local_copy,
-                    void (*parser)(TDB_DATA key, TDB_DATA data,
-                                   void *private_data),
-                    void *private_data)
+int ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
+               TDB_DATA key, bool local_copy,
+               void (*parser)(TDB_DATA key, TDB_DATA data,
+                              void *private_data),
+               void *private_data)
 {
-       struct ctdb_req_call req;
+       struct ctdb_req_call_old req;
        struct ctdb_req_header *hdr = NULL;
-       struct ctdb_reply_call *reply;
+       struct ctdb_reply_call_old *reply;
        struct iovec iov[2];
        ssize_t nwritten;
-       NTSTATUS status;
        uint32_t flags;
+       int ret;
 
        flags = local_copy ? CTDB_WANT_READONLY : 0;
 
        ZERO_STRUCT(req);
 
-       req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
+       req.hdr.length = offsetof(struct ctdb_req_call_old, data) + key.dsize;
        req.hdr.ctdb_magic   = CTDB_MAGIC;
-       req.hdr.ctdb_version = CTDB_VERSION;
+       req.hdr.ctdb_version = CTDB_PROTOCOL;
        req.hdr.operation    = CTDB_REQ_CALL;
        req.hdr.reqid        = ctdbd_next_reqid(conn);
        req.flags            = flags;
@@ -1428,7 +999,7 @@ NTSTATUS ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
        req.keylen           = key.dsize;
 
        iov[0].iov_base = &req;
-       iov[0].iov_len = offsetof(struct ctdb_req_call, data);
+       iov[0].iov_len = offsetof(struct ctdb_req_call_old, data);
        iov[1].iov_base = key.dptr;
        iov[1].iov_len = key.dsize;
 
@@ -1438,35 +1009,34 @@ NTSTATUS ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
                cluster_fatal("cluster dispatch daemon msg write error\n");
        }
 
-       status = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
+       ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
+       if (ret != 0) {
+               DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
                goto fail;
        }
 
-       if (hdr->operation != CTDB_REPLY_CALL) {
+       if ((hdr == NULL) || (hdr->operation != CTDB_REPLY_CALL)) {
                DEBUG(0, ("received invalid reply\n"));
-               status = NT_STATUS_INTERNAL_ERROR;
+               ret = EIO;
                goto fail;
        }
-       reply = (struct ctdb_reply_call *)hdr;
+       reply = (struct ctdb_reply_call_old *)hdr;
 
        if (reply->datalen == 0) {
                /*
                 * Treat an empty record as non-existing
                 */
-               status = NT_STATUS_NOT_FOUND;
+               ret = ENOENT;
                goto fail;
        }
 
        parser(key, make_tdb_data(&reply->data[0], reply->datalen),
               private_data);
 
-       status = NT_STATUS_OK;
+       ret = 0;
  fail:
        TALLOC_FREE(hdr);
-       return status;
+       return ret;
 }
 
 /*
@@ -1475,25 +1045,25 @@ NTSTATUS ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
   everything in-line.
 */
 
-NTSTATUS ctdbd_traverse(uint32_t db_id,
+int ctdbd_traverse(struct ctdbd_connection *master, uint32_t db_id,
                        void (*fn)(TDB_DATA key, TDB_DATA data,
                                   void *private_data),
                        void *private_data)
 {
        struct ctdbd_connection *conn;
-       NTSTATUS status;
-
+       int ret;
        TDB_DATA key, data;
        struct ctdb_traverse_start t;
        int cstatus;
 
        become_root();
-       status = ctdbd_init_connection(NULL, &conn);
+       ret = ctdbd_init_connection(NULL, master->sockname, master->timeout,
+                                   &conn);
        unbecome_root();
-       if (!NT_STATUS_IS_OK(status)) {
+       if (ret != 0) {
                DEBUG(0, ("ctdbd_init_connection failed: %s\n",
-                         nt_errstr(status)));
-               return status;
+                         strerror(ret)));
+               return ret;
        }
 
        t.db_id = db_id;
@@ -1503,34 +1073,33 @@ NTSTATUS ctdbd_traverse(uint32_t db_id,
        data.dptr = (uint8_t *)&t;
        data.dsize = sizeof(t);
 
-       status = ctdbd_control(conn, CTDB_CURRENT_NODE,
-                              CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
-                              data, NULL, NULL, &cstatus);
-
-       if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
+       ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
+                           CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid,
+                           0, data, NULL, NULL, &cstatus);
 
-               DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
+       if ((ret != 0) || (cstatus != 0)) {
+               DEBUG(0,("ctdbd_control failed: %s, %d\n", strerror(ret),
                         cstatus));
 
-               if (NT_STATUS_IS_OK(status)) {
+               if (ret == 0) {
                        /*
                         * We need a mapping here
                         */
-                       status = NT_STATUS_UNSUCCESSFUL;
+                       ret = EIO;
                }
                TALLOC_FREE(conn);
-               return status;
+               return ret;
        }
 
        while (True) {
-               struct ctdb_req_header *hdr;
-               struct ctdb_req_message *m;
-               struct ctdb_rec_data *d;
+               struct ctdb_req_header *hdr = NULL;
+               struct ctdb_req_message_old *m;
+               struct ctdb_rec_data_old *d;
 
-               status = ctdb_read_packet(conn->fd, conn, &hdr);
-               if (!NT_STATUS_IS_OK(status)) {
+               ret = ctdb_read_packet(conn->fd, conn->timeout, conn, &hdr);
+               if (ret != 0) {
                        DEBUG(0, ("ctdb_read_packet failed: %s\n",
-                                 nt_errstr(status)));
+                                 strerror(ret)));
                        cluster_fatal("ctdbd died\n");
                }
 
@@ -1538,16 +1107,16 @@ NTSTATUS ctdbd_traverse(uint32_t db_id,
                        DEBUG(0, ("Got operation %u, expected a message\n",
                                  (unsigned)hdr->operation));
                        TALLOC_FREE(conn);
-                       return NT_STATUS_UNEXPECTED_IO_ERROR;
+                       return EIO;
                }
 
-               m = (struct ctdb_req_message *)hdr;
-               d = (struct ctdb_rec_data *)&m->data[0];
+               m = (struct ctdb_req_message_old *)hdr;
+               d = (struct ctdb_rec_data_old *)&m->data[0];
                if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
                        DEBUG(0, ("Got invalid traverse data of length %d\n",
                                  (int)m->datalen));
                        TALLOC_FREE(conn);
-                       return NT_STATUS_UNEXPECTED_IO_ERROR;
+                       return EIO;
                }
 
                key.dsize = d->keylen;
@@ -1558,14 +1127,14 @@ NTSTATUS ctdbd_traverse(uint32_t db_id,
                if (key.dsize == 0 && data.dsize == 0) {
                        /* end of traverse */
                        TALLOC_FREE(conn);
-                       return NT_STATUS_OK;
+                       return 0;
                }
 
                if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
                        DEBUG(0, ("Got invalid ltdb header length %d\n",
                                  (int)data.dsize));
                        TALLOC_FREE(conn);
-                       return NT_STATUS_UNEXPECTED_IO_ERROR;
+                       return EIO;
                }
                data.dsize -= sizeof(struct ctdb_ltdb_header);
                data.dptr += sizeof(struct ctdb_ltdb_header);
@@ -1574,7 +1143,7 @@ NTSTATUS ctdbd_traverse(uint32_t db_id,
                        fn(key, data, private_data);
                }
        }
-       return NT_STATUS_OK;
+       return 0;
 }
 
 /*
@@ -1608,60 +1177,49 @@ static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
  * Register us as a server for a particular tcp connection
  */
 
-NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
-                           const struct sockaddr_storage *_server,
-                           const struct sockaddr_storage *_client,
-                           bool (*release_ip_handler)(const char *ip_addr,
-                                                      void *private_data),
-                           void *private_data)
+int ctdbd_register_ips(struct ctdbd_connection *conn,
+                      const struct sockaddr_storage *_server,
+                      const struct sockaddr_storage *_client,
+                      int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
+                                uint64_t dst_srvid,
+                                const uint8_t *msg, size_t msglen,
+                                void *private_data),
+                      void *private_data)
 {
-       /*
-        * we still use ctdb_control_tcp for ipv4
-        * because we want to work against older ctdb
-        * versions at runtime
-        */
-       struct ctdb_control_tcp p4;
        struct ctdb_control_tcp_addr p;
-       TDB_DATA data;
-       NTSTATUS status;
+       TDB_DATA data = { .dptr = (uint8_t *)&p, .dsize = sizeof(p) };
+       int ret;
        struct sockaddr_storage client;
        struct sockaddr_storage server;
 
        /*
         * Only one connection so far
         */
-       SMB_ASSERT(conn->release_ip_handler == NULL);
 
        smbd_ctdb_canonicalize_ip(_client, &client);
        smbd_ctdb_canonicalize_ip(_server, &server);
 
        switch (client.ss_family) {
        case AF_INET:
-               memcpy(&p4.dest, &server, sizeof(p4.dest));
-               memcpy(&p4.src, &client, sizeof(p4.src));
-               data.dptr = (uint8_t *)&p4;
-               data.dsize = sizeof(p4);
+               memcpy(&p.dest.ip, &server, sizeof(p.dest.ip));
+               memcpy(&p.src.ip, &client, sizeof(p.src.ip));
                break;
        case AF_INET6:
                memcpy(&p.dest.ip6, &server, sizeof(p.dest.ip6));
                memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
-               data.dptr = (uint8_t *)&p;
-               data.dsize = sizeof(p);
                break;
        default:
-               return NT_STATUS_INTERNAL_ERROR;
+               return EIO;
        }
 
-       conn->release_ip_handler = release_ip_handler;
-       conn->release_ip_priv = private_data;
-
        /*
         * We want to be told about IP releases
         */
 
-       status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+       ret = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP,
+                                 cb, private_data);
+       if (ret != 0) {
+               return ret;
        }
 
        /*
@@ -1669,89 +1227,88 @@ NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
         * can send an extra ack to trigger a reset for our client, so it
         * immediately reconnects
         */
-       return ctdbd_control(conn, CTDB_CURRENT_NODE, 
-                            CTDB_CONTROL_TCP_CLIENT, 0,
-                            CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
-}
-
-/*
- * We want to handle reconfigure events
- */
-NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
-{
-       return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
+       ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
+                           CTDB_CONTROL_TCP_CLIENT, 0,
+                           CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL,
+                           NULL);
+       if (ret != 0) {
+               return ret;
+       }
+       return 0;
 }
 
 /*
   call a control on the local node
  */
-NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32_t opcode,
-                            uint64_t srvid, uint32_t flags, TDB_DATA data,
-                            TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
-                            int *cstatus)
+int ctdbd_control_local(struct ctdbd_connection *conn, uint32_t opcode,
+                       uint64_t srvid, uint32_t flags, TDB_DATA data,
+                       TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
+                       int *cstatus)
 {
-       return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
+       return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data,
+                            mem_ctx, outdata, cstatus);
 }
 
-NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
+int ctdb_watch_us(struct ctdbd_connection *conn)
 {
-       struct ctdb_client_notify_register reg_data;
+       struct ctdb_notify_data_old reg_data;
        size_t struct_len;
-       NTSTATUS status;
+       int ret;
        int cstatus;
 
        reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
        reg_data.len = 1;
        reg_data.notify_data[0] = 0;
 
-       struct_len = offsetof(struct ctdb_client_notify_register,
+       struct_len = offsetof(struct ctdb_notify_data_old,
                              notify_data) + reg_data.len;
 
-       status = ctdbd_control_local(
+       ret = ctdbd_control_local(
                conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
                make_tdb_data((uint8_t *)&reg_data, struct_len),
                NULL, NULL, &cstatus);
-       if (!NT_STATUS_IS_OK(status)) {
+       if (ret != 0) {
                DEBUG(1, ("ctdbd_control_local failed: %s\n",
-                         nt_errstr(status)));
+                         strerror(ret)));
        }
-       return status;
+       return ret;
 }
 
-NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
+int ctdb_unwatch(struct ctdbd_connection *conn)
 {
        struct ctdb_client_notify_deregister dereg_data;
-       NTSTATUS status;
+       int ret;
        int cstatus;
 
        dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
 
-       status = ctdbd_control_local(
+       ret = ctdbd_control_local(
                conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
                make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
                NULL, NULL, &cstatus);
-       if (!NT_STATUS_IS_OK(status)) {
+       if (ret != 0) {
                DEBUG(1, ("ctdbd_control_local failed: %s\n",
-                         nt_errstr(status)));
+                         strerror(ret)));
        }
-       return status;
+       return ret;
 }
 
-NTSTATUS ctdbd_probe(void)
+int ctdbd_probe(const char *sockname, int timeout)
 {
        /*
         * Do a very early check if ctdbd is around to avoid an abort and core
         * later
         */
        struct ctdbd_connection *conn = NULL;
-       NTSTATUS status;
+       int ret;
 
-       status = ctdbd_messaging_connection(talloc_tos(), &conn);
+       ret = ctdbd_messaging_connection(talloc_tos(), sockname, timeout,
+                                        &conn);
 
        /*
         * We only care if we can connect.
         */
        TALLOC_FREE(conn);
 
-       return status;
+       return ret;
 }