From 917c0eca0d8fd6da10076fdfaa92d6af6bab2bba Mon Sep 17 00:00:00 2001 From: Amitay Isaacs Date: Thu, 6 Jul 2017 17:53:24 +1000 Subject: [PATCH] ctdb-protocol: Add marshalling for ctdb_dbid Signed-off-by: Amitay Isaacs Reviewed-by: Martin Schwenke --- ctdb/protocol/protocol_private.h | 5 ++ ctdb/protocol/protocol_types.c | 75 ++++++++++++++++++++++++++++ ctdb/tests/src/protocol_types_test.c | 2 + 3 files changed, 82 insertions(+) diff --git a/ctdb/protocol/protocol_private.h b/ctdb/protocol/protocol_private.h index 0d59a1f5e49..b529f8b3379 100644 --- a/ctdb/protocol/protocol_private.h +++ b/ctdb/protocol/protocol_private.h @@ -122,6 +122,11 @@ void ctdb_vnn_map_push(struct ctdb_vnn_map *in, uint8_t *buf, size_t *npush); int ctdb_vnn_map_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, struct ctdb_vnn_map **out, size_t *npull); +size_t ctdb_dbid_len(struct ctdb_dbid *in); +void ctdb_dbid_push(struct ctdb_dbid *in, uint8_t *buf, size_t *npush); +int ctdb_dbid_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, + struct ctdb_dbid **out, size_t *npull); + size_t ctdb_dbid_map_len(struct ctdb_dbid_map *dbmap); void ctdb_dbid_map_push(struct ctdb_dbid_map *dbmap, uint8_t *buf); int ctdb_dbid_map_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, diff --git a/ctdb/protocol/protocol_types.c b/ctdb/protocol/protocol_types.c index e324af12d32..b31f6de3fbf 100644 --- a/ctdb/protocol/protocol_types.c +++ b/ctdb/protocol/protocol_types.c @@ -932,6 +932,81 @@ fail: return ret; } +size_t ctdb_dbid_len(struct ctdb_dbid *in) +{ + return ctdb_uint32_len(&in->db_id) + + ctdb_uint8_len(&in->flags) + + ctdb_padding_len(3); +} + +void ctdb_dbid_push(struct ctdb_dbid *in, uint8_t *buf, size_t *npush) +{ + size_t offset = 0, np; + + ctdb_uint32_push(&in->db_id, buf+offset, &np); + offset += np; + + ctdb_uint8_push(&in->flags, buf+offset, &np); + offset += np; + + ctdb_padding_push(3, buf+offset, &np); + offset += np; + + *npush = offset; +} + +static int ctdb_dbid_pull_elems(uint8_t *buf, size_t buflen, + TALLOC_CTX *mem_ctx, struct ctdb_dbid *out, + size_t *npull) +{ + size_t offset = 0, np; + int ret; + + ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->db_id, &np); + if (ret != 0) { + return ret; + } + offset += np; + + ret = ctdb_uint8_pull(buf+offset, buflen-offset, &out->flags, &np); + if (ret != 0) { + return ret; + } + offset += np; + + ret = ctdb_padding_pull(buf+offset, buflen-offset, 3, &np); + if (ret != 0) { + return ret; + } + offset += np; + + *npull = offset; + return 0; +} + +int ctdb_dbid_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, + struct ctdb_dbid **out, size_t *npull) +{ + struct ctdb_dbid *val; + size_t np; + int ret; + + val = talloc(mem_ctx, struct ctdb_dbid); + if (val == NULL) { + return ENOMEM; + } + + ret = ctdb_dbid_pull_elems(buf, buflen, val, val, &np); + if (ret != 0) { + talloc_free(val); + return ret; + } + + *out = val; + *npull = np; + return 0; +} + struct ctdb_dbid_map_wire { uint32_t num; struct ctdb_dbid dbs[1]; diff --git a/ctdb/tests/src/protocol_types_test.c b/ctdb/tests/src/protocol_types_test.c index 79f5d40e854..55db9931957 100644 --- a/ctdb/tests/src/protocol_types_test.c +++ b/ctdb/tests/src/protocol_types_test.c @@ -65,6 +65,7 @@ static void test_ctdb_g_lock(void) PROTOCOL_TYPE3_TEST(struct ctdb_statistics, ctdb_statistics); PROTOCOL_TYPE3_TEST(struct ctdb_vnn_map, ctdb_vnn_map); +PROTOCOL_TYPE3_TEST(struct ctdb_dbid, ctdb_dbid); DEFINE_TEST(struct ctdb_dbid_map, ctdb_dbid_map); DEFINE_TEST(struct ctdb_pulldb, ctdb_pulldb); DEFINE_TEST(struct ctdb_pulldb_ext, ctdb_pulldb_ext); @@ -164,6 +165,7 @@ int main(int argc, char *argv[]) TEST_FUNC(ctdb_statistics)(); TEST_FUNC(ctdb_vnn_map)(); + TEST_FUNC(ctdb_dbid)(); TEST_FUNC(ctdb_dbid_map)(); TEST_FUNC(ctdb_pulldb)(); TEST_FUNC(ctdb_pulldb_ext)();