summaryrefslogtreecommitdiff
path: root/ctdb/protocol/protocol_basic.c
diff options
context:
space:
mode:
authorAmitay Isaacs <amitay@gmail.com>2017-07-13 17:28:42 +1000
committerMartin Schwenke <martins@samba.org>2017-08-30 14:59:21 +0200
commit73748776d988d5d3cbc07fe1691e6c2c632a0f3b (patch)
tree5dcca0256e41995e864ac31bcfe95f603422126b /ctdb/protocol/protocol_basic.c
parenta5a2243f073f00bbfd7692fb2fe68ea79830ae0d (diff)
downloadsamba-73748776d988d5d3cbc07fe1691e6c2c632a0f3b.tar.gz
ctdb-protocol: Separate marshalling for basic data types
This splits protocol_types.c and creates new protocol_basic.c. Signed-off-by: Amitay Isaacs <amitay@gmail.com> Reviewed-by: Martin Schwenke <martin@meltin.net>
Diffstat (limited to 'ctdb/protocol/protocol_basic.c')
-rw-r--r--ctdb/protocol/protocol_basic.c226
1 files changed, 226 insertions, 0 deletions
diff --git a/ctdb/protocol/protocol_basic.c b/ctdb/protocol/protocol_basic.c
new file mode 100644
index 00000000000..75c786d9675
--- /dev/null
+++ b/ctdb/protocol/protocol_basic.c
@@ -0,0 +1,226 @@
+/*
+ CTDB protocol marshalling
+
+ Copyright (C) Amitay Isaacs 2015-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 <tdb.h>
+
+#include "protocol.h"
+#include "protocol_private.h"
+
+/*
+ * Basic data types
+ */
+
+size_t ctdb_int32_len(int32_t val)
+{
+ return sizeof(int32_t);
+}
+
+void ctdb_int32_push(int32_t val, uint8_t *buf)
+{
+ memcpy(buf, &val, sizeof(int32_t));
+}
+
+int ctdb_int32_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
+ int32_t *out)
+{
+ if (buflen < sizeof(int32_t)) {
+ return EMSGSIZE;
+ }
+
+ *out = *(int32_t *)buf;
+ return 0;
+}
+
+size_t ctdb_uint32_len(uint32_t val)
+{
+ return sizeof(uint32_t);
+}
+
+void ctdb_uint32_push(uint32_t val, uint8_t *buf)
+{
+ memcpy(buf, &val, sizeof(uint32_t));
+}
+
+int ctdb_uint32_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
+ uint32_t *out)
+{
+ if (buflen < sizeof(uint32_t)) {
+ return EMSGSIZE;
+ }
+
+ *out = *(uint32_t *)buf;
+ return 0;
+}
+
+size_t ctdb_uint64_len(uint64_t val)
+{
+ return sizeof(uint64_t);
+}
+
+void ctdb_uint64_push(uint64_t val, uint8_t *buf)
+{
+ memcpy(buf, &val, sizeof(uint64_t));
+}
+
+int ctdb_uint64_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
+ uint64_t *out)
+{
+ if (buflen < sizeof(uint64_t)) {
+ return EMSGSIZE;
+ }
+
+ *out = *(uint64_t *)buf;
+ return 0;
+}
+
+size_t ctdb_double_len(double val)
+{
+ return sizeof(double);
+}
+
+void ctdb_double_push(double val, uint8_t *buf)
+{
+ memcpy(buf, &val, sizeof(double));
+}
+
+int ctdb_double_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
+ double *out)
+{
+ if (buflen < sizeof(double)) {
+ return EMSGSIZE;
+ }
+
+ *out = *(double *)buf;
+ return 0;
+}
+
+size_t ctdb_string_len(const char *str)
+{
+ if (str == NULL) {
+ return 0;
+ }
+ return strlen(str) + 1;
+}
+
+void ctdb_string_push(const char *str, uint8_t *buf)
+{
+ if (str == NULL) {
+ return;
+ }
+ memcpy(buf, str, strlen(str)+1);
+}
+
+int ctdb_string_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
+ const char **out)
+{
+ char *str;
+
+ if (buflen == 0) {
+ *out = NULL;
+ return 0;
+ }
+
+ str = talloc_strndup(mem_ctx, (char *)buf, buflen);
+ if (str == NULL) {
+ return ENOMEM;
+ }
+
+ *out = str;
+ return 0;
+}
+
+struct stringn_wire {
+ uint32_t length;
+ uint8_t str[1];
+};
+
+size_t ctdb_stringn_len(const char *str)
+{
+ return sizeof(uint32_t) + ctdb_string_len(str);
+}
+
+void ctdb_stringn_push(const char *str, uint8_t *buf)
+{
+ struct stringn_wire *wire = (struct stringn_wire *)buf;
+
+ wire->length = ctdb_string_len(str);
+ ctdb_string_push(str, wire->str);
+}
+
+int ctdb_stringn_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
+ const char **out)
+{
+ char *str;
+ struct stringn_wire *wire = (struct stringn_wire *)buf;
+
+ if (buflen < sizeof(uint32_t)) {
+ return EMSGSIZE;
+ }
+ if (wire->length > buflen) {
+ return EMSGSIZE;
+ }
+ if (sizeof(uint32_t) + wire->length < sizeof(uint32_t)) {
+ return EMSGSIZE;
+ }
+ if (buflen < sizeof(uint32_t) + wire->length) {
+ return EMSGSIZE;
+ }
+
+ if (wire->length == 0) {
+ *out = NULL;
+ return 0;
+ }
+
+ str = talloc_strndup(mem_ctx, (char *)wire->str, wire->length);
+ if (str == NULL) {
+ return ENOMEM;
+ }
+
+ *out = str;
+ return 0;
+}
+
+/*
+ * System defined data types
+ */
+
+size_t ctdb_pid_len(pid_t pid)
+{
+ return sizeof(pid_t);
+}
+
+void ctdb_pid_push(pid_t pid, uint8_t *buf)
+{
+ memcpy(buf, &pid, sizeof(pid_t));
+}
+
+int ctdb_pid_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
+ pid_t *out)
+{
+ if (buflen < sizeof(pid_t)) {
+ return EMSGSIZE;
+ }
+
+ *out = *(pid_t *)buf;
+ return 0;
+}