ctdb-protocol: Fix marshalling for ctdb_var_list
[vlendec/samba-autobuild/.git] / ctdb / protocol / protocol_types.c
index 868f55ab5567fec4666c8b7714fcba43bfbce4b3..c33c9495c900d1b353a9aba4e63cd55d3e81453e 100644 (file)
@@ -1,7 +1,7 @@
 /*
    CTDB protocol marshalling
 
-   Copyright (C) Amitay Isaacs  2015
+   Copyright (C) Amitay Isaacs  2015-2017
 
    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
 #include "protocol_private.h"
 #include "protocol_api.h"
 
-size_t ctdb_uint32_len(uint32_t val)
+size_t ctdb_tdb_data_len(TDB_DATA *in)
 {
-       return sizeof(uint32_t);
+       return in->dsize > UINT32_MAX ? UINT32_MAX : in->dsize;
 }
 
-void ctdb_uint32_push(uint32_t val, uint8_t *buf)
+void ctdb_tdb_data_push(TDB_DATA *in, uint8_t *buf, size_t *npush)
 {
-       memcpy(buf, &val, sizeof(uint32_t));
+       size_t len = ctdb_tdb_data_len(in);
+
+       if (len > 0) {
+               memcpy(buf, in->dptr, len);
+       }
+
+       *npush = len;
 }
 
-int ctdb_uint32_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                    uint32_t *out)
+int ctdb_tdb_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
+                      TDB_DATA *out, size_t *npull)
 {
-       if (buflen < sizeof(uint32_t)) {
+       TDB_DATA val;
+
+       if (buflen > UINT32_MAX) {
                return EMSGSIZE;
        }
 
-       *out = *(uint32_t *)buf;
+       val.dsize = buflen;
+       if (val.dsize > 0) {
+               val.dptr = talloc_memdup(mem_ctx, buf, buflen);
+               if (val.dptr == NULL) {
+                       return ENOMEM;
+               }
+       } else {
+               val.dptr = NULL;
+       }
+
+       *out = val;
+       *npull = buflen;
        return 0;
 }
 
-size_t ctdb_uint64_len(uint64_t val)
+size_t ctdb_tdb_datan_len(TDB_DATA *in)
 {
-       return sizeof(uint64_t);
+       uint32_t u32 = ctdb_tdb_data_len(in);
+
+       return ctdb_uint32_len(&u32) + u32;
 }
 
-void ctdb_uint64_push(uint64_t val, uint8_t *buf)
+void ctdb_tdb_datan_push(TDB_DATA *in, uint8_t *buf, size_t *npush)
 {
-       memcpy(buf, &val, sizeof(uint64_t));
+       size_t offset = 0, np;
+       uint32_t u32 = ctdb_tdb_data_len(in);
+
+       ctdb_uint32_push(&u32, buf+offset, &np);
+       offset += np;
+
+       ctdb_tdb_data_push(in, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
-int ctdb_uint64_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                    uint64_t *out)
+int ctdb_tdb_datan_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
+                       TDB_DATA *out, size_t *npull)
 {
-       if (buflen < sizeof(uint64_t)) {
+       size_t offset = 0, np;
+       uint32_t u32;
+       int ret;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       if (buflen-offset < u32) {
                return EMSGSIZE;
        }
 
-       *out = *(uint64_t *)buf;
+       ret = ctdb_tdb_data_pull(buf+offset, u32, mem_ctx, out, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       *npull = offset;
        return 0;
 }
 
-size_t ctdb_double_len(double val)
+size_t ctdb_latency_counter_len(struct ctdb_latency_counter *in)
 {
-       return sizeof(double);
+       return ctdb_int32_len(&in->num) +
+               ctdb_padding_len(4) +
+               ctdb_double_len(&in->min) +
+               ctdb_double_len(&in->max) +
+               ctdb_double_len(&in->total);
 }
 
-void ctdb_double_push(double val, uint8_t *buf)
+void ctdb_latency_counter_push(struct ctdb_latency_counter *in, uint8_t *buf,
+                              size_t *npush)
 {
-       memcpy(buf, &val, sizeof(double));
+       size_t offset = 0, np;
+
+       ctdb_int32_push(&in->num, buf+offset, &np);
+       offset += np;
+
+       ctdb_padding_push(4, buf+offset, &np);
+       offset += np;
+
+       ctdb_double_push(&in->min, buf+offset, &np);
+       offset += np;
+
+       ctdb_double_push(&in->max, buf+offset, &np);
+       offset += np;
+
+       ctdb_double_push(&in->total, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
-int ctdb_double_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                    double *out)
+int ctdb_latency_counter_pull(uint8_t *buf, size_t buflen,
+                             struct ctdb_latency_counter *out, size_t *npull)
 {
-       if (buflen < sizeof(double)) {
-               return EMSGSIZE;
+       size_t offset = 0, np;
+       int ret;
+
+       ret = ctdb_int32_pull(buf+offset, buflen-offset, &out->num, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_padding_pull(buf+offset, buflen-offset, 4, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_double_pull(buf+offset, buflen-offset, &out->min, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_double_pull(buf+offset, buflen-offset, &out->max, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_double_pull(buf+offset, buflen-offset, &out->total, &np);
+       if (ret != 0) {
+               return ret;
        }
+       offset += np;
 
-       *out = *(double *)buf;
+       *npull = offset;
        return 0;
 }
 
-size_t ctdb_uint8_array_len(struct ctdb_uint8_array *array)
-{
-       return array->num * sizeof(uint8_t);
-}
+size_t ctdb_statistics_len(struct ctdb_statistics *in)
+{
+       return ctdb_uint32_len(&in->num_clients) +
+               ctdb_uint32_len(&in->frozen) +
+               ctdb_uint32_len(&in->recovering) +
+               ctdb_uint32_len(&in->client_packets_sent) +
+               ctdb_uint32_len(&in->client_packets_recv) +
+               ctdb_uint32_len(&in->node_packets_sent) +
+               ctdb_uint32_len(&in->node_packets_recv) +
+               ctdb_uint32_len(&in->keepalive_packets_sent) +
+               ctdb_uint32_len(&in->keepalive_packets_recv) +
+               ctdb_uint32_len(&in->node.req_call) +
+               ctdb_uint32_len(&in->node.reply_call) +
+               ctdb_uint32_len(&in->node.req_dmaster) +
+               ctdb_uint32_len(&in->node.reply_dmaster) +
+               ctdb_uint32_len(&in->node.reply_error) +
+               ctdb_uint32_len(&in->node.req_message) +
+               ctdb_uint32_len(&in->node.req_control) +
+               ctdb_uint32_len(&in->node.reply_control) +
+               ctdb_uint32_len(&in->client.req_call) +
+               ctdb_uint32_len(&in->client.req_message) +
+               ctdb_uint32_len(&in->client.req_control) +
+               ctdb_uint32_len(&in->timeouts.call) +
+               ctdb_uint32_len(&in->timeouts.control) +
+               ctdb_uint32_len(&in->timeouts.traverse) +
+               ctdb_padding_len(4) +
+               ctdb_latency_counter_len(&in->reclock.ctdbd) +
+               ctdb_latency_counter_len(&in->reclock.recd) +
+               ctdb_uint32_len(&in->locks.num_calls) +
+               ctdb_uint32_len(&in->locks.num_current) +
+               ctdb_uint32_len(&in->locks.num_pending) +
+               ctdb_uint32_len(&in->locks.num_failed) +
+               ctdb_latency_counter_len(&in->locks.latency) +
+               MAX_COUNT_BUCKETS * ctdb_uint32_len(&in->locks.buckets[0]) +
+               ctdb_uint32_len(&in->total_calls) +
+               ctdb_uint32_len(&in->pending_calls) +
+               ctdb_uint32_len(&in->childwrite_calls) +
+               ctdb_uint32_len(&in->pending_childwrite_calls) +
+               ctdb_uint32_len(&in->memory_used) +
+               ctdb_uint32_len(&in->__last_counter) +
+               ctdb_uint32_len(&in->max_hop_count) +
+               MAX_COUNT_BUCKETS *
+                       ctdb_uint32_len(&in->hop_count_bucket[0]) +
+               ctdb_padding_len(4) +
+               ctdb_latency_counter_len(&in->call_latency) +
+               ctdb_latency_counter_len(&in->childwrite_latency) +
+               ctdb_uint32_len(&in->num_recoveries) +
+               ctdb_padding_len(4) +
+               ctdb_timeval_len(&in->statistics_start_time) +
+               ctdb_timeval_len(&in->statistics_current_time) +
+               ctdb_uint32_len(&in->total_ro_delegations) +
+               ctdb_uint32_len(&in->total_ro_revokes);
+}
+
+void ctdb_statistics_push(struct ctdb_statistics *in, uint8_t *buf,
+                         size_t *npush)
+{
+       size_t offset = 0, np;
+       int i;
 
-void ctdb_uint8_array_push(struct ctdb_uint8_array *array, uint8_t *buf)
-{
-       memcpy(buf, array->val, array->num * sizeof(uint8_t));
-}
+       ctdb_uint32_push(&in->num_clients, buf+offset, &np);
+       offset += np;
 
-int ctdb_uint8_array_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                         struct ctdb_uint8_array **out)
-{
-       struct ctdb_uint8_array *array;
+       ctdb_uint32_push(&in->frozen, buf+offset, &np);
+       offset += np;
 
-       array = talloc(mem_ctx, struct ctdb_uint8_array);
-       if (array == NULL) {
-               return ENOMEM;
+       ctdb_uint32_push(&in->recovering, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->client_packets_sent, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->client_packets_recv, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->node_packets_sent, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->node_packets_recv, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->keepalive_packets_sent, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->keepalive_packets_recv, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->node.req_call, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->node.reply_call, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->node.req_dmaster, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->node.reply_dmaster, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->node.reply_error, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->node.req_message, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->node.req_control, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->node.reply_control, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->client.req_call, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->client.req_message, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->client.req_control, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->timeouts.call, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->timeouts.control, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->timeouts.traverse, buf+offset, &np);
+       offset += np;
+
+       ctdb_padding_push(4, buf+offset, &np);
+       offset += np;
+
+       ctdb_latency_counter_push(&in->reclock.ctdbd, buf+offset, &np);
+       offset += np;
+
+       ctdb_latency_counter_push(&in->reclock.recd, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->locks.num_calls, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->locks.num_current, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->locks.num_pending, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->locks.num_failed, buf+offset, &np);
+       offset += np;
+
+       ctdb_latency_counter_push(&in->locks.latency, buf+offset, &np);
+       offset += np;
+
+       for (i=0; i<MAX_COUNT_BUCKETS; i++) {
+               ctdb_uint32_push(&in->locks.buckets[i], buf+offset, &np);
+               offset += np;
        }
 
-       array->num = buflen / sizeof(uint8_t);
+       ctdb_uint32_push(&in->total_calls, buf+offset, &np);
+       offset += np;
 
-       array->val = talloc_array(array, uint8_t, array->num);
-       if (array->val == NULL) {
-               talloc_free(array);
-               return ENOMEM;
+       ctdb_uint32_push(&in->pending_calls, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->childwrite_calls, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->pending_childwrite_calls, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->memory_used, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->__last_counter, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->max_hop_count, buf+offset, &np);
+       offset += np;
+
+       for (i=0; i<MAX_COUNT_BUCKETS; i++) {
+               ctdb_uint32_push(&in->hop_count_bucket[i], buf+offset, &np);
+               offset += np;
        }
-       memcpy(array->val, buf, buflen);
 
-       *out = array;
-       return 0;
-}
+       ctdb_padding_push(4, buf+offset, &np);
+       offset += np;
 
-size_t ctdb_uint64_array_len(struct ctdb_uint64_array *array)
-{
-       return array->num * sizeof(uint64_t);
-}
+       ctdb_latency_counter_push(&in->call_latency, buf+offset, &np);
+       offset += np;
 
-void ctdb_uint64_array_push(struct ctdb_uint64_array *array, uint8_t *buf)
-{
-       memcpy(buf, array->val, array->num * sizeof(uint64_t));
+       ctdb_latency_counter_push(&in->childwrite_latency, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->num_recoveries, buf+offset, &np);
+       offset += np;
+
+       ctdb_padding_push(4, buf+offset, &np);
+       offset += np;
+
+       ctdb_timeval_push(&in->statistics_start_time, buf+offset, &np);
+       offset += np;
+
+       ctdb_timeval_push(&in->statistics_current_time, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->total_ro_delegations, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->total_ro_revokes, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
-int ctdb_uint64_array_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                          struct ctdb_uint64_array **out)
+static int ctdb_statistics_pull_elems(uint8_t *buf, size_t buflen,
+                                     TALLOC_CTX *mem_ctx,
+                                     struct ctdb_statistics *out,
+                                     size_t *npull)
 {
-       struct ctdb_uint64_array *array;
+       size_t offset = 0, np;
+       int ret, i;
 
-       array = talloc(mem_ctx, struct ctdb_uint64_array);
-       if (array == NULL) {
-               return ENOMEM;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->num_clients,
+                              &np);
+       if (ret != 0) {
+               return ret;
        }
+       offset += np;
 
-       array->num = buflen / sizeof(uint64_t);
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->frozen, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
-       array->val = talloc_array(array, uint64_t, array->num);
-       if (array->val == NULL) {
-               talloc_free(array);
-               return ENOMEM;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->recovering,
+                              &np);
+       if (ret != 0) {
+               return ret;
        }
-       memcpy(array->val, buf, buflen);
+       offset += np;
 
-       *out = array;
-       return 0;
-}
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->client_packets_sent, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
-size_t ctdb_pid_len(pid_t pid)
-{
-       return sizeof(pid_t);
-}
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->client_packets_recv, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
-void ctdb_pid_push(pid_t pid, uint8_t *buf)
-{
-       memcpy(buf, &pid, sizeof(pid_t));
-}
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->node_packets_sent, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
-int ctdb_pid_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                 pid_t *out)
-{
-       if (buflen < sizeof(pid_t)) {
-               return EMSGSIZE;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->node_packets_recv, &np);
+       if (ret != 0) {
+               return ret;
        }
+       offset += np;
 
-       *out = *(pid_t *)buf;
-       return 0;
-}
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->keepalive_packets_sent, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
-size_t ctdb_string_len(const char *str)
-{
-       if (str == NULL) {
-               return 0;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->keepalive_packets_recv, &np);
+       if (ret != 0) {
+               return ret;
        }
-       return strlen(str) + 1;
-}
+       offset += np;
 
-void ctdb_string_push(const char *str, uint8_t *buf)
-{
-       if (str == NULL) {
-               return;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->node.req_call, &np);
+       if (ret != 0) {
+               return ret;
        }
-       memcpy(buf, str, strlen(str)+1);
-}
+       offset += np;
 
-int ctdb_string_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                    const char **out)
-{
-       char *str;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->node.reply_call, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
-       if (buflen == 0) {
-               *out = NULL;
-               return 0;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->node.req_dmaster, &np);
+       if (ret != 0) {
+               return ret;
        }
+       offset += np;
 
-       str = talloc_strndup(mem_ctx, (char *)buf, buflen);
-       if (str == NULL) {
-               return ENOMEM;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->node.reply_dmaster, &np);
+       if (ret != 0) {
+               return ret;
        }
+       offset += np;
 
-       *out = str;
-       return 0;
-}
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->node.reply_error, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
-struct stringn_wire {
-       uint32_t length;
-       uint8_t str[1];
-};
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->node.req_message, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
-size_t ctdb_stringn_len(const char *str)
-{
-       return sizeof(uint32_t) + strlen(str) + 1;
-}
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->node.req_control, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
-void ctdb_stringn_push(const char *str, uint8_t *buf)
-{
-       struct stringn_wire *wire = (struct stringn_wire *)buf;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->node.reply_control, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
-       if (str == NULL) {
-               wire->length = 0;
-       } else {
-               wire->length = strlen(str) + 1;
-               memcpy(wire->str, str, wire->length);
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->client.req_call, &np);
+       if (ret != 0) {
+               return ret;
        }
-}
+       offset += np;
 
-int ctdb_stringn_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                     const char **out)
-{
-       char *str;
-       struct stringn_wire *wire = (struct stringn_wire *)buf;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->client.req_message, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
-       if (buflen < sizeof(uint32_t)) {
-               return EMSGSIZE;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->client.req_control, &np);
+       if (ret != 0) {
+               return ret;
        }
+       offset += np;
 
-       if (buflen < sizeof(uint32_t) + wire->length) {
-               return EMSGSIZE;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->timeouts.call, &np);
+       if (ret != 0) {
+               return ret;
        }
+       offset += np;
 
-       str = talloc_strndup(mem_ctx, (char *)wire->str, wire->length);
-       if (str == NULL) {
-               return ENOMEM;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->timeouts.control, &np);
+       if (ret != 0) {
+               return ret;
        }
+       offset += np;
 
-       *out = str;
-       return 0;
-}
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->timeouts.traverse, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
-size_t ctdb_statistics_len(struct ctdb_statistics *stats)
-{
-       return sizeof(struct ctdb_statistics);
-}
+       ret = ctdb_padding_pull(buf+offset, buflen-offset, 4, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
-void ctdb_statistics_push(struct ctdb_statistics *stats, uint8_t *buf)
-{
-       memcpy(buf, stats, sizeof(struct ctdb_statistics));
+       ret = ctdb_latency_counter_pull(buf+offset, buflen-offset,
+                                       &out->reclock.ctdbd, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_latency_counter_pull(buf+offset, buflen-offset,
+                                       &out->reclock.recd, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->locks.num_calls, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->locks.num_current, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->locks.num_pending, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->locks.num_failed, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_latency_counter_pull(buf+offset, buflen-offset,
+                                       &out->locks.latency, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       for (i=0;  i<MAX_COUNT_BUCKETS; i++) {
+               ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                                      &out->locks.buckets[i], &np);
+               if (ret != 0) {
+                       return ret;
+               }
+               offset += np;
+       }
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->total_calls, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->pending_calls, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->childwrite_calls, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->pending_childwrite_calls, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->memory_used,
+                              &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->__last_counter, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->max_hop_count, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       for (i=0;  i<MAX_COUNT_BUCKETS; i++) {
+               ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                                      &out->hop_count_bucket[i], &np);
+               if (ret != 0) {
+                       return ret;
+               }
+               offset += np;
+       }
+
+       ret = ctdb_padding_pull(buf+offset, buflen-offset, 4, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_latency_counter_pull(buf+offset, buflen-offset,
+                                       &out->call_latency, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_latency_counter_pull(buf+offset, buflen-offset,
+                                       &out->childwrite_latency, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->num_recoveries, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_padding_pull(buf+offset, buflen-offset, 4, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_timeval_pull(buf+offset, buflen-offset,
+                               &out->statistics_start_time, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_timeval_pull(buf+offset, buflen-offset,
+                               &out->statistics_current_time, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->total_ro_delegations, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                              &out->total_ro_revokes, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       *npull = offset;
+       return 0;
 }
 
 int ctdb_statistics_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                        struct ctdb_statistics **out)
+                        struct ctdb_statistics **out, size_t *npull)
 {
-       struct ctdb_statistics *stats;
-       struct ctdb_statistics *wire = (struct ctdb_statistics *)buf;
+       struct ctdb_statistics *val;
+       size_t np;
+       int ret;
 
-       if (buflen < sizeof(struct ctdb_statistics)) {
-               return EMSGSIZE;
+       val = talloc(mem_ctx, struct ctdb_statistics);
+       if (val == NULL) {
+               return ENOMEM;
        }
 
-       stats = talloc(mem_ctx, struct ctdb_statistics);
-       if (stats == NULL) {
-               return ENOMEM;
+       ret = ctdb_statistics_pull_elems(buf, buflen, val, val, &np);
+       if (ret != 0) {
+               talloc_free(val);
+               return ret;
        }
-       memcpy(stats, wire, sizeof(struct ctdb_statistics));
 
-       *out = stats;
+       *out = val;
+       *npull = np;
        return 0;
 }
 
@@ -319,6 +811,14 @@ int ctdb_statistics_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        if (buflen < offsetof(struct ctdb_statistics_list_wire, stats)) {
                return EMSGSIZE;
        }
+       if (wire->num > buflen / sizeof(struct ctdb_statistics)) {
+               return EMSGSIZE;
+       }
+       if (offsetof(struct ctdb_statistics_list_wire, stats) +
+           wire->num * sizeof(struct ctdb_statistics) <
+           offsetof(struct ctdb_statistics_list_wire, stats)) {
+               return EMSGSIZE;
+       }
        if (buflen < offsetof(struct ctdb_statistics_list_wire, stats) +
                     wire->num * sizeof(struct ctdb_statistics)) {
                return EMSGSIZE;
@@ -345,293 +845,579 @@ int ctdb_statistics_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        return 0;
 }
 
-struct ctdb_vnn_map_wire {
-       uint32_t generation;
-       uint32_t size;
-       uint32_t map[1];
-};
+size_t ctdb_vnn_map_len(struct ctdb_vnn_map *in)
+{
+       size_t len;
+
+       len = ctdb_uint32_len(&in->generation) + ctdb_uint32_len(&in->size);
+       if (in->size > 0) {
+               len += in->size * ctdb_uint32_len(&in->map[0]);
+       }
+
+       return len;
+}
 
-size_t ctdb_vnn_map_len(struct ctdb_vnn_map *vnnmap)
+void ctdb_vnn_map_push(struct ctdb_vnn_map *in, uint8_t *buf, size_t *npush)
 {
-       return offsetof(struct ctdb_vnn_map, map) +
-              vnnmap->size * sizeof(uint32_t);
+       size_t offset = 0, np;
+       uint32_t i;
+
+       ctdb_uint32_push(&in->generation, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->size, buf+offset, &np);
+       offset += np;
+
+       for (i=0; i<in->size; i++) {
+               ctdb_uint32_push(&in->map[i], buf+offset, &np);
+               offset += np;
+       }
+
+       *npush = offset;
 }
 
-void ctdb_vnn_map_push(struct ctdb_vnn_map *vnnmap, uint8_t *buf)
+int ctdb_vnn_map_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
+                     struct ctdb_vnn_map **out, size_t *npull)
 {
-       struct ctdb_vnn_map_wire *wire = (struct ctdb_vnn_map_wire *)buf;
+       struct ctdb_vnn_map *val;
+       size_t offset = 0, np;
+       uint32_t i;
+       int ret;
+
+       val = talloc(mem_ctx, struct ctdb_vnn_map);
+       if (val == NULL) {
+               return ENOMEM;
+       }
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->generation,
+                              &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->size, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       if (val->size == 0) {
+               val->map = NULL;
+               goto done;
+       }
+
+       val->map = talloc_array(val, uint32_t, val->size);
+       if (val->map == NULL) {
+               ret = ENOMEM;
+               goto fail;
+       }
+
+       for (i=0; i<val->size; i++) {
+               ret = ctdb_uint32_pull(buf+offset, buflen-offset,
+                                      &val->map[i], &np);
+               if (ret != 0) {
+                       goto fail;
+               }
+               offset += np;
+       }
+
+done:
+       *out = val;
+       *npull = offset;
+       return 0;
+
+fail:
+       talloc_free(val);
+       return ret;
+}
+
+size_t ctdb_dbid_len(struct ctdb_dbid *in)
+{
+       return ctdb_uint32_len(&in->db_id) +
+               ctdb_uint8_len(&in->flags) +
+               ctdb_padding_len(3);
+}
+
+void ctdb_dbid_push(struct ctdb_dbid *in, uint8_t *buf, size_t *npush)
+{
+       size_t offset = 0, np;
+
+       ctdb_uint32_push(&in->db_id, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint8_push(&in->flags, buf+offset, &np);
+       offset += np;
+
+       ctdb_padding_push(3, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
+}
+
+static int ctdb_dbid_pull_elems(uint8_t *buf, size_t buflen,
+                               TALLOC_CTX *mem_ctx, struct ctdb_dbid *out,
+                               size_t *npull)
+{
+       size_t offset = 0, np;
+       int ret;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->db_id, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint8_pull(buf+offset, buflen-offset, &out->flags, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_padding_pull(buf+offset, buflen-offset, 3, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
-       memcpy(wire, vnnmap, offsetof(struct ctdb_vnn_map, map));
-       memcpy(wire->map, vnnmap->map, vnnmap->size * sizeof(uint32_t));
+       *npull = offset;
+       return 0;
 }
 
-int ctdb_vnn_map_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                     struct ctdb_vnn_map **out)
+int ctdb_dbid_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
+                  struct ctdb_dbid **out, size_t *npull)
 {
-       struct ctdb_vnn_map *vnnmap;
-       struct ctdb_vnn_map_wire *wire = (struct ctdb_vnn_map_wire *)buf;
-
-       if (buflen < offsetof(struct ctdb_vnn_map_wire, map)) {
-               return EMSGSIZE;
-       }
-       if (buflen < offsetof(struct ctdb_vnn_map_wire, map) +
-                    wire->size * sizeof(uint32_t)) {
-               return EMSGSIZE;
-       }
+       struct ctdb_dbid *val;
+       size_t np;
+       int ret;
 
-       vnnmap = talloc(mem_ctx, struct ctdb_vnn_map);
-       if (vnnmap == NULL) {
+       val = talloc(mem_ctx, struct ctdb_dbid);
+       if (val == NULL) {
                return ENOMEM;
        }
 
-       memcpy(vnnmap, wire, offsetof(struct ctdb_vnn_map, map));
-
-       vnnmap->map = talloc_memdup(vnnmap, wire->map,
-                                   wire->size * sizeof(uint32_t));
-       if (vnnmap->map == NULL) {
-               talloc_free(vnnmap);
-               return ENOMEM;
+       ret = ctdb_dbid_pull_elems(buf, buflen, val, val, &np);
+       if (ret != 0) {
+               talloc_free(val);
+               return ret;
        }
 
-       *out = vnnmap;
+       *out = val;
+       *npull = np;
        return 0;
 }
 
-struct ctdb_dbid_map_wire {
-       uint32_t num;
-       struct ctdb_dbid dbs[1];
-};
-
-size_t ctdb_dbid_map_len(struct ctdb_dbid_map *dbmap)
+size_t ctdb_dbid_map_len(struct ctdb_dbid_map *in)
 {
-       return sizeof(uint32_t) + dbmap->num * sizeof(struct ctdb_dbid);
+       size_t len;
+
+       len = ctdb_uint32_len(&in->num);
+       if (in->num > 0) {
+               len += in->num * ctdb_dbid_len(&in->dbs[0]);
+       }
+
+       return len;
 }
 
-void ctdb_dbid_map_push(struct ctdb_dbid_map *dbmap, uint8_t *buf)
+void ctdb_dbid_map_push(struct ctdb_dbid_map *in, uint8_t *buf, size_t *npush)
 {
-       struct ctdb_dbid_map_wire *wire = (struct ctdb_dbid_map_wire *)buf;
+       size_t offset = 0, np;
+       uint32_t i;
+
+       ctdb_uint32_push(&in->num, buf+offset, &np);
+       offset += np;
+
+       for (i=0; i<in->num; i++) {
+               ctdb_dbid_push(&in->dbs[i], buf+offset, &np);
+               offset += np;
+       }
 
-       wire->num = dbmap->num;
-       memcpy(wire->dbs, dbmap->dbs, dbmap->num * sizeof(struct ctdb_dbid));
+       *npush = offset;
 }
 
 int ctdb_dbid_map_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                      struct ctdb_dbid_map **out)
+                      struct ctdb_dbid_map **out, size_t *npull)
 {
-       struct ctdb_dbid_map *dbmap;
-       struct ctdb_dbid_map_wire *wire = (struct ctdb_dbid_map_wire *)buf;
+       struct ctdb_dbid_map *val;
+       size_t offset = 0, np;
+       uint32_t i;
+       int ret;
 
-       if (buflen < sizeof(uint32_t)) {
-               return EMSGSIZE;
+       val = talloc(mem_ctx, struct ctdb_dbid_map);
+       if (val == NULL) {
+               return ENOMEM;
        }
-       if (buflen < sizeof(uint32_t) + wire->num * sizeof(struct ctdb_dbid)) {
-               return EMSGSIZE;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->num, &np);
+       if (ret != 0) {
+               goto fail;
        }
+       offset += np;
 
-       dbmap = talloc(mem_ctx, struct ctdb_dbid_map);
-       if (dbmap == NULL) {
-               return ENOMEM;
+       if (val->num == 0) {
+               val->dbs = NULL;
+               goto done;
        }
 
-       dbmap->num = wire->num;
+       val->dbs = talloc_array(val, struct ctdb_dbid, val->num);
+       if (val->dbs == NULL) {
+               ret = ENOMEM;
+               goto fail;
+       }
 
-       dbmap->dbs = talloc_memdup(dbmap, wire->dbs,
-                                  wire->num * sizeof(struct ctdb_dbid));
-       if (dbmap->dbs == NULL) {
-               talloc_free(dbmap);
-               return ENOMEM;
+       for (i=0; i<val->num; i++) {
+               ret = ctdb_dbid_pull_elems(buf+offset, buflen-offset, val,
+                                          &val->dbs[i], &np);
+               if (ret != 0) {
+                       goto fail;
+               }
+               offset += np;
        }
 
-       *out = dbmap;
+done:
+       *out = val;
+       *npull = offset;
        return 0;
+
+fail:
+       talloc_free(val);
+       return ret;
 }
 
-size_t ctdb_pulldb_len(struct ctdb_pulldb *pulldb)
+size_t ctdb_pulldb_len(struct ctdb_pulldb *in)
 {
-       return sizeof(struct ctdb_pulldb);
+       return ctdb_uint32_len(&in->db_id) +
+               ctdb_uint32_len(&in->lmaster);
 }
 
-void ctdb_pulldb_push(struct ctdb_pulldb *pulldb, uint8_t *buf)
+void ctdb_pulldb_push(struct ctdb_pulldb *in, uint8_t *buf, size_t *npush)
 {
-       memcpy(buf, pulldb, sizeof(struct ctdb_pulldb));
+       size_t offset = 0, np;
+
+       ctdb_uint32_push(&in->db_id, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->lmaster, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
 int ctdb_pulldb_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                    struct ctdb_pulldb **out)
+                    struct ctdb_pulldb **out, size_t *npull)
 {
-       struct ctdb_pulldb *pulldb;
+       struct ctdb_pulldb *val;
+       size_t offset = 0, np;
+       int ret;
 
-       if (buflen < sizeof(struct ctdb_pulldb)) {
-               return EMSGSIZE;
+       val = talloc(mem_ctx, struct ctdb_pulldb);
+       if (val == NULL) {
+               return ENOMEM;
        }
 
-       pulldb = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_pulldb));
-       if (pulldb == NULL) {
-               return ENOMEM;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->db_id, &np);
+       if (ret != 0) {
+               talloc_free(val);
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->lmaster, &np);
+       if (ret != 0) {
+               talloc_free(val);
+               return ret;
        }
+       offset += np;
 
-       *out = pulldb;
+       *out = val;
+       *npull = offset;
        return 0;
 }
 
-size_t ctdb_pulldb_ext_len(struct ctdb_pulldb_ext *pulldb)
+size_t ctdb_pulldb_ext_len(struct ctdb_pulldb_ext *in)
 {
-       return sizeof(struct ctdb_pulldb_ext);
+       return ctdb_uint32_len(&in->db_id) +
+               ctdb_uint32_len(&in->lmaster) +
+               ctdb_uint64_len(&in->srvid);
 }
 
-void ctdb_pulldb_ext_push(struct ctdb_pulldb_ext *pulldb, uint8_t *buf)
+void ctdb_pulldb_ext_push(struct ctdb_pulldb_ext *in, uint8_t *buf,
+                         size_t *npush)
 {
-       memcpy(buf, pulldb, sizeof(struct ctdb_pulldb_ext));
+       size_t offset = 0, np;
+
+       ctdb_uint32_push(&in->db_id, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->lmaster, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint64_push(&in->srvid, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
 int ctdb_pulldb_ext_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                        struct ctdb_pulldb_ext **out)
+                        struct ctdb_pulldb_ext **out, size_t *npull)
 {
-       struct ctdb_pulldb_ext *pulldb;
+       struct ctdb_pulldb_ext *val;
+       size_t offset = 0, np;
+       int ret;
 
-       if (buflen < sizeof(struct ctdb_pulldb_ext)) {
-               return EMSGSIZE;
+       val = talloc(mem_ctx, struct ctdb_pulldb_ext);
+       if (val == NULL) {
+               return ENOMEM;
        }
 
-       pulldb = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_pulldb_ext));
-       if (pulldb == NULL) {
-               return ENOMEM;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->db_id, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->lmaster, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_uint64_pull(buf+offset, buflen-offset, &val->srvid, &np);
+       if (ret != 0) {
+               goto fail;
        }
+       offset += np;
 
-       *out = pulldb;
+       *out = val;
+       *npull = offset;
        return 0;
+
+fail:
+       talloc_free(val);
+       return ret;
 }
 
-size_t ctdb_ltdb_header_len(struct ctdb_ltdb_header *header)
+size_t ctdb_ltdb_header_len(struct ctdb_ltdb_header *in)
 {
-       return sizeof(struct ctdb_ltdb_header);
+       return ctdb_uint64_len(&in->rsn) +
+               ctdb_uint32_len(&in->dmaster) +
+               ctdb_uint32_len(&in->reserved1) +
+               ctdb_uint32_len(&in->flags) +
+               ctdb_padding_len(4);
 }
 
-void ctdb_ltdb_header_push(struct ctdb_ltdb_header *header, uint8_t *buf)
+void ctdb_ltdb_header_push(struct ctdb_ltdb_header *in, uint8_t *buf,
+                          size_t *npush)
 {
-       memcpy(buf, header, sizeof(struct ctdb_ltdb_header));
+       size_t offset = 0, np;
+
+       ctdb_uint64_push(&in->rsn, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->dmaster, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->reserved1, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->flags, buf+offset, &np);
+       offset += np;
+
+       ctdb_padding_push(4, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
 int ctdb_ltdb_header_pull(uint8_t *buf, size_t buflen,
-                         struct ctdb_ltdb_header *header)
+                         struct ctdb_ltdb_header *out, size_t *npull)
 {
-       if (buflen < sizeof(struct ctdb_ltdb_header)) {
-               return EMSGSIZE;
+       size_t offset = 0, np;
+       int ret;
+
+       ret = ctdb_uint64_pull(buf+offset, buflen-offset, &out->rsn, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->dmaster, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->reserved1,
+                              &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->flags, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_padding_pull(buf+offset, buflen-offset, 4, &np);
+       if (ret != 0) {
+               return ret;
        }
+       offset += np;
 
-       memcpy(header, buf, sizeof(struct ctdb_ltdb_header));
+       *npull = offset;
        return 0;
 }
 
 int ctdb_ltdb_header_extract(TDB_DATA *data, struct ctdb_ltdb_header *header)
 {
+       size_t np;
        int ret;
 
-       ret = ctdb_ltdb_header_pull(data->dptr, data->dsize, header);
+       ret = ctdb_ltdb_header_pull(data->dptr, data->dsize, header, &np);
        if (ret != 0) {
                return ret;
        }
 
-       data->dptr += sizeof(struct ctdb_ltdb_header);
-       data->dsize -= sizeof(struct ctdb_ltdb_header);
+       data->dptr += np;
+       data->dsize -= np;
 
        return 0;
 }
 
-struct ctdb_rec_data_wire {
-       uint32_t length;
-       uint32_t reqid;
-       uint32_t keylen;
-       uint32_t datalen;
-       uint8_t data[1];
-};
-
-size_t ctdb_rec_data_len(struct ctdb_rec_data *rec)
+size_t ctdb_rec_data_len(struct ctdb_rec_data *in)
 {
-       return offsetof(struct ctdb_rec_data_wire, data) +
-              rec->key.dsize + rec->data.dsize +
-              (rec->header == NULL ? 0 : sizeof(struct ctdb_ltdb_header));
+       uint32_t u32;
+
+       u32 = ctdb_uint32_len(&in->reqid) +
+               ctdb_tdb_datan_len(&in->key) +
+               ctdb_tdb_datan_len(&in->data);
+
+       if (in->header != NULL) {
+               u32 += ctdb_ltdb_header_len(in->header);
+       }
+
+       return ctdb_uint32_len(&u32) + u32;
 }
 
-void ctdb_rec_data_push(struct ctdb_rec_data *rec, uint8_t *buf)
+void ctdb_rec_data_push(struct ctdb_rec_data *in, uint8_t *buf, size_t *npush)
 {
-       struct ctdb_rec_data_wire *wire = (struct ctdb_rec_data_wire *)buf;
-       size_t offset;
+       size_t offset = 0, np;
+       uint32_t u32;
 
-       wire->length = ctdb_rec_data_len(rec);
-       wire->reqid = rec->reqid;
-       wire->keylen = rec->key.dsize;
-       wire->datalen = rec->data.dsize;
-       if (rec->header != NULL) {
-               wire->datalen += sizeof(struct ctdb_ltdb_header);
-       }
+       u32 = ctdb_rec_data_len(in);
+       ctdb_uint32_push(&u32, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->reqid, buf+offset, &np);
+       offset += np;
+
+       u32 = ctdb_tdb_data_len(&in->key);
+       ctdb_uint32_push(&u32, buf+offset, &np);
+       offset += np;
 
-       memcpy(wire->data, rec->key.dptr, rec->key.dsize);
-       offset = rec->key.dsize;
-       if (rec->header != NULL) {
-               memcpy(&wire->data[offset], rec->header,
-                      sizeof(struct ctdb_ltdb_header));
-               offset += sizeof(struct ctdb_ltdb_header);
+       u32 = ctdb_tdb_data_len(&in->data);
+       if (in->header != NULL) {
+               u32 += ctdb_ltdb_header_len(in->header);
        }
-       if (rec->data.dsize > 0) {
-               memcpy(&wire->data[offset], rec->data.dptr, rec->data.dsize);
+
+       ctdb_uint32_push(&u32, buf+offset, &np);
+       offset += np;
+
+       ctdb_tdb_data_push(&in->key, buf+offset, &np);
+       offset += np;
+
+       /* If ltdb header is not NULL, then it is pushed as part of the data */
+       if (in->header != NULL) {
+               ctdb_ltdb_header_push(in->header, buf+offset, &np);
+               offset += np;
        }
+       ctdb_tdb_data_push(&in->data, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
 static int ctdb_rec_data_pull_data(uint8_t *buf, size_t buflen,
                                   uint32_t *reqid,
-                                  struct ctdb_ltdb_header **header,
                                   TDB_DATA *key, TDB_DATA *data,
-                                  size_t *reclen)
+                                  size_t *npull)
 {
-       struct ctdb_rec_data_wire *wire = (struct ctdb_rec_data_wire *)buf;
-       size_t offset, n;
+       size_t offset = 0, np;
+       size_t len;
+       uint32_t u32;
+       int ret;
 
-       if (buflen < offsetof(struct ctdb_rec_data_wire, data)) {
-               return EMSGSIZE;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
+       if (ret != 0) {
+               return ret;
        }
-       n = offsetof(struct ctdb_rec_data_wire, data) +
-               wire->keylen + wire->datalen;
-       if (buflen < n) {
+       offset += np;
+
+       if (buflen < u32) {
                return EMSGSIZE;
        }
+       len = u32;
+
+       ret = ctdb_uint32_pull(buf+offset, len-offset, reqid, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, len-offset, &u32, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+       key->dsize = u32;
 
-       *reqid = wire->reqid;
+       ret = ctdb_uint32_pull(buf+offset, len-offset, &u32, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+       data->dsize = u32;
 
-       key->dsize = wire->keylen;
-       key->dptr = wire->data;
-       offset = wire->keylen;
+       if (len-offset < key->dsize) {
+               return EMSGSIZE;
+       }
 
-       /* Always set header to NULL.  If it is required, exact it using
-        * ctdb_rec_data_extract_header()
-        */
-       *header = NULL;
+       key->dptr = buf+offset;
+       offset += key->dsize;
 
-       data->dsize = wire->datalen;
-       data->dptr = &wire->data[offset];
+       if (len-offset < data->dsize) {
+               return EMSGSIZE;
+       }
 
-       *reclen = n;
+       data->dptr = buf+offset;
+       offset += data->dsize;
 
+       *npull = offset;
        return 0;
 }
 
 static int ctdb_rec_data_pull_elems(uint8_t *buf, size_t buflen,
                                    TALLOC_CTX *mem_ctx,
-                                   struct ctdb_rec_data *out)
+                                   struct ctdb_rec_data *out,
+                                   size_t *npull)
 {
        uint32_t reqid;
-       struct ctdb_ltdb_header *header;
        TDB_DATA key, data;
-       size_t reclen;
+       size_t np;
        int ret;
 
-       ret = ctdb_rec_data_pull_data(buf, buflen, &reqid, &header,
-                                     &key, &data, &reclen);
+       ret = ctdb_rec_data_pull_data(buf, buflen, &reqid, &key, &data, &np);
        if (ret != 0) {
                return ret;
        }
 
        out->reqid = reqid;
+
+       /* Always set header to NULL.  If it is required, extract it using
+        * ctdb_rec_data_extract_header()
+        */
        out->header = NULL;
 
        out->key.dsize = key.dsize;
@@ -650,80 +1436,114 @@ static int ctdb_rec_data_pull_elems(uint8_t *buf, size_t buflen,
                }
        }
 
+       *npull = np;
        return 0;
 }
 
 int ctdb_rec_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                      struct ctdb_rec_data **out)
+                      struct ctdb_rec_data **out, size_t *npull)
 {
-       struct ctdb_rec_data *rec;
+       struct ctdb_rec_data *val;
+       size_t np;
        int ret;
 
-       rec = talloc(mem_ctx, struct ctdb_rec_data);
-       if (rec == NULL) {
+       val = talloc(mem_ctx, struct ctdb_rec_data);
+       if (val == NULL) {
                return ENOMEM;
        }
 
-       ret = ctdb_rec_data_pull_elems(buf, buflen, rec, rec);
+       ret = ctdb_rec_data_pull_elems(buf, buflen, val, val, &np);
        if (ret != 0) {
-               TALLOC_FREE(rec);
+               TALLOC_FREE(val);
+               return ret;
        }
 
-       *out = rec;
+       *out = val;
+       *npull = np;
        return ret;
 }
 
-struct ctdb_rec_buffer_wire {
-       uint32_t db_id;
-       uint32_t count;
-       uint8_t data[1];
-};
-
-size_t ctdb_rec_buffer_len(struct ctdb_rec_buffer *recbuf)
+size_t ctdb_rec_buffer_len(struct ctdb_rec_buffer *in)
 {
-       return offsetof(struct ctdb_rec_buffer_wire, data) + recbuf->buflen;
+       return ctdb_uint32_len(&in->db_id) +
+               ctdb_uint32_len(&in->count) +
+               in->buflen;
 }
 
-void ctdb_rec_buffer_push(struct ctdb_rec_buffer *recbuf, uint8_t *buf)
+void ctdb_rec_buffer_push(struct ctdb_rec_buffer *in, uint8_t *buf,
+                         size_t *npush)
 {
-       struct ctdb_rec_buffer_wire *wire = (struct ctdb_rec_buffer_wire *)buf;
+       size_t offset = 0, np;
 
-       wire->db_id = recbuf->db_id;
-       wire->count = recbuf->count;
-       if (recbuf->buflen > 0) {
-               memcpy(wire->data, recbuf->buf, recbuf->buflen);
-       }
+       ctdb_uint32_push(&in->db_id, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->count, buf+offset, &np);
+       offset += np;
+
+       memcpy(buf+offset, in->buf, in->buflen);
+       offset += in->buflen;
+
+       *npush = offset;
 }
 
 int ctdb_rec_buffer_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                        struct ctdb_rec_buffer **out)
+                        struct ctdb_rec_buffer **out, size_t *npull)
 {
-       struct ctdb_rec_buffer *recbuf;
-       struct ctdb_rec_buffer_wire *wire = (struct ctdb_rec_buffer_wire *)buf;
-       size_t offset;
+       struct ctdb_rec_buffer *val;
+       size_t offset = 0, np;
+       size_t length;
+       int ret;
 
-       if (buflen < offsetof(struct ctdb_rec_buffer_wire, data)) {
-               return EMSGSIZE;
+       val = talloc(mem_ctx, struct ctdb_rec_buffer);
+       if (val == NULL) {
+               return ENOMEM;
        }
 
-       recbuf = talloc(mem_ctx, struct ctdb_rec_buffer);
-       if (recbuf == NULL) {
-               return ENOMEM;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->db_id, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->count, &np);
+       if (ret != 0) {
+               goto fail;
        }
+       offset += np;
 
-       recbuf->db_id = wire->db_id;
-       recbuf->count = wire->count;
+       /* Since there is no buflen provided, walk the records to
+        * validate the length of the buffer.
+        */
+       val->buf = buf+offset;
+       val->buflen = buflen-offset;
 
-       offset = offsetof(struct ctdb_rec_buffer_wire, data);
-       recbuf->buflen = buflen - offset;
-       recbuf->buf = talloc_memdup(recbuf, wire->data, recbuf->buflen);
-       if (recbuf->buf == NULL) {
-               talloc_free(recbuf);
-               return ENOMEM;
+       length = 0;
+       ret = ctdb_rec_buffer_traverse(val, NULL, &length);
+       if (ret != 0) {
+               goto fail;
        }
 
-       *out = recbuf;
+       if (length > buflen-offset) {
+               ret = EMSGSIZE;
+               goto fail;
+       }
+
+       val->buf = talloc_memdup(val, buf+offset, length);
+       if (val->buf == NULL) {
+               ret = ENOMEM;
+               goto fail;
+       }
+       val->buflen = length;
+       offset += length;
+
+       *out = val;
+       *npull = offset;
        return 0;
+
+fail:
+       talloc_free(val);
+       return ret;
 }
 
 struct ctdb_rec_buffer *ctdb_rec_buffer_init(TALLOC_CTX *mem_ctx,
@@ -746,7 +1566,7 @@ int ctdb_rec_buffer_add(TALLOC_CTX *mem_ctx, struct ctdb_rec_buffer *recbuf,
                        TDB_DATA key, TDB_DATA data)
 {
        struct ctdb_rec_data recdata;
-       size_t len;
+       size_t len, np;
        uint8_t *ptr;
 
        recdata.reqid = reqid;
@@ -762,11 +1582,11 @@ int ctdb_rec_buffer_add(TALLOC_CTX *mem_ctx, struct ctdb_rec_buffer *recbuf,
                return ENOMEM;
        }
 
-       ctdb_rec_data_push(&recdata, &ptr[recbuf->buflen]);
+       ctdb_rec_data_push(&recdata, &ptr[recbuf->buflen], &np);
 
        recbuf->count++;
        recbuf->buf = ptr;
-       recbuf->buflen += len;
+       recbuf->buflen += np;
        return 0;
 }
 
@@ -774,7 +1594,6 @@ int ctdb_rec_buffer_traverse(struct ctdb_rec_buffer *recbuf,
                             ctdb_rec_parser_func_t func,
                             void *private_data)
 {
-       struct ctdb_ltdb_header *header;
        TDB_DATA key, data;
        uint32_t reqid;
        size_t offset, reclen;
@@ -784,21 +1603,32 @@ int ctdb_rec_buffer_traverse(struct ctdb_rec_buffer *recbuf,
        for (i=0; i<recbuf->count; i++) {
                ret = ctdb_rec_data_pull_data(&recbuf->buf[offset],
                                              recbuf->buflen - offset,
-                                             &reqid, &header,
-                                             &key, &data, &reclen);
+                                             &reqid, &key, &data, &reclen);
                if (ret != 0) {
                        return ret;
                }
 
-               ret = func(reqid, header, key, data, private_data);
-               if (ret != 0) {
-                       break;
+               if (func != NULL) {
+                       ret = func(reqid, NULL, key, data, private_data);
+                       if (ret != 0) {
+                               break;
+                       }
                }
 
                offset += reclen;
        }
 
-       return ret;
+       if (ret != 0) {
+               return ret;
+       }
+
+       if (func == NULL) {
+               size_t *length = (size_t *)private_data;
+
+               *length = offset;
+       }
+
+       return 0;
 }
 
 int ctdb_rec_buffer_write(struct ctdb_rec_buffer *recbuf, int fd)
@@ -863,375 +1693,687 @@ int ctdb_rec_buffer_read(int fd, TALLOC_CTX *mem_ctx,
        return 0;
 }
 
-size_t ctdb_traverse_start_len(struct ctdb_traverse_start *traverse)
+size_t ctdb_traverse_start_len(struct ctdb_traverse_start *in)
 {
-       return sizeof(struct ctdb_traverse_start);
+       return ctdb_uint32_len(&in->db_id) +
+               ctdb_uint32_len(&in->reqid) +
+               ctdb_uint64_len(&in->srvid);
 }
 
-void ctdb_traverse_start_push(struct ctdb_traverse_start *traverse,
-                             uint8_t *buf)
+void ctdb_traverse_start_push(struct ctdb_traverse_start *in, uint8_t *buf,
+                             size_t *npush)
 {
-       memcpy(buf, traverse, sizeof(struct ctdb_traverse_start));
+       size_t offset = 0, np;
+
+       ctdb_uint32_push(&in->db_id, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->reqid, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint64_push(&in->srvid, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
 int ctdb_traverse_start_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                            struct ctdb_traverse_start **out)
+                            struct ctdb_traverse_start **out, size_t *npull)
 {
-       struct ctdb_traverse_start *traverse;
+       struct ctdb_traverse_start *val;
+       size_t offset = 0, np;
+       int ret;
 
-       if (buflen < sizeof(struct ctdb_traverse_start)) {
-               return EMSGSIZE;
+       val = talloc(mem_ctx, struct ctdb_traverse_start);
+       if (val == NULL) {
+               return ENOMEM;
+       }
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->db_id, &np);
+       if (ret != 0) {
+               goto fail;
        }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->reqid, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_uint64_pull(buf+offset, buflen-offset, &val->srvid, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       *out = val;
+       *npull = offset;
+       return 0;
+
+fail:
+       talloc_free(val);
+       return ret;
+}
+
+size_t ctdb_traverse_all_len(struct ctdb_traverse_all *in)
+{
+       return ctdb_uint32_len(&in->db_id) +
+               ctdb_uint32_len(&in->reqid) +
+               ctdb_uint32_len(&in->pnn) +
+               ctdb_uint32_len(&in->client_reqid) +
+               ctdb_uint64_len(&in->srvid);
+}
+
+void ctdb_traverse_all_push(struct ctdb_traverse_all *in, uint8_t *buf,
+                           size_t *npush)
+{
+       size_t offset = 0, np;
+
+       ctdb_uint32_push(&in->db_id, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->reqid, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->pnn, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->client_reqid, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint64_push(&in->srvid, buf+offset, &np);
+       offset += np;
 
-       traverse = talloc_memdup(mem_ctx, buf,
-                                sizeof(struct ctdb_traverse_start));
-       if (traverse == NULL) {
+       *npush = offset;
+}
+
+int ctdb_traverse_all_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
+                          struct ctdb_traverse_all **out, size_t *npull)
+{
+       struct ctdb_traverse_all *val;
+       size_t offset = 0, np;
+       int ret;
+
+       val = talloc(mem_ctx, struct ctdb_traverse_all);
+       if (val == NULL) {
                return ENOMEM;
        }
 
-       *out = traverse;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->db_id, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->reqid, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->pnn, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->client_reqid,
+                              &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_uint64_pull(buf+offset, buflen-offset, &val->srvid, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       *out = val;
+       *npull = offset;
        return 0;
-}
 
-size_t ctdb_traverse_all_len(struct ctdb_traverse_all *traverse)
-{
-       return sizeof(struct ctdb_traverse_all);
+fail:
+       talloc_free(val);
+       return ret;
 }
 
-void ctdb_traverse_all_push(struct ctdb_traverse_all *traverse, uint8_t *buf)
+size_t ctdb_traverse_start_ext_len(struct ctdb_traverse_start_ext *in)
 {
-       memcpy(buf, traverse, sizeof(struct ctdb_traverse_all));
+       return ctdb_uint32_len(&in->db_id) +
+               ctdb_uint32_len(&in->reqid) +
+               ctdb_uint64_len(&in->srvid) +
+               ctdb_bool_len(&in->withemptyrecords) +
+               ctdb_padding_len(7);
 }
 
-int ctdb_traverse_all_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                          struct ctdb_traverse_all **out)
+void ctdb_traverse_start_ext_push(struct ctdb_traverse_start_ext *in,
+                                 uint8_t *buf, size_t *npush)
 {
-       struct ctdb_traverse_all *traverse;
+       size_t offset = 0, np;
 
-       if (buflen < sizeof(struct ctdb_traverse_all)) {
-               return EMSGSIZE;
-       }
+       ctdb_uint32_push(&in->db_id, buf+offset, &np);
+       offset += np;
 
-       traverse = talloc_memdup(mem_ctx, buf,
-                                sizeof(struct ctdb_traverse_all));
-       if (traverse == NULL) {
-               return ENOMEM;
-       }
+       ctdb_uint32_push(&in->reqid, buf+offset, &np);
+       offset += np;
 
-       *out = traverse;
-       return 0;
-}
+       ctdb_uint64_push(&in->srvid, buf+offset, &np);
+       offset += np;
 
-size_t ctdb_traverse_start_ext_len(struct ctdb_traverse_start_ext *traverse)
-{
-       return sizeof(struct ctdb_traverse_start_ext);
-}
+       ctdb_bool_push(&in->withemptyrecords, buf+offset, &np);
+       offset += np;
 
-void ctdb_traverse_start_ext_push(struct ctdb_traverse_start_ext *traverse,
-                                 uint8_t *buf)
-{
-       memcpy(buf, traverse, sizeof(struct ctdb_traverse_start_ext));
+       ctdb_padding_push(7, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
 int ctdb_traverse_start_ext_pull(uint8_t *buf, size_t buflen,
                                 TALLOC_CTX *mem_ctx,
-                                struct ctdb_traverse_start_ext **out)
+                                struct ctdb_traverse_start_ext **out,
+                                size_t *npull)
 {
-       struct ctdb_traverse_start_ext *traverse;
+       struct ctdb_traverse_start_ext *val;
+       size_t offset = 0, np;
+       int ret;
 
-       if (buflen < sizeof(struct ctdb_traverse_start_ext)) {
-               return EMSGSIZE;
+       val = talloc(mem_ctx, struct ctdb_traverse_start_ext);
+       if (val == NULL) {
+               return ENOMEM;
        }
 
-       traverse = talloc_memdup(mem_ctx, buf,
-                                sizeof(struct ctdb_traverse_start_ext));
-       if (traverse == NULL) {
-               return ENOMEM;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->db_id, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->reqid, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_uint64_pull(buf+offset, buflen-offset, &val->srvid, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_bool_pull(buf+offset, buflen-offset,
+                            &val->withemptyrecords, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_padding_pull(buf+offset, buflen-offset, 7, &np);
+       if (ret != 0) {
+               goto fail;
        }
+       offset += np;
 
-       *out = traverse;
+       *out = val;
+       *npull = offset;
        return 0;
+
+fail:
+       talloc_free(val);
+       return ret;
 }
 
-size_t ctdb_traverse_all_ext_len(struct ctdb_traverse_all_ext *traverse)
+size_t ctdb_traverse_all_ext_len(struct ctdb_traverse_all_ext *in)
 {
-       return sizeof(struct ctdb_traverse_all_ext);
+       return ctdb_uint32_len(&in->db_id) +
+               ctdb_uint32_len(&in->reqid) +
+               ctdb_uint32_len(&in->pnn) +
+               ctdb_uint32_len(&in->client_reqid) +
+               ctdb_uint64_len(&in->srvid) +
+               ctdb_bool_len(&in->withemptyrecords) +
+               ctdb_padding_len(7);
 }
 
-void ctdb_traverse_all_ext_push(struct ctdb_traverse_all_ext *traverse,
-                               uint8_t *buf)
+void ctdb_traverse_all_ext_push(struct ctdb_traverse_all_ext *in,
+                               uint8_t *buf, size_t *npush)
 {
-       memcpy(buf, traverse, sizeof(struct ctdb_traverse_all_ext));
+       size_t offset = 0, np;
+
+       ctdb_uint32_push(&in->db_id, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->reqid, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->pnn, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->client_reqid, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint64_push(&in->srvid, buf+offset, &np);
+       offset += np;
+
+       ctdb_bool_push(&in->withemptyrecords, buf+offset, &np);
+       offset += np;
+
+       ctdb_padding_push(7, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
 int ctdb_traverse_all_ext_pull(uint8_t *buf, size_t buflen,
                               TALLOC_CTX *mem_ctx,
-                              struct ctdb_traverse_all_ext **out)
+                              struct ctdb_traverse_all_ext **out,
+                              size_t *npull)
 {
-       struct ctdb_traverse_all_ext *traverse;
+       struct ctdb_traverse_all_ext *val;
+       size_t offset = 0, np;
+       int ret;
 
-       if (buflen < sizeof(struct ctdb_traverse_all_ext)) {
-               return EMSGSIZE;
+       val = talloc(mem_ctx, struct ctdb_traverse_all_ext);
+       if (val == NULL) {
+               return ENOMEM;
        }
 
-       traverse = talloc_memdup(mem_ctx, buf,
-                                sizeof(struct ctdb_traverse_all_ext));
-       if (traverse == NULL) {
-               return ENOMEM;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->db_id, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->reqid, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->pnn, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->client_reqid,
+                              &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_uint64_pull(buf+offset, buflen-offset, &val->srvid, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_bool_pull(buf+offset, buflen-offset,
+                            &val->withemptyrecords, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_padding_pull(buf+offset, buflen-offset, 7, &np);
+       if (ret != 0) {
+               goto fail;
        }
+       offset += np;
 
-       *out = traverse;
+       *out = val;
+       *npull = offset;
        return 0;
+
+fail:
+       talloc_free(val);
+       return ret;
 }
 
-size_t ctdb_sock_addr_len(ctdb_sock_addr *addr)
+size_t ctdb_sock_addr_len(ctdb_sock_addr *in)
 {
        return sizeof(ctdb_sock_addr);
 }
 
-void ctdb_sock_addr_push(ctdb_sock_addr *addr, uint8_t *buf)
+void ctdb_sock_addr_push(ctdb_sock_addr *in, uint8_t *buf, size_t *npush)
 {
-       memcpy(buf, addr, sizeof(ctdb_sock_addr));
+       memcpy(buf, in, sizeof(ctdb_sock_addr));
+       *npush = sizeof(ctdb_sock_addr);
 }
 
-static int ctdb_sock_addr_pull_elems(uint8_t *buf, size_t buflen,
-                                    TALLOC_CTX *mem_ctx, ctdb_sock_addr *out)
+int ctdb_sock_addr_pull_elems(uint8_t *buf, size_t buflen,
+                             TALLOC_CTX *mem_ctx, ctdb_sock_addr *out,
+                             size_t *npull)
 {
        if (buflen < sizeof(ctdb_sock_addr)) {
                return EMSGSIZE;
        }
 
        memcpy(out, buf, sizeof(ctdb_sock_addr));
+       *npull = sizeof(ctdb_sock_addr);
 
        return 0;
 }
 
 int ctdb_sock_addr_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                       ctdb_sock_addr **out)
+                       ctdb_sock_addr **out, size_t *npull)
 {
-       ctdb_sock_addr *addr;
+       ctdb_sock_addr *val;
+       size_t np;
        int ret;
 
-       addr = talloc(mem_ctx, ctdb_sock_addr);
-       if (addr == NULL) {
-               return false;
+       val = talloc(mem_ctx, ctdb_sock_addr);
+       if (val == NULL) {
+               return ENOMEM;
        }
 
-       ret = ctdb_sock_addr_pull_elems(buf, buflen, addr, addr);
+       ret = ctdb_sock_addr_pull_elems(buf, buflen, val, val, &np);
        if (ret != 0) {
-               TALLOC_FREE(addr);
+               talloc_free(val);
+               return ret;
        }
 
-       *out = addr;
+       *out = val;
+       *npull = np;
        return ret;
 }
 
-size_t ctdb_connection_len(struct ctdb_connection *conn)
+size_t ctdb_connection_len(struct ctdb_connection *in)
 {
-       return sizeof(struct ctdb_connection);
+       return ctdb_sock_addr_len(&in->src) +
+               ctdb_sock_addr_len(&in->dst);
 }
 
-void ctdb_connection_push(struct ctdb_connection *conn, uint8_t *buf)
+void ctdb_connection_push(struct ctdb_connection *in, uint8_t *buf,
+                         size_t *npush)
 {
-       memcpy(buf, conn, sizeof(struct ctdb_connection));
+       size_t offset = 0, np;
+
+       ctdb_sock_addr_push(&in->src, buf+offset, &np);
+       offset += np;
+
+       ctdb_sock_addr_push(&in->dst, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
 static int ctdb_connection_pull_elems(uint8_t *buf, size_t buflen,
                                      TALLOC_CTX *mem_ctx,
-                                     struct ctdb_connection *out)
+                                     struct ctdb_connection *out,
+                                     size_t *npull)
 {
-       if (buflen < sizeof(struct ctdb_connection)) {
-               return EMSGSIZE;
+       size_t offset = 0, np;
+       int ret;
+
+       ret = ctdb_sock_addr_pull_elems(buf+offset, buflen-offset,
+                                       mem_ctx, &out->src, &np);
+       if (ret != 0) {
+               return ret;
        }
+       offset += np;
 
-       memcpy(out, buf, sizeof(struct ctdb_connection));
+       ret = ctdb_sock_addr_pull_elems(buf+offset, buflen-offset,
+                                       mem_ctx, &out->dst, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
+       *npull = offset;
        return 0;
 }
 
 int ctdb_connection_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                        struct ctdb_connection **out)
+                        struct ctdb_connection **out, size_t *npull)
 {
-       struct ctdb_connection *conn;
+       struct ctdb_connection *val;
+       size_t np;
        int ret;
 
-       conn = talloc(mem_ctx, struct ctdb_connection);
-       if (conn == NULL) {
+       val = talloc(mem_ctx, struct ctdb_connection);
+       if (val == NULL) {
                return ENOMEM;
        }
 
-       ret = ctdb_connection_pull_elems(buf, buflen, conn, conn);
+       ret = ctdb_connection_pull_elems(buf, buflen, val, val, &np);
        if (ret != 0) {
-               TALLOC_FREE(conn);
+               talloc_free(val);
+               return ret;
        }
 
-       *out = conn;
+       *out = val;
+       *npull = np;
        return ret;
 }
 
-struct ctdb_tunable_wire {
-       uint32_t value;
-       uint32_t length;
-       uint8_t name[1];
-};
-
-size_t ctdb_tunable_len(struct ctdb_tunable *tunable)
+size_t ctdb_tunable_len(struct ctdb_tunable *in)
 {
-       return offsetof(struct ctdb_tunable_wire, name) +
-              strlen(tunable->name) + 1;
+       return ctdb_uint32_len(&in->value) +
+               ctdb_stringn_len(&in->name);
 }
 
-void ctdb_tunable_push(struct ctdb_tunable *tunable, uint8_t *buf)
+void ctdb_tunable_push(struct ctdb_tunable *in, uint8_t *buf, size_t *npush)
 {
-       struct ctdb_tunable_wire *wire = (struct ctdb_tunable_wire *)buf;
+       size_t offset = 0, np;
 
-       wire->value = tunable->value;
-       wire->length = strlen(tunable->name) + 1;
-       memcpy(wire->name, tunable->name, wire->length);
+       ctdb_uint32_push(&in->value, buf+offset, &np);
+       offset += np;
+
+       ctdb_stringn_push(&in->name, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
 int ctdb_tunable_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                     struct ctdb_tunable **out)
+                     struct ctdb_tunable **out, size_t *npull)
 {
-       struct ctdb_tunable *tunable;
-       struct ctdb_tunable_wire *wire = (struct ctdb_tunable_wire *)buf;
+       struct ctdb_tunable *val;
+       size_t offset = 0, np;
+       int ret;
 
-       if (buflen < offsetof(struct ctdb_tunable_wire, name)) {
-               return EMSGSIZE;
-       }
-       if (buflen < offsetof(struct ctdb_tunable_wire, name) + wire->length) {
-               return EMSGSIZE;
+       val = talloc(mem_ctx, struct ctdb_tunable);
+       if (val == NULL) {
+               return ENOMEM;
        }
 
-       tunable = talloc(mem_ctx, struct ctdb_tunable);
-       if (tunable == NULL) {
-               return ENOMEM;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->value, &np);
+       if (ret != 0) {
+               goto fail;
        }
+       offset += np;
 
-       tunable->value = wire->value;
-       tunable->name = talloc_memdup(tunable, wire->name, wire->length);
-       if (tunable->name == NULL) {
-               talloc_free(tunable);
-               return ENOMEM;
+       ret = ctdb_stringn_pull(buf+offset, buflen-offset, mem_ctx,
+                               &val->name, &np);
+       if (ret != 0) {
+               goto fail;
        }
+       offset += np;
 
-       *out = tunable;
+       *out = val;
+       *npull = offset;
        return 0;
+
+fail:
+       talloc_free(val);
+       return ret;
 }
 
-size_t ctdb_node_flag_change_len(struct ctdb_node_flag_change *flag_change)
+size_t ctdb_node_flag_change_len(struct ctdb_node_flag_change *in)
 {
-       return sizeof(struct ctdb_node_flag_change);
+       return ctdb_uint32_len(&in->pnn) +
+               ctdb_uint32_len(&in->new_flags) +
+               ctdb_uint32_len(&in->old_flags);
 }
 
-void ctdb_node_flag_change_push(struct ctdb_node_flag_change *flag_change,
-                               uint8_t *buf)
+void ctdb_node_flag_change_push(struct ctdb_node_flag_change *in,
+                               uint8_t *buf, size_t *npush)
 {
-       memcpy(buf, flag_change, sizeof(struct ctdb_node_flag_change));
+       size_t offset = 0, np;
+
+       ctdb_uint32_push(&in->pnn, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->new_flags, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->old_flags, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
-int ctdb_node_flag_change_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                              struct ctdb_node_flag_change **out)
+int ctdb_node_flag_change_pull(uint8_t *buf, size_t buflen,
+                              TALLOC_CTX *mem_ctx,
+                              struct ctdb_node_flag_change **out,
+                              size_t *npull)
 {
-       struct ctdb_node_flag_change *flag_change;
+       struct ctdb_node_flag_change *val;
+       size_t offset = 0, np;
+       int ret;
 
-       if (buflen < sizeof(struct ctdb_node_flag_change)) {
-               return EMSGSIZE;
+       val = talloc(mem_ctx, struct ctdb_node_flag_change);
+       if (val == NULL) {
+               return ENOMEM;
        }
 
-       flag_change = talloc_memdup(mem_ctx, buf,
-                                   sizeof(struct ctdb_node_flag_change));
-       if (flag_change == NULL) {
-               return ENOMEM;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->pnn, &np);
+       if (ret != 0) {
+               goto fail;
        }
+       offset += np;
 
-       *out = flag_change;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->new_flags,
+                              &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->old_flags,
+                              &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       *out = val;
+       *npull = offset;
        return 0;
-}
 
-struct ctdb_var_list_wire {
-       uint32_t length;
-       char list_str[1];
-};
+fail:
+       talloc_free(val);
+       return ret;
+}
 
-size_t ctdb_var_list_len(struct ctdb_var_list *var_list)
+size_t ctdb_var_list_len(struct ctdb_var_list *in)
 {
+       uint32_t u32 = 0;
        int i;
-       size_t len = sizeof(uint32_t);
 
-       for (i=0; i<var_list->count; i++) {
-               len += strlen(var_list->var[i]) + 1;
+       for (i=0; i<in->count; i++) {
+               u32 += ctdb_string_len(&in->var[i]);
        }
-       return len;
+
+       return ctdb_uint32_len(&u32) + u32;
 }
 
-void ctdb_var_list_push(struct ctdb_var_list *var_list, uint8_t *buf)
+void ctdb_var_list_push(struct ctdb_var_list *in, uint8_t *buf, size_t *npush)
 {
-       struct ctdb_var_list_wire *wire = (struct ctdb_var_list_wire *)buf;
-       int i, n;
-       size_t offset = 0;
+       size_t offset = 0, np;
+       uint32_t u32;
+       int i;
+       uint8_t sep = ':';
 
-       if (var_list->count > 0) {
-               n = sprintf(wire->list_str, "%s", var_list->var[0]);
-               offset += n;
-       }
-       for (i=1; i<var_list->count; i++) {
-               n = sprintf(&wire->list_str[offset], ":%s", var_list->var[i]);
-               offset += n;
+       /* The length only corresponds to the payload size */
+       u32 = ctdb_var_list_len(in);
+       u32 -= ctdb_uint32_len(&u32);
+
+       ctdb_uint32_push(&u32, buf+offset, &np);
+       offset += np;
+
+       /* The variables are separated by ':' and the complete string is null
+        * terminated.
+        */
+       for (i=0; i<in->count; i++) {
+               ctdb_string_push(&in->var[i], buf+offset, &np);
+               offset += np;
+
+               if (i < in->count - 1) {
+                       /* Replace '\0' with ':' */
+                       ctdb_uint8_push(&sep, buf+offset-1, &np);
+               }
        }
-       wire->length = offset + 1;
+
+       *npush = offset;
 }
 
 int ctdb_var_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                      struct ctdb_var_list **out)
+                      struct ctdb_var_list **out, size_t *npull)
 {
-       struct ctdb_var_list *var_list = NULL;
-       struct ctdb_var_list_wire *wire = (struct ctdb_var_list_wire *)buf;
-       char *str, *s, *tok, *ptr;
-       const char **list;
+       struct ctdb_var_list *val;
+       const char *str, **list;
+       char *s, *tok, *ptr = NULL;
+       size_t offset = 0, np;
+       uint32_t u32;
+       int ret;
 
-       if (buflen < sizeof(uint32_t)) {
-               return EMSGSIZE;
+       val = talloc_zero(mem_ctx, struct ctdb_var_list);
+       if (val == NULL) {
+               return ENOMEM;
        }
-       if (buflen < sizeof(uint32_t) + wire->length) {
-               return EMSGSIZE;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
+       if (ret != 0) {
+               goto fail;
        }
+       offset += np;
 
-       str = talloc_strndup(mem_ctx, (char *)wire->list_str, wire->length);
-       if (str == NULL) {
-               return ENOMEM;
+       if (buflen-offset < u32) {
+               ret = EMSGSIZE;
+               goto fail;
        }
 
-       var_list = talloc_zero(mem_ctx, struct ctdb_var_list);
-       if (var_list == NULL) {
+       ret = ctdb_string_pull(buf+offset, u32, val, &str, &np);
+       if (ret != 0) {
                goto fail;
        }
+       offset += np;
 
-       s = str;
+       s = discard_const(str);
        while ((tok = strtok_r(s, ":", &ptr)) != NULL) {
-               s = NULL;
-               list = talloc_realloc(var_list, var_list->var, const char *,
-                                     var_list->count+1);
+               list = talloc_realloc(val, val->var, const char *,
+                                     val->count+1);
                if (list == NULL) {
+                       ret = ENOMEM;
                        goto fail;
                }
 
-               var_list->var = list;
-               var_list->var[var_list->count] = talloc_strdup(var_list, tok);
-               if (var_list->var[var_list->count] == NULL) {
+               val->var = list;
+
+               s = talloc_strdup(val, tok);
+               if (s == NULL) {
+                       ret = ENOMEM;
                        goto fail;
                }
-               var_list->count++;
+
+               val->var[val->count] = s;
+               val->count += 1;
+               s = NULL;
        }
 
-       talloc_free(str);
-       *out = var_list;
+       talloc_free(discard_const(str));
+       *out = val;
+       *npull = offset;
        return 0;
 
 fail:
-       talloc_free(str);
-       talloc_free(var_list);
-       return ENOMEM;
+       talloc_free(val);
+       return ret;
 }
 
 size_t ctdb_tunable_list_len(struct ctdb_tunable_list *tun_list)
@@ -1278,7 +2420,7 @@ void ctdb_tickle_list_push(struct ctdb_tickle_list *tickles, uint8_t *buf)
 {
        struct ctdb_tickle_list_wire *wire =
                (struct ctdb_tickle_list_wire *)buf;
-       size_t offset;
+       size_t offset, np;
        int i;
 
        memcpy(&wire->addr, &tickles->addr, sizeof(ctdb_sock_addr));
@@ -1286,8 +2428,8 @@ void ctdb_tickle_list_push(struct ctdb_tickle_list *tickles, uint8_t *buf)
 
        offset = offsetof(struct ctdb_tickle_list_wire, conn);
        for (i=0; i<tickles->num; i++) {
-               ctdb_connection_push(&tickles->conn[i], &buf[offset]);
-               offset += ctdb_connection_len(&tickles->conn[i]);
+               ctdb_connection_push(&tickles->conn[i], &buf[offset], &np);
+               offset += np;
        }
 }
 
@@ -1297,12 +2439,20 @@ int ctdb_tickle_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        struct ctdb_tickle_list *tickles;
        struct ctdb_tickle_list_wire *wire =
                (struct ctdb_tickle_list_wire *)buf;
-       size_t offset;
+       size_t offset, np;
        int i, ret;
 
        if (buflen < offsetof(struct ctdb_tickle_list_wire, conn)) {
                return EMSGSIZE;
        }
+       if (wire->num > buflen / sizeof(struct ctdb_connection)) {
+               return EMSGSIZE;
+       }
+       if (offsetof(struct ctdb_tickle_list_wire, conn) +
+           wire->num * sizeof(struct ctdb_connection) <
+           offsetof(struct ctdb_tickle_list_wire, conn)) {
+               return EMSGSIZE;
+       }
        if (buflen < offsetof(struct ctdb_tickle_list_wire, conn) +
                     wire->num * sizeof(struct ctdb_connection)) {
                return EMSGSIZE;
@@ -1326,12 +2476,12 @@ int ctdb_tickle_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        for (i=0; i<wire->num; i++) {
                ret = ctdb_connection_pull_elems(&buf[offset], buflen-offset,
                                                 tickles->conn,
-                                                &tickles->conn[i]);
+                                                &tickles->conn[i], &np);
                if (ret != 0) {
                        talloc_free(tickles);
                        return ret;
                }
-               offset += ctdb_connection_len(&tickles->conn[i]);
+               offset += np;
        }
 
        *out = tickles;
@@ -1380,6 +2530,13 @@ int ctdb_addr_info_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        if (buflen < offsetof(struct ctdb_addr_info_wire, iface)) {
                return EMSGSIZE;
        }
+       if (wire->len > buflen) {
+               return EMSGSIZE;
+       }
+       if (offsetof(struct ctdb_addr_info_wire, iface) + wire->len <
+           offsetof(struct ctdb_addr_info_wire, iface)) {
+               return EMSGSIZE;
+       }
        if (buflen < offsetof(struct ctdb_addr_info_wire, iface) + wire->len) {
                return EMSGSIZE;
        }
@@ -1553,6 +2710,13 @@ int ctdb_public_ip_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        if (buflen < sizeof(uint32_t)) {
                return EMSGSIZE;
        }
+       if (wire->num > buflen / sizeof(struct ctdb_public_ip)) {
+               return EMSGSIZE;
+       }
+       if (sizeof(uint32_t) + wire->num * sizeof(struct ctdb_public_ip) <
+           sizeof(uint32_t)) {
+               return EMSGSIZE;
+       }
        if (buflen < sizeof(uint32_t) +
                     wire->num * sizeof(struct ctdb_public_ip)) {
                return EMSGSIZE;
@@ -1670,6 +2834,21 @@ int ctdb_node_map_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        int i;
        bool ret;
 
+       if (buflen < sizeof(uint32_t)) {
+               return EMSGSIZE;
+       }
+       if (wire->num > buflen / sizeof(struct ctdb_node_and_flags)) {
+               return EMSGSIZE;
+       }
+       if (sizeof(uint32_t) + wire->num * sizeof(struct ctdb_node_and_flags) <
+           sizeof(uint32_t)) {
+               return EMSGSIZE;
+       }
+       if (buflen < sizeof(uint32_t) +
+                    wire->num * sizeof(struct ctdb_node_and_flags)) {
+               return EMSGSIZE;
+       }
+
        nodemap = talloc(mem_ctx, struct ctdb_node_map);
        if (nodemap == NULL) {
                return ENOMEM;
@@ -1805,6 +2984,12 @@ int ctdb_script_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        if (buflen < offset) {
                return EMSGSIZE;
        }
+       if (wire->num_scripts > buflen / sizeof(struct ctdb_script)) {
+               return EMSGSIZE;
+       }
+       if (offset + wire->num_scripts * sizeof(struct ctdb_script) < offset) {
+               return EMSGSIZE;
+       }
        if (buflen < offset + wire->num_scripts * sizeof(struct ctdb_script)) {
                return EMSGSIZE;
        }
@@ -1866,34 +3051,6 @@ int ctdb_ban_state_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        return 0;
 }
 
-size_t ctdb_db_priority_len(struct ctdb_db_priority *db_prio)
-{
-       return sizeof(struct ctdb_db_priority);
-}
-
-void ctdb_db_priority_push(struct ctdb_db_priority *db_prio, uint8_t *buf)
-{
-       memcpy(buf, db_prio, sizeof(struct ctdb_db_priority));
-}
-
-int ctdb_db_priority_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                         struct ctdb_db_priority **out)
-{
-       struct ctdb_db_priority *db_prio;
-
-       if (buflen < sizeof(struct ctdb_db_priority)) {
-               return EMSGSIZE;
-       }
-
-       db_prio = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_db_priority));
-       if (db_prio == NULL) {
-               return ENOMEM;
-       }
-
-       *out = db_prio;
-       return 0;
-}
-
 struct ctdb_notify_data_wire {
        uint64_t srvid;
        uint32_t len;
@@ -1926,6 +3083,13 @@ int ctdb_notify_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        if (buflen < offsetof(struct ctdb_notify_data_wire, data)) {
                return EMSGSIZE;
        }
+       if (wire->len > buflen) {
+               return EMSGSIZE;
+       }
+       if (offsetof(struct ctdb_notify_data_wire, data) + wire->len <
+           offsetof(struct ctdb_notify_data_wire, data)) {
+               return EMSGSIZE;
+       }
        if (buflen < offsetof(struct ctdb_notify_data_wire, data) + wire->len) {
                return EMSGSIZE;
        }
@@ -2021,6 +3185,13 @@ int ctdb_iface_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        if (buflen < sizeof(uint32_t)) {
                return EMSGSIZE;
        }
+       if (wire->num > buflen / sizeof(struct ctdb_iface)) {
+               return EMSGSIZE;
+       }
+       if (sizeof(uint32_t) + wire->num * sizeof(struct ctdb_iface) <
+           sizeof(uint32_t)) {
+               return EMSGSIZE;
+       }
        if (buflen < sizeof(uint32_t) + wire->num * sizeof(struct ctdb_iface)) {
                return EMSGSIZE;
        }
@@ -2081,6 +3252,18 @@ int ctdb_public_ip_info_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        if (buflen < offsetof(struct ctdb_public_ip_info_wire, ifaces)) {
                return EMSGSIZE;
        }
+       if (wire->num > buflen / sizeof(struct ctdb_iface)) {
+               return EMSGSIZE;
+       }
+       if (offsetof(struct ctdb_public_ip_info_wire, ifaces) +
+           wire->num * sizeof(struct ctdb_iface) <
+           offsetof(struct ctdb_public_ip_info_wire, ifaces)) {
+               return EMSGSIZE;
+       }
+       if (buflen < offsetof(struct ctdb_public_ip_info_wire, ifaces) +
+                    wire->num * sizeof(struct ctdb_iface)) {
+               return EMSGSIZE;
+       }
 
        ipinfo = talloc(mem_ctx, struct ctdb_public_ip_info);
        if (ipinfo == NULL) {
@@ -2140,6 +3323,13 @@ int ctdb_key_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        if (buflen < offsetof(struct ctdb_key_data_wire, key)) {
                return EMSGSIZE;
        }
+       if (wire->keylen > buflen) {
+               return EMSGSIZE;
+       }
+       if (offsetof(struct ctdb_key_data_wire, key) + wire->keylen <
+           offsetof(struct ctdb_key_data_wire, key)) {
+               return EMSGSIZE;
+       }
        if (buflen < offsetof(struct ctdb_key_data_wire, key) + wire->keylen) {
                return EMSGSIZE;
        }
@@ -2210,9 +3400,23 @@ int ctdb_db_statistics_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        if (buflen < sizeof(struct ctdb_db_statistics)) {
                return EMSGSIZE;
        }
+
        offset = 0;
        for (i=0; i<wire->dbstats.num_hot_keys; i++) {
+               if (wire->dbstats.hot_keys[i].key.dsize > buflen) {
+                       return EMSGSIZE;
+               }
+               if (offset + wire->dbstats.hot_keys[i].key.dsize < offset) {
+                       return EMSGSIZE;
+               }
                offset += wire->dbstats.hot_keys[i].key.dsize;
+               if (offset > buflen) {
+                       return EMSGSIZE;
+               }
+       }
+       if (sizeof(struct ctdb_db_statistics) + offset <
+           sizeof(struct ctdb_db_statistics)) {
+               return EMSGSIZE;
        }
        if (buflen < sizeof(struct ctdb_db_statistics) + offset) {
                return EMSGSIZE;
@@ -2333,35 +3537,6 @@ int ctdb_disable_message_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        return 0;
 }
 
-size_t ctdb_tdb_data_len(TDB_DATA data)
-{
-       return data.dsize;
-}
-
-void ctdb_tdb_data_push(TDB_DATA data, uint8_t *buf)
-{
-       memcpy(buf, data.dptr, data.dsize);
-}
-
-int ctdb_tdb_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                      TDB_DATA *out)
-{
-       TDB_DATA data;
-
-       data.dsize = buflen;
-       if (data.dsize > 0) {
-               data.dptr = talloc_memdup(mem_ctx, buf, buflen);
-               if (data.dptr == NULL) {
-                       return ENOMEM;
-               }
-       } else {
-               data.dptr = NULL;
-       }
-
-       *out = data;
-       return 0;
-}
-
 size_t ctdb_server_id_len(struct ctdb_server_id *sid)
 {
        return sizeof(struct ctdb_server_id);