1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-22 22:04:08 +03:00

ctdb-protocol: Fix marshalling for ctdb_key_data

Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
This commit is contained in:
Amitay Isaacs 2017-07-12 19:05:13 +10:00 committed by Martin Schwenke
parent 132f201fc1
commit 819b6310cb
5 changed files with 124 additions and 47 deletions

View File

@ -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:

View File

@ -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);

View File

@ -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 {

View File

@ -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;
}

View File

@ -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);