diff options
author | Amitay Isaacs <amitay@gmail.com> | 2017-07-07 17:21:54 +1000 |
---|---|---|
committer | Martin Schwenke <martins@samba.org> | 2017-09-01 08:52:07 +0200 |
commit | 1787a4b63292aee0e9df4e4031188ac239d804d8 (patch) | |
tree | e8a30034ab27dff14ca078c8918cff031ce48bbc /ctdb/protocol | |
parent | 15c91774d943fba1985f33d10e24030264ce64c8 (diff) | |
download | samba-1787a4b63292aee0e9df4e4031188ac239d804d8.tar.gz |
ctdb-protocol: Add a generic packet header
This will avoid duplication when new daemons (and new client-server
protocols) are created out of the main ctdb daemon.
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
Diffstat (limited to 'ctdb/protocol')
-rw-r--r-- | ctdb/protocol/protocol.h | 9 | ||||
-rw-r--r-- | ctdb/protocol/protocol_api.h | 13 | ||||
-rw-r--r-- | ctdb/protocol/protocol_sock.c | 81 |
3 files changed, 103 insertions, 0 deletions
diff --git a/ctdb/protocol/protocol.h b/ctdb/protocol/protocol.h index 67a66c715c1..ad757d8c27d 100644 --- a/ctdb/protocol/protocol.h +++ b/ctdb/protocol/protocol.h @@ -1008,6 +1008,15 @@ struct ctdb_g_lock_list { }; /* + * Generic packet header + */ + +struct sock_packet_header { + uint32_t length; + uint32_t reqid; +}; + +/* * Eventd protocol */ diff --git a/ctdb/protocol/protocol_api.h b/ctdb/protocol/protocol_api.h index 522b009b151..22fc4483205 100644 --- a/ctdb/protocol/protocol_api.h +++ b/ctdb/protocol/protocol_api.h @@ -677,6 +677,19 @@ int ctdb_event_reply_pull(uint8_t *buf, size_t buflen, int ctdb_allocate_pkt(TALLOC_CTX *mem_ctx, size_t datalen, uint8_t **buf, size_t *buflen); +/* From protocol/protocol_sock.c */ + +size_t sock_packet_header_len(struct sock_packet_header *in); +void sock_packet_header_push(struct sock_packet_header *in, uint8_t *buf, + size_t *npush); +int sock_packet_header_pull(uint8_t *buf, size_t buflen, + struct sock_packet_header *out, size_t *npull); + +void sock_packet_header_set_reqid(struct sock_packet_header *h, + uint32_t reqid); +void sock_packet_header_set_length(struct sock_packet_header *h, + uint32_t length); + /* From protocol/protocol_util.c */ const char *ctdb_runstate_to_string(enum ctdb_runstate runstate); diff --git a/ctdb/protocol/protocol_sock.c b/ctdb/protocol/protocol_sock.c new file mode 100644 index 00000000000..e32f08767e7 --- /dev/null +++ b/ctdb/protocol/protocol_sock.c @@ -0,0 +1,81 @@ +/* + CTDB generic sock packet marshalling + + Copyright (C) Amitay Isaacs 2017 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ + +#include "replace.h" +#include "system/network.h" + +#include <talloc.h> + +#include "protocol.h" +#include "protocol_private.h" +#include "protocol_api.h" + +size_t sock_packet_header_len(struct sock_packet_header *in) +{ + return ctdb_uint32_len(&in->length) + + ctdb_uint32_len(&in->reqid); +} + +void sock_packet_header_push(struct sock_packet_header *in, uint8_t *buf, + size_t *npush) +{ + size_t offset = 0, np; + + ctdb_uint32_push(&in->length, buf+offset, &np); + offset += np; + + ctdb_uint32_push(&in->reqid, buf+offset, &np); + offset += np; + + *npush = offset; +} + +int sock_packet_header_pull(uint8_t *buf, size_t buflen, + struct sock_packet_header *out, size_t *npull) +{ + size_t offset = 0, np; + int ret; + + ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->length, &np); + if (ret != 0) { + return ret; + } + offset += np; + + ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->reqid, &np); + if (ret != 0) { + return ret; + } + offset += np; + + *npull = offset; + return 0; +} + +void sock_packet_header_set_reqid(struct sock_packet_header *h, + uint32_t reqid) +{ + h->reqid = reqid; +} + +void sock_packet_header_set_length(struct sock_packet_header *h, + uint32_t length) +{ + h->length = length; +} |