ctdb-protocol: Fix marshalling for ctdb_dbid_map
authorAmitay Isaacs <amitay@gmail.com>
Thu, 29 Jun 2017 09:33:04 +0000 (19:33 +1000)
committerMartin Schwenke <martins@samba.org>
Wed, 30 Aug 2017 12:59:22 +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/protocol/protocol_private.h
ctdb/protocol/protocol_types.c
ctdb/tests/src/protocol_common.c
ctdb/tests/src/protocol_types_compat_test.c
ctdb/tests/src/protocol_types_test.c

index 475f578dcdfb8e02e13f2c85fb4972a8a71bac04..24b96b20bb9b120cabf9f1b7b95a5a1eedc63cf0 100644 (file)
@@ -1435,7 +1435,7 @@ static void ctdb_reply_control_data_push(struct ctdb_reply_control_data *cd,
                break;
 
        case CTDB_CONTROL_GET_DBMAP:
-               ctdb_dbid_map_push(cd->data.dbmap, buf);
+               ctdb_dbid_map_push(cd->data.dbmap, buf, &np);
                break;
 
        case CTDB_CONTROL_PULL_DB:
@@ -1602,7 +1602,7 @@ static int ctdb_reply_control_data_pull(uint8_t *buf, size_t buflen,
 
        case CTDB_CONTROL_GET_DBMAP:
                ret = ctdb_dbid_map_pull(buf, buflen, mem_ctx,
-                                        &cd->data.dbmap);
+                                        &cd->data.dbmap, &np);
                break;
 
        case CTDB_CONTROL_PULL_DB:
index b529f8b33794e9fc9372f09b24ebf3c9f79c2a57..2162151a68ae4eb6339bc8da550731c2d57f78a2 100644 (file)
@@ -127,10 +127,11 @@ void ctdb_dbid_push(struct ctdb_dbid *in, uint8_t *buf, size_t *npush);
 int ctdb_dbid_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
                   struct ctdb_dbid **out, size_t *npull);
 
-size_t ctdb_dbid_map_len(struct ctdb_dbid_map *dbmap);
-void ctdb_dbid_map_push(struct ctdb_dbid_map *dbmap, uint8_t *buf);
+size_t ctdb_dbid_map_len(struct ctdb_dbid_map *in);
+void ctdb_dbid_map_push(struct ctdb_dbid_map *in, uint8_t *buf,
+                       size_t *npush);
 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);
 
 size_t ctdb_pulldb_len(struct ctdb_pulldb *pulldb);
 void ctdb_pulldb_push(struct ctdb_pulldb *pulldb, uint8_t *buf);
index b31f6de3fbf857141c94016a88ae7f3fa4e52f32..5058c803d171001947b6c92695415e0879d3ed25 100644 (file)
@@ -1007,60 +1007,81 @@ int ctdb_dbid_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        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;
 
-       wire->num = dbmap->num;
-       memcpy(wire->dbs, dbmap->dbs, dbmap->num * sizeof(struct ctdb_dbid));
+       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;
+       }
+
+       *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;
-       }
-       if (wire->num > buflen / sizeof(struct ctdb_dbid)) {
-               return EMSGSIZE;
-       }
-       if (sizeof(uint32_t) + wire->num * sizeof(struct ctdb_dbid) <
-           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)
index 83649d9b1e243a80f40a0e057707d3bbd1e2e69e..4cc250765c599eb0b7ef5ad1904d045c88eeb11b 100644 (file)
@@ -468,7 +468,7 @@ void fill_ctdb_dbid_map(TALLOC_CTX *mem_ctx, struct ctdb_dbid_map *p)
 
        p->num = rand_int(40);
        if (p->num > 0) {
-               p->dbs = talloc_array(mem_ctx, struct ctdb_dbid, p->num);
+               p->dbs = talloc_zero_array(mem_ctx, struct ctdb_dbid, p->num);
                assert(p->dbs != NULL);
                for (i=0; i<p->num; i++) {
                        fill_ctdb_dbid(mem_ctx, &p->dbs[i]);
index ef95041719d0c12e9d9f2329939a362e254a9a9c..86db077c878081f67e411626212eb451b6bbf1c3 100644 (file)
@@ -185,9 +185,67 @@ static int ctdb_vnn_map_pull_old(uint8_t *buf, size_t buflen,
        return 0;
 }
 
+struct ctdb_dbid_map_wire {
+       uint32_t num;
+       struct ctdb_dbid dbs[1];
+};
+
+static size_t ctdb_dbid_map_len_old(struct ctdb_dbid_map *in)
+{
+       return sizeof(uint32_t) + in->num * sizeof(struct ctdb_dbid);
+}
+
+static void ctdb_dbid_map_push_old(struct ctdb_dbid_map *in, uint8_t *buf)
+{
+       struct ctdb_dbid_map_wire *wire = (struct ctdb_dbid_map_wire *)buf;
+
+       wire->num = in->num;
+       memcpy(wire->dbs, in->dbs, in->num * sizeof(struct ctdb_dbid));
+}
+
+static int ctdb_dbid_map_pull_old(uint8_t *buf, size_t buflen,
+                                 TALLOC_CTX *mem_ctx,
+                                 struct ctdb_dbid_map **out)
+{
+       struct ctdb_dbid_map *val;
+       struct ctdb_dbid_map_wire *wire = (struct ctdb_dbid_map_wire *)buf;
+
+       if (buflen < sizeof(uint32_t)) {
+               return EMSGSIZE;
+       }
+       if (wire->num > buflen / sizeof(struct ctdb_dbid)) {
+               return EMSGSIZE;
+       }
+       if (sizeof(uint32_t) + wire->num * sizeof(struct ctdb_dbid) <
+           sizeof(uint32_t)) {
+               return EMSGSIZE;
+       }
+       if (buflen < sizeof(uint32_t) + wire->num * sizeof(struct ctdb_dbid)) {
+               return EMSGSIZE;
+       }
+
+       val = talloc(mem_ctx, struct ctdb_dbid_map);
+       if (val == NULL) {
+               return ENOMEM;
+       }
+
+       val->num = wire->num;
+
+       val->dbs = talloc_memdup(val, wire->dbs,
+                                wire->num * sizeof(struct ctdb_dbid));
+       if (val->dbs == NULL) {
+               talloc_free(val);
+               return ENOMEM;
+       }
+
+       *out = val;
+       return 0;
+}
+
 
 COMPAT_TYPE3_TEST(struct ctdb_statistics, ctdb_statistics);
 COMPAT_TYPE3_TEST(struct ctdb_vnn_map, ctdb_vnn_map);
+COMPAT_TYPE3_TEST(struct ctdb_dbid_map, ctdb_dbid_map);
 
 int main(int argc, char *argv[])
 {
@@ -198,6 +256,7 @@ int main(int argc, char *argv[])
 
        COMPAT_TEST_FUNC(ctdb_statistics)();
        COMPAT_TEST_FUNC(ctdb_vnn_map)();
+       COMPAT_TEST_FUNC(ctdb_dbid_map)();
 
        return 0;
 }
index 55db9931957b8b0403458706f14bffd35940e0ff..4b8ff30be958b80890ddd3dd963ca005e6a18255 100644 (file)
@@ -66,7 +66,7 @@ static void test_ctdb_g_lock(void)
 PROTOCOL_TYPE3_TEST(struct ctdb_statistics, ctdb_statistics);
 PROTOCOL_TYPE3_TEST(struct ctdb_vnn_map, ctdb_vnn_map);
 PROTOCOL_TYPE3_TEST(struct ctdb_dbid, ctdb_dbid);
-DEFINE_TEST(struct ctdb_dbid_map, ctdb_dbid_map);
+PROTOCOL_TYPE3_TEST(struct ctdb_dbid_map, ctdb_dbid_map);
 DEFINE_TEST(struct ctdb_pulldb, ctdb_pulldb);
 DEFINE_TEST(struct ctdb_pulldb_ext, ctdb_pulldb_ext);
 DEFINE_TEST(struct ctdb_rec_data, ctdb_rec_data);