ctdb-protocol: Fix marshalling for ctdb_election_message
authorAmitay Isaacs <amitay@gmail.com>
Thu, 13 Jul 2017 04:56:50 +0000 (14:56 +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_message.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 2ae7724233031c8a0601a6e9f32e68791a4484dc..b7eaa4fbe9feed7597717cb0efb05e04070e0e41 100644 (file)
@@ -125,7 +125,7 @@ static void ctdb_message_data_push(union ctdb_message_data *mdata,
                break;
 
        case CTDB_SRVID_ELECTION:
-               ctdb_election_message_push(mdata->election, buf);
+               ctdb_election_message_push(mdata->election, buf, &np);
                break;
 
        case CTDB_SRVID_RECONFIGURE:
@@ -206,7 +206,7 @@ static int ctdb_message_data_pull(uint8_t *buf, size_t buflen,
 
        case CTDB_SRVID_ELECTION:
                ret = ctdb_election_message_pull(buf, buflen, mem_ctx,
-                                                &mdata->election);
+                                                &mdata->election, &np);
                break;
 
        case CTDB_SRVID_RECONFIGURE:
index 280fc9904d8ae095c638800689d84eb7b278444c..8e47d73c2dd03bdeeb925b025ed432f4793af20b 100644 (file)
@@ -306,11 +306,12 @@ void ctdb_db_statistics_push(struct ctdb_db_statistics *in, uint8_t *buf,
 int ctdb_db_statistics_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
                            struct ctdb_db_statistics **out, size_t *npull);
 
-size_t ctdb_election_message_len(struct ctdb_election_message *election);
-void ctdb_election_message_push(struct ctdb_election_message *election,
-                               uint8_t *buf);
+size_t ctdb_election_message_len(struct ctdb_election_message *in);
+void ctdb_election_message_push(struct ctdb_election_message *in,
+                               uint8_t *buf, size_t *npush);
 int ctdb_election_message_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                              struct ctdb_election_message **out);
+                              struct ctdb_election_message **out,
+                              size_t *npull);
 
 size_t ctdb_srvid_message_len(struct ctdb_srvid_message *msg);
 void ctdb_srvid_message_push(struct ctdb_srvid_message *msg, uint8_t *buf);
index f3d5d60b3025e1c9a1f69eddcddfe60edff36926..3ee9fb7f64f0592b38ca65d0bcf5f11b8355e01b 100644 (file)
@@ -4626,34 +4626,92 @@ int ctdb_db_statistics_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        return 0;
 }
 
-size_t ctdb_election_message_len(struct ctdb_election_message *election)
+size_t ctdb_election_message_len(struct ctdb_election_message *in)
 {
-       return sizeof(struct ctdb_election_message);
+       return ctdb_uint32_len(&in->num_connected) +
+               ctdb_padding_len(4) +
+               ctdb_timeval_len(&in->priority_time) +
+               ctdb_uint32_len(&in->pnn) +
+               ctdb_uint32_len(&in->node_flags);
 }
 
-void ctdb_election_message_push(struct ctdb_election_message *election,
-                               uint8_t *buf)
+void ctdb_election_message_push(struct ctdb_election_message *in,
+                               uint8_t *buf, size_t *npush)
 {
-       memcpy(buf, election, sizeof(struct ctdb_election_message));
+       size_t offset = 0, np;
+
+       ctdb_uint32_push(&in->num_connected, buf+offset, &np);
+       offset += np;
+
+       ctdb_padding_push(4, buf+offset, &np);
+       offset += np;
+
+       ctdb_timeval_push(&in->priority_time, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->pnn, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&in->node_flags, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
-int ctdb_election_message_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                              struct ctdb_election_message **out)
+int ctdb_election_message_pull(uint8_t *buf, size_t buflen,
+                              TALLOC_CTX *mem_ctx,
+                              struct ctdb_election_message **out,
+                              size_t *npull)
 {
-       struct ctdb_election_message *election;
+       struct ctdb_election_message *val;
+       size_t offset = 0, np;
+       int ret;
 
-       if (buflen < sizeof(struct ctdb_election_message)) {
-               return EMSGSIZE;
+       val = talloc(mem_ctx, struct ctdb_election_message);
+       if (val == NULL) {
+               return ENOMEM;
        }
 
-       election = talloc_memdup(mem_ctx, buf,
-                                sizeof(struct ctdb_election_message));
-       if (election == NULL) {
-               return ENOMEM;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->num_connected,
+                              &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_padding_pull(buf+offset, buflen-offset, 4, &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       ret = ctdb_timeval_pull(buf+offset, buflen-offset,
+                               &val->priority_time, &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;
 
-       *out = election;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->node_flags,
+                              &np);
+       if (ret != 0) {
+               goto fail;
+       }
+       offset += np;
+
+       *out = val;
+       *npull = offset;
        return 0;
+
+fail:
+       talloc_free(val);
+       return ret;
 }
 
 size_t ctdb_srvid_message_len(struct ctdb_srvid_message *msg)
index 5b0d67d2b42e4e5c34d5a4898e7d2b4ef35cde65..015a3b78adc294e9f6a3e61faf84d983c757e878 100644 (file)
@@ -1288,7 +1288,7 @@ void fill_ctdb_election_message(TALLOC_CTX *mem_ctx,
                                struct ctdb_election_message *p)
 {
        p->num_connected = rand_int(32);
-       fill_buffer(&p->priority_time, sizeof(struct timeval));
+       fill_ctdb_timeval(&p->priority_time);
        p->pnn = rand_int(32);
        p->node_flags = rand32();
 }
@@ -1297,7 +1297,7 @@ void verify_ctdb_election_message(struct ctdb_election_message *p1,
                                  struct ctdb_election_message *p2)
 {
        assert(p1->num_connected == p2->num_connected);
-       verify_buffer(p1, p2, sizeof(struct timeval));
+       verify_ctdb_timeval(&p1->priority_time, &p2->priority_time);
        assert(p1->pnn == p2->pnn);
        assert(p1->node_flags == p2->node_flags);
 }
index 8599344d803dd5b4e3e12f8d00eb2614c889497a..6665fc4b4174815ddcd1d98b95a7b17d0f199c3a 100644 (file)
@@ -2076,6 +2076,37 @@ static int ctdb_db_statistics_pull_old(uint8_t *buf, size_t buflen,
        return 0;
 }
 
+static size_t ctdb_election_message_len_old(struct ctdb_election_message *in)
+{
+       return sizeof(struct ctdb_election_message);
+}
+
+static void ctdb_election_message_push_old(struct ctdb_election_message *in,
+                                          uint8_t *buf)
+{
+       memcpy(buf, in, sizeof(struct ctdb_election_message));
+}
+
+static int ctdb_election_message_pull_old(uint8_t *buf, size_t buflen,
+                                         TALLOC_CTX *mem_ctx,
+                                         struct ctdb_election_message **out)
+{
+       struct ctdb_election_message *val;
+
+       if (buflen < sizeof(struct ctdb_election_message)) {
+               return EMSGSIZE;
+       }
+
+       val = talloc_memdup(mem_ctx, buf,
+                           sizeof(struct ctdb_election_message));
+       if (val == NULL) {
+               return ENOMEM;
+       }
+
+       *out = val;
+       return 0;
+}
+
 
 COMPAT_TYPE3_TEST(struct ctdb_statistics, ctdb_statistics);
 COMPAT_TYPE3_TEST(struct ctdb_vnn_map, ctdb_vnn_map);
@@ -2116,6 +2147,8 @@ COMPAT_TYPE3_TEST(struct ctdb_statistics_list, ctdb_statistics_list);
 COMPAT_TYPE3_TEST(struct ctdb_key_data, ctdb_key_data);
 COMPAT_TYPE3_TEST(struct ctdb_db_statistics, ctdb_db_statistics);
 
+COMPAT_TYPE3_TEST(struct ctdb_election_message, ctdb_election_message);
+
 int main(int argc, char *argv[])
 {
        if (argc == 2) {
@@ -2160,5 +2193,7 @@ int main(int argc, char *argv[])
        COMPAT_TEST_FUNC(ctdb_key_data)();
        COMPAT_TEST_FUNC(ctdb_db_statistics)();
 
+       COMPAT_TEST_FUNC(ctdb_election_message)();
+
        return 0;
 }
index 4e1e6e2afc6cd0be9c68d15e4527a120c447f6f0..f51f3fc996797906fdd6440f42169eaf89f9d4c8 100644 (file)
@@ -84,7 +84,7 @@ PROTOCOL_TYPE3_TEST(struct ctdb_public_ip_info, ctdb_public_ip_info);
 PROTOCOL_TYPE3_TEST(struct ctdb_statistics_list, ctdb_statistics_list);
 PROTOCOL_TYPE3_TEST(struct ctdb_key_data, ctdb_key_data);
 PROTOCOL_TYPE3_TEST(struct ctdb_db_statistics, ctdb_db_statistics);
-DEFINE_TEST(struct ctdb_election_message, ctdb_election_message);
+PROTOCOL_TYPE3_TEST(struct ctdb_election_message, ctdb_election_message);
 DEFINE_TEST(struct ctdb_srvid_message, ctdb_srvid_message);
 DEFINE_TEST(struct ctdb_disable_message, ctdb_disable_message);
 DEFINE_TEST(struct ctdb_g_lock_list, ctdb_g_lock_list);