ctdb-protocol: Fix marshalling for ctdb_connection
authorAmitay Isaacs <amitay@gmail.com>
Thu, 29 Jun 2017 14:31:21 +0000 (00:31 +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 01238b3628422ca2dd040a7bd9300dd69737361d..057dc25719a0cf4fb7f27fd49e3c894902a1bb01 100644 (file)
@@ -511,15 +511,15 @@ static void ctdb_req_control_data_push(struct ctdb_req_control_data *cd,
                break;
 
        case CTDB_CONTROL_TCP_CLIENT:
-               ctdb_connection_push(cd->data.conn, buf);
+               ctdb_connection_push(cd->data.conn, buf, &np);
                break;
 
        case CTDB_CONTROL_TCP_ADD:
-               ctdb_connection_push(cd->data.conn, buf);
+               ctdb_connection_push(cd->data.conn, buf, &np);
                break;
 
        case CTDB_CONTROL_TCP_REMOVE:
-               ctdb_connection_push(cd->data.conn, buf);
+               ctdb_connection_push(cd->data.conn, buf, &np);
                break;
 
        case CTDB_CONTROL_SET_TUNABLE:
@@ -637,7 +637,7 @@ static void ctdb_req_control_data_push(struct ctdb_req_control_data *cd,
                break;
 
        case CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE:
-               ctdb_connection_push(cd->data.conn, buf);
+               ctdb_connection_push(cd->data.conn, buf, &np);
                break;
 
        case CTDB_CONTROL_SCHEDULE_FOR_DELETION:
@@ -803,17 +803,17 @@ static int ctdb_req_control_data_pull(uint8_t *buf, size_t buflen,
 
        case CTDB_CONTROL_TCP_CLIENT:
                ret = ctdb_connection_pull(buf, buflen, mem_ctx,
-                                          &cd->data.conn);
+                                          &cd->data.conn, &np);
                break;
 
        case CTDB_CONTROL_TCP_ADD:
                ret = ctdb_connection_pull(buf, buflen, mem_ctx,
-                                          &cd->data.conn);
+                                          &cd->data.conn, &np);
                break;
 
        case CTDB_CONTROL_TCP_REMOVE:
                ret = ctdb_connection_pull(buf, buflen, mem_ctx,
-                                          &cd->data.conn);
+                                          &cd->data.conn, &np);
                break;
 
        case CTDB_CONTROL_SET_TUNABLE:
@@ -955,7 +955,7 @@ static int ctdb_req_control_data_pull(uint8_t *buf, size_t buflen,
 
        case CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE:
                ret = ctdb_connection_pull(buf, buflen, mem_ctx,
-                                          &cd->data.conn);
+                                          &cd->data.conn, &np);
                break;
 
        case CTDB_CONTROL_SCHEDULE_FOR_DELETION:
index 644748504ab8e36655627120a73d0ed332928385..a198be9d55afc39bfbc7af4a3697ea1dee3e6817 100644 (file)
@@ -180,10 +180,11 @@ int ctdb_sock_addr_pull_elems(uint8_t *buf, size_t buflen,
 int ctdb_sock_addr_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
                        ctdb_sock_addr **out, size_t *npull);
 
-size_t ctdb_connection_len(struct ctdb_connection *conn);
-void ctdb_connection_push(struct ctdb_connection *conn, uint8_t *buf);
+size_t ctdb_connection_len(struct ctdb_connection *in);
+void ctdb_connection_push(struct ctdb_connection *in, uint8_t *buf,
+                         size_t *npush);
 int ctdb_connection_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                        struct ctdb_connection **out);
+                        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);
index 5a064c93d7618bbe1f91ed666a2ab83162bc4361..7e3fb75db6e9b60d85b64a1aa18ae9b0e385f24d 100644 (file)
@@ -2081,46 +2081,72 @@ int ctdb_sock_addr_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        return ret;
 }
 
-size_t ctdb_connection_len(struct ctdb_connection *conn)
+size_t ctdb_connection_len(struct ctdb_connection *in)
 {
-       return sizeof(struct ctdb_connection);
+       return ctdb_sock_addr_len(&in->src) +
+               ctdb_sock_addr_len(&in->dst);
 }
 
-void ctdb_connection_push(struct ctdb_connection *conn, uint8_t *buf)
+void ctdb_connection_push(struct ctdb_connection *in, uint8_t *buf,
+                         size_t *npush)
 {
-       memcpy(buf, conn, sizeof(struct ctdb_connection));
+       size_t offset = 0, np;
+
+       ctdb_sock_addr_push(&in->src, buf+offset, &np);
+       offset += np;
+
+       ctdb_sock_addr_push(&in->dst, buf+offset, &np);
+       offset += np;
+
+       *npush = offset;
 }
 
 static int ctdb_connection_pull_elems(uint8_t *buf, size_t buflen,
                                      TALLOC_CTX *mem_ctx,
-                                     struct ctdb_connection *out)
+                                     struct ctdb_connection *out,
+                                     size_t *npull)
 {
-       if (buflen < sizeof(struct ctdb_connection)) {
-               return EMSGSIZE;
+       size_t offset = 0, np;
+       int ret;
+
+       ret = ctdb_sock_addr_pull_elems(buf+offset, buflen-offset,
+                                       mem_ctx, &out->src, &np);
+       if (ret != 0) {
+               return ret;
        }
+       offset += np;
 
-       memcpy(out, buf, sizeof(struct ctdb_connection));
+       ret = ctdb_sock_addr_pull_elems(buf+offset, buflen-offset,
+                                       mem_ctx, &out->dst, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
 
+       *npull = offset;
        return 0;
 }
 
 int ctdb_connection_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                        struct ctdb_connection **out)
+                        struct ctdb_connection **out, size_t *npull)
 {
-       struct ctdb_connection *conn;
+       struct ctdb_connection *val;
+       size_t np;
        int ret;
 
-       conn = talloc(mem_ctx, struct ctdb_connection);
-       if (conn == NULL) {
+       val = talloc(mem_ctx, struct ctdb_connection);
+       if (val == NULL) {
                return ENOMEM;
        }
 
-       ret = ctdb_connection_pull_elems(buf, buflen, conn, conn);
+       ret = ctdb_connection_pull_elems(buf, buflen, val, val, &np);
        if (ret != 0) {
-               TALLOC_FREE(conn);
+               talloc_free(val);
+               return ret;
        }
 
-       *out = conn;
+       *out = val;
+       *npull = np;
        return ret;
 }
 
@@ -2346,7 +2372,7 @@ void ctdb_tickle_list_push(struct ctdb_tickle_list *tickles, uint8_t *buf)
 {
        struct ctdb_tickle_list_wire *wire =
                (struct ctdb_tickle_list_wire *)buf;
-       size_t offset;
+       size_t offset, np;
        int i;
 
        memcpy(&wire->addr, &tickles->addr, sizeof(ctdb_sock_addr));
@@ -2354,8 +2380,8 @@ void ctdb_tickle_list_push(struct ctdb_tickle_list *tickles, uint8_t *buf)
 
        offset = offsetof(struct ctdb_tickle_list_wire, conn);
        for (i=0; i<tickles->num; i++) {
-               ctdb_connection_push(&tickles->conn[i], &buf[offset]);
-               offset += ctdb_connection_len(&tickles->conn[i]);
+               ctdb_connection_push(&tickles->conn[i], &buf[offset], &np);
+               offset += np;
        }
 }
 
@@ -2365,7 +2391,7 @@ int ctdb_tickle_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        struct ctdb_tickle_list *tickles;
        struct ctdb_tickle_list_wire *wire =
                (struct ctdb_tickle_list_wire *)buf;
-       size_t offset;
+       size_t offset, np;
        int i, ret;
 
        if (buflen < offsetof(struct ctdb_tickle_list_wire, conn)) {
@@ -2402,12 +2428,12 @@ int ctdb_tickle_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
        for (i=0; i<wire->num; i++) {
                ret = ctdb_connection_pull_elems(&buf[offset], buflen-offset,
                                                 tickles->conn,
-                                                &tickles->conn[i]);
+                                                &tickles->conn[i], &np);
                if (ret != 0) {
                        talloc_free(tickles);
                        return ret;
                }
-               offset += ctdb_connection_len(&tickles->conn[i]);
+               offset += np;
        }
 
        *out = tickles;
index 327e855a6f427f8ab56d45ab0d287dc3dcc85d09..7a66cdefced44eac12d4c55e46e2bb8b2560e7c9 100644 (file)
@@ -691,6 +691,51 @@ static int ctdb_sock_addr_pull_old(uint8_t *buf, size_t buflen,
        return ret;
 }
 
+static size_t ctdb_connection_len_old(struct ctdb_connection *in)
+{
+       return sizeof(struct ctdb_connection);
+}
+
+static void ctdb_connection_push_old(struct ctdb_connection *in, uint8_t *buf)
+{
+       memcpy(buf, in, sizeof(struct ctdb_connection));
+}
+
+static int ctdb_connection_pull_elems_old(uint8_t *buf, size_t buflen,
+                                         TALLOC_CTX *mem_ctx,
+                                         struct ctdb_connection *out)
+{
+       if (buflen < sizeof(struct ctdb_connection)) {
+               return EMSGSIZE;
+       }
+
+       memcpy(out, buf, sizeof(struct ctdb_connection));
+
+       return 0;
+}
+
+static int ctdb_connection_pull_old(uint8_t *buf, size_t buflen,
+                                   TALLOC_CTX *mem_ctx,
+                                   struct ctdb_connection **out)
+{
+       struct ctdb_connection *val;
+       int ret;
+
+       val = talloc(mem_ctx, struct ctdb_connection);
+       if (val == NULL) {
+               return ENOMEM;
+       }
+
+       ret = ctdb_connection_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);
@@ -707,6 +752,7 @@ COMPAT_TYPE3_TEST(struct ctdb_traverse_all, ctdb_traverse_all);
 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);
 
 int main(int argc, char *argv[])
 {
@@ -728,6 +774,7 @@ int main(int argc, char *argv[])
        COMPAT_TEST_FUNC(ctdb_traverse_start_ext)();
        COMPAT_TEST_FUNC(ctdb_traverse_all_ext)();
        COMPAT_TEST_FUNC(ctdb_sock_addr)();
+       COMPAT_TEST_FUNC(ctdb_connection)();
 
        return 0;
 }
index 3b66871e9f2e696777ea767a50091afcfa5b1ccf..70c21ac2449f6d886c6ff5a1d97aae261629ea67 100644 (file)
@@ -61,7 +61,7 @@ PROTOCOL_TYPE3_TEST(struct ctdb_traverse_all, ctdb_traverse_all);
 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);
-DEFINE_TEST(struct ctdb_connection, ctdb_connection);
+PROTOCOL_TYPE3_TEST(struct ctdb_connection, ctdb_connection);
 DEFINE_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);