ctdb-protocol: Fix marshalling for ctdb_req_control
authorAmitay Isaacs <amitay@gmail.com>
Thu, 3 Aug 2017 08:00:24 +0000 (18:00 +1000)
committerMartin Schwenke <martins@samba.org>
Wed, 30 Aug 2017 12:59:25 +0000 (14:59 +0200)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/protocol/protocol_control.c
ctdb/tests/cunit/protocol_test_101.sh
ctdb/tests/src/protocol_ctdb_compat_test.c
ctdb/tests/src/protocol_ctdb_test.c

index 318d8a428ee1b5c2b6fc57e60ef3bc070de95b09..4c2a086364e0428d0e6cc5aee9524cd2025eb4bd 100644 (file)
 #include "protocol_api.h"
 #include "protocol_private.h"
 
-struct ctdb_req_control_wire {
-       struct ctdb_req_header hdr;
-       uint32_t opcode;
-       uint32_t pad;
-       uint64_t srvid;
-       uint32_t client_id;
-       uint32_t flags;
-       uint32_t datalen;
-       uint8_t data[1];
-};
-
 struct ctdb_reply_control_wire {
        struct ctdb_req_header hdr;
        int32_t status;
@@ -1785,35 +1774,61 @@ static int ctdb_reply_control_data_pull(uint8_t *buf, size_t buflen,
 size_t ctdb_req_control_len(struct ctdb_req_header *h,
                            struct ctdb_req_control *c)
 {
-       return offsetof(struct ctdb_req_control_wire, data) +
+       uint32_t u32 = 0;
+
+       return ctdb_req_header_len(h) +
+               ctdb_uint32_len(&c->opcode) +
+               ctdb_uint32_len(&c->pad) +
+               ctdb_uint64_len(&c->srvid) +
+               ctdb_uint32_len(&c->client_id) +
+               ctdb_uint32_len(&c->flags) +
+               ctdb_uint32_len(&u32) +
                ctdb_req_control_data_len(&c->rdata);
 }
 
 int ctdb_req_control_push(struct ctdb_req_header *h,
-                         struct ctdb_req_control *request,
+                         struct ctdb_req_control *c,
                          uint8_t *buf, size_t *buflen)
 {
-       struct ctdb_req_control_wire *wire =
-               (struct ctdb_req_control_wire *)buf;
-       size_t length, np;
+       size_t offset = 0, np;
+       size_t length;
+       uint32_t u32;
 
-       length = ctdb_req_control_len(h, request);
+       length = ctdb_req_control_len(h, c);
        if (*buflen < length) {
                *buflen = length;
                return EMSGSIZE;
        }
 
        h->length = *buflen;
-       ctdb_req_header_push(h, (uint8_t *)&wire->hdr, &np);
+       ctdb_req_header_push(h, buf+offset, &np);
+       offset += np;
 
-       wire->opcode = request->opcode;
-       wire->pad = request->pad;
-       wire->srvid = request->srvid;
-       wire->client_id = request->client_id;
-       wire->flags = request->flags;
+       ctdb_uint32_push(&c->opcode, buf+offset, &np);
+       offset += np;
 
-       wire->datalen = ctdb_req_control_data_len(&request->rdata);
-       ctdb_req_control_data_push(&request->rdata, wire->data, &np);
+       ctdb_uint32_push(&c->pad, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint64_push(&c->srvid, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&c->client_id, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&c->flags, buf+offset, &np);
+       offset += np;
+
+       u32 = ctdb_req_control_data_len(&c->rdata);
+       ctdb_uint32_push(&u32, buf+offset, &np);
+       offset += np;
+
+       ctdb_req_control_data_push(&c->rdata, buf+offset, &np);
+       offset += np;
+
+       if (offset > *buflen) {
+               return EMSGSIZE;
+       }
 
        return 0;
 }
@@ -1823,44 +1838,71 @@ int ctdb_req_control_pull(uint8_t *buf, size_t buflen,
                          TALLOC_CTX *mem_ctx,
                          struct ctdb_req_control *c)
 {
-       struct ctdb_req_control_wire *wire =
-               (struct ctdb_req_control_wire *)buf;
-       size_t length, np;
+       struct ctdb_req_header header;
+       size_t offset = 0, np;
+       uint32_t u32;
        int ret;
 
-       length = offsetof(struct ctdb_req_control_wire, data);
-       if (buflen < length) {
-               return EMSGSIZE;
+       ret = ctdb_req_header_pull(buf+offset, buflen-offset, &header, &np);
+       if (ret != 0) {
+               return ret;
        }
-       if (wire->datalen > buflen) {
-               return EMSGSIZE;
+       offset += np;
+
+       if (h != NULL) {
+               *h = header;
        }
-       if (length + wire->datalen < length) {
-               return EMSGSIZE;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &c->opcode, &np);
+       if (ret != 0) {
+               return ret;
        }
-       if (buflen < length + wire->datalen) {
-               return EMSGSIZE;
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &c->pad, &np);
+       if (ret != 0) {
+               return ret;
        }
+       offset += np;
 
-       if (h != NULL) {
-               ret = ctdb_req_header_pull((uint8_t *)&wire->hdr, buflen, h,
-                                          &np);
-               if (ret != 0) {
-                       return ret;
-               }
+       ret = ctdb_uint64_pull(buf+offset, buflen-offset, &c->srvid, &np);
+       if (ret != 0) {
+               return ret;
        }
+       offset += np;
 
-       c->opcode = wire->opcode;
-       c->pad = wire->pad;
-       c->srvid = wire->srvid;
-       c->client_id = wire->client_id;
-       c->flags = wire->flags;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &c->client_id, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
-       ret = ctdb_req_control_data_pull(wire->data, wire->datalen,
-                                        c->opcode, mem_ctx, &c->rdata, &np);
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &c->flags, &np);
        if (ret != 0) {
                return ret;
        }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       if (u32 > buflen-offset) {
+               return EMSGSIZE;
+       }
+
+       ret = ctdb_req_control_data_pull(buf+offset, u32, c->opcode, mem_ctx,
+                                        &c->rdata, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       if (offset > buflen) {
+               return EMSGSIZE;
+       }
 
        return 0;
 }
index 2f1305292967f6db162677b205d3c4cda2686ebf..9a177d2b179121fcdb8e7bd0845756cfca576c12 100755 (executable)
@@ -27,8 +27,7 @@ output=$(
     echo "ctdb_reply_dmaster"
     generate_control_output "ctdb_req_control_data"
     generate_control_output "ctdb_reply_control_data"
-    echo "ctdb_req_control"
-    echo "$control_output"
+    generate_control_output "ctdb_req_control"
     echo "ctdb_reply_control"
     echo "$control_output"
     echo "ctdb_req_message"
index 60a3d94621af3514f7477dbc0dc85faf22e1e0e2..c1593f70401eeb87c2133d97a6b4a7a4264708c8 100644 (file)
@@ -26,6 +26,7 @@
 #include "protocol/protocol_types.c"
 #include "protocol/protocol_header.c"
 #include "protocol/protocol_call.c"
+#include "protocol/protocol_control.c"
 
 #include "tests/src/protocol_common.h"
 #include "tests/src/protocol_common_ctdb.h"
@@ -691,6 +692,100 @@ static int ctdb_reply_dmaster_pull_old(uint8_t *buf, size_t buflen,
        return 0;
 }
 
+struct ctdb_req_control_wire {
+       struct ctdb_req_header hdr;
+       uint32_t opcode;
+       uint32_t pad;
+       uint64_t srvid;
+       uint32_t client_id;
+       uint32_t flags;
+       uint32_t datalen;
+       uint8_t data[1];
+};
+
+static size_t ctdb_req_control_len_old(struct ctdb_req_header *h,
+                                      struct ctdb_req_control *c)
+{
+       return offsetof(struct ctdb_req_control_wire, data) +
+               ctdb_req_control_data_len(&c->rdata);
+}
+
+static int ctdb_req_control_push_old(struct ctdb_req_header *h,
+                                    struct ctdb_req_control *c,
+                                    uint8_t *buf, size_t *buflen)
+{
+       struct ctdb_req_control_wire *wire =
+               (struct ctdb_req_control_wire *)buf;
+       size_t length, np;
+
+       length = ctdb_req_control_len_old(h, c);
+       if (*buflen < length) {
+               *buflen = length;
+               return EMSGSIZE;
+       }
+
+       h->length = *buflen;
+       ctdb_req_header_push_old(h, (uint8_t *)&wire->hdr);
+
+       wire->opcode = c->opcode;
+       wire->pad = c->pad;
+       wire->srvid = c->srvid;
+       wire->client_id = c->client_id;
+       wire->flags = c->flags;
+
+       wire->datalen = ctdb_req_control_data_len(&c->rdata);
+       ctdb_req_control_data_push(&c->rdata, wire->data, &np);
+
+       return 0;
+}
+
+static int ctdb_req_control_pull_old(uint8_t *buf, size_t buflen,
+                                    struct ctdb_req_header *h,
+                                    TALLOC_CTX *mem_ctx,
+                                    struct ctdb_req_control *c)
+{
+       struct ctdb_req_control_wire *wire =
+               (struct ctdb_req_control_wire *)buf;
+       size_t length, np;
+       int ret;
+
+       length = offsetof(struct ctdb_req_control_wire, data);
+       if (buflen < length) {
+               return EMSGSIZE;
+       }
+       if (wire->datalen > buflen) {
+               return EMSGSIZE;
+       }
+       if (length + wire->datalen < length) {
+               return EMSGSIZE;
+       }
+       if (buflen < length + wire->datalen) {
+               return EMSGSIZE;
+       }
+
+       if (h != NULL) {
+               ret = ctdb_req_header_pull_old((uint8_t *)&wire->hdr, buflen,
+                                              h);
+               if (ret != 0) {
+                       return ret;
+               }
+       }
+
+       c->opcode = wire->opcode;
+       c->pad = wire->pad;
+       c->srvid = wire->srvid;
+       c->client_id = wire->client_id;
+       c->flags = wire->flags;
+
+       ret = ctdb_req_control_data_pull(wire->data, wire->datalen,
+                                        c->opcode, mem_ctx, &c->rdata, &np);
+       if (ret != 0) {
+               return ret;
+       }
+
+       return 0;
+}
+
 
 COMPAT_CTDB1_TEST(struct ctdb_req_header, ctdb_req_header);
 
@@ -700,8 +795,14 @@ COMPAT_CTDB4_TEST(struct ctdb_reply_error, ctdb_reply_error, CTDB_REPLY_ERROR);
 COMPAT_CTDB4_TEST(struct ctdb_req_dmaster, ctdb_req_dmaster, CTDB_REQ_DMASTER);
 COMPAT_CTDB4_TEST(struct ctdb_reply_dmaster, ctdb_reply_dmaster, CTDB_REPLY_DMASTER);
 
+COMPAT_CTDB5_TEST(struct ctdb_req_control, ctdb_req_control, CTDB_REQ_CONTROL);
+
+#define NUM_CONTROLS   151
+
 int main(int argc, char *argv[])
 {
+       uint32_t opcode;
+
        if (argc == 2) {
                int seed = atoi(argv[1]);
                srandom(seed);
@@ -715,5 +816,9 @@ int main(int argc, char *argv[])
        COMPAT_TEST_FUNC(ctdb_req_dmaster)();
        COMPAT_TEST_FUNC(ctdb_reply_dmaster)();
 
+       for (opcode=0; opcode<NUM_CONTROLS; opcode++) {
+               COMPAT_TEST_FUNC(ctdb_req_control)(opcode);
+       }
+
        return 0;
 }
index 58acaf32242c5bbacac192eb4fa621d6fc026e9d..9636bb5cc3faa113f66d85052a124e059e2eff94 100644 (file)
@@ -294,52 +294,8 @@ PROTOCOL_CTDB4_TEST(struct ctdb_reply_dmaster, ctdb_reply_dmaster,
 PROTOCOL_CTDB2_TEST(struct ctdb_req_control_data, ctdb_req_control_data);
 PROTOCOL_CTDB2_TEST(struct ctdb_reply_control_data, ctdb_reply_control_data);
 
-static void test_ctdb_req_control(void)
-{
-       TALLOC_CTX *mem_ctx;
-       uint8_t *pkt;
-       size_t datalen, pkt_len, len;
-       int ret;
-       struct ctdb_req_header h, h2;
-       struct ctdb_req_control c, c2;
-       uint32_t opcode;
-
-       printf("ctdb_req_control\n");
-       fflush(stdout);
-
-       ctdb_req_header_fill(&h, GENERATION, CTDB_REQ_CONTROL,
-                            DESTNODE, SRCNODE, REQID);
-
-       for (opcode=0; opcode<NUM_CONTROLS; opcode++) {
-               mem_ctx = talloc_new(NULL);
-               assert(mem_ctx != NULL);
-
-               printf("%u.. ", opcode);
-               fflush(stdout);
-               fill_ctdb_req_control(mem_ctx, &c, opcode);
-               datalen = ctdb_req_control_len(&h, &c);
-               ret = ctdb_allocate_pkt(mem_ctx, datalen, &pkt, &pkt_len);
-               assert(ret == 0);
-               assert(pkt != NULL);
-               assert(pkt_len >= datalen);
-               len = 0;
-               ret = ctdb_req_control_push(&h, &c, pkt, &len);
-               assert(ret == EMSGSIZE);
-               assert(len == datalen);
-               ret = ctdb_req_control_push(&h, &c, pkt, &pkt_len);
-               assert(ret == 0);
-               ret = ctdb_req_control_pull(pkt, pkt_len, &h2, mem_ctx, &c2);
-               assert(ret == 0);
-               verify_ctdb_req_header(&h, &h2);
-               assert(h2.length == pkt_len);
-               verify_ctdb_req_control(&c, &c2);
-
-               talloc_free(mem_ctx);
-       }
-
-       printf("\n");
-       fflush(stdout);
-}
+PROTOCOL_CTDB5_TEST(struct ctdb_req_control, ctdb_req_control,
+                       CTDB_REQ_CONTROL);
 
 static void test_ctdb_reply_control(void)
 {
@@ -451,7 +407,9 @@ int main(int argc, char *argv[])
                TEST_FUNC(ctdb_reply_control_data)(opcode);
        }
 
-       test_ctdb_req_control();
+       for (opcode=0; opcode<NUM_CONTROLS; opcode++) {
+               TEST_FUNC(ctdb_req_control)(opcode);
+       }
        test_ctdb_reply_control();
 
        test_ctdb_req_message_data();