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)
41 ctdb_uint32_push(&u32, buf, &np);
44 static int ctdb_event_pull(uint8_t *buf, size_t buflen,
45 TALLOC_CTX *mem_ctx, enum ctdb_event *out)
47 uint32_t uint32_value;
48 enum ctdb_event value;
52 ret = ctdb_uint32_pull(buf, buflen, &uint32_value, &np);
57 switch (uint32_value) {
59 value = CTDB_EVENT_INIT;
63 value = CTDB_EVENT_SETUP;
67 value = CTDB_EVENT_STARTUP;
71 value = CTDB_EVENT_START_RECOVERY;
75 value = CTDB_EVENT_RECOVERED;
79 value = CTDB_EVENT_TAKE_IP;
83 value = CTDB_EVENT_RELEASE_IP;
87 value = CTDB_EVENT_STOPPED;
91 value = CTDB_EVENT_MONITOR;
95 value = CTDB_EVENT_STATUS;
99 value = CTDB_EVENT_SHUTDOWN;
103 value = CTDB_EVENT_RELOAD;
107 value = CTDB_EVENT_UPDATE_IP;
111 value = CTDB_EVENT_IPREALLOCATED;
122 static size_t ctdb_event_command_len(enum ctdb_event_command in)
126 return ctdb_uint32_len(&u32);
129 static void ctdb_event_command_push(enum ctdb_event_command in, uint8_t *buf)
134 ctdb_uint32_push(&u32, buf, &np);
137 static int ctdb_event_command_pull(uint8_t *buf, size_t buflen,
139 enum ctdb_event_command *out)
141 uint32_t uint32_value;
142 enum ctdb_event_command value;
146 ret = ctdb_uint32_pull(buf, buflen, &uint32_value, &np);
151 switch (uint32_value) {
153 value = CTDB_EVENT_COMMAND_RUN;
157 value = CTDB_EVENT_COMMAND_STATUS;
161 value = CTDB_EVENT_COMMAND_SCRIPT_LIST;
165 value = CTDB_EVENT_COMMAND_SCRIPT_ENABLE;
169 value = CTDB_EVENT_COMMAND_SCRIPT_DISABLE;
180 static size_t ctdb_event_status_state_len(enum ctdb_event_status_state in)
184 return ctdb_uint32_len(&u32);
187 static void ctdb_event_status_state_push(enum ctdb_event_status_state in,
193 ctdb_uint32_push(&u32, buf, &np);
196 static int ctdb_event_status_state_pull(uint8_t *buf, size_t buflen,
198 enum ctdb_event_status_state *out)
200 uint32_t uint32_value;
201 enum ctdb_event_status_state value;
205 ret = ctdb_uint32_pull(buf, buflen, &uint32_value, &np);
210 switch (uint32_value) {
212 value = CTDB_EVENT_LAST_RUN;
216 value = CTDB_EVENT_LAST_PASS;
220 value = CTDB_EVENT_LAST_FAIL;
231 static size_t ctdb_event_request_run_len(struct ctdb_event_request_run *in)
233 return ctdb_event_len(in->event) +
234 ctdb_uint32_len(&in->timeout) +
235 ctdb_stringn_len(&in->arg_str);
238 static void ctdb_event_request_run_push(struct ctdb_event_request_run *in,
241 size_t offset = 0, np;
243 ctdb_event_push(in->event, buf);
244 offset += ctdb_event_len(in->event);
246 ctdb_uint32_push(&in->timeout, buf+offset, &np);
249 ctdb_stringn_push(&in->arg_str, buf+offset, &np);
252 static int ctdb_event_request_run_pull(uint8_t *buf, size_t buflen,
254 struct ctdb_event_request_run **out)
256 struct ctdb_event_request_run *rdata;
257 size_t offset = 0, np;
260 rdata = talloc(mem_ctx, struct ctdb_event_request_run);
265 ret = ctdb_event_pull(buf, buflen, rdata, &rdata->event);
269 offset += ctdb_event_len(rdata->event);
271 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &rdata->timeout,
278 ret = ctdb_stringn_pull(buf+offset, buflen-offset,
279 rdata, &rdata->arg_str, &np);
292 static size_t ctdb_event_request_status_len(
293 struct ctdb_event_request_status *in)
295 return ctdb_event_len(in->event) +
296 ctdb_event_status_state_len(in->state);
299 static void ctdb_event_request_status_push(
300 struct ctdb_event_request_status *in,
305 ctdb_event_push(in->event, buf);
306 offset += ctdb_event_len(in->event);
308 ctdb_event_status_state_push(in->state, buf+offset);
311 static int ctdb_event_request_status_pull(
312 uint8_t *buf, size_t buflen,
314 struct ctdb_event_request_status **out)
316 struct ctdb_event_request_status *rdata;
320 rdata = talloc(mem_ctx, struct ctdb_event_request_status);
325 ret = ctdb_event_pull(buf, buflen, rdata, &rdata->event);
330 offset += ctdb_event_len(rdata->event);
332 ret = ctdb_event_status_state_pull(buf+offset, buflen-offset,
333 rdata, &rdata->state);
343 static size_t ctdb_event_request_script_enable_len(
344 struct ctdb_event_request_script_enable *in)
346 return ctdb_stringn_len(&in->script_name);
349 static void ctdb_event_request_script_enable_push(
350 struct ctdb_event_request_script_enable *in,
355 ctdb_stringn_push(&in->script_name, buf, &np);
358 static int ctdb_event_request_script_enable_pull(
359 uint8_t *buf, size_t buflen,
361 struct ctdb_event_request_script_enable **out)
363 struct ctdb_event_request_script_enable *rdata;
367 rdata = talloc(mem_ctx, struct ctdb_event_request_script_enable);
372 ret = ctdb_stringn_pull(buf, buflen, rdata, &rdata->script_name, &np);
382 static size_t ctdb_event_request_script_disable_len(
383 struct ctdb_event_request_script_disable *in)
385 return ctdb_stringn_len(&in->script_name);
388 static void ctdb_event_request_script_disable_push(
389 struct ctdb_event_request_script_disable *in,
394 ctdb_stringn_push(&in->script_name, buf, &np);
397 static int ctdb_event_request_script_disable_pull(
398 uint8_t *buf, size_t buflen,
400 struct ctdb_event_request_script_disable **out)
402 struct ctdb_event_request_script_disable *rdata;
406 rdata = talloc(mem_ctx, struct ctdb_event_request_script_disable);
411 ret = ctdb_stringn_pull(buf, buflen, rdata, &rdata->script_name, &np);
421 static size_t ctdb_event_request_data_len(struct ctdb_event_request_data *in)
425 len += ctdb_event_command_len(in->command);
427 switch(in->command) {
428 case CTDB_EVENT_COMMAND_RUN:
429 len += ctdb_event_request_run_len(in->data.run);
432 case CTDB_EVENT_COMMAND_STATUS:
433 len += ctdb_event_request_status_len(in->data.status);
436 case CTDB_EVENT_COMMAND_SCRIPT_LIST:
439 case CTDB_EVENT_COMMAND_SCRIPT_ENABLE:
440 len += ctdb_event_request_script_enable_len(
441 in->data.script_enable);
444 case CTDB_EVENT_COMMAND_SCRIPT_DISABLE:
445 len += ctdb_event_request_script_disable_len(
446 in->data.script_disable);
453 static void ctdb_event_request_data_push(struct ctdb_event_request_data *in,
458 ctdb_event_command_push(in->command, buf);
459 offset += ctdb_event_command_len(in->command);
461 switch (in->command) {
462 case CTDB_EVENT_COMMAND_RUN:
463 ctdb_event_request_run_push(in->data.run, buf+offset);
466 case CTDB_EVENT_COMMAND_STATUS:
467 ctdb_event_request_status_push(in->data.status, buf+offset);
470 case CTDB_EVENT_COMMAND_SCRIPT_LIST:
473 case CTDB_EVENT_COMMAND_SCRIPT_ENABLE:
474 ctdb_event_request_script_enable_push(
475 in->data.script_enable,
479 case CTDB_EVENT_COMMAND_SCRIPT_DISABLE:
480 ctdb_event_request_script_disable_push(
481 in->data.script_disable,
487 static int ctdb_event_request_data_pull(uint8_t *buf, size_t buflen,
489 struct ctdb_event_request_data *out)
494 ret = ctdb_event_command_pull(buf, buflen, mem_ctx, &out->command);
498 offset += ctdb_event_command_len(out->command);
500 switch (out->command) {
501 case CTDB_EVENT_COMMAND_RUN:
502 ret = ctdb_event_request_run_pull(buf+offset, buflen-offset,
503 mem_ctx, &out->data.run);
506 case CTDB_EVENT_COMMAND_STATUS:
507 ret = ctdb_event_request_status_pull(
508 buf+offset, buflen-offset,
509 mem_ctx, &out->data.status);
512 case CTDB_EVENT_COMMAND_SCRIPT_LIST:
516 case CTDB_EVENT_COMMAND_SCRIPT_ENABLE:
517 ret = ctdb_event_request_script_enable_pull(
518 buf+offset, buflen-offset,
520 &out->data.script_enable);
523 case CTDB_EVENT_COMMAND_SCRIPT_DISABLE:
524 ret = ctdb_event_request_script_disable_pull(
525 buf+offset, buflen-offset,
527 &out->data.script_disable);
538 static size_t ctdb_event_reply_status_len(struct ctdb_event_reply_status *in)
540 return ctdb_int32_len(&in->status) +
541 ctdb_script_list_len(in->script_list);
544 static void ctdb_event_reply_status_push(struct ctdb_event_reply_status *in,
547 size_t offset = 0, np;
549 ctdb_int32_push(&in->status, buf, &np);
552 ctdb_script_list_push(in->script_list, buf+offset);
555 static int ctdb_event_reply_status_pull(uint8_t *buf, size_t buflen,
557 struct ctdb_event_reply_status **out)
559 struct ctdb_event_reply_status *rdata;
560 size_t offset = 0, np;
563 rdata = talloc(mem_ctx, struct ctdb_event_reply_status);
568 ret = ctdb_int32_pull(buf, buflen, &rdata->status, &np);
575 ret = ctdb_script_list_pull(buf+offset, buflen-offset,
576 rdata, &rdata->script_list);
586 static size_t ctdb_event_reply_script_list_len(
587 struct ctdb_event_reply_script_list *in)
589 return ctdb_script_list_len(in->script_list);
592 static void ctdb_event_reply_script_list_push(
593 struct ctdb_event_reply_script_list *in,
596 ctdb_script_list_push(in->script_list, buf);
599 static int ctdb_event_reply_script_list_pull(
600 uint8_t *buf, size_t buflen,
602 struct ctdb_event_reply_script_list **out)
604 struct ctdb_event_reply_script_list *rdata;
607 rdata = talloc(mem_ctx, struct ctdb_event_reply_script_list);
612 ret = ctdb_script_list_pull(buf, buflen, rdata, &rdata->script_list);
622 static size_t ctdb_event_reply_data_len(struct ctdb_event_reply_data *in)
626 len += ctdb_event_command_len(in->command);
627 len += ctdb_int32_len(&in->result);
629 switch (in->command) {
630 case CTDB_EVENT_COMMAND_RUN:
633 case CTDB_EVENT_COMMAND_STATUS:
634 len += ctdb_event_reply_status_len(in->data.status);
637 case CTDB_EVENT_COMMAND_SCRIPT_LIST:
638 len += ctdb_event_reply_script_list_len(in->data.script_list);
641 case CTDB_EVENT_COMMAND_SCRIPT_ENABLE:
644 case CTDB_EVENT_COMMAND_SCRIPT_DISABLE:
651 static void ctdb_event_reply_data_push(struct ctdb_event_reply_data *in,
654 size_t offset = 0, np;
656 ctdb_event_command_push(in->command, buf);
657 offset += ctdb_event_command_len(in->command);
659 ctdb_int32_push(&in->result, buf+offset, &np);
662 switch (in->command) {
663 case CTDB_EVENT_COMMAND_RUN:
666 case CTDB_EVENT_COMMAND_STATUS:
667 ctdb_event_reply_status_push(in->data.status, buf+offset);
670 case CTDB_EVENT_COMMAND_SCRIPT_LIST:
671 ctdb_event_reply_script_list_push(in->data.script_list,
675 case CTDB_EVENT_COMMAND_SCRIPT_ENABLE:
678 case CTDB_EVENT_COMMAND_SCRIPT_DISABLE:
683 static int ctdb_event_reply_data_pull(uint8_t *buf, size_t buflen,
685 struct ctdb_event_reply_data *out)
687 size_t offset = 0, np;
690 ret = ctdb_event_command_pull(buf, buflen, mem_ctx, &out->command);
694 offset += ctdb_event_command_len(out->command);
696 ret = ctdb_int32_pull(buf+offset, buflen-offset, &out->result, &np);
702 switch (out->command) {
703 case CTDB_EVENT_COMMAND_RUN:
706 case CTDB_EVENT_COMMAND_STATUS:
707 ret = ctdb_event_reply_status_pull(
708 buf+offset, buflen-offset,
709 mem_ctx, &out->data.status);
712 case CTDB_EVENT_COMMAND_SCRIPT_LIST:
713 ret = ctdb_event_reply_script_list_pull(
714 buf+offset, buflen-offset,
715 mem_ctx, &out->data.script_list);
718 case CTDB_EVENT_COMMAND_SCRIPT_ENABLE:
721 case CTDB_EVENT_COMMAND_SCRIPT_DISABLE:
732 static size_t ctdb_event_header_len(struct ctdb_event_header *in)
734 return ctdb_uint32_len(&in->length) + ctdb_uint32_len(&in->reqid);
737 static void ctdb_event_header_push(struct ctdb_event_header *in, uint8_t *buf)
739 size_t offset = 0, np;
741 ctdb_uint32_push(&in->length, buf, &np);
744 ctdb_uint32_push(&in->reqid, buf+offset, &np);
747 static int ctdb_event_header_pull(uint8_t *buf, size_t buflen,
749 struct ctdb_event_header *out)
751 size_t offset = 0, np;
754 ret = ctdb_uint32_pull(buf, buflen, &out->length, &np);
760 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->reqid, &np);
768 void ctdb_event_header_fill(struct ctdb_event_header *h, uint32_t reqid)
770 h->length = ctdb_event_header_len(h);
774 size_t ctdb_event_request_len(struct ctdb_event_request *in)
776 return ctdb_event_header_len(&in->header) +
777 ctdb_event_request_data_len(&in->rdata);
780 int ctdb_event_request_push(struct ctdb_event_request *in,
781 uint8_t *buf, size_t *buflen)
783 size_t len, offset = 0;
785 len = ctdb_event_request_len(in);
791 in->header.length = *buflen;
793 ctdb_event_header_push(&in->header, buf);
794 offset += ctdb_event_header_len(&in->header);
796 ctdb_event_request_data_push(&in->rdata, buf+offset);
801 int ctdb_event_request_pull(uint8_t *buf, size_t buflen,
803 struct ctdb_event_request *out)
808 ret = ctdb_event_header_pull(buf, buflen, mem_ctx, &out->header);
812 offset += ctdb_event_header_len(&out->header);
814 ret = ctdb_event_request_data_pull(buf+offset, buflen-offset,
815 mem_ctx, &out->rdata);
823 size_t ctdb_event_reply_len(struct ctdb_event_reply *in)
825 return ctdb_event_header_len(&in->header) +
826 ctdb_event_reply_data_len(&in->rdata);
829 int ctdb_event_reply_push(struct ctdb_event_reply *in,
830 uint8_t *buf, size_t *buflen)
832 size_t len, offset = 0;
834 len = ctdb_event_reply_len(in);
840 in->header.length = *buflen;
842 ctdb_event_header_push(&in->header, buf);
843 offset += ctdb_event_header_len(&in->header);
845 ctdb_event_reply_data_push(&in->rdata, buf+offset);
850 int ctdb_event_reply_pull(uint8_t *buf, size_t buflen,
852 struct ctdb_event_reply *out)
857 ret = ctdb_event_header_pull(buf, buflen, mem_ctx, &out->header);
861 offset += ctdb_event_header_len(&out->header);
863 ret = ctdb_event_reply_data_pull(buf+offset, buflen-offset,
864 mem_ctx, &out->rdata);