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, &np);
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, &np);
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,
598 ctdb_script_list_push(in->script_list, buf, &np);
601 static int ctdb_event_reply_script_list_pull(
602 uint8_t *buf, size_t buflen,
604 struct ctdb_event_reply_script_list **out)
606 struct ctdb_event_reply_script_list *rdata;
610 rdata = talloc(mem_ctx, struct ctdb_event_reply_script_list);
615 ret = ctdb_script_list_pull(buf, buflen, rdata, &rdata->script_list,
626 static size_t ctdb_event_reply_data_len(struct ctdb_event_reply_data *in)
630 len += ctdb_event_command_len(in->command);
631 len += ctdb_int32_len(&in->result);
633 switch (in->command) {
634 case CTDB_EVENT_COMMAND_RUN:
637 case CTDB_EVENT_COMMAND_STATUS:
638 len += ctdb_event_reply_status_len(in->data.status);
641 case CTDB_EVENT_COMMAND_SCRIPT_LIST:
642 len += ctdb_event_reply_script_list_len(in->data.script_list);
645 case CTDB_EVENT_COMMAND_SCRIPT_ENABLE:
648 case CTDB_EVENT_COMMAND_SCRIPT_DISABLE:
655 static void ctdb_event_reply_data_push(struct ctdb_event_reply_data *in,
658 size_t offset = 0, np;
660 ctdb_event_command_push(in->command, buf);
661 offset += ctdb_event_command_len(in->command);
663 ctdb_int32_push(&in->result, buf+offset, &np);
666 switch (in->command) {
667 case CTDB_EVENT_COMMAND_RUN:
670 case CTDB_EVENT_COMMAND_STATUS:
671 ctdb_event_reply_status_push(in->data.status, buf+offset);
674 case CTDB_EVENT_COMMAND_SCRIPT_LIST:
675 ctdb_event_reply_script_list_push(in->data.script_list,
679 case CTDB_EVENT_COMMAND_SCRIPT_ENABLE:
682 case CTDB_EVENT_COMMAND_SCRIPT_DISABLE:
687 static int ctdb_event_reply_data_pull(uint8_t *buf, size_t buflen,
689 struct ctdb_event_reply_data *out)
691 size_t offset = 0, np;
694 ret = ctdb_event_command_pull(buf, buflen, mem_ctx, &out->command);
698 offset += ctdb_event_command_len(out->command);
700 ret = ctdb_int32_pull(buf+offset, buflen-offset, &out->result, &np);
706 switch (out->command) {
707 case CTDB_EVENT_COMMAND_RUN:
710 case CTDB_EVENT_COMMAND_STATUS:
711 ret = ctdb_event_reply_status_pull(
712 buf+offset, buflen-offset,
713 mem_ctx, &out->data.status);
716 case CTDB_EVENT_COMMAND_SCRIPT_LIST:
717 ret = ctdb_event_reply_script_list_pull(
718 buf+offset, buflen-offset,
719 mem_ctx, &out->data.script_list);
722 case CTDB_EVENT_COMMAND_SCRIPT_ENABLE:
725 case CTDB_EVENT_COMMAND_SCRIPT_DISABLE:
736 static size_t ctdb_event_header_len(struct ctdb_event_header *in)
738 return ctdb_uint32_len(&in->length) + ctdb_uint32_len(&in->reqid);
741 static void ctdb_event_header_push(struct ctdb_event_header *in, uint8_t *buf)
743 size_t offset = 0, np;
745 ctdb_uint32_push(&in->length, buf, &np);
748 ctdb_uint32_push(&in->reqid, buf+offset, &np);
751 static int ctdb_event_header_pull(uint8_t *buf, size_t buflen,
753 struct ctdb_event_header *out)
755 size_t offset = 0, np;
758 ret = ctdb_uint32_pull(buf, buflen, &out->length, &np);
764 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->reqid, &np);
772 void ctdb_event_header_fill(struct ctdb_event_header *h, uint32_t reqid)
774 h->length = ctdb_event_header_len(h);
778 size_t ctdb_event_request_len(struct ctdb_event_request *in)
780 return ctdb_event_header_len(&in->header) +
781 ctdb_event_request_data_len(&in->rdata);
784 int ctdb_event_request_push(struct ctdb_event_request *in,
785 uint8_t *buf, size_t *buflen)
787 size_t len, offset = 0;
789 len = ctdb_event_request_len(in);
795 in->header.length = *buflen;
797 ctdb_event_header_push(&in->header, buf);
798 offset += ctdb_event_header_len(&in->header);
800 ctdb_event_request_data_push(&in->rdata, buf+offset);
805 int ctdb_event_request_pull(uint8_t *buf, size_t buflen,
807 struct ctdb_event_request *out)
812 ret = ctdb_event_header_pull(buf, buflen, mem_ctx, &out->header);
816 offset += ctdb_event_header_len(&out->header);
818 ret = ctdb_event_request_data_pull(buf+offset, buflen-offset,
819 mem_ctx, &out->rdata);
827 size_t ctdb_event_reply_len(struct ctdb_event_reply *in)
829 return ctdb_event_header_len(&in->header) +
830 ctdb_event_reply_data_len(&in->rdata);
833 int ctdb_event_reply_push(struct ctdb_event_reply *in,
834 uint8_t *buf, size_t *buflen)
836 size_t len, offset = 0;
838 len = ctdb_event_reply_len(in);
844 in->header.length = *buflen;
846 ctdb_event_header_push(&in->header, buf);
847 offset += ctdb_event_header_len(&in->header);
849 ctdb_event_reply_data_push(&in->rdata, buf+offset);
854 int ctdb_event_reply_pull(uint8_t *buf, size_t buflen,
856 struct ctdb_event_reply *out)
861 ret = ctdb_event_header_pull(buf, buflen, mem_ctx, &out->header);
865 offset += ctdb_event_header_len(&out->header);
867 ret = ctdb_event_reply_data_pull(buf+offset, buflen-offset,
868 mem_ctx, &out->rdata);