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"
25 #include "protocol_basic.h"
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
)
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)) {
49 *npull
= sizeof(uint8_t);
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)) {
70 memcpy(out
, buf
, sizeof(uint16_t));
71 *npull
= sizeof(uint16_t);
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)) {
92 memcpy(out
, buf
, sizeof(int32_t));
93 *npull
= sizeof(int32_t);
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)) {
114 memcpy(out
, buf
, sizeof(uint32_t));
115 *npull
= sizeof(uint32_t);
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)) {
136 memcpy(out
, buf
, sizeof(uint64_t));
137 *npull
= sizeof(uint64_t);
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)) {
158 memcpy(out
, buf
, sizeof(double));
159 *npull
= sizeof(double);
163 size_t ctdb_bool_len(bool *in
)
167 return ctdb_uint8_len(&u8
);
170 void ctdb_bool_push(bool *in
, uint8_t *buf
, size_t *npush
)
175 ctdb_uint8_push(&u8
, buf
, &np
);
179 int ctdb_bool_pull(uint8_t *buf
, size_t buflen
, bool *out
, size_t *npull
)
185 ret
= ctdb_uint8_pull(buf
, buflen
, &u8
, &np
);
192 } else if (u8
== 1) {
202 size_t ctdb_chararray_len(char *in
, size_t len
)
207 void ctdb_chararray_push(char *in
, size_t len
, uint8_t *buf
, size_t *npush
)
209 memcpy(buf
, in
, len
);
213 int ctdb_chararray_pull(uint8_t *buf
, size_t buflen
, char *out
, size_t len
,
220 memcpy(out
, buf
, len
);
226 size_t ctdb_string_len(const char **in
)
232 return strlen(*in
) + 1;
235 void ctdb_string_push(const char **in
, uint8_t *buf
, size_t *npush
)
239 len
= ctdb_string_len(in
);
241 memcpy(buf
, *in
, len
);
247 int ctdb_string_pull(uint8_t *buf
, size_t buflen
, TALLOC_CTX
*mem_ctx
,
248 const char **out
, size_t *npull
)
252 if (buflen
> UINT32_MAX
) {
262 str
= talloc_strndup(mem_ctx
, (char *)buf
, buflen
);
268 *npull
= ctdb_string_len(&str
);
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
);
287 ctdb_string_push(in
, buf
+offset
, &np
);
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
;
300 ret
= ctdb_uint32_pull(buf
+offset
, buflen
-offset
, &u32
, &np
);
306 if (buflen
-offset
< u32
) {
310 ret
= ctdb_string_pull(buf
+offset
, u32
, mem_ctx
, out
, &np
);
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
)) {
341 memcpy(out
, buf
, sizeof(pid_t
));
342 *npull
= sizeof(pid_t
);
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
,
360 if (buflen
< sizeof(struct timeval
)) {
364 memcpy(out
, buf
, sizeof(struct timeval
));
365 *npull
= sizeof(struct timeval
);
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
) {
398 *npull
= aligned_count
;