From 819b6310cb643a8f679eae5e6ca3b13b29f55164 Mon Sep 17 00:00:00 2001 From: Amitay Isaacs Date: Wed, 12 Jul 2017 19:05:13 +1000 Subject: [PATCH] ctdb-protocol: Fix marshalling for ctdb_key_data Signed-off-by: Amitay Isaacs Reviewed-by: Martin Schwenke --- ctdb/protocol/protocol_control.c | 4 +- ctdb/protocol/protocol_private.h | 6 +- ctdb/protocol/protocol_types.c | 97 ++++++++++++--------- ctdb/tests/src/protocol_types_compat_test.c | 62 +++++++++++++ ctdb/tests/src/protocol_types_test.c | 2 +- 5 files changed, 124 insertions(+), 47 deletions(-) diff --git a/ctdb/protocol/protocol_control.c b/ctdb/protocol/protocol_control.c index 3c12fbed2a2..9476c3564f4 100644 --- a/ctdb/protocol/protocol_control.c +++ b/ctdb/protocol/protocol_control.c @@ -641,7 +641,7 @@ static void ctdb_req_control_data_push(struct ctdb_req_control_data *cd, break; case CTDB_CONTROL_SCHEDULE_FOR_DELETION: - ctdb_key_data_push(cd->data.key, buf); + ctdb_key_data_push(cd->data.key, buf, &np); break; case CTDB_CONTROL_SET_DB_READONLY: @@ -960,7 +960,7 @@ static int ctdb_req_control_data_pull(uint8_t *buf, size_t buflen, case CTDB_CONTROL_SCHEDULE_FOR_DELETION: ret = ctdb_key_data_pull(buf, buflen, mem_ctx, - &cd->data.key); + &cd->data.key, &np); break; case CTDB_CONTROL_SET_DB_READONLY: diff --git a/ctdb/protocol/protocol_private.h b/ctdb/protocol/protocol_private.h index ecd06891589..61453c49b5f 100644 --- a/ctdb/protocol/protocol_private.h +++ b/ctdb/protocol/protocol_private.h @@ -295,10 +295,10 @@ void ctdb_public_ip_info_push(struct ctdb_public_ip_info *in, uint8_t *buf, int ctdb_public_ip_info_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, struct ctdb_public_ip_info **out, size_t *npull); -size_t ctdb_key_data_len(struct ctdb_key_data *key); -void ctdb_key_data_push(struct ctdb_key_data *key, uint8_t *buf); +size_t ctdb_key_data_len(struct ctdb_key_data *in); +void ctdb_key_data_push(struct ctdb_key_data *in, uint8_t *buf, size_t *npush); int ctdb_key_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, - struct ctdb_key_data **out); + struct ctdb_key_data **out, size_t *npull); size_t ctdb_db_statistics_len(struct ctdb_db_statistics *dbstats); void ctdb_db_statistics_push(struct ctdb_db_statistics *dbstats, void *buf); diff --git a/ctdb/protocol/protocol_types.c b/ctdb/protocol/protocol_types.c index 885256080f8..f384c2ff7ad 100644 --- a/ctdb/protocol/protocol_types.c +++ b/ctdb/protocol/protocol_types.c @@ -4250,63 +4250,78 @@ fail: return ret; } -struct ctdb_key_data_wire { - uint32_t db_id; - struct ctdb_ltdb_header header; - uint32_t keylen; - uint8_t key[1]; -}; - -size_t ctdb_key_data_len(struct ctdb_key_data *key) +size_t ctdb_key_data_len(struct ctdb_key_data *in) { - return offsetof(struct ctdb_key_data_wire, key) + key->key.dsize; + return ctdb_uint32_len(&in->db_id) + + ctdb_padding_len(4) + + ctdb_ltdb_header_len(&in->header) + + ctdb_tdb_datan_len(&in->key); } -void ctdb_key_data_push(struct ctdb_key_data *key, uint8_t *buf) +void ctdb_key_data_push(struct ctdb_key_data *in, uint8_t *buf, size_t *npush) { - struct ctdb_key_data_wire *wire = (struct ctdb_key_data_wire *)buf; + size_t offset = 0, np; - memcpy(wire, key, offsetof(struct ctdb_key_data, key)); - wire->keylen = key->key.dsize; - memcpy(wire->key, key->key.dptr, key->key.dsize); + ctdb_uint32_push(&in->db_id, buf+offset, &np); + offset += np; + + ctdb_padding_push(4, buf+offset, &np); + offset += np; + + ctdb_ltdb_header_push(&in->header, buf+offset, &np); + offset += np; + + ctdb_tdb_datan_push(&in->key, buf+offset, &np); + offset += np; + + *npush = offset; } int ctdb_key_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, - struct ctdb_key_data **out) + struct ctdb_key_data **out, size_t *npull) { - struct ctdb_key_data *key_data; - struct ctdb_key_data_wire *wire = (struct ctdb_key_data_wire *)buf; + struct ctdb_key_data *val; + size_t offset = 0, np; + int ret; - if (buflen < offsetof(struct ctdb_key_data_wire, key)) { - return EMSGSIZE; - } - if (wire->keylen > buflen) { - return EMSGSIZE; - } - if (offsetof(struct ctdb_key_data_wire, key) + wire->keylen < - offsetof(struct ctdb_key_data_wire, key)) { - return EMSGSIZE; - } - if (buflen < offsetof(struct ctdb_key_data_wire, key) + wire->keylen) { - return EMSGSIZE; - } - - key_data = talloc(mem_ctx, struct ctdb_key_data); - if (key_data == NULL) { + val = talloc(mem_ctx, struct ctdb_key_data); + if (val == NULL) { return ENOMEM; } - memcpy(key_data, wire, offsetof(struct ctdb_key_data, key)); - - key_data->key.dsize = wire->keylen; - key_data->key.dptr = talloc_memdup(key_data, wire->key, wire->keylen); - if (key_data->key.dptr == NULL) { - talloc_free(key_data); - return ENOMEM; + ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->db_id, &np); + if (ret != 0) { + goto fail; } + offset += np; - *out = key_data; + ret = ctdb_padding_pull(buf+offset, buflen-offset, 4, &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_ltdb_header_pull(buf+offset, buflen-offset, &val->header, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_tdb_datan_pull(buf+offset, buflen-offset, val, &val->key, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + *out = val; + *npull = offset; return 0; + +fail: + talloc_free(val); + return ret; } struct ctdb_db_statistics_wire { diff --git a/ctdb/tests/src/protocol_types_compat_test.c b/ctdb/tests/src/protocol_types_compat_test.c index 233bb16574f..cf9abf3b281 100644 --- a/ctdb/tests/src/protocol_types_compat_test.c +++ b/ctdb/tests/src/protocol_types_compat_test.c @@ -1917,6 +1917,66 @@ static int ctdb_statistics_list_pull_old(uint8_t *buf, size_t buflen, return 0; } +struct ctdb_key_data_wire { + uint32_t db_id; + struct ctdb_ltdb_header header; + uint32_t keylen; + uint8_t key[1]; +}; + +static size_t ctdb_key_data_len_old(struct ctdb_key_data *in) +{ + return offsetof(struct ctdb_key_data_wire, key) + in->key.dsize; +} + +static void ctdb_key_data_push_old(struct ctdb_key_data *in, uint8_t *buf) +{ + struct ctdb_key_data_wire *wire = (struct ctdb_key_data_wire *)buf; + + memcpy(wire, in, offsetof(struct ctdb_key_data, key)); + wire->keylen = in->key.dsize; + memcpy(wire->key, in->key.dptr, in->key.dsize); +} + +static int ctdb_key_data_pull_old(uint8_t *buf, size_t buflen, + TALLOC_CTX *mem_ctx, + struct ctdb_key_data **out) +{ + struct ctdb_key_data *val; + struct ctdb_key_data_wire *wire = (struct ctdb_key_data_wire *)buf; + + if (buflen < offsetof(struct ctdb_key_data_wire, key)) { + return EMSGSIZE; + } + if (wire->keylen > buflen) { + return EMSGSIZE; + } + if (offsetof(struct ctdb_key_data_wire, key) + wire->keylen < + offsetof(struct ctdb_key_data_wire, key)) { + return EMSGSIZE; + } + if (buflen < offsetof(struct ctdb_key_data_wire, key) + wire->keylen) { + return EMSGSIZE; + } + + val = talloc(mem_ctx, struct ctdb_key_data); + if (val == NULL) { + return ENOMEM; + } + + memcpy(val, wire, offsetof(struct ctdb_key_data, key)); + + val->key.dsize = wire->keylen; + val->key.dptr = talloc_memdup(val, wire->key, wire->keylen); + if (val->key.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); @@ -1954,6 +2014,7 @@ COMPAT_TYPE3_TEST(struct ctdb_iface, ctdb_iface); COMPAT_TYPE3_TEST(struct ctdb_iface_list, ctdb_iface_list); COMPAT_TYPE3_TEST(struct ctdb_public_ip_info, ctdb_public_ip_info); COMPAT_TYPE3_TEST(struct ctdb_statistics_list, ctdb_statistics_list); +COMPAT_TYPE3_TEST(struct ctdb_key_data, ctdb_key_data); int main(int argc, char *argv[]) { @@ -1996,6 +2057,7 @@ int main(int argc, char *argv[]) COMPAT_TEST_FUNC(ctdb_iface_list)(); COMPAT_TEST_FUNC(ctdb_public_ip_info)(); COMPAT_TEST_FUNC(ctdb_statistics_list)(); + COMPAT_TEST_FUNC(ctdb_key_data)(); return 0; } diff --git a/ctdb/tests/src/protocol_types_test.c b/ctdb/tests/src/protocol_types_test.c index 055f7a1e4fd..d1227615386 100644 --- a/ctdb/tests/src/protocol_types_test.c +++ b/ctdb/tests/src/protocol_types_test.c @@ -82,7 +82,7 @@ PROTOCOL_TYPE3_TEST(struct ctdb_iface, ctdb_iface); PROTOCOL_TYPE3_TEST(struct ctdb_iface_list, ctdb_iface_list); PROTOCOL_TYPE3_TEST(struct ctdb_public_ip_info, ctdb_public_ip_info); PROTOCOL_TYPE3_TEST(struct ctdb_statistics_list, ctdb_statistics_list); -DEFINE_TEST(struct ctdb_key_data, ctdb_key_data); +PROTOCOL_TYPE3_TEST(struct ctdb_key_data, ctdb_key_data); DEFINE_TEST(struct ctdb_db_statistics, ctdb_db_statistics); DEFINE_TEST(struct ctdb_election_message, ctdb_election_message); DEFINE_TEST(struct ctdb_srvid_message, ctdb_srvid_message);