ctdb-protocol: Add a generic packet header
authorAmitay Isaacs <amitay@gmail.com>
Fri, 7 Jul 2017 07:21:54 +0000 (17:21 +1000)
committerMartin Schwenke <martins@samba.org>
Fri, 1 Sep 2017 06:52:07 +0000 (08:52 +0200)
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>
ctdb/protocol/protocol.h
ctdb/protocol/protocol_api.h
ctdb/protocol/protocol_sock.c [new file with mode: 0644]
ctdb/tests/src/protocol_common.c
ctdb/tests/src/protocol_common.h
ctdb/tests/src/protocol_types_test.c
ctdb/wscript

index 67a66c715c1fbf02b0a09e389aabb82bf073d263..ad757d8c27df07d12a1e3c3b8498c9ebbb599454 100644 (file)
@@ -1007,6 +1007,15 @@ struct ctdb_g_lock_list {
        struct ctdb_g_lock *lock;
 };
 
+/*
+ * Generic packet header
+ */
+
+struct sock_packet_header {
+       uint32_t length;
+       uint32_t reqid;
+};
+
 /*
  * Eventd protocol
  */
index 522b009b151dada54db7c64222fc5492a763ee31..22fc448320541efa4ffcd3af0f5a5cf3096f2a35 100644 (file)
@@ -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 (file)
index 0000000..e32f087
--- /dev/null
@@ -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;
+}
index f5fe3c7901bfc476e8096191ceae7578b3e2ea70..e9ba658259c232ef051ed2ba045f58bf701dc5a9 100644 (file)
@@ -1383,3 +1383,16 @@ void verify_ctdb_g_lock_list(struct ctdb_g_lock_list *p1,
                verify_ctdb_g_lock(&p1->lock[i], &p2->lock[i]);
        }
 }
+
+void fill_sock_packet_header(struct sock_packet_header *p)
+{
+       p->length = rand32();
+       p->reqid = rand32();
+}
+
+void verify_sock_packet_header(struct sock_packet_header *p1,
+                              struct sock_packet_header *p2)
+{
+       assert(p1->length == p2->length);
+       assert(p1->reqid == p2->reqid);
+}
index 5a270328b5fe64d9f53ad5e2855eb6131f558143..01b1e34a440e7e58b67f12d2f2d02b27b134dfff 100644 (file)
@@ -350,4 +350,8 @@ void fill_ctdb_g_lock_list(TALLOC_CTX *mem_ctx, struct ctdb_g_lock_list *p);
 void verify_ctdb_g_lock_list(struct ctdb_g_lock_list *p1,
                             struct ctdb_g_lock_list *p2);
 
+void fill_sock_packet_header(struct sock_packet_header *p);
+void verify_sock_packet_header(struct sock_packet_header *p1,
+                              struct sock_packet_header *p2);
+
 #endif /* __CTDB_PROTOCOL_COMMON_H__ */
index 15ccd49999f9f23a4fce5c65e823dbc68f251452..7dbc99edfc69671996942d994929256c6ba19e80 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "protocol/protocol_basic.c"
 #include "protocol/protocol_types.c"
+#include "protocol/protocol_sock.c"
 
 #include "tests/src/protocol_common.h"
 
@@ -75,6 +76,8 @@ PROTOCOL_TYPE1_TEST(struct ctdb_server_id, ctdb_server_id);
 PROTOCOL_TYPE1_TEST(struct ctdb_g_lock, ctdb_g_lock);
 PROTOCOL_TYPE3_TEST(struct ctdb_g_lock_list, ctdb_g_lock_list);
 
+PROTOCOL_TYPE1_TEST(struct sock_packet_header, sock_packet_header);
+
 static void test_ctdb_rec_buffer_read_write(void)
 {
        TALLOC_CTX *mem_ctx = talloc_new(NULL);
@@ -176,6 +179,8 @@ int main(int argc, char *argv[])
        TEST_FUNC(ctdb_g_lock)();
        TEST_FUNC(ctdb_g_lock_list)();
 
+       TEST_FUNC(sock_packet_header)();
+
        test_ctdb_rec_buffer_read_write();
 
        return 0;
index a35b138eba4c42dbd81e0bdb0bbf53db4100b9d0..2a686d084241e2b681a785d9b73e8200a6cbcbe9 100644 (file)
@@ -400,7 +400,8 @@ def build(bld):
                                              protocol_client.c
                                              protocol_debug.c
                                              protocol_util.c
-                                             protocol_event.c'''),
+                                             protocol_event.c
+                                             protocol_sock.c'''),
                         includes='include',
                         deps='replace talloc tdb')