2 CTDB protocol marshalling
4 Copyright (C) Amitay Isaacs 2015-2017
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/network.h"
27 #include "protocol_private.h"
33 size_t ctdb_uint8_len(uint8_t *in)
35 return sizeof(uint8_t);
38 void ctdb_uint8_push(uint8_t *in, uint8_t *buf, size_t *npush)
41 *npush = sizeof(uint8_t);
44 int ctdb_uint8_pull(uint8_t *buf, size_t buflen, uint8_t *out, size_t *npull)
46 if (buflen < sizeof(uint8_t)) {
51 *npull = sizeof(uint8_t);
55 size_t ctdb_uint16_len(uint16_t *in)
57 return sizeof(uint16_t);
60 void ctdb_uint16_push(uint16_t *in, uint8_t *buf, size_t *npush)
62 memcpy(buf, in, sizeof(uint16_t));
63 *npush = sizeof(uint16_t);
66 int ctdb_uint16_pull(uint8_t *buf, size_t buflen, uint16_t *out, size_t *npull)
68 if (buflen < sizeof(uint16_t)) {
72 memcpy(out, buf, sizeof(uint16_t));
73 *npull = sizeof(uint16_t);
77 size_t ctdb_int32_len(int32_t *in)
79 return sizeof(int32_t);
82 void ctdb_int32_push(int32_t *in, uint8_t *buf, size_t *npush)
84 memcpy(buf, in, sizeof(int32_t));
85 *npush = sizeof(int32_t);
88 int ctdb_int32_pull(uint8_t *buf, size_t buflen, int32_t *out, size_t *npull)
90 if (buflen < sizeof(int32_t)) {
94 memcpy(out, buf, sizeof(int32_t));
95 *npull = sizeof(int32_t);
99 size_t ctdb_uint32_len(uint32_t *in)
101 return sizeof(uint32_t);
104 void ctdb_uint32_push(uint32_t *in, uint8_t *buf, size_t *npush)
106 memcpy(buf, in, sizeof(uint32_t));
107 *npush = sizeof(uint32_t);
110 int ctdb_uint32_pull(uint8_t *buf, size_t buflen, uint32_t *out, size_t *npull)
112 if (buflen < sizeof(uint32_t)) {
116 memcpy(out, buf, sizeof(uint32_t));
117 *npull = sizeof(uint32_t);
121 size_t ctdb_uint64_len(uint64_t *in)
123 return sizeof(uint64_t);
126 void ctdb_uint64_push(uint64_t *in, uint8_t *buf, size_t *npush)
128 memcpy(buf, in, sizeof(uint64_t));
129 *npush = sizeof(uint64_t);
132 int ctdb_uint64_pull(uint8_t *buf, size_t buflen, uint64_t *out, size_t *npull)
134 if (buflen < sizeof(uint64_t)) {
138 memcpy(out, buf, sizeof(uint64_t));
139 *npull = sizeof(uint64_t);
143 size_t ctdb_double_len(double *in)
145 return sizeof(double);
148 void ctdb_double_push(double *in, uint8_t *buf, size_t *npush)
150 memcpy(buf, in, sizeof(double));
151 *npush = sizeof(double);
154 int ctdb_double_pull(uint8_t *buf, size_t buflen, double *out, size_t *npull)
156 if (buflen < sizeof(double)) {
160 memcpy(out, buf, sizeof(double));
161 *npull = sizeof(double);
165 size_t ctdb_bool_len(bool *in)
169 return ctdb_uint8_len(&u8);
172 void ctdb_bool_push(bool *in, uint8_t *buf, size_t *npush)
177 ctdb_uint8_push(&u8, buf, &np);
181 int ctdb_bool_pull(uint8_t *buf, size_t buflen, bool *out, size_t *npull)
187 ret = ctdb_uint8_pull(buf, buflen, &u8, &np);
194 } else if (u8 == 1) {
204 size_t ctdb_chararray_len(char *in, size_t len)
209 void ctdb_chararray_push(char *in, size_t len, uint8_t *buf, size_t *npush)
211 memcpy(buf, in, len);
215 int ctdb_chararray_pull(uint8_t *buf, size_t buflen, char *out, size_t len,
222 memcpy(out, buf, len);
228 size_t ctdb_string_len(const char **in)
234 return strlen(*in) + 1;
237 void ctdb_string_push(const char **in, uint8_t *buf, size_t *npush)
241 len = ctdb_string_len(in);
243 memcpy(buf, *in, len);
249 int ctdb_string_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
250 const char **out, size_t *npull)
254 if (buflen > UINT32_MAX) {
264 str = talloc_strndup(mem_ctx, (char *)buf, buflen);
270 *npull = ctdb_string_len(&str);
274 size_t ctdb_stringn_len(const char **in)
276 uint32_t u32 = ctdb_string_len(in);
278 return ctdb_uint32_len(&u32) + u32;
281 void ctdb_stringn_push(const char **in, uint8_t *buf, size_t *npush)
283 size_t offset = 0, np;
284 uint32_t u32 = ctdb_string_len(in);
286 ctdb_uint32_push(&u32, buf+offset, &np);
289 ctdb_string_push(in, buf+offset, &np);
295 int ctdb_stringn_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
296 const char **out, size_t *npull)
298 size_t offset = 0, np;
302 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
308 if (buflen-offset < u32) {
312 ret = ctdb_string_pull(buf+offset, u32, mem_ctx, out, &np);
323 * System defined data types
326 size_t ctdb_pid_len(pid_t pid)
328 return sizeof(pid_t);
331 void ctdb_pid_push(pid_t pid, uint8_t *buf)
333 memcpy(buf, &pid, sizeof(pid_t));
336 int ctdb_pid_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
339 if (buflen < sizeof(pid_t)) {
343 *out = *(pid_t *)buf;