2 CTDB protocol marshalling
4 Copyright (C) Amitay Isaacs 2015
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_api.h"
28 #include "protocol_private.h"
30 size_t ctdb_req_call_len(struct ctdb_req_header *h, struct ctdb_req_call *c)
32 return ctdb_req_header_len(h) +
33 ctdb_uint32_len(&c->flags) +
34 ctdb_uint32_len(&c->db_id) +
35 ctdb_uint32_len(&c->callid) +
36 ctdb_uint32_len(&c->hopcount) +
37 ctdb_tdb_datan_len(&c->key) +
38 ctdb_tdb_datan_len(&c->calldata);
41 int ctdb_req_call_push(struct ctdb_req_header *h, struct ctdb_req_call *c,
42 uint8_t *buf, size_t *buflen)
48 if (c->key.dsize == 0) {
52 length = ctdb_req_call_len(h, c);
53 if (*buflen < length) {
59 ctdb_req_header_push(h, buf+offset, &np);
62 ctdb_uint32_push(&c->flags, buf+offset, &np);
65 ctdb_uint32_push(&c->db_id, buf+offset, &np);
68 ctdb_uint32_push(&c->callid, buf+offset, &np);
71 ctdb_uint32_push(&c->hopcount, buf+offset, &np);
74 u32 = ctdb_tdb_data_len(&c->key);
75 ctdb_uint32_push(&u32, buf+offset, &np);
78 u32 = ctdb_tdb_data_len(&c->calldata);
79 ctdb_uint32_push(&u32, buf+offset, &np);
82 ctdb_tdb_data_push(&c->key, buf+offset, &np);
85 ctdb_tdb_data_push(&c->calldata, buf+offset, &np);
91 int ctdb_req_call_pull(uint8_t *buf, size_t buflen,
92 struct ctdb_req_header *h,
94 struct ctdb_req_call *c)
96 struct ctdb_req_header header;
97 size_t offset = 0, np;
101 ret = ctdb_req_header_pull(buf+offset, buflen-offset, &header, &np);
111 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &c->flags, &np);
117 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &c->db_id, &np);
123 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &c->callid, &np);
129 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &c->hopcount, &np);
135 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
142 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
147 c->calldata.dsize = u32;
149 if (buflen-offset < c->key.dsize) {
153 ret = ctdb_tdb_data_pull(buf+offset, c->key.dsize, mem_ctx, &c->key,
160 if (buflen-offset < c->calldata.dsize) {
164 ret = ctdb_tdb_data_pull(buf+offset, c->calldata.dsize,
165 mem_ctx, &c->calldata, &np);
174 size_t ctdb_reply_call_len(struct ctdb_req_header *h,
175 struct ctdb_reply_call *c)
177 return ctdb_req_header_len(h) +
178 ctdb_int32_len(&c->status) +
179 ctdb_tdb_datan_len(&c->data);
182 int ctdb_reply_call_push(struct ctdb_req_header *h, struct ctdb_reply_call *c,
183 uint8_t *buf, size_t *buflen)
185 size_t offset = 0, np;
188 length = ctdb_reply_call_len(h, c);
189 if (*buflen < length) {
195 ctdb_req_header_push(h, buf+offset, &np);
198 ctdb_int32_push(&c->status, buf+offset, &np);
201 ctdb_tdb_datan_push(&c->data, buf+offset, &np);
207 int ctdb_reply_call_pull(uint8_t *buf, size_t buflen,
208 struct ctdb_req_header *h,
210 struct ctdb_reply_call *c)
212 struct ctdb_req_header header;
213 size_t offset = 0, np;
216 ret = ctdb_req_header_pull(buf+offset, buflen-offset, &header, &np);
226 ret = ctdb_int32_pull(buf+offset, buflen-offset, &c->status, &np);
232 ret = ctdb_tdb_datan_pull(buf+offset, buflen-offset,
233 mem_ctx, &c->data, &np);
242 size_t ctdb_reply_error_len(struct ctdb_req_header *h,
243 struct ctdb_reply_error *c)
245 return ctdb_req_header_len(h) +
246 ctdb_int32_len(&c->status) +
247 ctdb_tdb_datan_len(&c->msg);
250 int ctdb_reply_error_push(struct ctdb_req_header *h, struct ctdb_reply_error *c,
251 uint8_t *buf, size_t *buflen)
253 size_t offset = 0, np;
256 length = ctdb_reply_error_len(h, c);
257 if (*buflen < length) {
263 ctdb_req_header_push(h, buf+offset, &np);
266 ctdb_int32_push(&c->status, buf+offset, &np);
269 ctdb_tdb_datan_push(&c->msg, buf+offset, &np);
275 int ctdb_reply_error_pull(uint8_t *buf, size_t buflen,
276 struct ctdb_req_header *h,
278 struct ctdb_reply_error *c)
280 struct ctdb_req_header header;
281 size_t offset = 0, np;
284 ret = ctdb_req_header_pull(buf+offset, buflen-offset, &header, &np);
294 ret = ctdb_int32_pull(buf+offset, buflen-offset, &c->status, &np);
300 ret = ctdb_tdb_datan_pull(buf+offset, buflen-offset, mem_ctx, &c->msg,
310 size_t ctdb_req_dmaster_len(struct ctdb_req_header *h,
311 struct ctdb_req_dmaster *c)
313 return ctdb_req_header_len(h) +
314 ctdb_uint32_len(&c->db_id) +
315 ctdb_padding_len(4) +
316 ctdb_uint64_len(&c->rsn) +
317 ctdb_uint32_len(&c->dmaster) +
318 ctdb_tdb_datan_len(&c->key) +
319 ctdb_tdb_datan_len(&c->data);
322 int ctdb_req_dmaster_push(struct ctdb_req_header *h, struct ctdb_req_dmaster *c,
323 uint8_t *buf, size_t *buflen)
325 size_t offset = 0, np;
329 length = ctdb_req_dmaster_len(h, c);
330 if (*buflen < length) {
336 ctdb_req_header_push(h, buf+offset, &np);
339 ctdb_uint32_push(&c->db_id, buf+offset, &np);
342 ctdb_padding_push(4, buf+offset, &np);
345 ctdb_uint64_push(&c->rsn, buf+offset, &np);
348 ctdb_uint32_push(&c->dmaster, buf+offset, &np);
351 u32 = ctdb_tdb_data_len(&c->key);
352 ctdb_uint32_push(&u32, buf+offset, &np);
355 u32 = ctdb_tdb_data_len(&c->data);
356 ctdb_uint32_push(&u32, buf+offset, &np);
359 ctdb_tdb_data_push(&c->key, buf+offset, &np);
362 ctdb_tdb_data_push(&c->data, buf+offset, &np);
368 int ctdb_req_dmaster_pull(uint8_t *buf, size_t buflen,
369 struct ctdb_req_header *h,
371 struct ctdb_req_dmaster *c)
373 struct ctdb_req_header header;
374 size_t offset = 0, np;
378 ret = ctdb_req_header_pull(buf+offset, buflen-offset, &header, &np);
388 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &c->db_id, &np);
394 ret = ctdb_padding_pull(buf+offset, buflen-offset, 4, &np);
400 ret = ctdb_uint64_pull(buf+offset, buflen-offset, &c->rsn, &np);
406 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &c->dmaster, &np);
412 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
419 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
426 if (buflen-offset < c->key.dsize) {
430 ret = ctdb_tdb_data_pull(buf+offset, c->key.dsize, mem_ctx, &c->key,
437 if (buflen-offset < c->data.dsize) {
441 ret = ctdb_tdb_data_pull(buf+offset, c->data.dsize, mem_ctx, &c->data,
451 size_t ctdb_reply_dmaster_len(struct ctdb_req_header *h,
452 struct ctdb_reply_dmaster *c)
454 return ctdb_req_header_len(h) +
455 ctdb_uint32_len(&c->db_id) +
456 ctdb_padding_len(4) +
457 ctdb_uint64_len(&c->rsn) +
458 ctdb_tdb_datan_len(&c->key) +
459 ctdb_tdb_datan_len(&c->data);
462 int ctdb_reply_dmaster_push(struct ctdb_req_header *h,
463 struct ctdb_reply_dmaster *c,
464 uint8_t *buf, size_t *buflen)
466 size_t offset = 0, np;
470 length = ctdb_reply_dmaster_len(h, c);
471 if (*buflen < length) {
477 ctdb_req_header_push(h, buf+offset, &np);
480 ctdb_uint32_push(&c->db_id, buf+offset, &np);
483 ctdb_padding_push(4, buf+offset, &np);
486 ctdb_uint64_push(&c->rsn, buf+offset, &np);
489 u32 = ctdb_tdb_data_len(&c->key);
490 ctdb_uint32_push(&u32, buf+offset, &np);
493 u32 = ctdb_tdb_data_len(&c->data);
494 ctdb_uint32_push(&u32, buf+offset, &np);
497 ctdb_tdb_data_push(&c->key, buf+offset, &np);
500 ctdb_tdb_data_push(&c->data, buf+offset, &np);
506 int ctdb_reply_dmaster_pull(uint8_t *buf, size_t buflen,
507 struct ctdb_req_header *h,
509 struct ctdb_reply_dmaster *c)
511 struct ctdb_req_header header;
512 size_t offset = 0, np;
516 ret = ctdb_req_header_pull(buf+offset, buflen-offset, &header, &np);
526 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &c->db_id, &np);
532 ret = ctdb_padding_pull(buf+offset, buflen-offset, 4, &np);
538 ret = ctdb_uint64_pull(buf+offset, buflen-offset, &c->rsn, &np);
544 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
551 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
558 if (buflen-offset < c->key.dsize) {
562 ret = ctdb_tdb_data_pull(buf+offset, c->key.dsize, mem_ctx, &c->key,
569 if (buflen-offset < c->data.dsize) {
573 ret = ctdb_tdb_data_pull(buf+offset, c->data.dsize, mem_ctx, &c->data,