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"
27 #include "protocol_private.h"
28 #include "protocol_api.h"
30 size_t ctdb_tdb_data_len(TDB_DATA *in)
32 return in->dsize > UINT32_MAX ? UINT32_MAX : in->dsize;
35 void ctdb_tdb_data_push(TDB_DATA *in, uint8_t *buf, size_t *npush)
37 size_t len = ctdb_tdb_data_len(in);
40 memcpy(buf, in->dptr, len);
46 int ctdb_tdb_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
47 TDB_DATA *out, size_t *npull)
51 if (buflen > UINT32_MAX) {
57 val.dptr = talloc_memdup(mem_ctx, buf, buflen);
58 if (val.dptr == NULL) {
70 size_t ctdb_statistics_len(struct ctdb_statistics *stats)
72 return sizeof(struct ctdb_statistics);
75 void ctdb_statistics_push(struct ctdb_statistics *stats, uint8_t *buf)
77 memcpy(buf, stats, sizeof(struct ctdb_statistics));
80 int ctdb_statistics_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
81 struct ctdb_statistics **out)
83 struct ctdb_statistics *stats;
84 struct ctdb_statistics *wire = (struct ctdb_statistics *)buf;
86 if (buflen < sizeof(struct ctdb_statistics)) {
90 stats = talloc(mem_ctx, struct ctdb_statistics);
94 memcpy(stats, wire, sizeof(struct ctdb_statistics));
100 struct ctdb_statistics_list_wire {
102 struct ctdb_statistics stats[1];
105 size_t ctdb_statistics_list_len(struct ctdb_statistics_list *stats_list)
107 return offsetof(struct ctdb_statistics_list_wire, stats) +
108 stats_list->num * sizeof(struct ctdb_statistics);
111 void ctdb_statistics_list_push(struct ctdb_statistics_list *stats_list,
114 struct ctdb_statistics_list_wire *wire =
115 (struct ctdb_statistics_list_wire *)buf;
117 wire->num = stats_list->num;
118 memcpy(wire->stats, stats_list->stats,
119 stats_list->num * sizeof(struct ctdb_statistics));
122 int ctdb_statistics_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
123 struct ctdb_statistics_list **out)
125 struct ctdb_statistics_list *stats_list;
126 struct ctdb_statistics_list_wire *wire =
127 (struct ctdb_statistics_list_wire *)buf;
129 if (buflen < offsetof(struct ctdb_statistics_list_wire, stats)) {
132 if (wire->num > buflen / sizeof(struct ctdb_statistics)) {
135 if (offsetof(struct ctdb_statistics_list_wire, stats) +
136 wire->num * sizeof(struct ctdb_statistics) <
137 offsetof(struct ctdb_statistics_list_wire, stats)) {
140 if (buflen < offsetof(struct ctdb_statistics_list_wire, stats) +
141 wire->num * sizeof(struct ctdb_statistics)) {
145 stats_list = talloc(mem_ctx, struct ctdb_statistics_list);
146 if (stats_list == NULL) {
150 stats_list->num = wire->num;
152 stats_list->stats = talloc_array(stats_list, struct ctdb_statistics,
154 if (stats_list->stats == NULL) {
155 talloc_free(stats_list);
159 memcpy(stats_list->stats, wire->stats,
160 wire->num * sizeof(struct ctdb_statistics));
166 struct ctdb_vnn_map_wire {
172 size_t ctdb_vnn_map_len(struct ctdb_vnn_map *vnnmap)
174 return offsetof(struct ctdb_vnn_map, map) +
175 vnnmap->size * sizeof(uint32_t);
178 void ctdb_vnn_map_push(struct ctdb_vnn_map *vnnmap, uint8_t *buf)
180 struct ctdb_vnn_map_wire *wire = (struct ctdb_vnn_map_wire *)buf;
182 memcpy(wire, vnnmap, offsetof(struct ctdb_vnn_map, map));
183 memcpy(wire->map, vnnmap->map, vnnmap->size * sizeof(uint32_t));
186 int ctdb_vnn_map_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
187 struct ctdb_vnn_map **out)
189 struct ctdb_vnn_map *vnnmap;
190 struct ctdb_vnn_map_wire *wire = (struct ctdb_vnn_map_wire *)buf;
192 if (buflen < offsetof(struct ctdb_vnn_map_wire, map)) {
195 if (wire->size > buflen / sizeof(uint32_t)) {
198 if (offsetof(struct ctdb_vnn_map_wire, map) +
199 wire->size * sizeof(uint32_t) <
200 offsetof(struct ctdb_vnn_map_wire, map)) {
203 if (buflen < offsetof(struct ctdb_vnn_map_wire, map) +
204 wire->size * sizeof(uint32_t)) {
208 vnnmap = talloc(mem_ctx, struct ctdb_vnn_map);
209 if (vnnmap == NULL) {
213 memcpy(vnnmap, wire, offsetof(struct ctdb_vnn_map, map));
215 vnnmap->map = talloc_memdup(vnnmap, wire->map,
216 wire->size * sizeof(uint32_t));
217 if (vnnmap->map == NULL) {
226 struct ctdb_dbid_map_wire {
228 struct ctdb_dbid dbs[1];
231 size_t ctdb_dbid_map_len(struct ctdb_dbid_map *dbmap)
233 return sizeof(uint32_t) + dbmap->num * sizeof(struct ctdb_dbid);
236 void ctdb_dbid_map_push(struct ctdb_dbid_map *dbmap, uint8_t *buf)
238 struct ctdb_dbid_map_wire *wire = (struct ctdb_dbid_map_wire *)buf;
240 wire->num = dbmap->num;
241 memcpy(wire->dbs, dbmap->dbs, dbmap->num * sizeof(struct ctdb_dbid));
244 int ctdb_dbid_map_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
245 struct ctdb_dbid_map **out)
247 struct ctdb_dbid_map *dbmap;
248 struct ctdb_dbid_map_wire *wire = (struct ctdb_dbid_map_wire *)buf;
250 if (buflen < sizeof(uint32_t)) {
253 if (wire->num > buflen / sizeof(struct ctdb_dbid)) {
256 if (sizeof(uint32_t) + wire->num * sizeof(struct ctdb_dbid) <
260 if (buflen < sizeof(uint32_t) + wire->num * sizeof(struct ctdb_dbid)) {
264 dbmap = talloc(mem_ctx, struct ctdb_dbid_map);
269 dbmap->num = wire->num;
271 dbmap->dbs = talloc_memdup(dbmap, wire->dbs,
272 wire->num * sizeof(struct ctdb_dbid));
273 if (dbmap->dbs == NULL) {
282 size_t ctdb_pulldb_len(struct ctdb_pulldb *pulldb)
284 return sizeof(struct ctdb_pulldb);
287 void ctdb_pulldb_push(struct ctdb_pulldb *pulldb, uint8_t *buf)
289 memcpy(buf, pulldb, sizeof(struct ctdb_pulldb));
292 int ctdb_pulldb_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
293 struct ctdb_pulldb **out)
295 struct ctdb_pulldb *pulldb;
297 if (buflen < sizeof(struct ctdb_pulldb)) {
301 pulldb = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_pulldb));
302 if (pulldb == NULL) {
310 size_t ctdb_pulldb_ext_len(struct ctdb_pulldb_ext *pulldb)
312 return sizeof(struct ctdb_pulldb_ext);
315 void ctdb_pulldb_ext_push(struct ctdb_pulldb_ext *pulldb, uint8_t *buf)
317 memcpy(buf, pulldb, sizeof(struct ctdb_pulldb_ext));
320 int ctdb_pulldb_ext_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
321 struct ctdb_pulldb_ext **out)
323 struct ctdb_pulldb_ext *pulldb;
325 if (buflen < sizeof(struct ctdb_pulldb_ext)) {
329 pulldb = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_pulldb_ext));
330 if (pulldb == NULL) {
338 size_t ctdb_ltdb_header_len(struct ctdb_ltdb_header *header)
340 return sizeof(struct ctdb_ltdb_header);
343 void ctdb_ltdb_header_push(struct ctdb_ltdb_header *header, uint8_t *buf)
345 memcpy(buf, header, sizeof(struct ctdb_ltdb_header));
348 int ctdb_ltdb_header_pull(uint8_t *buf, size_t buflen,
349 struct ctdb_ltdb_header *header)
351 if (buflen < sizeof(struct ctdb_ltdb_header)) {
355 memcpy(header, buf, sizeof(struct ctdb_ltdb_header));
359 int ctdb_ltdb_header_extract(TDB_DATA *data, struct ctdb_ltdb_header *header)
363 ret = ctdb_ltdb_header_pull(data->dptr, data->dsize, header);
368 data->dptr += sizeof(struct ctdb_ltdb_header);
369 data->dsize -= sizeof(struct ctdb_ltdb_header);
374 struct ctdb_rec_data_wire {
382 size_t ctdb_rec_data_len(struct ctdb_rec_data *rec)
384 return offsetof(struct ctdb_rec_data_wire, data) +
385 rec->key.dsize + rec->data.dsize +
386 (rec->header == NULL ? 0 : sizeof(struct ctdb_ltdb_header));
389 void ctdb_rec_data_push(struct ctdb_rec_data *rec, uint8_t *buf)
391 struct ctdb_rec_data_wire *wire = (struct ctdb_rec_data_wire *)buf;
394 wire->length = ctdb_rec_data_len(rec);
395 wire->reqid = rec->reqid;
396 wire->keylen = rec->key.dsize;
397 wire->datalen = rec->data.dsize;
398 if (rec->header != NULL) {
399 wire->datalen += sizeof(struct ctdb_ltdb_header);
402 memcpy(wire->data, rec->key.dptr, rec->key.dsize);
403 offset = rec->key.dsize;
404 if (rec->header != NULL) {
405 memcpy(&wire->data[offset], rec->header,
406 sizeof(struct ctdb_ltdb_header));
407 offset += sizeof(struct ctdb_ltdb_header);
409 if (rec->data.dsize > 0) {
410 memcpy(&wire->data[offset], rec->data.dptr, rec->data.dsize);
414 static int ctdb_rec_data_pull_data(uint8_t *buf, size_t buflen,
416 struct ctdb_ltdb_header **header,
417 TDB_DATA *key, TDB_DATA *data,
420 struct ctdb_rec_data_wire *wire = (struct ctdb_rec_data_wire *)buf;
423 if (buflen < offsetof(struct ctdb_rec_data_wire, data)) {
426 if (wire->keylen > buflen || wire->datalen > buflen) {
429 if (offsetof(struct ctdb_rec_data_wire, data) + wire->keylen <
430 offsetof(struct ctdb_rec_data_wire, data)) {
433 if (offsetof(struct ctdb_rec_data_wire, data) +
434 wire->keylen + wire->datalen <
435 offsetof(struct ctdb_rec_data_wire, data)) {
438 if (buflen < offsetof(struct ctdb_rec_data_wire, data) +
439 wire->keylen + wire->datalen) {
443 *reqid = wire->reqid;
445 key->dsize = wire->keylen;
446 key->dptr = wire->data;
447 offset = wire->keylen;
449 /* Always set header to NULL. If it is required, exact it using
450 * ctdb_rec_data_extract_header()
454 data->dsize = wire->datalen;
455 data->dptr = &wire->data[offset];
457 *reclen = offsetof(struct ctdb_rec_data_wire, data) +
458 wire->keylen + wire->datalen;
463 static int ctdb_rec_data_pull_elems(uint8_t *buf, size_t buflen,
465 struct ctdb_rec_data *out)
468 struct ctdb_ltdb_header *header;
473 ret = ctdb_rec_data_pull_data(buf, buflen, &reqid, &header,
474 &key, &data, &reclen);
482 out->key.dsize = key.dsize;
484 out->key.dptr = talloc_memdup(mem_ctx, key.dptr, key.dsize);
485 if (out->key.dptr == NULL) {
490 out->data.dsize = data.dsize;
491 if (data.dsize > 0) {
492 out->data.dptr = talloc_memdup(mem_ctx, data.dptr, data.dsize);
493 if (out->data.dptr == NULL) {
501 int ctdb_rec_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
502 struct ctdb_rec_data **out)
504 struct ctdb_rec_data *rec;
507 rec = talloc(mem_ctx, struct ctdb_rec_data);
512 ret = ctdb_rec_data_pull_elems(buf, buflen, rec, rec);
521 struct ctdb_rec_buffer_wire {
527 size_t ctdb_rec_buffer_len(struct ctdb_rec_buffer *recbuf)
529 return offsetof(struct ctdb_rec_buffer_wire, data) + recbuf->buflen;
532 void ctdb_rec_buffer_push(struct ctdb_rec_buffer *recbuf, uint8_t *buf)
534 struct ctdb_rec_buffer_wire *wire = (struct ctdb_rec_buffer_wire *)buf;
536 wire->db_id = recbuf->db_id;
537 wire->count = recbuf->count;
538 if (recbuf->buflen > 0) {
539 memcpy(wire->data, recbuf->buf, recbuf->buflen);
543 int ctdb_rec_buffer_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
544 struct ctdb_rec_buffer **out)
546 struct ctdb_rec_buffer *recbuf;
547 struct ctdb_rec_buffer_wire *wire = (struct ctdb_rec_buffer_wire *)buf;
550 if (buflen < offsetof(struct ctdb_rec_buffer_wire, data)) {
554 recbuf = talloc(mem_ctx, struct ctdb_rec_buffer);
555 if (recbuf == NULL) {
559 recbuf->db_id = wire->db_id;
560 recbuf->count = wire->count;
562 offset = offsetof(struct ctdb_rec_buffer_wire, data);
563 recbuf->buflen = buflen - offset;
564 recbuf->buf = talloc_memdup(recbuf, wire->data, recbuf->buflen);
565 if (recbuf->buf == NULL) {
574 struct ctdb_rec_buffer *ctdb_rec_buffer_init(TALLOC_CTX *mem_ctx,
577 struct ctdb_rec_buffer *recbuf;
579 recbuf = talloc_zero(mem_ctx, struct ctdb_rec_buffer);
580 if (recbuf == NULL) {
584 recbuf->db_id = db_id;
589 int ctdb_rec_buffer_add(TALLOC_CTX *mem_ctx, struct ctdb_rec_buffer *recbuf,
590 uint32_t reqid, struct ctdb_ltdb_header *header,
591 TDB_DATA key, TDB_DATA data)
593 struct ctdb_rec_data recdata;
597 recdata.reqid = reqid;
598 recdata.header = header;
602 len = ctdb_rec_data_len(&recdata);
604 ptr = talloc_realloc(mem_ctx, recbuf->buf, uint8_t,
605 recbuf->buflen + len);
610 ctdb_rec_data_push(&recdata, &ptr[recbuf->buflen]);
614 recbuf->buflen += len;
618 int ctdb_rec_buffer_traverse(struct ctdb_rec_buffer *recbuf,
619 ctdb_rec_parser_func_t func,
622 struct ctdb_ltdb_header *header;
625 size_t offset, reclen;
629 for (i=0; i<recbuf->count; i++) {
630 ret = ctdb_rec_data_pull_data(&recbuf->buf[offset],
631 recbuf->buflen - offset,
633 &key, &data, &reclen);
638 ret = func(reqid, header, key, data, private_data);
649 int ctdb_rec_buffer_write(struct ctdb_rec_buffer *recbuf, int fd)
653 n = write(fd, &recbuf->db_id, sizeof(uint32_t));
654 if (n == -1 || n != sizeof(uint32_t)) {
655 return (errno != 0 ? errno : EIO);
657 n = write(fd, &recbuf->count, sizeof(uint32_t));
658 if (n == -1 || n != sizeof(uint32_t)) {
659 return (errno != 0 ? errno : EIO);
661 n = write(fd, &recbuf->buflen, sizeof(size_t));
662 if (n == -1 || n != sizeof(size_t)) {
663 return (errno != 0 ? errno : EIO);
665 n = write(fd, recbuf->buf, recbuf->buflen);
666 if (n == -1 || n != recbuf->buflen) {
667 return (errno != 0 ? errno : EIO);
673 int ctdb_rec_buffer_read(int fd, TALLOC_CTX *mem_ctx,
674 struct ctdb_rec_buffer **out)
676 struct ctdb_rec_buffer *recbuf;
679 recbuf = talloc(mem_ctx, struct ctdb_rec_buffer);
680 if (recbuf == NULL) {
684 n = read(fd, &recbuf->db_id, sizeof(uint32_t));
685 if (n == -1 || n != sizeof(uint32_t)) {
686 return (errno != 0 ? errno : EIO);
688 n = read(fd, &recbuf->count, sizeof(uint32_t));
689 if (n == -1 || n != sizeof(uint32_t)) {
690 return (errno != 0 ? errno : EIO);
692 n = read(fd, &recbuf->buflen, sizeof(size_t));
693 if (n == -1 || n != sizeof(size_t)) {
694 return (errno != 0 ? errno : EIO);
697 recbuf->buf = talloc_size(recbuf, recbuf->buflen);
698 if (recbuf->buf == NULL) {
702 n = read(fd, recbuf->buf, recbuf->buflen);
703 if (n == -1 || n != recbuf->buflen) {
704 return (errno != 0 ? errno : EIO);
711 size_t ctdb_traverse_start_len(struct ctdb_traverse_start *traverse)
713 return sizeof(struct ctdb_traverse_start);
716 void ctdb_traverse_start_push(struct ctdb_traverse_start *traverse,
719 memcpy(buf, traverse, sizeof(struct ctdb_traverse_start));
722 int ctdb_traverse_start_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
723 struct ctdb_traverse_start **out)
725 struct ctdb_traverse_start *traverse;
727 if (buflen < sizeof(struct ctdb_traverse_start)) {
731 traverse = talloc_memdup(mem_ctx, buf,
732 sizeof(struct ctdb_traverse_start));
733 if (traverse == NULL) {
741 size_t ctdb_traverse_all_len(struct ctdb_traverse_all *traverse)
743 return sizeof(struct ctdb_traverse_all);
746 void ctdb_traverse_all_push(struct ctdb_traverse_all *traverse, uint8_t *buf)
748 memcpy(buf, traverse, sizeof(struct ctdb_traverse_all));
751 int ctdb_traverse_all_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
752 struct ctdb_traverse_all **out)
754 struct ctdb_traverse_all *traverse;
756 if (buflen < sizeof(struct ctdb_traverse_all)) {
760 traverse = talloc_memdup(mem_ctx, buf,
761 sizeof(struct ctdb_traverse_all));
762 if (traverse == NULL) {
770 size_t ctdb_traverse_start_ext_len(struct ctdb_traverse_start_ext *traverse)
772 return sizeof(struct ctdb_traverse_start_ext);
775 void ctdb_traverse_start_ext_push(struct ctdb_traverse_start_ext *traverse,
778 memcpy(buf, traverse, sizeof(struct ctdb_traverse_start_ext));
781 int ctdb_traverse_start_ext_pull(uint8_t *buf, size_t buflen,
783 struct ctdb_traverse_start_ext **out)
785 struct ctdb_traverse_start_ext *traverse;
787 if (buflen < sizeof(struct ctdb_traverse_start_ext)) {
791 traverse = talloc_memdup(mem_ctx, buf,
792 sizeof(struct ctdb_traverse_start_ext));
793 if (traverse == NULL) {
801 size_t ctdb_traverse_all_ext_len(struct ctdb_traverse_all_ext *traverse)
803 return sizeof(struct ctdb_traverse_all_ext);
806 void ctdb_traverse_all_ext_push(struct ctdb_traverse_all_ext *traverse,
809 memcpy(buf, traverse, sizeof(struct ctdb_traverse_all_ext));
812 int ctdb_traverse_all_ext_pull(uint8_t *buf, size_t buflen,
814 struct ctdb_traverse_all_ext **out)
816 struct ctdb_traverse_all_ext *traverse;
818 if (buflen < sizeof(struct ctdb_traverse_all_ext)) {
822 traverse = talloc_memdup(mem_ctx, buf,
823 sizeof(struct ctdb_traverse_all_ext));
824 if (traverse == NULL) {
832 size_t ctdb_sock_addr_len(ctdb_sock_addr *addr)
834 return sizeof(ctdb_sock_addr);
837 void ctdb_sock_addr_push(ctdb_sock_addr *addr, uint8_t *buf)
839 memcpy(buf, addr, sizeof(ctdb_sock_addr));
842 static int ctdb_sock_addr_pull_elems(uint8_t *buf, size_t buflen,
843 TALLOC_CTX *mem_ctx, ctdb_sock_addr *out)
845 if (buflen < sizeof(ctdb_sock_addr)) {
849 memcpy(out, buf, sizeof(ctdb_sock_addr));
854 int ctdb_sock_addr_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
855 ctdb_sock_addr **out)
857 ctdb_sock_addr *addr;
860 addr = talloc(mem_ctx, ctdb_sock_addr);
865 ret = ctdb_sock_addr_pull_elems(buf, buflen, addr, addr);
874 size_t ctdb_connection_len(struct ctdb_connection *conn)
876 return sizeof(struct ctdb_connection);
879 void ctdb_connection_push(struct ctdb_connection *conn, uint8_t *buf)
881 memcpy(buf, conn, sizeof(struct ctdb_connection));
884 static int ctdb_connection_pull_elems(uint8_t *buf, size_t buflen,
886 struct ctdb_connection *out)
888 if (buflen < sizeof(struct ctdb_connection)) {
892 memcpy(out, buf, sizeof(struct ctdb_connection));
897 int ctdb_connection_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
898 struct ctdb_connection **out)
900 struct ctdb_connection *conn;
903 conn = talloc(mem_ctx, struct ctdb_connection);
908 ret = ctdb_connection_pull_elems(buf, buflen, conn, conn);
917 struct ctdb_tunable_wire {
923 size_t ctdb_tunable_len(struct ctdb_tunable *tunable)
925 return offsetof(struct ctdb_tunable_wire, name) +
926 strlen(tunable->name) + 1;
929 void ctdb_tunable_push(struct ctdb_tunable *tunable, uint8_t *buf)
931 struct ctdb_tunable_wire *wire = (struct ctdb_tunable_wire *)buf;
933 wire->value = tunable->value;
934 wire->length = strlen(tunable->name) + 1;
935 memcpy(wire->name, tunable->name, wire->length);
938 int ctdb_tunable_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
939 struct ctdb_tunable **out)
941 struct ctdb_tunable *tunable;
942 struct ctdb_tunable_wire *wire = (struct ctdb_tunable_wire *)buf;
944 if (buflen < offsetof(struct ctdb_tunable_wire, name)) {
947 if (wire->length > buflen) {
950 if (offsetof(struct ctdb_tunable_wire, name) + wire->length <
951 offsetof(struct ctdb_tunable_wire, name)) {
954 if (buflen < offsetof(struct ctdb_tunable_wire, name) + wire->length) {
958 tunable = talloc(mem_ctx, struct ctdb_tunable);
959 if (tunable == NULL) {
963 tunable->value = wire->value;
964 tunable->name = talloc_memdup(tunable, wire->name, wire->length);
965 if (tunable->name == NULL) {
966 talloc_free(tunable);
974 size_t ctdb_node_flag_change_len(struct ctdb_node_flag_change *flag_change)
976 return sizeof(struct ctdb_node_flag_change);
979 void ctdb_node_flag_change_push(struct ctdb_node_flag_change *flag_change,
982 memcpy(buf, flag_change, sizeof(struct ctdb_node_flag_change));
985 int ctdb_node_flag_change_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
986 struct ctdb_node_flag_change **out)
988 struct ctdb_node_flag_change *flag_change;
990 if (buflen < sizeof(struct ctdb_node_flag_change)) {
994 flag_change = talloc_memdup(mem_ctx, buf,
995 sizeof(struct ctdb_node_flag_change));
996 if (flag_change == NULL) {
1004 struct ctdb_var_list_wire {
1009 size_t ctdb_var_list_len(struct ctdb_var_list *var_list)
1012 size_t len = sizeof(uint32_t);
1014 for (i=0; i<var_list->count; i++) {
1015 len += strlen(var_list->var[i]) + 1;
1020 void ctdb_var_list_push(struct ctdb_var_list *var_list, uint8_t *buf)
1022 struct ctdb_var_list_wire *wire = (struct ctdb_var_list_wire *)buf;
1026 if (var_list->count > 0) {
1027 n = sprintf(wire->list_str, "%s", var_list->var[0]);
1030 for (i=1; i<var_list->count; i++) {
1031 n = sprintf(&wire->list_str[offset], ":%s", var_list->var[i]);
1034 wire->length = offset + 1;
1037 int ctdb_var_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1038 struct ctdb_var_list **out)
1040 struct ctdb_var_list *var_list = NULL;
1041 struct ctdb_var_list_wire *wire = (struct ctdb_var_list_wire *)buf;
1042 char *str, *s, *tok, *ptr;
1045 if (buflen < sizeof(uint32_t)) {
1048 if (wire->length > buflen) {
1051 if (sizeof(uint32_t) + wire->length < sizeof(uint32_t)) {
1054 if (buflen < sizeof(uint32_t) + wire->length) {
1058 str = talloc_strndup(mem_ctx, (char *)wire->list_str, wire->length);
1063 var_list = talloc_zero(mem_ctx, struct ctdb_var_list);
1064 if (var_list == NULL) {
1069 while ((tok = strtok_r(s, ":", &ptr)) != NULL) {
1071 list = talloc_realloc(var_list, var_list->var, const char *,
1077 var_list->var = list;
1078 var_list->var[var_list->count] = talloc_strdup(var_list, tok);
1079 if (var_list->var[var_list->count] == NULL) {
1091 talloc_free(var_list);
1095 size_t ctdb_tunable_list_len(struct ctdb_tunable_list *tun_list)
1097 return sizeof(struct ctdb_tunable_list);
1100 void ctdb_tunable_list_push(struct ctdb_tunable_list *tun_list, uint8_t *buf)
1102 memcpy(buf, tun_list, sizeof(struct ctdb_tunable_list));
1105 int ctdb_tunable_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1106 struct ctdb_tunable_list **out)
1108 struct ctdb_tunable_list *tun_list;
1110 if (buflen < sizeof(struct ctdb_tunable_list)) {
1114 tun_list = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_tunable_list));
1115 if (tun_list == NULL) {
1123 struct ctdb_tickle_list_wire {
1124 ctdb_sock_addr addr;
1126 struct ctdb_connection conn[1];
1129 size_t ctdb_tickle_list_len(struct ctdb_tickle_list *tickles)
1131 return offsetof(struct ctdb_tickle_list, conn) +
1132 tickles->num * sizeof(struct ctdb_connection);
1135 void ctdb_tickle_list_push(struct ctdb_tickle_list *tickles, uint8_t *buf)
1137 struct ctdb_tickle_list_wire *wire =
1138 (struct ctdb_tickle_list_wire *)buf;
1142 memcpy(&wire->addr, &tickles->addr, sizeof(ctdb_sock_addr));
1143 wire->num = tickles->num;
1145 offset = offsetof(struct ctdb_tickle_list_wire, conn);
1146 for (i=0; i<tickles->num; i++) {
1147 ctdb_connection_push(&tickles->conn[i], &buf[offset]);
1148 offset += ctdb_connection_len(&tickles->conn[i]);
1152 int ctdb_tickle_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1153 struct ctdb_tickle_list **out)
1155 struct ctdb_tickle_list *tickles;
1156 struct ctdb_tickle_list_wire *wire =
1157 (struct ctdb_tickle_list_wire *)buf;
1161 if (buflen < offsetof(struct ctdb_tickle_list_wire, conn)) {
1164 if (wire->num > buflen / sizeof(struct ctdb_connection)) {
1167 if (offsetof(struct ctdb_tickle_list_wire, conn) +
1168 wire->num * sizeof(struct ctdb_connection) <
1169 offsetof(struct ctdb_tickle_list_wire, conn)) {
1172 if (buflen < offsetof(struct ctdb_tickle_list_wire, conn) +
1173 wire->num * sizeof(struct ctdb_connection)) {
1177 tickles = talloc(mem_ctx, struct ctdb_tickle_list);
1178 if (tickles == NULL) {
1182 offset = offsetof(struct ctdb_tickle_list, conn);
1183 memcpy(tickles, wire, offset);
1185 tickles->conn = talloc_array(tickles, struct ctdb_connection,
1187 if (tickles->conn == NULL) {
1188 talloc_free(tickles);
1192 for (i=0; i<wire->num; i++) {
1193 ret = ctdb_connection_pull_elems(&buf[offset], buflen-offset,
1197 talloc_free(tickles);
1200 offset += ctdb_connection_len(&tickles->conn[i]);
1207 struct ctdb_addr_info_wire {
1208 ctdb_sock_addr addr;
1214 size_t ctdb_addr_info_len(struct ctdb_addr_info *arp)
1218 len = offsetof(struct ctdb_addr_info_wire, iface);
1219 if (arp->iface != NULL) {
1220 len += strlen(arp->iface)+1;
1226 void ctdb_addr_info_push(struct ctdb_addr_info *addr_info, uint8_t *buf)
1228 struct ctdb_addr_info_wire *wire = (struct ctdb_addr_info_wire *)buf;
1230 wire->addr = addr_info->addr;
1231 wire->mask = addr_info->mask;
1232 if (addr_info->iface == NULL) {
1235 wire->len = strlen(addr_info->iface)+1;
1236 memcpy(wire->iface, addr_info->iface, wire->len);
1240 int ctdb_addr_info_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1241 struct ctdb_addr_info **out)
1243 struct ctdb_addr_info *addr_info;
1244 struct ctdb_addr_info_wire *wire = (struct ctdb_addr_info_wire *)buf;
1246 if (buflen < offsetof(struct ctdb_addr_info_wire, iface)) {
1249 if (wire->len > buflen) {
1252 if (offsetof(struct ctdb_addr_info_wire, iface) + wire->len <
1253 offsetof(struct ctdb_addr_info_wire, iface)) {
1256 if (buflen < offsetof(struct ctdb_addr_info_wire, iface) + wire->len) {
1260 addr_info = talloc(mem_ctx, struct ctdb_addr_info);
1261 if (addr_info == NULL) {
1265 addr_info->addr = wire->addr;
1266 addr_info->mask = wire->mask;
1268 if (wire->len == 0) {
1269 addr_info->iface = NULL;
1271 addr_info->iface = talloc_strndup(addr_info, wire->iface,
1273 if (addr_info->iface == NULL) {
1274 talloc_free(addr_info);
1283 size_t ctdb_transdb_len(struct ctdb_transdb *transdb)
1285 return sizeof(struct ctdb_transdb);
1288 void ctdb_transdb_push(struct ctdb_transdb *transdb, uint8_t *buf)
1290 memcpy(buf, transdb, sizeof(struct ctdb_transdb));
1293 int ctdb_transdb_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1294 struct ctdb_transdb **out)
1296 struct ctdb_transdb *transdb;
1298 if (buflen < sizeof(struct ctdb_transdb)) {
1302 transdb = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_transdb));
1303 if (transdb == NULL) {
1311 size_t ctdb_uptime_len(struct ctdb_uptime *uptime)
1313 return sizeof(struct ctdb_uptime);
1316 void ctdb_uptime_push(struct ctdb_uptime *uptime, uint8_t *buf)
1318 memcpy(buf, uptime, sizeof(struct ctdb_uptime));
1321 int ctdb_uptime_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1322 struct ctdb_uptime **out)
1324 struct ctdb_uptime *uptime;
1326 if (buflen < sizeof(struct ctdb_uptime)) {
1330 uptime = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_uptime));
1331 if (uptime == NULL) {
1339 size_t ctdb_public_ip_len(struct ctdb_public_ip *pubip)
1341 return sizeof(struct ctdb_public_ip);
1344 void ctdb_public_ip_push(struct ctdb_public_ip *pubip, uint8_t *buf)
1346 memcpy(buf, pubip, sizeof(struct ctdb_public_ip));
1349 static int ctdb_public_ip_pull_elems(uint8_t *buf, size_t buflen,
1350 TALLOC_CTX *mem_ctx,
1351 struct ctdb_public_ip *out)
1353 if (buflen < sizeof(struct ctdb_public_ip)) {
1357 memcpy(out, buf, sizeof(struct ctdb_public_ip));
1362 int ctdb_public_ip_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1363 struct ctdb_public_ip **out)
1365 struct ctdb_public_ip *pubip;
1368 pubip = talloc(mem_ctx, struct ctdb_public_ip);
1369 if (pubip == NULL) {
1373 ret = ctdb_public_ip_pull_elems(buf, buflen, pubip, pubip);
1382 struct ctdb_public_ip_list_wire {
1384 struct ctdb_public_ip ip[1];
1387 size_t ctdb_public_ip_list_len(struct ctdb_public_ip_list *pubip_list)
1392 len = sizeof(uint32_t);
1393 for (i=0; i<pubip_list->num; i++) {
1394 len += ctdb_public_ip_len(&pubip_list->ip[i]);
1399 void ctdb_public_ip_list_push(struct ctdb_public_ip_list *pubip_list,
1402 struct ctdb_public_ip_list_wire *wire =
1403 (struct ctdb_public_ip_list_wire *)buf;
1407 wire->num = pubip_list->num;
1409 offset = offsetof(struct ctdb_public_ip_list_wire, ip);
1410 for (i=0; i<pubip_list->num; i++) {
1411 ctdb_public_ip_push(&pubip_list->ip[i], &buf[offset]);
1412 offset += ctdb_public_ip_len(&pubip_list->ip[i]);
1416 int ctdb_public_ip_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1417 struct ctdb_public_ip_list **out)
1419 struct ctdb_public_ip_list *pubip_list;
1420 struct ctdb_public_ip_list_wire *wire =
1421 (struct ctdb_public_ip_list_wire *)buf;
1426 if (buflen < sizeof(uint32_t)) {
1429 if (wire->num > buflen / sizeof(struct ctdb_public_ip)) {
1432 if (sizeof(uint32_t) + wire->num * sizeof(struct ctdb_public_ip) <
1436 if (buflen < sizeof(uint32_t) +
1437 wire->num * sizeof(struct ctdb_public_ip)) {
1441 pubip_list = talloc(mem_ctx, struct ctdb_public_ip_list);
1442 if (pubip_list == NULL) {
1446 pubip_list->num = wire->num;
1447 if (wire->num == 0) {
1448 pubip_list->ip = NULL;
1452 pubip_list->ip = talloc_array(pubip_list, struct ctdb_public_ip,
1454 if (pubip_list->ip == NULL) {
1455 talloc_free(pubip_list);
1459 offset = offsetof(struct ctdb_public_ip_list_wire, ip);
1460 for (i=0; i<wire->num; i++) {
1461 ret = ctdb_public_ip_pull_elems(&buf[offset], buflen-offset,
1463 &pubip_list->ip[i]);
1465 talloc_free(pubip_list);
1468 offset += ctdb_public_ip_len(&pubip_list->ip[i]);
1475 size_t ctdb_node_and_flags_len(struct ctdb_node_and_flags *node)
1477 return sizeof(struct ctdb_node_and_flags);
1480 void ctdb_node_and_flags_push(struct ctdb_node_and_flags *node, uint8_t *buf)
1482 memcpy(buf, node, sizeof(struct ctdb_node_and_flags));
1485 static int ctdb_node_and_flags_pull_elems(TALLOC_CTX *mem_ctx,
1486 uint8_t *buf, size_t buflen,
1487 struct ctdb_node_and_flags *out)
1489 if (buflen < sizeof(struct ctdb_node_and_flags)) {
1493 memcpy(out, buf, sizeof(struct ctdb_node_and_flags));
1498 int ctdb_node_and_flags_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1499 struct ctdb_node_and_flags **out)
1501 struct ctdb_node_and_flags *node;
1504 node = talloc(mem_ctx, struct ctdb_node_and_flags);
1509 ret = ctdb_node_and_flags_pull_elems(node, buf, buflen, node);
1518 struct ctdb_node_map_wire {
1520 struct ctdb_node_and_flags node[1];
1523 size_t ctdb_node_map_len(struct ctdb_node_map *nodemap)
1525 return sizeof(uint32_t) +
1526 nodemap->num * sizeof(struct ctdb_node_and_flags);
1529 void ctdb_node_map_push(struct ctdb_node_map *nodemap, uint8_t *buf)
1531 struct ctdb_node_map_wire *wire = (struct ctdb_node_map_wire *)buf;
1535 wire->num = nodemap->num;
1537 offset = offsetof(struct ctdb_node_map_wire, node);
1538 for (i=0; i<nodemap->num; i++) {
1539 ctdb_node_and_flags_push(&nodemap->node[i], &buf[offset]);
1540 offset += ctdb_node_and_flags_len(&nodemap->node[i]);
1544 int ctdb_node_map_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1545 struct ctdb_node_map **out)
1547 struct ctdb_node_map *nodemap;
1548 struct ctdb_node_map_wire *wire = (struct ctdb_node_map_wire *)buf;
1553 if (buflen < sizeof(uint32_t)) {
1556 if (wire->num > buflen / sizeof(struct ctdb_node_and_flags)) {
1559 if (sizeof(uint32_t) + wire->num * sizeof(struct ctdb_node_and_flags) <
1563 if (buflen < sizeof(uint32_t) +
1564 wire->num * sizeof(struct ctdb_node_and_flags)) {
1568 nodemap = talloc(mem_ctx, struct ctdb_node_map);
1569 if (nodemap == NULL) {
1573 nodemap->num = wire->num;
1574 nodemap->node = talloc_array(nodemap, struct ctdb_node_and_flags,
1576 if (nodemap->node == NULL) {
1577 talloc_free(nodemap);
1581 offset = offsetof(struct ctdb_node_map_wire, node);
1582 for (i=0; i<wire->num; i++) {
1583 ret = ctdb_node_and_flags_pull_elems(nodemap->node,
1588 talloc_free(nodemap);
1591 offset += ctdb_node_and_flags_len(&nodemap->node[i]);
1598 size_t ctdb_script_len(struct ctdb_script *script)
1600 return sizeof(struct ctdb_script);
1603 void ctdb_script_push(struct ctdb_script *script, uint8_t *buf)
1605 memcpy(buf, script, sizeof(struct ctdb_script));
1608 static int ctdb_script_pull_elems(uint8_t *buf, size_t buflen,
1609 TALLOC_CTX *mem_ctx,
1610 struct ctdb_script *out)
1612 if (buflen < sizeof(struct ctdb_script)) {
1616 memcpy(out, buf, sizeof(struct ctdb_script));
1621 int ctdb_script_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1622 struct ctdb_script **out)
1624 struct ctdb_script *script;
1627 script = talloc(mem_ctx, struct ctdb_script);
1628 if (script == NULL) {
1632 ret = ctdb_script_pull_elems(buf, buflen, script, script);
1634 TALLOC_FREE(script);
1641 struct ctdb_script_list_wire {
1642 uint32_t num_scripts;
1643 struct ctdb_script script[1];
1646 size_t ctdb_script_list_len(struct ctdb_script_list *script_list)
1651 if (script_list == NULL) {
1655 len = offsetof(struct ctdb_script_list_wire, script);
1656 for (i=0; i<script_list->num_scripts; i++) {
1657 len += ctdb_script_len(&script_list->script[i]);
1662 void ctdb_script_list_push(struct ctdb_script_list *script_list, uint8_t *buf)
1664 struct ctdb_script_list_wire *wire =
1665 (struct ctdb_script_list_wire *)buf;
1669 if (script_list == NULL) {
1673 wire->num_scripts = script_list->num_scripts;
1675 offset = offsetof(struct ctdb_script_list_wire, script);
1676 for (i=0; i<script_list->num_scripts; i++) {
1677 ctdb_script_push(&script_list->script[i], &buf[offset]);
1678 offset += ctdb_script_len(&script_list->script[i]);
1682 int ctdb_script_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1683 struct ctdb_script_list **out)
1685 struct ctdb_script_list *script_list;
1686 struct ctdb_script_list_wire *wire =
1687 (struct ctdb_script_list_wire *)buf;
1692 /* If event scripts have never been run, the result will be NULL */
1698 offset = offsetof(struct ctdb_script_list_wire, script);
1700 if (buflen < offset) {
1703 if (wire->num_scripts > buflen / sizeof(struct ctdb_script)) {
1706 if (offset + wire->num_scripts * sizeof(struct ctdb_script) < offset) {
1709 if (buflen < offset + wire->num_scripts * sizeof(struct ctdb_script)) {
1713 script_list = talloc(mem_ctx, struct ctdb_script_list);
1714 if (script_list == NULL) {
1719 script_list->num_scripts = wire->num_scripts;
1720 script_list->script = talloc_array(script_list, struct ctdb_script,
1722 if (script_list->script == NULL) {
1723 talloc_free(script_list);
1727 for (i=0; i<wire->num_scripts; i++) {
1728 ret = ctdb_script_pull_elems(&buf[offset], buflen-offset,
1729 script_list->script,
1730 &script_list->script[i]);
1732 talloc_free(script_list);
1735 offset += ctdb_script_len(&script_list->script[i]);
1742 size_t ctdb_ban_state_len(struct ctdb_ban_state *ban_state)
1744 return sizeof(struct ctdb_ban_state);
1747 void ctdb_ban_state_push(struct ctdb_ban_state *ban_state, uint8_t *buf)
1749 memcpy(buf, ban_state, sizeof(struct ctdb_ban_state));
1752 int ctdb_ban_state_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1753 struct ctdb_ban_state **out)
1755 struct ctdb_ban_state *ban_state;
1757 if (buflen < sizeof(struct ctdb_ban_state)) {
1761 ban_state = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_ban_state));
1762 if (ban_state == NULL) {
1770 struct ctdb_notify_data_wire {
1776 size_t ctdb_notify_data_len(struct ctdb_notify_data *notify)
1778 return offsetof(struct ctdb_notify_data_wire, data) +
1782 void ctdb_notify_data_push(struct ctdb_notify_data *notify, uint8_t *buf)
1784 struct ctdb_notify_data_wire *wire =
1785 (struct ctdb_notify_data_wire *)buf;
1787 wire->srvid = notify->srvid;
1788 wire->len = notify->data.dsize;
1789 memcpy(wire->data, notify->data.dptr, notify->data.dsize);
1792 int ctdb_notify_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1793 struct ctdb_notify_data **out)
1795 struct ctdb_notify_data *notify;
1796 struct ctdb_notify_data_wire *wire =
1797 (struct ctdb_notify_data_wire *)buf;
1799 if (buflen < offsetof(struct ctdb_notify_data_wire, data)) {
1802 if (wire->len > buflen) {
1805 if (offsetof(struct ctdb_notify_data_wire, data) + wire->len <
1806 offsetof(struct ctdb_notify_data_wire, data)) {
1809 if (buflen < offsetof(struct ctdb_notify_data_wire, data) + wire->len) {
1813 notify = talloc(mem_ctx, struct ctdb_notify_data);
1814 if (notify == NULL) {
1818 notify->srvid = wire->srvid;
1819 notify->data.dsize = wire->len;
1820 notify->data.dptr = talloc_memdup(notify, wire->data, wire->len);
1821 if (notify->data.dptr == NULL) {
1822 talloc_free(notify);
1830 size_t ctdb_iface_len(struct ctdb_iface *iface)
1832 return sizeof(struct ctdb_iface);
1835 void ctdb_iface_push(struct ctdb_iface *iface, uint8_t *buf)
1837 memcpy(buf, iface, sizeof(struct ctdb_iface));
1840 static int ctdb_iface_pull_elems(uint8_t *buf, size_t buflen,
1841 TALLOC_CTX *mem_ctx,
1842 struct ctdb_iface *out)
1844 if (buflen < sizeof(struct ctdb_iface)) {
1848 memcpy(out, buf, sizeof(struct ctdb_iface));
1853 int ctdb_iface_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1854 struct ctdb_iface **out)
1856 struct ctdb_iface *iface;
1859 iface = talloc(mem_ctx, struct ctdb_iface);
1860 if (iface == NULL) {
1864 ret = ctdb_iface_pull_elems(buf, buflen, iface, iface);
1873 struct ctdb_iface_list_wire {
1875 struct ctdb_iface iface[1];
1878 size_t ctdb_iface_list_len(struct ctdb_iface_list *iface_list)
1880 return sizeof(uint32_t) +
1881 iface_list->num * sizeof(struct ctdb_iface);
1884 void ctdb_iface_list_push(struct ctdb_iface_list *iface_list, uint8_t *buf)
1886 struct ctdb_iface_list_wire *wire =
1887 (struct ctdb_iface_list_wire *)buf;
1889 wire->num = iface_list->num;
1890 memcpy(wire->iface, iface_list->iface,
1891 iface_list->num * sizeof(struct ctdb_iface));
1894 int ctdb_iface_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1895 struct ctdb_iface_list **out)
1897 struct ctdb_iface_list *iface_list;
1898 struct ctdb_iface_list_wire *wire =
1899 (struct ctdb_iface_list_wire *)buf;
1901 if (buflen < sizeof(uint32_t)) {
1904 if (wire->num > buflen / sizeof(struct ctdb_iface)) {
1907 if (sizeof(uint32_t) + wire->num * sizeof(struct ctdb_iface) <
1911 if (buflen < sizeof(uint32_t) + wire->num * sizeof(struct ctdb_iface)) {
1915 iface_list = talloc(mem_ctx, struct ctdb_iface_list);
1916 if (iface_list == NULL) {
1920 iface_list->num = wire->num;
1921 iface_list->iface = talloc_array(iface_list, struct ctdb_iface,
1923 if (iface_list->iface == NULL) {
1924 talloc_free(iface_list);
1928 memcpy(iface_list->iface, wire->iface,
1929 wire->num * sizeof(struct ctdb_iface));
1935 struct ctdb_public_ip_info_wire {
1936 struct ctdb_public_ip ip;
1937 uint32_t active_idx;
1939 struct ctdb_iface ifaces[1];
1942 size_t ctdb_public_ip_info_len(struct ctdb_public_ip_info *ipinfo)
1944 return offsetof(struct ctdb_public_ip_info_wire, num) +
1945 ctdb_iface_list_len(ipinfo->ifaces);
1948 void ctdb_public_ip_info_push(struct ctdb_public_ip_info *ipinfo, uint8_t *buf)
1950 struct ctdb_public_ip_info_wire *wire =
1951 (struct ctdb_public_ip_info_wire *)buf;
1954 offset = offsetof(struct ctdb_public_ip_info_wire, num);
1955 memcpy(wire, ipinfo, offset);
1956 wire->num = ipinfo->ifaces->num;
1957 memcpy(wire->ifaces, ipinfo->ifaces->iface,
1958 ipinfo->ifaces->num * sizeof(struct ctdb_iface));
1961 int ctdb_public_ip_info_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1962 struct ctdb_public_ip_info **out)
1964 struct ctdb_public_ip_info *ipinfo;
1965 struct ctdb_public_ip_info_wire *wire =
1966 (struct ctdb_public_ip_info_wire *)buf;
1968 if (buflen < offsetof(struct ctdb_public_ip_info_wire, ifaces)) {
1971 if (wire->num > buflen / sizeof(struct ctdb_iface)) {
1974 if (offsetof(struct ctdb_public_ip_info_wire, ifaces) +
1975 wire->num * sizeof(struct ctdb_iface) <
1976 offsetof(struct ctdb_public_ip_info_wire, ifaces)) {
1979 if (buflen < offsetof(struct ctdb_public_ip_info_wire, ifaces) +
1980 wire->num * sizeof(struct ctdb_iface)) {
1984 ipinfo = talloc(mem_ctx, struct ctdb_public_ip_info);
1985 if (ipinfo == NULL) {
1989 memcpy(ipinfo, wire, offsetof(struct ctdb_public_ip_info_wire, num));
1991 ipinfo->ifaces = talloc(ipinfo, struct ctdb_iface_list);
1992 if (ipinfo->ifaces == NULL) {
1993 talloc_free(ipinfo);
1997 ipinfo->ifaces->num = wire->num;
1998 ipinfo->ifaces->iface = talloc_array(ipinfo->ifaces, struct ctdb_iface,
2000 if (ipinfo->ifaces->iface == NULL) {
2001 talloc_free(ipinfo);
2005 memcpy(ipinfo->ifaces->iface, wire->ifaces,
2006 wire->num * sizeof(struct ctdb_iface));
2012 struct ctdb_key_data_wire {
2014 struct ctdb_ltdb_header header;
2019 size_t ctdb_key_data_len(struct ctdb_key_data *key)
2021 return offsetof(struct ctdb_key_data_wire, key) + key->key.dsize;
2024 void ctdb_key_data_push(struct ctdb_key_data *key, uint8_t *buf)
2026 struct ctdb_key_data_wire *wire = (struct ctdb_key_data_wire *)buf;
2028 memcpy(wire, key, offsetof(struct ctdb_key_data, key));
2029 wire->keylen = key->key.dsize;
2030 memcpy(wire->key, key->key.dptr, key->key.dsize);
2033 int ctdb_key_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2034 struct ctdb_key_data **out)
2036 struct ctdb_key_data *key_data;
2037 struct ctdb_key_data_wire *wire = (struct ctdb_key_data_wire *)buf;
2039 if (buflen < offsetof(struct ctdb_key_data_wire, key)) {
2042 if (wire->keylen > buflen) {
2045 if (offsetof(struct ctdb_key_data_wire, key) + wire->keylen <
2046 offsetof(struct ctdb_key_data_wire, key)) {
2049 if (buflen < offsetof(struct ctdb_key_data_wire, key) + wire->keylen) {
2053 key_data = talloc(mem_ctx, struct ctdb_key_data);
2054 if (key_data == NULL) {
2058 memcpy(key_data, wire, offsetof(struct ctdb_key_data, key));
2060 key_data->key.dsize = wire->keylen;
2061 key_data->key.dptr = talloc_memdup(key_data, wire->key, wire->keylen);
2062 if (key_data->key.dptr == NULL) {
2063 talloc_free(key_data);
2071 struct ctdb_db_statistics_wire {
2072 struct ctdb_db_statistics dbstats;
2073 char hot_keys_wire[1];
2076 size_t ctdb_db_statistics_len(struct ctdb_db_statistics *dbstats)
2081 len = sizeof(struct ctdb_db_statistics);
2082 for (i=0; i<MAX_HOT_KEYS; i++) {
2083 len += dbstats->hot_keys[i].key.dsize;
2088 void ctdb_db_statistics_push(struct ctdb_db_statistics *dbstats, void *buf)
2090 struct ctdb_db_statistics_wire *wire =
2091 (struct ctdb_db_statistics_wire *)buf;
2095 dbstats->num_hot_keys = MAX_HOT_KEYS;
2096 memcpy(wire, dbstats, sizeof(struct ctdb_db_statistics));
2099 for (i=0; i<MAX_HOT_KEYS; i++) {
2100 memcpy(&wire->hot_keys_wire[offset],
2101 dbstats->hot_keys[i].key.dptr,
2102 dbstats->hot_keys[i].key.dsize);
2103 offset += dbstats->hot_keys[i].key.dsize;
2107 int ctdb_db_statistics_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2108 struct ctdb_db_statistics **out)
2110 struct ctdb_db_statistics *dbstats;
2111 struct ctdb_db_statistics_wire *wire =
2112 (struct ctdb_db_statistics_wire *)buf;
2116 if (buflen < sizeof(struct ctdb_db_statistics)) {
2121 for (i=0; i<wire->dbstats.num_hot_keys; i++) {
2122 if (wire->dbstats.hot_keys[i].key.dsize > buflen) {
2125 if (offset + wire->dbstats.hot_keys[i].key.dsize < offset) {
2128 offset += wire->dbstats.hot_keys[i].key.dsize;
2129 if (offset > buflen) {
2133 if (sizeof(struct ctdb_db_statistics) + offset <
2134 sizeof(struct ctdb_db_statistics)) {
2137 if (buflen < sizeof(struct ctdb_db_statistics) + offset) {
2141 dbstats = talloc(mem_ctx, struct ctdb_db_statistics);
2142 if (dbstats == NULL) {
2146 memcpy(dbstats, wire, sizeof(struct ctdb_db_statistics));
2149 for (i=0; i<wire->dbstats.num_hot_keys; i++) {
2153 key_size = dbstats->hot_keys[i].key.dsize;
2154 ptr = talloc_memdup(mem_ctx, &wire->hot_keys_wire[offset],
2157 talloc_free(dbstats);
2160 dbstats->hot_keys[i].key.dptr = ptr;
2168 size_t ctdb_election_message_len(struct ctdb_election_message *election)
2170 return sizeof(struct ctdb_election_message);
2173 void ctdb_election_message_push(struct ctdb_election_message *election,
2176 memcpy(buf, election, sizeof(struct ctdb_election_message));
2179 int ctdb_election_message_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2180 struct ctdb_election_message **out)
2182 struct ctdb_election_message *election;
2184 if (buflen < sizeof(struct ctdb_election_message)) {
2188 election = talloc_memdup(mem_ctx, buf,
2189 sizeof(struct ctdb_election_message));
2190 if (election == NULL) {
2198 size_t ctdb_srvid_message_len(struct ctdb_srvid_message *msg)
2200 return sizeof(struct ctdb_srvid_message);
2203 void ctdb_srvid_message_push(struct ctdb_srvid_message *msg, uint8_t *buf)
2205 memcpy(buf, msg, sizeof(struct ctdb_srvid_message));
2208 int ctdb_srvid_message_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2209 struct ctdb_srvid_message **out)
2211 struct ctdb_srvid_message *msg;
2213 if (buflen < sizeof(struct ctdb_srvid_message)) {
2217 msg = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_srvid_message));
2226 size_t ctdb_disable_message_len(struct ctdb_disable_message *disable)
2228 return sizeof(struct ctdb_disable_message);
2231 void ctdb_disable_message_push(struct ctdb_disable_message *disable,
2234 memcpy(buf, disable, sizeof(struct ctdb_disable_message));
2237 int ctdb_disable_message_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2238 struct ctdb_disable_message **out)
2240 struct ctdb_disable_message *disable;
2242 if (buflen < sizeof(struct ctdb_disable_message)) {
2246 disable = talloc_memdup(mem_ctx, buf,
2247 sizeof(struct ctdb_disable_message));
2248 if (disable == NULL) {
2256 size_t ctdb_server_id_len(struct ctdb_server_id *sid)
2258 return sizeof(struct ctdb_server_id);
2261 void ctdb_server_id_push(struct ctdb_server_id *sid, uint8_t *buf)
2263 memcpy(buf, sid, sizeof(struct ctdb_server_id));
2266 int ctdb_server_id_pull(uint8_t *buf, size_t buflen,
2267 struct ctdb_server_id *sid)
2269 if (buflen < sizeof(struct ctdb_server_id)) {
2273 memcpy(sid, buf, sizeof(struct ctdb_server_id));
2277 size_t ctdb_g_lock_len(struct ctdb_g_lock *lock)
2279 return sizeof(struct ctdb_g_lock);
2282 void ctdb_g_lock_push(struct ctdb_g_lock *lock, uint8_t *buf)
2284 memcpy(buf, lock, sizeof(struct ctdb_g_lock));
2287 int ctdb_g_lock_pull(uint8_t *buf, size_t buflen, struct ctdb_g_lock *lock)
2289 if (buflen < sizeof(struct ctdb_g_lock)) {
2293 memcpy(lock, buf, sizeof(struct ctdb_g_lock));
2297 size_t ctdb_g_lock_list_len(struct ctdb_g_lock_list *lock_list)
2299 return lock_list->num * sizeof(struct ctdb_g_lock);
2302 void ctdb_g_lock_list_push(struct ctdb_g_lock_list *lock_list, uint8_t *buf)
2307 for (i=0; i<lock_list->num; i++) {
2308 ctdb_g_lock_push(&lock_list->lock[i], &buf[offset]);
2309 offset += sizeof(struct ctdb_g_lock);
2313 int ctdb_g_lock_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2314 struct ctdb_g_lock_list **out)
2316 struct ctdb_g_lock_list *lock_list;
2321 lock_list = talloc_zero(mem_ctx, struct ctdb_g_lock_list);
2322 if (lock_list == NULL) {
2326 count = buflen / sizeof(struct ctdb_g_lock);
2327 lock_list->lock = talloc_array(lock_list, struct ctdb_g_lock, count);
2328 if (lock_list->lock == NULL) {
2329 talloc_free(lock_list);
2334 for (i=0; i<count; i++) {
2335 ret = ctdb_g_lock_pull(&buf[offset], buflen-offset,
2336 &lock_list->lock[i]);
2338 talloc_free(lock_list);
2341 offset += sizeof(struct ctdb_g_lock);
2344 lock_list->num = count;