ctdb-protocol: Fix marshalling for ctdb_iface
authorAmitay Isaacs <amitay@gmail.com>
Wed, 12 Jul 2017 08:38:19 +0000 (18:38 +1000)
committerMartin Schwenke <martins@samba.org>
Wed, 30 Aug 2017 12:59:24 +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 ff770b072597bf5fd9392e3e4660627a8aa209a4..90d8d7036a276ba8393c84781bf4efd559b0ec01 100644 (file)
@@ -633,7 +633,7 @@ static void ctdb_req_control_data_push(struct ctdb_req_control_data *cd,
                break;
 
        case CTDB_CONTROL_SET_IFACE_LINK_STATE:
-               ctdb_iface_push(cd->data.iface, buf);
+               ctdb_iface_push(cd->data.iface, buf, &np);
                break;
 
        case CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE:
@@ -950,7 +950,7 @@ static int ctdb_req_control_data_pull(uint8_t *buf, size_t buflen,
 
        case CTDB_CONTROL_SET_IFACE_LINK_STATE:
                ret = ctdb_iface_pull(buf, buflen, mem_ctx,
-                                     &cd->data.iface);
+                                     &cd->data.iface, &np);
                break;
 
        case CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE:
index 0067ac13abccd35fb2cf94bb5e85c22b83fc598b..1f50270fd77a0478168695272271f522d62ae517 100644 (file)
@@ -277,10 +277,10 @@ void ctdb_notify_data_push(struct ctdb_notify_data *in, uint8_t *buf,
 int ctdb_notify_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
                          struct ctdb_notify_data **out, size_t *npull);
 
-size_t ctdb_iface_len(struct ctdb_iface *iface);
-void ctdb_iface_push(struct ctdb_iface *iface, uint8_t *buf);
+size_t ctdb_iface_len(struct ctdb_iface *in);
+void ctdb_iface_push(struct ctdb_iface *in, uint8_t *buf, size_t *npush);
 int ctdb_iface_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                   struct ctdb_iface **out);
+                   struct ctdb_iface **out, size_t *npull);
 
 size_t ctdb_iface_list_len(struct ctdb_iface_list *iface_list);
 void ctdb_iface_list_push(struct ctdb_iface_list *iface_list, uint8_t *buf);
index 7239615be9d21af4f1a811962392810e04632662..200d424c15e92142a0e1bcf6aee7ab707a59eecb 100644 (file)
@@ -4007,46 +4007,81 @@ fail:
        return ret;
 }
 
-size_t ctdb_iface_len(struct ctdb_iface *iface)
+size_t ctdb_iface_len(struct ctdb_iface *in)
 {
-       return sizeof(struct ctdb_iface);
+       return ctdb_chararray_len(in->name, CTDB_IFACE_SIZE+2) +
+               ctdb_uint16_len(&in->link_state) +
+               ctdb_uint32_len(&in->references);
 }
 
-void ctdb_iface_push(struct ctdb_iface *iface, uint8_t *buf)
+void ctdb_iface_push(struct ctdb_iface *in, uint8_t *buf, size_t *npush)
 {
-       memcpy(buf, iface, sizeof(struct ctdb_iface));
+       size_t offset = 0, np;
+
+       ctdb_chararray_push(in->name, CTDB_IFACE_SIZE+2, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint16_push(&in->link_state, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->references, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
 static int ctdb_iface_pull_elems(uint8_t *buf, size_t buflen,
                                 TALLOC_CTX *mem_ctx,
-                                struct ctdb_iface *out)
+                                struct ctdb_iface *out, size_t *npull)
 {
-       if (buflen < sizeof(struct ctdb_iface)) {
-               return EMSGSIZE;
+       size_t offset = 0, np;
+       int ret;
+
+       ret = ctdb_chararray_pull(buf+offset, buflen-offset,
+                                 out->name, CTDB_IFACE_SIZE+2, &np);
+       if (ret != 0) {
+               return ret;
        }
+       offset += np;
 
-       memcpy(out, buf, sizeof(struct ctdb_iface));
+       ret = ctdb_uint16_pull(buf+offset, buflen-offset, &out->link_state,
+                              &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->references,
+                              &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       *npull = offset;
        return 0;
 }
 
 int ctdb_iface_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                   struct ctdb_iface **out)
+                   struct ctdb_iface **out, size_t *npull)
 {
-       struct ctdb_iface *iface;
+       struct ctdb_iface *val;
+       size_t np;
        int ret;
 
-       iface = talloc(mem_ctx, struct ctdb_iface);
-       if (iface == NULL) {
+       val = talloc(mem_ctx, struct ctdb_iface);
+       if (val == NULL) {
                return ENOMEM;
        }
 
-       ret = ctdb_iface_pull_elems(buf, buflen, iface, iface);
+       ret = ctdb_iface_pull_elems(buf, buflen, val, val, &np);
        if (ret != 0) {
-               TALLOC_FREE(iface);
+               talloc_free(val);
+               return ret;
        }
 
-       *out = iface;
+       *out = val;
+       *npull = np;
        return ret;
 }
 
index d4f2f03d6496271dad4706ac1778ff07c1605eb5..de3b8da7a554998e161c83788e98120ff47d3762 100644 (file)
@@ -1125,12 +1125,16 @@ void verify_ctdb_notify_data(struct ctdb_notify_data *p1,
 
 void fill_ctdb_iface(TALLOC_CTX *mem_ctx, struct ctdb_iface *p)
 {
-       fill_buffer(p, sizeof(struct ctdb_iface));
+       fill_string(p->name, CTDB_IFACE_SIZE+2);
+       p->link_state = rand16();
+       p->references = rand32();
 }
 
 void verify_ctdb_iface(struct ctdb_iface *p1, struct ctdb_iface *p2)
 {
-       verify_buffer(p1, p2, sizeof(struct ctdb_iface));
+       verify_string(p1->name, p2->name);
+       assert(p1->link_state == p2->link_state);
+       assert(p1->references == p2->references);
 }
 
 void fill_ctdb_iface_list(TALLOC_CTX *mem_ctx, struct ctdb_iface_list *p)
index 408ba72937ab19b3cec6dc8d2ba6c7915b622d5b..6ee576aaef140cd685473f08545d6e6613d207f1 100644 (file)
@@ -1668,6 +1668,50 @@ static int ctdb_notify_data_pull_old(uint8_t *buf, size_t buflen,
        return 0;
 }
 
+static size_t ctdb_iface_len_old(struct ctdb_iface *in)
+{
+       return sizeof(struct ctdb_iface);
+}
+
+static void ctdb_iface_push_old(struct ctdb_iface *in, uint8_t *buf)
+{
+       memcpy(buf, in, sizeof(struct ctdb_iface));
+}
+
+static int ctdb_iface_pull_elems_old(uint8_t *buf, size_t buflen,
+                                    TALLOC_CTX *mem_ctx,
+                                    struct ctdb_iface *out)
+{
+       if (buflen < sizeof(struct ctdb_iface)) {
+               return EMSGSIZE;
+       }
+
+       memcpy(out, buf, sizeof(struct ctdb_iface));
+
+       return 0;
+}
+
+static int ctdb_iface_pull_old(uint8_t *buf, size_t buflen,
+                              TALLOC_CTX *mem_ctx, struct ctdb_iface **out)
+{
+       struct ctdb_iface *val;
+       int ret;
+
+       val = talloc(mem_ctx, struct ctdb_iface);
+       if (val == NULL) {
+               return ENOMEM;
+       }
+
+       ret = ctdb_iface_pull_elems_old(buf, buflen, val, val);
+       if (ret != 0) {
+               TALLOC_FREE(val);
+               return ret;
+       }
+
+       *out = val;
+       return ret;
+}
+
 
 COMPAT_TYPE3_TEST(struct ctdb_statistics, ctdb_statistics);
 COMPAT_TYPE3_TEST(struct ctdb_vnn_map, ctdb_vnn_map);
@@ -1701,6 +1745,7 @@ COMPAT_TYPE3_TEST(struct ctdb_script, ctdb_script);
 COMPAT_TYPE3_TEST(struct ctdb_script_list, ctdb_script_list);
 COMPAT_TYPE3_TEST(struct ctdb_ban_state, ctdb_ban_state);
 COMPAT_TYPE3_TEST(struct ctdb_notify_data, ctdb_notify_data);
+COMPAT_TYPE3_TEST(struct ctdb_iface, ctdb_iface);
 
 int main(int argc, char *argv[])
 {
@@ -1739,6 +1784,7 @@ int main(int argc, char *argv[])
        COMPAT_TEST_FUNC(ctdb_script_list)();
        COMPAT_TEST_FUNC(ctdb_ban_state)();
        COMPAT_TEST_FUNC(ctdb_notify_data)();
+       COMPAT_TEST_FUNC(ctdb_iface)();
 
        return 0;
 }
index f3c0ad70eb785d01ab79a31bb6cff9decc7f1d8e..36f3e4a0d2320ae060e20298ff15d150d99c9ae4 100644 (file)
@@ -78,7 +78,7 @@ PROTOCOL_TYPE3_TEST(struct ctdb_script, ctdb_script);
 PROTOCOL_TYPE3_TEST(struct ctdb_script_list, ctdb_script_list);
 PROTOCOL_TYPE3_TEST(struct ctdb_ban_state, ctdb_ban_state);
 PROTOCOL_TYPE3_TEST(struct ctdb_notify_data, ctdb_notify_data);
-DEFINE_TEST(struct ctdb_iface, ctdb_iface);
+PROTOCOL_TYPE3_TEST(struct ctdb_iface, ctdb_iface);
 DEFINE_TEST(struct ctdb_iface_list, ctdb_iface_list);
 DEFINE_TEST(struct ctdb_public_ip_info, ctdb_public_ip_info);
 DEFINE_TEST(struct ctdb_statistics_list, ctdb_statistics_list);