ctdb-protocol: Fix marshalling for ctdb_notify_data
authorAmitay Isaacs <amitay@gmail.com>
Wed, 12 Jul 2017 08:29:12 +0000 (18:29 +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_types_compat_test.c
ctdb/tests/src/protocol_types_test.c

index 39bbeeefbe4ebd52046cea25fb62b11030ee607d..ff770b072597bf5fd9392e3e4660627a8aa209a4 100644 (file)
@@ -599,7 +599,7 @@ static void ctdb_req_control_data_push(struct ctdb_req_control_data *cd,
                break;
 
        case CTDB_CONTROL_REGISTER_NOTIFY:
-               ctdb_notify_data_push(cd->data.notify, buf);
+               ctdb_notify_data_push(cd->data.notify, buf, &np);
                break;
 
        case CTDB_CONTROL_DEREGISTER_NOTIFY:
@@ -911,7 +911,7 @@ static int ctdb_req_control_data_pull(uint8_t *buf, size_t buflen,
 
        case CTDB_CONTROL_REGISTER_NOTIFY:
                ret = ctdb_notify_data_pull(buf, buflen, mem_ctx,
-                                           &cd->data.notify);
+                                           &cd->data.notify, &np);
                break;
 
        case CTDB_CONTROL_DEREGISTER_NOTIFY:
index 219a714bb0b6d4fa20ec6654842fae6b054dc484..0067ac13abccd35fb2cf94bb5e85c22b83fc598b 100644 (file)
@@ -271,10 +271,11 @@ void ctdb_ban_state_push(struct ctdb_ban_state *in, uint8_t *buf,
 int ctdb_ban_state_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
                        struct ctdb_ban_state **out, size_t *npull);
 
-size_t ctdb_notify_data_len(struct ctdb_notify_data *notify);
-void ctdb_notify_data_push(struct ctdb_notify_data *notify, uint8_t *buf);
+size_t ctdb_notify_data_len(struct ctdb_notify_data *in);
+void ctdb_notify_data_push(struct ctdb_notify_data *in, uint8_t *buf,
+                          size_t *npush);
 int ctdb_notify_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                         struct ctdb_notify_data **out);
+                         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);
index 77a54bd3dbbc3fa5fd55f37c4c22624eb632df0b..7239615be9d21af4f1a811962392810e04632662 100644 (file)
@@ -3953,64 +3953,58 @@ fail:
        return ret;
 }
 
-struct ctdb_notify_data_wire {
-       uint64_t srvid;
-       uint32_t len;
-       uint8_t data[1];
-};
-
-size_t ctdb_notify_data_len(struct ctdb_notify_data *notify)
+size_t ctdb_notify_data_len(struct ctdb_notify_data *in)
 {
-       return offsetof(struct ctdb_notify_data_wire, data) +
-              notify->data.dsize;
+       return ctdb_uint64_len(&in->srvid) +
+               ctdb_tdb_datan_len(&in->data);
 }
 
-void ctdb_notify_data_push(struct ctdb_notify_data *notify, uint8_t *buf)
+void ctdb_notify_data_push(struct ctdb_notify_data *in, uint8_t *buf,
+                          size_t *npush)
 {
-       struct ctdb_notify_data_wire *wire =
-               (struct ctdb_notify_data_wire *)buf;
+       size_t offset = 0, np;
+
+       ctdb_uint64_push(&in->srvid, buf+offset, &np);
+       offset += np;
+
+       ctdb_tdb_datan_push(&in->data, buf+offset, &np);
+       offset += np;
 
-       wire->srvid = notify->srvid;
-       wire->len = notify->data.dsize;
-       memcpy(wire->data, notify->data.dptr, notify->data.dsize);
+       *npush = offset;
 }
 
 int ctdb_notify_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                         struct ctdb_notify_data **out)
+                         struct ctdb_notify_data **out, size_t *npull)
 {
-       struct ctdb_notify_data *notify;
-       struct ctdb_notify_data_wire *wire =
-               (struct ctdb_notify_data_wire *)buf;
+       struct ctdb_notify_data *val;
+       size_t offset = 0, np;
+       int ret;
 
-       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;
+       val = talloc(mem_ctx, struct ctdb_notify_data);
+       if (val == NULL) {
+               return ENOMEM;
        }
 
-       notify = talloc(mem_ctx, struct ctdb_notify_data);
-       if (notify == NULL) {
-               return ENOMEM;
+       ret = ctdb_uint64_pull(buf+offset, buflen-offset, &val->srvid, &np);
+       if (ret != 0) {
+               goto fail;
        }
+       offset += np;
 
-       notify->srvid = wire->srvid;
-       notify->data.dsize = wire->len;
-       notify->data.dptr = talloc_memdup(notify, wire->data, wire->len);
-       if (notify->data.dptr == NULL) {
-               talloc_free(notify);
-               return ENOMEM;
+       ret = ctdb_tdb_datan_pull(buf+offset, buflen-offset, val, &val->data,
+                                 &np);
+       if (ret != 0) {
+               goto fail;
        }
+       offset += np;
 
-       *out = notify;
+       *out = val;
+       *npull = offset;
        return 0;
+
+fail:
+       talloc_free(val);
+       return ret;
 }
 
 size_t ctdb_iface_len(struct ctdb_iface *iface)
index 97f28157726a55af4ad2d5d918a5fb6a2c0f351d..408ba72937ab19b3cec6dc8d2ba6c7915b622d5b 100644 (file)
@@ -1606,6 +1606,68 @@ static int ctdb_ban_state_pull_old(uint8_t *buf, size_t buflen,
        return 0;
 }
 
+struct ctdb_notify_data_wire {
+       uint64_t srvid;
+       uint32_t len;
+       uint8_t data[1];
+};
+
+static size_t ctdb_notify_data_len_old(struct ctdb_notify_data *in)
+{
+       return offsetof(struct ctdb_notify_data_wire, data) +
+              in->data.dsize;
+}
+
+static void ctdb_notify_data_push_old(struct ctdb_notify_data *in,
+                                     uint8_t *buf)
+{
+       struct ctdb_notify_data_wire *wire =
+               (struct ctdb_notify_data_wire *)buf;
+
+       wire->srvid = in->srvid;
+       wire->len = in->data.dsize;
+       memcpy(wire->data, in->data.dptr, in->data.dsize);
+}
+
+static int ctdb_notify_data_pull_old(uint8_t *buf, size_t buflen,
+                                    TALLOC_CTX *mem_ctx,
+                                    struct ctdb_notify_data **out)
+{
+       struct ctdb_notify_data *val;
+       struct ctdb_notify_data_wire *wire =
+               (struct ctdb_notify_data_wire *)buf;
+
+       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;
+       }
+
+       val = talloc(mem_ctx, struct ctdb_notify_data);
+       if (val == NULL) {
+               return ENOMEM;
+       }
+
+       val->srvid = wire->srvid;
+       val->data.dsize = wire->len;
+       val->data.dptr = talloc_memdup(val, wire->data, wire->len);
+       if (val->data.dptr == 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);
@@ -1638,6 +1700,7 @@ COMPAT_TYPE3_TEST(struct ctdb_node_map, ctdb_node_map);
 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);
 
 int main(int argc, char *argv[])
 {
@@ -1675,6 +1738,7 @@ int main(int argc, char *argv[])
        COMPAT_TEST_FUNC(ctdb_script)();
        COMPAT_TEST_FUNC(ctdb_script_list)();
        COMPAT_TEST_FUNC(ctdb_ban_state)();
+       COMPAT_TEST_FUNC(ctdb_notify_data)();
 
        return 0;
 }
index 96f503c911ca6db80c72ed65e6ba292f7b910c91..f3c0ad70eb785d01ab79a31bb6cff9decc7f1d8e 100644 (file)
@@ -77,7 +77,7 @@ PROTOCOL_TYPE3_TEST(struct ctdb_node_map, ctdb_node_map);
 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);
-DEFINE_TEST(struct ctdb_notify_data, ctdb_notify_data);
+PROTOCOL_TYPE3_TEST(struct ctdb_notify_data, ctdb_notify_data);
 DEFINE_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);