2 CTDB eventd protocol marshalling
4 Copyright (C) Amitay Isaacs 2016
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"
26 #include "protocol_private.h"
27 #include "protocol_api.h"
29 static size_t ctdb_event_len(enum ctdb_event in)
33 return ctdb_uint32_len(&u32);
36 static void ctdb_event_push(enum ctdb_event in, uint8_t *buf, size_t *npush)
41 ctdb_uint32_push(&u32, buf, &np);
46 static int ctdb_event_pull(uint8_t *buf, size_t buflen,
47 TALLOC_CTX *mem_ctx, enum ctdb_event *out,
50 uint32_t uint32_value;
51 enum ctdb_event value;
55 ret = ctdb_uint32_pull(buf, buflen, &uint32_value, &np);
60 switch (uint32_value) {
62 value = CTDB_EVENT_INIT;
66 value = CTDB_EVENT_SETUP;
70 value = CTDB_EVENT_STARTUP;
74 value = CTDB_EVENT_START_RECOVERY;
78 value = CTDB_EVENT_RECOVERED;
82 value = CTDB_EVENT_TAKE_IP;
86 value = CTDB_EVENT_RELEASE_IP;
90 value = CTDB_EVENT_STOPPED;
94 value = CTDB_EVENT_MONITOR;
98 value = CTDB_EVENT_STATUS;
102 value = CTDB_EVENT_SHUTDOWN;
106 value = CTDB_EVENT_RELOAD;
110 value = CTDB_EVENT_UPDATE_IP;
114 value = CTDB_EVENT_IPREALLOCATED;
126 static size_t ctdb_event_command_len(enum ctdb_event_command in)
130 return ctdb_uint32_len(&u32);
133 static void ctdb_event_command_push(enum ctdb_event_command in, uint8_t *buf)
138 ctdb_uint32_push(&u32, buf, &np);
141 static int ctdb_event_command_pull(uint8_t *buf, size_t buflen,
143 enum ctdb_event_command *out)
145 uint32_t uint32_value;
146 enum ctdb_event_command value;
150 ret = ctdb_uint32_pull(buf, buflen, &uint32_value, &np);
155 switch (uint32_value) {
157 value = CTDB_EVENT_COMMAND_RUN;
161 value = CTDB_EVENT_COMMAND_STATUS;
165 value = CTDB_EVENT_COMMAND_SCRIPT_LIST;
169 value = CTDB_EVENT_COMMAND_SCRIPT_ENABLE;
173 value = CTDB_EVENT_COMMAND_SCRIPT_DISABLE;
184 static size_t ctdb_event_status_state_len(enum ctdb_event_status_state in)
188 return ctdb_uint32_len(&u32);
191 static void ctdb_event_status_state_push(enum ctdb_event_status_state in,
197 ctdb_uint32_push(&u32, buf, &np);
200 static int ctdb_event_status_state_pull(uint8_t *buf, size_t buflen,
202 enum ctdb_event_status_state *out)
204 uint32_t uint32_value;
205 enum ctdb_event_status_state value;
209 ret = ctdb_uint32_pull(buf, buflen, &uint32_value, &np);
214 switch (uint32_value) {
216 value = CTDB_EVENT_LAST_RUN;
220 value = CTDB_EVENT_LAST_PASS;
224 value = CTDB_EVENT_LAST_FAIL;
235 static size_t ctdb_event_request_run_len(struct ctdb_event_request_run *in)
237 return ctdb_event_len(in->event) +
238 ctdb_uint32_len(&in->timeout) +
239 ctdb_stringn_len(&in->arg_str);
242 static void ctdb_event_request_run_push(struct ctdb_event_request_run *in,
245 size_t offset = 0, np;
247 ctdb_event_push(in->event, buf, &np);
250 ctdb_uint32_push(&in->timeout, buf+offset, &np);
253 ctdb_stringn_push(&in->arg_str, buf+offset, &np);
256 static int ctdb_event_request_run_pull(uint8_t *buf, size_t buflen,
258 struct ctdb_event_request_run **out)
260 struct ctdb_event_request_run *rdata;
261 size_t offset = 0, np;
264 rdata = talloc(mem_ctx, struct ctdb_event_request_run);
269 ret = ctdb_event_pull(buf, buflen, rdata, &rdata->event, &np);
275 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &rdata->timeout,
282 ret = ctdb_stringn_pull(buf+offset, buflen-offset,
283 rdata, &rdata->arg_str, &np);
296 static size_t ctdb_event_request_status_len(
297 struct ctdb_event_request_status *in)
299 return ctdb_event_len(in->event) +
300 ctdb_event_status_state_len(in->state);
303 static void ctdb_event_request_status_push(
304 struct ctdb_event_request_status *in,
307 size_t offset = 0, np;
309 ctdb_event_push(in->event, buf, &np);
312 ctdb_event_status_state_push(in->state, buf+offset);
315 static int ctdb_event_request_status_pull(
316 uint8_t *buf, size_t buflen,
318 struct ctdb_event_request_status **out)
320 struct ctdb_event_request_status *rdata;
321 size_t offset = 0, np;
324 rdata = talloc(mem_ctx, struct ctdb_event_request_status);
329 ret = ctdb_event_pull(buf, buflen, rdata, &rdata->event, &np);
336 ret = ctdb_event_status_state_pull(buf+offset, buflen-offset,
337 rdata, &rdata->state);
347 static size_t ctdb_event_request_script_enable_len(
348 struct ctdb_event_request_script_enable *in)
350 return ctdb_stringn_len(&in->script_name);
353 static void ctdb_event_request_script_enable_push(
354 struct ctdb_event_request_script_enable *in,
359 ctdb_stringn_push(&in->script_name, buf, &np);
362 static int ctdb_event_request_script_enable_pull(
363 uint8_t *buf, size_t buflen,
365 struct ctdb_event_request_script_enable **out)
367 struct ctdb_event_request_script_enable *rdata;
371 rdata = talloc(mem_ctx, struct ctdb_event_request_script_enable);
376 ret = ctdb_stringn_pull(buf, buflen, rdata, &rdata->script_name, &np);
386 static size_t ctdb_event_request_script_disable_len(
387 struct ctdb_event_request_script_disable *in)
389 return ctdb_stringn_len(&in->script_name);
392 static void ctdb_event_request_script_disable_push(
393 struct ctdb_event_request_script_disable *in,
398 ctdb_stringn_push(&in->script_name, buf, &np);
401 static int ctdb_event_request_script_disable_pull(
402 uint8_t *buf, size_t buflen,
404 struct ctdb_event_request_script_disable **out)
406 struct ctdb_event_request_script_disable *rdata;
410 rdata = talloc(mem_ctx, struct ctdb_event_request_script_disable);
415 ret = ctdb_stringn_pull(buf, buflen, rdata, &rdata->script_name, &np);
425 static size_t ctdb_event_request_data_len(struct ctdb_event_request_data *in)
429 len += ctdb_event_command_len(in->command);
431 switch(in->command) {
432 case CTDB_EVENT_COMMAND_RUN:
433 len += ctdb_event_request_run_len(in->data.run);
436 case CTDB_EVENT_COMMAND_STATUS:
437 len += ctdb_event_request_status_len(in->data.status);
440 case CTDB_EVENT_COMMAND_SCRIPT_LIST:
443 case CTDB_EVENT_COMMAND_SCRIPT_ENABLE:
444 len += ctdb_event_request_script_enable_len(
445 in->data.script_enable);
448 case CTDB_EVENT_COMMAND_SCRIPT_DISABLE:
449 len += ctdb_event_request_script_disable_len(
450 in->data.script_disable);
457 static void ctdb_event_request_data_push(struct ctdb_event_request_data *in,
462 ctdb_event_command_push(in->command, buf);
463 offset += ctdb_event_command_len(in->command);
465 switch (in->command) {
466 case CTDB_EVENT_COMMAND_RUN:
467 ctdb_event_request_run_push(in->data.run, buf+offset);
470 case CTDB_EVENT_COMMAND_STATUS:
471 ctdb_event_request_status_push(in->data.status, buf+offset);
474 case CTDB_EVENT_COMMAND_SCRIPT_LIST:
477 case CTDB_EVENT_COMMAND_SCRIPT_ENABLE:
478 ctdb_event_request_script_enable_push(
479 in->data.script_enable,
483 case CTDB_EVENT_COMMAND_SCRIPT_DISABLE:
484 ctdb_event_request_script_disable_push(
485 in->data.script_disable,
491 static int ctdb_event_request_data_pull(uint8_t *buf, size_t buflen,
493 struct ctdb_event_request_data *out)
498 ret = ctdb_event_command_pull(buf, buflen, mem_ctx, &out->command);
502 offset += ctdb_event_command_len(out->command);
504 switch (out->command) {
505 case CTDB_EVENT_COMMAND_RUN:
506 ret = ctdb_event_request_run_pull(buf+offset, buflen-offset,
507 mem_ctx, &out->data.run);
510 case CTDB_EVENT_COMMAND_STATUS:
511 ret = ctdb_event_request_status_pull(
512 buf+offset, buflen-offset,
513 mem_ctx, &out->data.status);
516 case CTDB_EVENT_COMMAND_SCRIPT_LIST:
520 case CTDB_EVENT_COMMAND_SCRIPT_ENABLE:
521 ret = ctdb_event_request_script_enable_pull(
522 buf+offset, buflen-offset,
524 &out->data.script_enable);
527 case CTDB_EVENT_COMMAND_SCRIPT_DISABLE:
528 ret = ctdb_event_request_script_disable_pull(
529 buf+offset, buflen-offset,
531 &out->data.script_disable);
542 static size_t ctdb_event_reply_status_len(struct ctdb_event_reply_status *in)
544 return ctdb_int32_len(&in->status) +
545 ctdb_script_list_len(in->script_list);
548 static void ctdb_event_reply_status_push(struct ctdb_event_reply_status *in,
551 size_t offset = 0, np;
553 ctdb_int32_push(&in->status, buf, &np);
556 ctdb_script_list_push(in->script_list, buf+offset, &np);
559 static int ctdb_event_reply_status_pull(uint8_t *buf, size_t buflen,
561 struct ctdb_event_reply_status **out)
563 struct ctdb_event_reply_status *rdata;
564 size_t offset = 0, np;
567 rdata = talloc(mem_ctx, struct ctdb_event_reply_status);
572 ret = ctdb_int32_pull(buf, buflen, &rdata->status, &np);
579 ret = ctdb_script_list_pull(buf+offset, buflen-offset,
580 rdata, &rdata->script_list, &np);
590 static size_t ctdb_event_reply_script_list_len(
591 struct ctdb_event_reply_script_list *in)
593 return ctdb_script_list_len(in->script_list);
596 static void ctdb_event_reply_script_list_push(
597 struct ctdb_event_reply_script_list *in,
602 ctdb_script_list_push(in->script_list, buf, &np);
605 static int ctdb_event_reply_script_list_pull(
606 uint8_t *buf, size_t buflen,
608 struct ctdb_event_reply_script_list **out)
610 struct ctdb_event_reply_script_list *rdata;
614 rdata = talloc(mem_ctx, struct ctdb_event_reply_script_list);
619 ret = ctdb_script_list_pull(buf, buflen, rdata, &rdata->script_list,
630 static size_t ctdb_event_reply_data_len(struct ctdb_event_reply_data *in)
634 len += ctdb_event_command_len(in->command);
635 len += ctdb_int32_len(&in->result);
637 switch (in->command) {
638 case CTDB_EVENT_COMMAND_RUN:
641 case CTDB_EVENT_COMMAND_STATUS:
642 len += ctdb_event_reply_status_len(in->data.status);
645 case CTDB_EVENT_COMMAND_SCRIPT_LIST:
646 len += ctdb_event_reply_script_list_len(in->data.script_list);
649 case CTDB_EVENT_COMMAND_SCRIPT_ENABLE:
652 case CTDB_EVENT_COMMAND_SCRIPT_DISABLE:
659 static void ctdb_event_reply_data_push(struct ctdb_event_reply_data *in,
662 size_t offset = 0, np;
664 ctdb_event_command_push(in->command, buf);
665 offset += ctdb_event_command_len(in->command);
667 ctdb_int32_push(&in->result, buf+offset, &np);
670 switch (in->command) {
671 case CTDB_EVENT_COMMAND_RUN:
674 case CTDB_EVENT_COMMAND_STATUS:
675 ctdb_event_reply_status_push(in->data.status, buf+offset);
678 case CTDB_EVENT_COMMAND_SCRIPT_LIST:
679 ctdb_event_reply_script_list_push(in->data.script_list,
683 case CTDB_EVENT_COMMAND_SCRIPT_ENABLE:
686 case CTDB_EVENT_COMMAND_SCRIPT_DISABLE:
691 static int ctdb_event_reply_data_pull(uint8_t *buf, size_t buflen,
693 struct ctdb_event_reply_data *out)
695 size_t offset = 0, np;
698 ret = ctdb_event_command_pull(buf, buflen, mem_ctx, &out->command);
702 offset += ctdb_event_command_len(out->command);
704 ret = ctdb_int32_pull(buf+offset, buflen-offset, &out->result, &np);
710 switch (out->command) {
711 case CTDB_EVENT_COMMAND_RUN:
714 case CTDB_EVENT_COMMAND_STATUS:
715 ret = ctdb_event_reply_status_pull(
716 buf+offset, buflen-offset,
717 mem_ctx, &out->data.status);
720 case CTDB_EVENT_COMMAND_SCRIPT_LIST:
721 ret = ctdb_event_reply_script_list_pull(
722 buf+offset, buflen-offset,
723 mem_ctx, &out->data.script_list);
726 case CTDB_EVENT_COMMAND_SCRIPT_ENABLE:
729 case CTDB_EVENT_COMMAND_SCRIPT_DISABLE:
740 static size_t ctdb_event_header_len(struct ctdb_event_header *in)
742 return ctdb_uint32_len(&in->length) + ctdb_uint32_len(&in->reqid);
745 static void ctdb_event_header_push(struct ctdb_event_header *in, uint8_t *buf)
747 size_t offset = 0, np;
749 ctdb_uint32_push(&in->length, buf, &np);
752 ctdb_uint32_push(&in->reqid, buf+offset, &np);
755 static int ctdb_event_header_pull(uint8_t *buf, size_t buflen,
757 struct ctdb_event_header *out)
759 size_t offset = 0, np;
762 ret = ctdb_uint32_pull(buf, buflen, &out->length, &np);
768 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->reqid, &np);
776 void ctdb_event_header_fill(struct ctdb_event_header *h, uint32_t reqid)
778 h->length = ctdb_event_header_len(h);
782 size_t ctdb_event_request_len(struct ctdb_event_request *in)
784 return ctdb_event_header_len(&in->header) +
785 ctdb_event_request_data_len(&in->rdata);
788 int ctdb_event_request_push(struct ctdb_event_request *in,
789 uint8_t *buf, size_t *buflen)
791 size_t len, offset = 0;
793 len = ctdb_event_request_len(in);
799 in->header.length = *buflen;
801 ctdb_event_header_push(&in->header, buf);
802 offset += ctdb_event_header_len(&in->header);
804 ctdb_event_request_data_push(&in->rdata, buf+offset);
809 int ctdb_event_request_pull(uint8_t *buf, size_t buflen,
811 struct ctdb_event_request *out)
816 ret = ctdb_event_header_pull(buf, buflen, mem_ctx, &out->header);
820 offset += ctdb_event_header_len(&out->header);
822 ret = ctdb_event_request_data_pull(buf+offset, buflen-offset,
823 mem_ctx, &out->rdata);
831 size_t ctdb_event_reply_len(struct ctdb_event_reply *in)
833 return ctdb_event_header_len(&in->header) +
834 ctdb_event_reply_data_len(&in->rdata);
837 int ctdb_event_reply_push(struct ctdb_event_reply *in,
838 uint8_t *buf, size_t *buflen)
840 size_t len, offset = 0;
842 len = ctdb_event_reply_len(in);
848 in->header.length = *buflen;
850 ctdb_event_header_push(&in->header, buf);
851 offset += ctdb_event_header_len(&in->header);
853 ctdb_event_reply_data_push(&in->rdata, buf+offset);
858 int ctdb_event_reply_pull(uint8_t *buf, size_t buflen,
860 struct ctdb_event_reply *out)
865 ret = ctdb_event_header_pull(buf, buflen, mem_ctx, &out->header);
869 offset += ctdb_event_header_len(&out->header);
871 ret = ctdb_event_reply_data_pull(buf+offset, buflen-offset,
872 mem_ctx, &out->rdata);