ctdb-protocol: Fix marshalling for ctdb_tunable
authorAmitay Isaacs <amitay@gmail.com>
Thu, 29 Jun 2017 14:36:18 +0000 (00:36 +1000)
committerMartin Schwenke <martins@samba.org>
Wed, 30 Aug 2017 12:59:23 +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 057dc25719a0cf4fb7f27fd49e3c894902a1bb01..61e0d7c517501c77f43b0ec54876ed2b5fce0e8d 100644 (file)
@@ -523,7 +523,7 @@ static void ctdb_req_control_data_push(struct ctdb_req_control_data *cd,
                break;
 
        case CTDB_CONTROL_SET_TUNABLE:
-               ctdb_tunable_push(cd->data.tunable, buf);
+               ctdb_tunable_push(cd->data.tunable, buf, &np);
                break;
 
        case CTDB_CONTROL_GET_TUNABLE:
@@ -818,7 +818,7 @@ static int ctdb_req_control_data_pull(uint8_t *buf, size_t buflen,
 
        case CTDB_CONTROL_SET_TUNABLE:
                ret = ctdb_tunable_pull(buf, buflen, mem_ctx,
-                                       &cd->data.tunable);
+                                       &cd->data.tunable, &np);
                break;
 
        case CTDB_CONTROL_GET_TUNABLE:
index a198be9d55afc39bfbc7af4a3697ea1dee3e6817..716ddd61d81d5fc7031ad324685c8253d478dda6 100644 (file)
@@ -186,10 +186,10 @@ void ctdb_connection_push(struct ctdb_connection *in, uint8_t *buf,
 int ctdb_connection_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
                         struct ctdb_connection **out, size_t *npull);
 
-size_t ctdb_tunable_len(struct ctdb_tunable *tunable);
-void ctdb_tunable_push(struct ctdb_tunable *tunable, uint8_t *buf);
+size_t ctdb_tunable_len(struct ctdb_tunable *in);
+void ctdb_tunable_push(struct ctdb_tunable *in, uint8_t *buf, size_t *npush);
 int ctdb_tunable_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                     struct ctdb_tunable **out);
+                     struct ctdb_tunable **out, size_t *npull);
 
 size_t ctdb_node_flag_change_len(struct ctdb_node_flag_change *flag_change);
 void ctdb_node_flag_change_push(struct ctdb_node_flag_change *flag_change,
index 7e3fb75db6e9b60d85b64a1aa18ae9b0e385f24d..c32d4efded4fc9109e5530861d10987a2930dd17 100644 (file)
@@ -2150,61 +2150,57 @@ int ctdb_connection_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        return ret;
 }
 
-struct ctdb_tunable_wire {
-       uint32_t value;
-       uint32_t length;
-       uint8_t name[1];
-};
-
-size_t ctdb_tunable_len(struct ctdb_tunable *tunable)
+size_t ctdb_tunable_len(struct ctdb_tunable *in)
 {
-       return offsetof(struct ctdb_tunable_wire, name) +
-              strlen(tunable->name) + 1;
+       return ctdb_uint32_len(&in->value) +
+               ctdb_stringn_len(&in->name);
 }
 
-void ctdb_tunable_push(struct ctdb_tunable *tunable, uint8_t *buf)
+void ctdb_tunable_push(struct ctdb_tunable *in, uint8_t *buf, size_t *npush)
 {
-       struct ctdb_tunable_wire *wire = (struct ctdb_tunable_wire *)buf;
+       size_t offset = 0, np;
 
-       wire->value = tunable->value;
-       wire->length = strlen(tunable->name) + 1;
-       memcpy(wire->name, tunable->name, wire->length);
+       ctdb_uint32_push(&in->value, buf+offset, &np);
+       offset += np;
+
+       ctdb_stringn_push(&in->name, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
 int ctdb_tunable_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                     struct ctdb_tunable **out)
+                     struct ctdb_tunable **out, size_t *npull)
 {
-       struct ctdb_tunable *tunable;
-       struct ctdb_tunable_wire *wire = (struct ctdb_tunable_wire *)buf;
+       struct ctdb_tunable *val;
+       size_t offset = 0, np;
+       int ret;
 
-       if (buflen < offsetof(struct ctdb_tunable_wire, name)) {
-               return EMSGSIZE;
-       }
-       if (wire->length > buflen) {
-               return EMSGSIZE;
-       }
-       if (offsetof(struct ctdb_tunable_wire, name) + wire->length <
-           offsetof(struct ctdb_tunable_wire, name)) {
-               return EMSGSIZE;
-       }
-       if (buflen < offsetof(struct ctdb_tunable_wire, name) + wire->length) {
-               return EMSGSIZE;
+       val = talloc(mem_ctx, struct ctdb_tunable);
+       if (val == NULL) {
+               return ENOMEM;
        }
 
-       tunable = talloc(mem_ctx, struct ctdb_tunable);
-       if (tunable == NULL) {
-               return ENOMEM;
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->value, &np);
+       if (ret != 0) {
+               goto fail;
        }
+       offset += np;
 
-       tunable->value = wire->value;
-       tunable->name = talloc_memdup(tunable, wire->name, wire->length);
-       if (tunable->name == NULL) {
-               talloc_free(tunable);
-               return ENOMEM;
+       ret = ctdb_stringn_pull(buf+offset, buflen-offset, mem_ctx,
+                               &val->name, &np);
+       if (ret != 0) {
+               goto fail;
        }
+       offset += np;
 
-       *out = tunable;
+       *out = val;
+       *npull = offset;
        return 0;
+
+fail:
+       talloc_free(val);
+       return ret;
 }
 
 size_t ctdb_node_flag_change_len(struct ctdb_node_flag_change *flag_change)
index 7a66cdefced44eac12d4c55e46e2bb8b2560e7c9..c2bbb1fa7719c18bfbf63e7a0e93d72847356c6c 100644 (file)
@@ -736,6 +736,64 @@ static int ctdb_connection_pull_old(uint8_t *buf, size_t buflen,
        return ret;
 }
 
+struct ctdb_tunable_wire {
+       uint32_t value;
+       uint32_t length;
+       uint8_t name[1];
+};
+
+static size_t ctdb_tunable_len_old(struct ctdb_tunable *in)
+{
+       return offsetof(struct ctdb_tunable_wire, name) +
+              strlen(in->name) + 1;
+}
+
+static void ctdb_tunable_push_old(struct ctdb_tunable *in, uint8_t *buf)
+{
+       struct ctdb_tunable_wire *wire = (struct ctdb_tunable_wire *)buf;
+
+       wire->value = in->value;
+       wire->length = strlen(in->name) + 1;
+       memcpy(wire->name, in->name, wire->length);
+}
+
+static int ctdb_tunable_pull_old(uint8_t *buf, size_t buflen,
+                                TALLOC_CTX *mem_ctx,
+                                struct ctdb_tunable **out)
+{
+       struct ctdb_tunable *val;
+       struct ctdb_tunable_wire *wire = (struct ctdb_tunable_wire *)buf;
+
+       if (buflen < offsetof(struct ctdb_tunable_wire, name)) {
+               return EMSGSIZE;
+       }
+       if (wire->length > buflen) {
+               return EMSGSIZE;
+       }
+       if (offsetof(struct ctdb_tunable_wire, name) + wire->length <
+           offsetof(struct ctdb_tunable_wire, name)) {
+               return EMSGSIZE;
+       }
+       if (buflen < offsetof(struct ctdb_tunable_wire, name) + wire->length) {
+               return EMSGSIZE;
+       }
+
+       val = talloc(mem_ctx, struct ctdb_tunable);
+       if (val == NULL) {
+               return ENOMEM;
+       }
+
+       val->value = wire->value;
+       val->name = talloc_memdup(val, wire->name, wire->length);
+       if (val->name == 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);
@@ -753,6 +811,7 @@ COMPAT_TYPE3_TEST(struct ctdb_traverse_start_ext, ctdb_traverse_start_ext);
 COMPAT_TYPE3_TEST(struct ctdb_traverse_all_ext, ctdb_traverse_all_ext);
 COMPAT_TYPE3_TEST(ctdb_sock_addr, ctdb_sock_addr);
 COMPAT_TYPE3_TEST(struct ctdb_connection, ctdb_connection);
+COMPAT_TYPE3_TEST(struct ctdb_tunable, ctdb_tunable);
 
 int main(int argc, char *argv[])
 {
@@ -775,6 +834,7 @@ int main(int argc, char *argv[])
        COMPAT_TEST_FUNC(ctdb_traverse_all_ext)();
        COMPAT_TEST_FUNC(ctdb_sock_addr)();
        COMPAT_TEST_FUNC(ctdb_connection)();
+       COMPAT_TEST_FUNC(ctdb_tunable)();
 
        return 0;
 }
index 70c21ac2449f6d886c6ff5a1d97aae261629ea67..f7092e74b843abdc6f2c1457eb86e73740e1c6d2 100644 (file)
@@ -62,7 +62,7 @@ PROTOCOL_TYPE3_TEST(struct ctdb_traverse_start_ext, ctdb_traverse_start_ext);
 PROTOCOL_TYPE3_TEST(struct ctdb_traverse_all_ext, ctdb_traverse_all_ext);
 PROTOCOL_TYPE3_TEST(ctdb_sock_addr, ctdb_sock_addr);
 PROTOCOL_TYPE3_TEST(struct ctdb_connection, ctdb_connection);
-DEFINE_TEST(struct ctdb_tunable, ctdb_tunable);
+PROTOCOL_TYPE3_TEST(struct ctdb_tunable, ctdb_tunable);
 DEFINE_TEST(struct ctdb_node_flag_change, ctdb_node_flag_change);
 DEFINE_TEST(struct ctdb_var_list, ctdb_var_list);
 DEFINE_TEST(struct ctdb_tunable_list, ctdb_tunable_list);