wscript: separate embedded_heimdal from system_heimdal
[Samba.git] / ctdb / protocol / protocol_basic.c
blob94b18c1cd109cf3f9a9ccca16a61318ffd6ef67c
1 /*
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/>.
20 #include "replace.h"
21 #include "system/network.h"
23 #include <talloc.h>
25 #include "protocol_basic.h"
28 * Basic data types
31 size_t ctdb_uint8_len(uint8_t *in)
33 return sizeof(uint8_t);
36 void ctdb_uint8_push(uint8_t *in, uint8_t *buf, size_t *npush)
38 *buf = *in;
39 *npush = sizeof(uint8_t);
42 int ctdb_uint8_pull(uint8_t *buf, size_t buflen, uint8_t *out, size_t *npull)
44 if (buflen < sizeof(uint8_t)) {
45 return EMSGSIZE;
48 *out = *buf;
49 *npull = sizeof(uint8_t);
50 return 0;
53 size_t ctdb_uint16_len(uint16_t *in)
55 return sizeof(uint16_t);
58 void ctdb_uint16_push(uint16_t *in, uint8_t *buf, size_t *npush)
60 memcpy(buf, in, sizeof(uint16_t));
61 *npush = sizeof(uint16_t);
64 int ctdb_uint16_pull(uint8_t *buf, size_t buflen, uint16_t *out, size_t *npull)
66 if (buflen < sizeof(uint16_t)) {
67 return EMSGSIZE;
70 memcpy(out, buf, sizeof(uint16_t));
71 *npull = sizeof(uint16_t);
72 return 0;
75 size_t ctdb_int32_len(int32_t *in)
77 return sizeof(int32_t);
80 void ctdb_int32_push(int32_t *in, uint8_t *buf, size_t *npush)
82 memcpy(buf, in, sizeof(int32_t));
83 *npush = sizeof(int32_t);
86 int ctdb_int32_pull(uint8_t *buf, size_t buflen, int32_t *out, size_t *npull)
88 if (buflen < sizeof(int32_t)) {
89 return EMSGSIZE;
92 memcpy(out, buf, sizeof(int32_t));
93 *npull = sizeof(int32_t);
94 return 0;
97 size_t ctdb_uint32_len(uint32_t *in)
99 return sizeof(uint32_t);
102 void ctdb_uint32_push(uint32_t *in, uint8_t *buf, size_t *npush)
104 memcpy(buf, in, sizeof(uint32_t));
105 *npush = sizeof(uint32_t);
108 int ctdb_uint32_pull(uint8_t *buf, size_t buflen, uint32_t *out, size_t *npull)
110 if (buflen < sizeof(uint32_t)) {
111 return EMSGSIZE;
114 memcpy(out, buf, sizeof(uint32_t));
115 *npull = sizeof(uint32_t);
116 return 0;
119 size_t ctdb_uint64_len(uint64_t *in)
121 return sizeof(uint64_t);
124 void ctdb_uint64_push(uint64_t *in, uint8_t *buf, size_t *npush)
126 memcpy(buf, in, sizeof(uint64_t));
127 *npush = sizeof(uint64_t);
130 int ctdb_uint64_pull(uint8_t *buf, size_t buflen, uint64_t *out, size_t *npull)
132 if (buflen < sizeof(uint64_t)) {
133 return EMSGSIZE;
136 memcpy(out, buf, sizeof(uint64_t));
137 *npull = sizeof(uint64_t);
138 return 0;
141 size_t ctdb_double_len(double *in)
143 return sizeof(double);
146 void ctdb_double_push(double *in, uint8_t *buf, size_t *npush)
148 memcpy(buf, in, sizeof(double));
149 *npush = sizeof(double);
152 int ctdb_double_pull(uint8_t *buf, size_t buflen, double *out, size_t *npull)
154 if (buflen < sizeof(double)) {
155 return EMSGSIZE;
158 memcpy(out, buf, sizeof(double));
159 *npull = sizeof(double);
160 return 0;
163 size_t ctdb_bool_len(bool *in)
165 uint8_t u8 = *in;
167 return ctdb_uint8_len(&u8);
170 void ctdb_bool_push(bool *in, uint8_t *buf, size_t *npush)
172 size_t np;
173 uint8_t u8 = *in;
175 ctdb_uint8_push(&u8, buf, &np);
176 *npush = np;
179 int ctdb_bool_pull(uint8_t *buf, size_t buflen, bool *out, size_t *npull)
181 size_t np;
182 uint8_t u8;
183 int ret;
185 ret = ctdb_uint8_pull(buf, buflen, &u8, &np);
186 if (ret != 0) {
187 return ret;
190 if (u8 == 0) {
191 *out = false;
192 } else if (u8 == 1) {
193 *out = true;
194 } else {
195 return EINVAL;
198 *npull = np;
199 return 0;
202 size_t ctdb_chararray_len(char *in, size_t len)
204 return len;
207 void ctdb_chararray_push(char *in, size_t len, uint8_t *buf, size_t *npush)
209 memcpy(buf, in, len);
210 *npush = len;
213 int ctdb_chararray_pull(uint8_t *buf, size_t buflen, char *out, size_t len,
214 size_t *npull)
216 if (buflen < len) {
217 return EMSGSIZE;
220 memcpy(out, buf, len);
221 out[len-1] = '\0';
222 *npull = len;
223 return 0;
226 size_t ctdb_string_len(const char **in)
228 if (*in == NULL) {
229 return 0;
232 return strlen(*in) + 1;
235 void ctdb_string_push(const char **in, uint8_t *buf, size_t *npush)
237 size_t len;
239 len = ctdb_string_len(in);
240 if (len > 0) {
241 memcpy(buf, *in, len);
244 *npush = len;
247 int ctdb_string_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
248 const char **out, size_t *npull)
250 const char *str;
252 if (buflen > UINT32_MAX) {
253 return EMSGSIZE;
256 if (buflen == 0) {
257 *out = NULL;
258 *npull = 0;
259 return 0;
262 str = talloc_strndup(mem_ctx, (char *)buf, buflen);
263 if (str == NULL) {
264 return ENOMEM;
267 *out = str;
268 *npull = ctdb_string_len(&str);
269 return 0;
272 size_t ctdb_stringn_len(const char **in)
274 uint32_t u32 = ctdb_string_len(in);
276 return ctdb_uint32_len(&u32) + u32;
279 void ctdb_stringn_push(const char **in, uint8_t *buf, size_t *npush)
281 size_t offset = 0, np;
282 uint32_t u32 = ctdb_string_len(in);
284 ctdb_uint32_push(&u32, buf+offset, &np);
285 offset += np;
287 ctdb_string_push(in, buf+offset, &np);
288 offset += np;
290 *npush = offset;
293 int ctdb_stringn_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
294 const char **out, size_t *npull)
296 size_t offset = 0, np;
297 uint32_t u32;
298 int ret;
300 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
301 if (ret != 0) {
302 return ret;
304 offset += np;
306 if (buflen-offset < u32) {
307 return EMSGSIZE;
310 ret = ctdb_string_pull(buf+offset, u32, mem_ctx, out, &np);
311 if (ret != 0) {
312 return ret;
314 offset += np;
316 *npull = offset;
317 return 0;
321 * System defined data types
324 size_t ctdb_pid_len(pid_t *in)
326 return sizeof(pid_t);
329 void ctdb_pid_push(pid_t *in, uint8_t *buf, size_t *npush)
331 memcpy(buf, in, sizeof(pid_t));
332 *npush = sizeof(pid_t);
335 int ctdb_pid_pull(uint8_t *buf, size_t buflen, pid_t *out, size_t *npull)
337 if (buflen < sizeof(pid_t)) {
338 return EMSGSIZE;
341 memcpy(out, buf, sizeof(pid_t));
342 *npull = sizeof(pid_t);
343 return 0;
346 size_t ctdb_timeval_len(struct timeval *in)
348 return sizeof(struct timeval);
351 void ctdb_timeval_push(struct timeval *in, uint8_t *buf, size_t *npush)
353 memcpy(buf, in, sizeof(struct timeval));
354 *npush = sizeof(struct timeval);
357 int ctdb_timeval_pull(uint8_t *buf, size_t buflen, struct timeval *out,
358 size_t *npull)
360 if (buflen < sizeof(struct timeval)) {
361 return EMSGSIZE;
364 memcpy(out, buf, sizeof(struct timeval));
365 *npull = sizeof(struct timeval);
366 return 0;
370 * Dummy type to tackle structure padding
373 size_t ctdb_padding_len(int count)
375 return count % SIZEOF_VOID_P;
378 void ctdb_padding_push(int count, uint8_t *buf, size_t *npush)
380 uint8_t padding[count];
381 size_t aligned_count = count % SIZEOF_VOID_P;
383 if (aligned_count > 0) {
384 memset(padding, 0, aligned_count);
385 memcpy(buf, padding, aligned_count);
387 *npush = aligned_count;
390 int ctdb_padding_pull(uint8_t *buf, size_t buflen, int count, size_t *npull)
392 size_t aligned_count = count % SIZEOF_VOID_P;
394 if (buflen < aligned_count) {
395 return EMSGSIZE;
398 *npull = aligned_count;
399 return 0;