ctdb-protocol: Fix marshalling of struct ctdb_addr_info
[obnox/samba/samba-obnox.git] / ctdb / protocol / protocol_types.c
1 /*
2    CTDB protocol marshalling
3
4    Copyright (C) Amitay Isaacs  2015
5
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.
10
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.
15
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/>.
18 */
19
20 #include "replace.h"
21 #include "system/network.h"
22
23 #include <talloc.h>
24 #include <tdb.h>
25
26 #include "protocol.h"
27 #include "protocol_private.h"
28 #include "protocol_api.h"
29
30 size_t ctdb_uint32_len(uint32_t val)
31 {
32         return sizeof(uint32_t);
33 }
34
35 void ctdb_uint32_push(uint32_t val, uint8_t *buf)
36 {
37         memcpy(buf, &val, sizeof(uint32_t));
38 }
39
40 int ctdb_uint32_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
41                      uint32_t *out)
42 {
43         if (buflen < sizeof(uint32_t)) {
44                 return EMSGSIZE;
45         }
46
47         *out = *(uint32_t *)buf;
48         return 0;
49 }
50
51 size_t ctdb_uint64_len(uint64_t val)
52 {
53         return sizeof(uint64_t);
54 }
55
56 void ctdb_uint64_push(uint64_t val, uint8_t *buf)
57 {
58         memcpy(buf, &val, sizeof(uint64_t));
59 }
60
61 int ctdb_uint64_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
62                      uint64_t *out)
63 {
64         if (buflen < sizeof(uint64_t)) {
65                 return EMSGSIZE;
66         }
67
68         *out = *(uint64_t *)buf;
69         return 0;
70 }
71
72 size_t ctdb_double_len(double val)
73 {
74         return sizeof(double);
75 }
76
77 void ctdb_double_push(double val, uint8_t *buf)
78 {
79         memcpy(buf, &val, sizeof(double));
80 }
81
82 int ctdb_double_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
83                      double *out)
84 {
85         if (buflen < sizeof(double)) {
86                 return EMSGSIZE;
87         }
88
89         *out = *(double *)buf;
90         return 0;
91 }
92
93 size_t ctdb_uint8_array_len(struct ctdb_uint8_array *array)
94 {
95         return array->num * sizeof(uint8_t);
96 }
97
98 void ctdb_uint8_array_push(struct ctdb_uint8_array *array, uint8_t *buf)
99 {
100         memcpy(buf, array->val, array->num * sizeof(uint8_t));
101 }
102
103 int ctdb_uint8_array_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
104                           struct ctdb_uint8_array **out)
105 {
106         struct ctdb_uint8_array *array;
107
108         array = talloc(mem_ctx, struct ctdb_uint8_array);
109         if (array == NULL) {
110                 return ENOMEM;
111         }
112
113         array->num = buflen / sizeof(uint8_t);
114
115         array->val = talloc_array(array, uint8_t, array->num);
116         if (array->val == NULL) {
117                 talloc_free(array);
118                 return ENOMEM;
119         }
120         memcpy(array->val, buf, buflen);
121
122         *out = array;
123         return 0;
124 }
125
126 size_t ctdb_uint64_array_len(struct ctdb_uint64_array *array)
127 {
128         return array->num * sizeof(uint64_t);
129 }
130
131 void ctdb_uint64_array_push(struct ctdb_uint64_array *array, uint8_t *buf)
132 {
133         memcpy(buf, array->val, array->num * sizeof(uint64_t));
134 }
135
136 int ctdb_uint64_array_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
137                            struct ctdb_uint64_array **out)
138 {
139         struct ctdb_uint64_array *array;
140
141         array = talloc(mem_ctx, struct ctdb_uint64_array);
142         if (array == NULL) {
143                 return ENOMEM;
144         }
145
146         array->num = buflen / sizeof(uint64_t);
147
148         array->val = talloc_array(array, uint64_t, array->num);
149         if (array->val == NULL) {
150                 talloc_free(array);
151                 return ENOMEM;
152         }
153         memcpy(array->val, buf, buflen);
154
155         *out = array;
156         return 0;
157 }
158
159 size_t ctdb_pid_len(pid_t pid)
160 {
161         return sizeof(pid_t);
162 }
163
164 void ctdb_pid_push(pid_t pid, uint8_t *buf)
165 {
166         memcpy(buf, &pid, sizeof(pid_t));
167 }
168
169 int ctdb_pid_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
170                   pid_t *out)
171 {
172         if (buflen < sizeof(pid_t)) {
173                 return EMSGSIZE;
174         }
175
176         *out = *(pid_t *)buf;
177         return 0;
178 }
179
180 size_t ctdb_string_len(const char *str)
181 {
182         if (str == NULL) {
183                 return 0;
184         }
185         return strlen(str) + 1;
186 }
187
188 void ctdb_string_push(const char *str, uint8_t *buf)
189 {
190         if (str == NULL) {
191                 return;
192         }
193         memcpy(buf, str, strlen(str)+1);
194 }
195
196 int ctdb_string_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
197                      const char **out)
198 {
199         char *str;
200
201         if (buflen == 0) {
202                 return 0;
203         }
204
205         str = talloc_strndup(mem_ctx, (char *)buf, buflen);
206         if (str == NULL) {
207                 return ENOMEM;
208         }
209
210         *out = str;
211         return 0;
212 }
213
214 struct stringn_wire {
215         uint32_t length;
216         uint8_t str[1];
217 };
218
219 size_t ctdb_stringn_len(const char *str)
220 {
221         return sizeof(uint32_t) + strlen(str) + 1;
222 }
223
224 void ctdb_stringn_push(const char *str, uint8_t *buf)
225 {
226         struct stringn_wire *wire = (struct stringn_wire *)buf;
227
228         if (str == NULL) {
229                 wire->length = 0;
230         } else {
231                 wire->length = strlen(str) + 1;
232                 memcpy(wire->str, str, wire->length);
233         }
234 }
235
236 int ctdb_stringn_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
237                       const char **out)
238 {
239         char *str;
240         struct stringn_wire *wire = (struct stringn_wire *)buf;
241
242         if (buflen < sizeof(uint32_t)) {
243                 return EMSGSIZE;
244         }
245
246         if (buflen < sizeof(uint32_t) + wire->length) {
247                 return EMSGSIZE;
248         }
249
250         str = talloc_strndup(mem_ctx, (char *)wire->str, wire->length);
251         if (str == NULL) {
252                 return ENOMEM;
253         }
254
255         *out = str;
256         return 0;
257 }
258
259 size_t ctdb_statistics_len(struct ctdb_statistics *stats)
260 {
261         return sizeof(struct ctdb_statistics);
262 }
263
264 void ctdb_statistics_push(struct ctdb_statistics *stats, uint8_t *buf)
265 {
266         memcpy(buf, stats, sizeof(struct ctdb_statistics));
267 }
268
269 int ctdb_statistics_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
270                          struct ctdb_statistics **out)
271 {
272         struct ctdb_statistics *stats;
273         struct ctdb_statistics *wire = (struct ctdb_statistics *)buf;
274
275         if (buflen < sizeof(struct ctdb_statistics)) {
276                 return EMSGSIZE;
277         }
278
279         stats = talloc(mem_ctx, struct ctdb_statistics);
280         if (stats == NULL) {
281                 return ENOMEM;
282         }
283         memcpy(stats, wire, sizeof(struct ctdb_statistics));
284
285         *out = stats;
286         return 0;
287 }
288
289 struct ctdb_statistics_list_wire {
290         uint32_t num;
291         struct ctdb_statistics stats[1];
292 };
293
294 size_t ctdb_statistics_list_len(struct ctdb_statistics_list *stats_list)
295 {
296         return offsetof(struct ctdb_statistics_list_wire, stats) +
297                stats_list->num * sizeof(struct ctdb_statistics);
298 }
299
300 void ctdb_statistics_list_push(struct ctdb_statistics_list *stats_list,
301                                uint8_t *buf)
302 {
303         struct ctdb_statistics_list_wire *wire =
304                 (struct ctdb_statistics_list_wire *)buf;
305
306         wire->num = stats_list->num;
307         memcpy(wire->stats, stats_list->stats,
308                stats_list->num * sizeof(struct ctdb_statistics));
309 }
310
311 int ctdb_statistics_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
312                               struct ctdb_statistics_list **out)
313 {
314         struct ctdb_statistics_list *stats_list;
315         struct ctdb_statistics_list_wire *wire =
316                 (struct ctdb_statistics_list_wire *)buf;
317
318         if (buflen < offsetof(struct ctdb_statistics_list_wire, stats)) {
319                 return EMSGSIZE;
320         }
321         if (buflen < offsetof(struct ctdb_statistics_list_wire, stats) +
322                      wire->num * sizeof(struct ctdb_statistics)) {
323                 return EMSGSIZE;
324         }
325
326         stats_list = talloc(mem_ctx, struct ctdb_statistics_list);
327         if (stats_list == NULL) {
328                 return ENOMEM;
329         }
330
331         stats_list->num = wire->num;
332
333         stats_list->stats = talloc_array(stats_list, struct ctdb_statistics,
334                                          wire->num);
335         if (stats_list->stats == NULL) {
336                 talloc_free(stats_list);
337                 return ENOMEM;
338         }
339
340         memcpy(stats_list->stats, wire->stats,
341                wire->num * sizeof(struct ctdb_statistics));
342
343         *out = stats_list;
344         return 0;
345 }
346
347 struct ctdb_vnn_map_wire {
348         uint32_t generation;
349         uint32_t size;
350         uint32_t map[1];
351 };
352
353 size_t ctdb_vnn_map_len(struct ctdb_vnn_map *vnnmap)
354 {
355         return offsetof(struct ctdb_vnn_map, map) +
356                vnnmap->size * sizeof(uint32_t);
357 }
358
359 void ctdb_vnn_map_push(struct ctdb_vnn_map *vnnmap, uint8_t *buf)
360 {
361         struct ctdb_vnn_map_wire *wire = (struct ctdb_vnn_map_wire *)buf;
362
363         memcpy(wire, vnnmap, offsetof(struct ctdb_vnn_map, map));
364         memcpy(wire->map, vnnmap->map, vnnmap->size * sizeof(uint32_t));
365 }
366
367 int ctdb_vnn_map_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
368                       struct ctdb_vnn_map **out)
369 {
370         struct ctdb_vnn_map *vnnmap;
371         struct ctdb_vnn_map_wire *wire = (struct ctdb_vnn_map_wire *)buf;
372
373         if (buflen < offsetof(struct ctdb_vnn_map_wire, map)) {
374                 return EMSGSIZE;
375         }
376         if (buflen < offsetof(struct ctdb_vnn_map_wire, map) +
377                      wire->size * sizeof(uint32_t)) {
378                 return EMSGSIZE;
379         }
380
381         vnnmap = talloc(mem_ctx, struct ctdb_vnn_map);
382         if (vnnmap == NULL) {
383                 return ENOMEM;
384         }
385
386         memcpy(vnnmap, wire, offsetof(struct ctdb_vnn_map, map));
387
388         vnnmap->map = talloc_memdup(vnnmap, wire->map,
389                                     wire->size * sizeof(uint32_t));
390         if (vnnmap->map == NULL) {
391                 talloc_free(vnnmap);
392                 return ENOMEM;
393         }
394
395         *out = vnnmap;
396         return 0;
397 }
398
399 struct ctdb_dbid_map_wire {
400         uint32_t num;
401         struct ctdb_dbid dbs[1];
402 };
403
404 size_t ctdb_dbid_map_len(struct ctdb_dbid_map *dbmap)
405 {
406         return sizeof(uint32_t) + dbmap->num * sizeof(struct ctdb_dbid);
407 }
408
409 void ctdb_dbid_map_push(struct ctdb_dbid_map *dbmap, uint8_t *buf)
410 {
411         struct ctdb_dbid_map_wire *wire = (struct ctdb_dbid_map_wire *)buf;
412
413         wire->num = dbmap->num;
414         memcpy(wire->dbs, dbmap->dbs, dbmap->num * sizeof(struct ctdb_dbid));
415 }
416
417 int ctdb_dbid_map_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
418                        struct ctdb_dbid_map **out)
419 {
420         struct ctdb_dbid_map *dbmap;
421         struct ctdb_dbid_map_wire *wire = (struct ctdb_dbid_map_wire *)buf;
422
423         if (buflen < sizeof(uint32_t)) {
424                 return EMSGSIZE;
425         }
426         if (buflen < sizeof(uint32_t) + wire->num * sizeof(struct ctdb_dbid)) {
427                 return EMSGSIZE;
428         }
429
430         dbmap = talloc(mem_ctx, struct ctdb_dbid_map);
431         if (dbmap == NULL) {
432                 return ENOMEM;
433         }
434
435         dbmap->num = wire->num;
436
437         dbmap->dbs = talloc_memdup(dbmap, wire->dbs,
438                                    wire->num * sizeof(struct ctdb_dbid));
439         if (dbmap->dbs == NULL) {
440                 talloc_free(dbmap);
441                 return ENOMEM;
442         }
443
444         *out = dbmap;
445         return 0;
446 }
447
448 size_t ctdb_pulldb_len(struct ctdb_pulldb *pulldb)
449 {
450         return sizeof(struct ctdb_pulldb);
451 }
452
453 void ctdb_pulldb_push(struct ctdb_pulldb *pulldb, uint8_t *buf)
454 {
455         memcpy(buf, pulldb, sizeof(struct ctdb_pulldb));
456 }
457
458 int ctdb_pulldb_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
459                      struct ctdb_pulldb **out)
460 {
461         struct ctdb_pulldb *pulldb;
462
463         if (buflen < sizeof(struct ctdb_pulldb)) {
464                 return EMSGSIZE;
465         }
466
467         pulldb = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_pulldb));
468         if (pulldb == NULL) {
469                 return ENOMEM;
470         }
471
472         *out = pulldb;
473         return 0;
474 }
475
476 size_t ctdb_ltdb_header_len(struct ctdb_ltdb_header *header)
477 {
478         return sizeof(struct ctdb_ltdb_header);
479 }
480
481 void ctdb_ltdb_header_push(struct ctdb_ltdb_header *header, uint8_t *buf)
482 {
483         memcpy(buf, header, sizeof(struct ctdb_ltdb_header));
484 }
485
486 int ctdb_ltdb_header_pull(uint8_t *buf, size_t buflen,
487                           struct ctdb_ltdb_header *header)
488 {
489         if (buflen < sizeof(struct ctdb_ltdb_header)) {
490                 return EMSGSIZE;
491         }
492
493         memcpy(header, buf, sizeof(struct ctdb_ltdb_header));
494         return 0;
495 }
496
497 struct ctdb_rec_data_wire {
498         uint32_t length;
499         uint32_t reqid;
500         uint32_t keylen;
501         uint32_t datalen;
502         uint8_t data[1];
503 };
504
505 size_t ctdb_rec_data_len(struct ctdb_rec_data *rec)
506 {
507         return offsetof(struct ctdb_rec_data_wire, data) +
508                rec->key.dsize + rec->data.dsize +
509                (rec->header == NULL ? 0 : sizeof(struct ctdb_ltdb_header));
510 }
511
512 void ctdb_rec_data_push(struct ctdb_rec_data *rec, uint8_t *buf)
513 {
514         struct ctdb_rec_data_wire *wire = (struct ctdb_rec_data_wire *)buf;
515         size_t offset;
516
517         wire->length = ctdb_rec_data_len(rec);
518         wire->reqid = rec->reqid;
519         wire->keylen = rec->key.dsize;
520         wire->datalen = rec->data.dsize;
521
522         memcpy(wire->data, rec->key.dptr, rec->key.dsize);
523         offset = rec->key.dsize;
524         if (rec->header != NULL) {
525                 memcpy(&wire->data[offset], rec->header,
526                        sizeof(struct ctdb_ltdb_header));
527                 offset += sizeof(struct ctdb_ltdb_header);
528         }
529         if (rec->data.dsize > 0) {
530                 memcpy(&wire->data[offset], rec->data.dptr, rec->data.dsize);
531         }
532 }
533
534 static int ctdb_rec_data_pull_data(uint8_t *buf, size_t buflen,
535                                    uint32_t *reqid,
536                                    struct ctdb_ltdb_header **header,
537                                    TDB_DATA *key, TDB_DATA *data,
538                                    size_t *reclen)
539 {
540         struct ctdb_rec_data_wire *wire = (struct ctdb_rec_data_wire *)buf;
541         size_t offset, n;
542
543         if (buflen < offsetof(struct ctdb_rec_data_wire, data)) {
544                 return EMSGSIZE;
545         }
546         n = offsetof(struct ctdb_rec_data_wire, data) +
547                 wire->keylen + wire->datalen;
548         if (buflen < n) {
549                 return EMSGSIZE;
550         }
551
552         *reqid = wire->reqid;
553
554         key->dsize = wire->keylen;
555         key->dptr = wire->data;
556         offset = wire->keylen;
557
558         if (wire->length - n == sizeof(struct ctdb_ltdb_header)) {
559                 *header = (struct ctdb_ltdb_header *)&wire->data[offset];
560                 offset += sizeof(struct ctdb_ltdb_header);
561         } else {
562                 *header = NULL;
563         }
564
565         data->dsize = wire->datalen;
566         data->dptr = &wire->data[offset];
567
568         *reclen = n;
569
570         return 0;
571 }
572
573 static int ctdb_rec_data_pull_elems(uint8_t *buf, size_t buflen,
574                                     TALLOC_CTX *mem_ctx,
575                                     struct ctdb_rec_data *out)
576 {
577         uint32_t reqid;
578         struct ctdb_ltdb_header *header;
579         TDB_DATA key, data;
580         size_t reclen;
581         int ret;
582
583         ret = ctdb_rec_data_pull_data(buf, buflen, &reqid, &header,
584                                       &key, &data, &reclen);
585         if (ret != 0) {
586                 return ret;
587         }
588
589         out->reqid = reqid;
590
591         if (header != NULL) {
592                 out->header = talloc_memdup(mem_ctx, header,
593                                             sizeof(struct ctdb_ltdb_header));
594                 if (out->header == NULL) {
595                         return ENOMEM;
596                 }
597         } else {
598                 out->header = NULL;
599         }
600
601         out->key.dsize = key.dsize;
602         if (key.dsize > 0) {
603                 out->key.dptr = talloc_memdup(mem_ctx, key.dptr, key.dsize);
604                 if (out->key.dptr == NULL) {
605                         return ENOMEM;
606                 }
607         }
608
609         out->data.dsize = data.dsize;
610         if (data.dsize > 0) {
611                 out->data.dptr = talloc_memdup(mem_ctx, data.dptr, data.dsize);
612                 if (out->data.dptr == NULL) {
613                         return ENOMEM;
614                 }
615         }
616
617         return 0;
618 }
619
620 int ctdb_rec_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
621                        struct ctdb_rec_data **out)
622 {
623         struct ctdb_rec_data *rec;
624         int ret;
625
626         rec = talloc(mem_ctx, struct ctdb_rec_data);
627         if (rec == NULL) {
628                 return ENOMEM;
629         }
630
631         ret = ctdb_rec_data_pull_elems(buf, buflen, rec, rec);
632         if (ret != 0) {
633                 TALLOC_FREE(rec);
634         }
635
636         *out = rec;
637         return ret;
638 }
639
640 struct ctdb_rec_buffer_wire {
641         uint32_t db_id;
642         uint32_t count;
643         uint8_t data[1];
644 };
645
646 size_t ctdb_rec_buffer_len(struct ctdb_rec_buffer *recbuf)
647 {
648         return offsetof(struct ctdb_rec_buffer_wire, data) + recbuf->buflen;
649 }
650
651 void ctdb_rec_buffer_push(struct ctdb_rec_buffer *recbuf, uint8_t *buf)
652 {
653         struct ctdb_rec_buffer_wire *wire = (struct ctdb_rec_buffer_wire *)buf;
654
655         wire->db_id = recbuf->db_id;
656         wire->count = recbuf->count;
657         if (recbuf->buflen > 0) {
658                 memcpy(wire->data, recbuf->buf, recbuf->buflen);
659         }
660 }
661
662 int ctdb_rec_buffer_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
663                          struct ctdb_rec_buffer **out)
664 {
665         struct ctdb_rec_buffer *recbuf;
666         struct ctdb_rec_buffer_wire *wire = (struct ctdb_rec_buffer_wire *)buf;
667         size_t offset;
668
669         if (buflen < offsetof(struct ctdb_rec_buffer_wire, data)) {
670                 return EMSGSIZE;
671         }
672
673         recbuf = talloc(mem_ctx, struct ctdb_rec_buffer);
674         if (recbuf == NULL) {
675                 return ENOMEM;
676         }
677
678         recbuf->db_id = wire->db_id;
679         recbuf->count = wire->count;
680
681         offset = offsetof(struct ctdb_rec_buffer_wire, data);
682         recbuf->buflen = buflen - offset;
683         recbuf->buf = talloc_memdup(recbuf, wire->data, recbuf->buflen);
684         if (recbuf->buf == NULL) {
685                 talloc_free(recbuf);
686                 return ENOMEM;
687         }
688
689         *out = recbuf;
690         return 0;
691 }
692
693 struct ctdb_rec_buffer *ctdb_rec_buffer_init(TALLOC_CTX *mem_ctx,
694                                              uint32_t db_id)
695 {
696         struct ctdb_rec_buffer *recbuf;
697
698         recbuf = talloc_zero(mem_ctx, struct ctdb_rec_buffer);
699         if (recbuf == NULL) {
700                 return recbuf;
701         }
702
703         recbuf->db_id = db_id;
704
705         return recbuf;
706 }
707
708 int ctdb_rec_buffer_add(TALLOC_CTX *mem_ctx, struct ctdb_rec_buffer *recbuf,
709                         uint32_t reqid, struct ctdb_ltdb_header *header,
710                         TDB_DATA key, TDB_DATA data)
711 {
712         struct ctdb_rec_data recdata;
713         size_t len;
714         uint8_t *ptr;
715
716         recdata.reqid = reqid;
717         recdata.header = header;
718         recdata.key = key;
719         recdata.data = data;
720
721         len = ctdb_rec_data_len(&recdata);
722
723         ptr = talloc_realloc(mem_ctx, recbuf->buf, uint8_t,
724                              recbuf->buflen + len);
725         if (ptr == NULL) {
726                 return ENOMEM;
727         }
728
729         ctdb_rec_data_push(&recdata, &ptr[recbuf->buflen]);
730
731         recbuf->count++;
732         recbuf->buf = ptr;
733         recbuf->buflen += len;
734         return 0;
735 }
736
737 int ctdb_rec_buffer_traverse(struct ctdb_rec_buffer *recbuf,
738                              ctdb_rec_parser_func_t func,
739                              void *private_data)
740 {
741         struct ctdb_ltdb_header *header;
742         TDB_DATA key, data;
743         uint32_t reqid;
744         size_t offset, reclen;
745         int ret = 0, i;
746
747         offset = 0;
748         for (i=0; i<recbuf->count; i++) {
749                 ret = ctdb_rec_data_pull_data(&recbuf->buf[offset],
750                                               recbuf->buflen - offset,
751                                               &reqid, &header,
752                                               &key, &data, &reclen);
753                 if (ret != 0) {
754                         return ret;
755                 }
756
757                 ret = func(reqid, header, key, data, private_data);
758                 if (ret != 0) {
759                         break;
760                 }
761
762                 offset += reclen;
763         }
764
765         return ret;
766 }
767
768 size_t ctdb_traverse_start_len(struct ctdb_traverse_start *traverse)
769 {
770         return sizeof(struct ctdb_traverse_start);
771 }
772
773 void ctdb_traverse_start_push(struct ctdb_traverse_start *traverse,
774                               uint8_t *buf)
775 {
776         memcpy(buf, traverse, sizeof(struct ctdb_traverse_start));
777 }
778
779 int ctdb_traverse_start_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
780                              struct ctdb_traverse_start **out)
781 {
782         struct ctdb_traverse_start *traverse;
783
784         if (buflen < sizeof(struct ctdb_traverse_start)) {
785                 return EMSGSIZE;
786         }
787
788         traverse = talloc_memdup(mem_ctx, buf,
789                                  sizeof(struct ctdb_traverse_start));
790         if (traverse == NULL) {
791                 return ENOMEM;
792         }
793
794         *out = traverse;
795         return 0;
796 }
797
798 size_t ctdb_traverse_all_len(struct ctdb_traverse_all *traverse)
799 {
800         return sizeof(struct ctdb_traverse_all);
801 }
802
803 void ctdb_traverse_all_push(struct ctdb_traverse_all *traverse, uint8_t *buf)
804 {
805         memcpy(buf, traverse, sizeof(struct ctdb_traverse_all));
806 }
807
808 int ctdb_traverse_all_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
809                            struct ctdb_traverse_all **out)
810 {
811         struct ctdb_traverse_all *traverse;
812
813         if (buflen < sizeof(struct ctdb_traverse_all)) {
814                 return EMSGSIZE;
815         }
816
817         traverse = talloc_memdup(mem_ctx, buf,
818                                  sizeof(struct ctdb_traverse_all));
819         if (traverse == NULL) {
820                 return ENOMEM;
821         }
822
823         *out = traverse;
824         return 0;
825 }
826
827 size_t ctdb_traverse_start_ext_len(struct ctdb_traverse_start_ext *traverse)
828 {
829         return sizeof(struct ctdb_traverse_start_ext);
830 }
831
832 void ctdb_traverse_start_ext_push(struct ctdb_traverse_start_ext *traverse,
833                                   uint8_t *buf)
834 {
835         memcpy(buf, traverse, sizeof(struct ctdb_traverse_start_ext));
836 }
837
838 int ctdb_traverse_start_ext_pull(uint8_t *buf, size_t buflen,
839                                  TALLOC_CTX *mem_ctx,
840                                  struct ctdb_traverse_start_ext **out)
841 {
842         struct ctdb_traverse_start_ext *traverse;
843
844         if (buflen < sizeof(struct ctdb_traverse_start_ext)) {
845                 return EMSGSIZE;
846         }
847
848         traverse = talloc_memdup(mem_ctx, buf,
849                                  sizeof(struct ctdb_traverse_start_ext));
850         if (traverse == NULL) {
851                 return ENOMEM;
852         }
853
854         *out = traverse;
855         return 0;
856 }
857
858 size_t ctdb_traverse_all_ext_len(struct ctdb_traverse_all_ext *traverse)
859 {
860         return sizeof(struct ctdb_traverse_all_ext);
861 }
862
863 void ctdb_traverse_all_ext_push(struct ctdb_traverse_all_ext *traverse,
864                                 uint8_t *buf)
865 {
866         memcpy(buf, traverse, sizeof(struct ctdb_traverse_all_ext));
867 }
868
869 int ctdb_traverse_all_ext_pull(uint8_t *buf, size_t buflen,
870                                TALLOC_CTX *mem_ctx,
871                                struct ctdb_traverse_all_ext **out)
872 {
873         struct ctdb_traverse_all_ext *traverse;
874
875         if (buflen < sizeof(struct ctdb_traverse_all_ext)) {
876                 return EMSGSIZE;
877         }
878
879         traverse = talloc_memdup(mem_ctx, buf,
880                                  sizeof(struct ctdb_traverse_all_ext));
881         if (traverse == NULL) {
882                 return ENOMEM;
883         }
884
885         *out = traverse;
886         return 0;
887 }
888
889 size_t ctdb_sock_addr_len(ctdb_sock_addr *addr)
890 {
891         return sizeof(ctdb_sock_addr);
892 }
893
894 void ctdb_sock_addr_push(ctdb_sock_addr *addr, uint8_t *buf)
895 {
896         memcpy(buf, addr, sizeof(ctdb_sock_addr));
897 }
898
899 static int ctdb_sock_addr_pull_elems(uint8_t *buf, size_t buflen,
900                                      TALLOC_CTX *mem_ctx, ctdb_sock_addr *out)
901 {
902         if (buflen < sizeof(ctdb_sock_addr)) {
903                 return EMSGSIZE;
904         }
905
906         memcpy(out, buf, sizeof(ctdb_sock_addr));
907
908         return 0;
909 }
910
911 int ctdb_sock_addr_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
912                         ctdb_sock_addr **out)
913 {
914         ctdb_sock_addr *addr;
915         int ret;
916
917         addr = talloc(mem_ctx, ctdb_sock_addr);
918         if (addr == NULL) {
919                 return false;
920         }
921
922         ret = ctdb_sock_addr_pull_elems(buf, buflen, addr, addr);
923         if (ret != 0) {
924                 TALLOC_FREE(addr);
925         }
926
927         *out = addr;
928         return ret;
929 }
930
931 size_t ctdb_connection_len(struct ctdb_connection *conn)
932 {
933         return sizeof(struct ctdb_connection);
934 }
935
936 void ctdb_connection_push(struct ctdb_connection *conn, uint8_t *buf)
937 {
938         memcpy(buf, conn, sizeof(struct ctdb_connection));
939 }
940
941 static int ctdb_connection_pull_elems(uint8_t *buf, size_t buflen,
942                                       TALLOC_CTX *mem_ctx,
943                                       struct ctdb_connection *out)
944 {
945         if (buflen < sizeof(struct ctdb_connection)) {
946                 return EMSGSIZE;
947         }
948
949         memcpy(out, buf, sizeof(struct ctdb_connection));
950
951         return 0;
952 }
953
954 int ctdb_connection_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
955                          struct ctdb_connection **out)
956 {
957         struct ctdb_connection *conn;
958         int ret;
959
960         conn = talloc(mem_ctx, struct ctdb_connection);
961         if (conn == NULL) {
962                 return ENOMEM;
963         }
964
965         ret = ctdb_connection_pull_elems(buf, buflen, conn, conn);
966         if (ret != 0) {
967                 TALLOC_FREE(conn);
968         }
969
970         *out = conn;
971         return ret;
972 }
973
974 struct ctdb_tunable_wire {
975         uint32_t value;
976         uint32_t length;
977         uint8_t name[1];
978 };
979
980 size_t ctdb_tunable_len(struct ctdb_tunable *tunable)
981 {
982         return offsetof(struct ctdb_tunable_wire, name) +
983                strlen(tunable->name) + 1;
984 }
985
986 void ctdb_tunable_push(struct ctdb_tunable *tunable, uint8_t *buf)
987 {
988         struct ctdb_tunable_wire *wire = (struct ctdb_tunable_wire *)buf;
989
990         wire->value = tunable->value;
991         wire->length = strlen(tunable->name) + 1;
992         memcpy(wire->name, tunable->name, wire->length);
993 }
994
995 int ctdb_tunable_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
996                       struct ctdb_tunable **out)
997 {
998         struct ctdb_tunable *tunable;
999         struct ctdb_tunable_wire *wire = (struct ctdb_tunable_wire *)buf;
1000
1001         if (buflen < offsetof(struct ctdb_tunable_wire, name)) {
1002                 return EMSGSIZE;
1003         }
1004         if (buflen < offsetof(struct ctdb_tunable_wire, name) + wire->length) {
1005                 return EMSGSIZE;
1006         }
1007
1008         tunable = talloc(mem_ctx, struct ctdb_tunable);
1009         if (tunable == NULL) {
1010                 return ENOMEM;
1011         }
1012
1013         tunable->value = wire->value;
1014         tunable->name = talloc_memdup(tunable, wire->name, wire->length);
1015         if (tunable->name == NULL) {
1016                 talloc_free(tunable);
1017                 return ENOMEM;
1018         }
1019
1020         *out = tunable;
1021         return 0;
1022 }
1023
1024 size_t ctdb_node_flag_change_len(struct ctdb_node_flag_change *flag_change)
1025 {
1026         return sizeof(struct ctdb_node_flag_change);
1027 }
1028
1029 void ctdb_node_flag_change_push(struct ctdb_node_flag_change *flag_change,
1030                                 uint8_t *buf)
1031 {
1032         memcpy(buf, flag_change, sizeof(struct ctdb_node_flag_change));
1033 }
1034
1035 int ctdb_node_flag_change_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1036                                struct ctdb_node_flag_change **out)
1037 {
1038         struct ctdb_node_flag_change *flag_change;
1039
1040         if (buflen < sizeof(struct ctdb_node_flag_change)) {
1041                 return EMSGSIZE;
1042         }
1043
1044         flag_change = talloc_memdup(mem_ctx, buf,
1045                                     sizeof(struct ctdb_node_flag_change));
1046         if (flag_change == NULL) {
1047                 return ENOMEM;
1048         }
1049
1050         *out = flag_change;
1051         return 0;
1052 }
1053
1054 struct ctdb_var_list_wire {
1055         uint32_t length;
1056         char list_str[1];
1057 };
1058
1059 size_t ctdb_var_list_len(struct ctdb_var_list *var_list)
1060 {
1061         int i;
1062         size_t len = sizeof(uint32_t);
1063
1064         for (i=0; i<var_list->count; i++) {
1065                 len += strlen(var_list->var[i]) + 1;
1066         }
1067         return len;
1068 }
1069
1070 void ctdb_var_list_push(struct ctdb_var_list *var_list, uint8_t *buf)
1071 {
1072         struct ctdb_var_list_wire *wire = (struct ctdb_var_list_wire *)buf;
1073         int i, n;
1074         size_t offset = 0;
1075
1076         if (var_list->count > 0) {
1077                 n = sprintf(wire->list_str, "%s", var_list->var[0]);
1078                 offset += n;
1079         }
1080         for (i=1; i<var_list->count; i++) {
1081                 n = sprintf(&wire->list_str[offset], ":%s", var_list->var[i]);
1082                 offset += n;
1083         }
1084         wire->length = offset + 1;
1085 }
1086
1087 int ctdb_var_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1088                        struct ctdb_var_list **out)
1089 {
1090         struct ctdb_var_list *var_list = NULL;
1091         struct ctdb_var_list_wire *wire = (struct ctdb_var_list_wire *)buf;
1092         char *str, *s, *tok, *ptr;
1093         const char **list;
1094
1095         if (buflen < sizeof(uint32_t)) {
1096                 return EMSGSIZE;
1097         }
1098         if (buflen < sizeof(uint32_t) + wire->length) {
1099                 return EMSGSIZE;
1100         }
1101
1102         str = talloc_strndup(mem_ctx, (char *)wire->list_str, wire->length);
1103         if (str == NULL) {
1104                 return ENOMEM;
1105         }
1106
1107         var_list = talloc_zero(mem_ctx, struct ctdb_var_list);
1108         if (var_list == NULL) {
1109                 goto fail;
1110         }
1111
1112         s = str;
1113         while ((tok = strtok_r(s, ":", &ptr)) != NULL) {
1114                 s = NULL;
1115                 list = talloc_realloc(var_list, var_list->var, const char *,
1116                                       var_list->count+1);
1117                 if (list == NULL) {
1118                         goto fail;
1119                 }
1120
1121                 var_list->var = list;
1122                 var_list->var[var_list->count] = talloc_strdup(var_list, tok);
1123                 if (var_list->var[var_list->count] == NULL) {
1124                         goto fail;
1125                 }
1126                 var_list->count++;
1127         }
1128
1129         talloc_free(str);
1130         *out = var_list;
1131         return 0;
1132
1133 fail:
1134         talloc_free(str);
1135         talloc_free(var_list);
1136         return ENOMEM;
1137 }
1138
1139 size_t ctdb_tunable_list_len(struct ctdb_tunable_list *tun_list)
1140 {
1141         return sizeof(struct ctdb_tunable_list);
1142 }
1143
1144 void ctdb_tunable_list_push(struct ctdb_tunable_list *tun_list, uint8_t *buf)
1145 {
1146         memcpy(buf, tun_list, sizeof(struct ctdb_tunable_list));
1147 }
1148
1149 int ctdb_tunable_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1150                            struct ctdb_tunable_list **out)
1151 {
1152         struct ctdb_tunable_list *tun_list;
1153
1154         if (buflen < sizeof(struct ctdb_tunable_list)) {
1155                 return EMSGSIZE;
1156         }
1157
1158         tun_list = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_tunable_list));
1159         if (tun_list == NULL) {
1160                 return ENOMEM;
1161         }
1162
1163         *out = tun_list;
1164         return 0;
1165 }
1166
1167 struct ctdb_tickle_list_wire {
1168         ctdb_sock_addr addr;
1169         uint32_t num;
1170         struct ctdb_connection conn[1];
1171 };
1172
1173 size_t ctdb_tickle_list_len(struct ctdb_tickle_list *tickles)
1174 {
1175         return offsetof(struct ctdb_tickle_list, conn) +
1176                tickles->num * sizeof(struct ctdb_connection);
1177 }
1178
1179 void ctdb_tickle_list_push(struct ctdb_tickle_list *tickles, uint8_t *buf)
1180 {
1181         struct ctdb_tickle_list_wire *wire =
1182                 (struct ctdb_tickle_list_wire *)buf;
1183         size_t offset;
1184         int i;
1185
1186         memcpy(&wire->addr, &tickles->addr, sizeof(ctdb_sock_addr));
1187         wire->num = tickles->num;
1188
1189         offset = offsetof(struct ctdb_tickle_list_wire, conn);
1190         for (i=0; i<tickles->num; i++) {
1191                 ctdb_connection_push(&tickles->conn[i], &buf[offset]);
1192                 offset += ctdb_connection_len(&tickles->conn[i]);
1193         }
1194 }
1195
1196 int ctdb_tickle_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1197                            struct ctdb_tickle_list **out)
1198 {
1199         struct ctdb_tickle_list *tickles;
1200         struct ctdb_tickle_list_wire *wire =
1201                 (struct ctdb_tickle_list_wire *)buf;
1202         size_t offset;
1203         int i, ret;
1204
1205         if (buflen < offsetof(struct ctdb_tickle_list_wire, conn)) {
1206                 return EMSGSIZE;
1207         }
1208         if (buflen < offsetof(struct ctdb_tickle_list_wire, conn) +
1209                      wire->num * sizeof(struct ctdb_connection)) {
1210                 return EMSGSIZE;
1211         }
1212
1213         tickles = talloc(mem_ctx, struct ctdb_tickle_list);
1214         if (tickles == NULL) {
1215                 return ENOMEM;
1216         }
1217
1218         offset = offsetof(struct ctdb_tickle_list, conn);
1219         memcpy(tickles, wire, offset);
1220
1221         tickles->conn = talloc_array(tickles, struct ctdb_connection,
1222                                      wire->num);
1223         if (tickles->conn == NULL) {
1224                 talloc_free(tickles);
1225                 return ENOMEM;
1226         }
1227
1228         for (i=0; i<wire->num; i++) {
1229                 ret = ctdb_connection_pull_elems(&buf[offset], buflen-offset,
1230                                                  tickles->conn,
1231                                                  &tickles->conn[i]);
1232                 if (ret != 0) {
1233                         talloc_free(tickles);
1234                         return ret;
1235                 }
1236                 offset += ctdb_connection_len(&tickles->conn[i]);
1237         }
1238
1239         *out = tickles;
1240         return 0;
1241 }
1242
1243 size_t ctdb_client_id_len(struct ctdb_client_id *cid)
1244 {
1245         return sizeof(struct ctdb_client_id);
1246 }
1247
1248 void ctdb_client_id_push(struct ctdb_client_id *cid, uint8_t *buf)
1249 {
1250         memcpy(buf, cid, sizeof(struct ctdb_client_id));
1251 }
1252
1253 static int ctdb_client_id_pull_elems(uint8_t *buf, size_t buflen,
1254                                      TALLOC_CTX *mem_ctx,
1255                                      struct ctdb_client_id *out)
1256 {
1257         if (buflen < sizeof(struct ctdb_client_id)) {
1258                 return EMSGSIZE;
1259         }
1260
1261         memcpy(out, buf, sizeof(struct ctdb_client_id));
1262
1263         return 0;
1264 }
1265
1266 int ctdb_client_id_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1267                         struct ctdb_client_id **out)
1268 {
1269         struct ctdb_client_id *cid;
1270         int ret;
1271
1272         cid = talloc(mem_ctx, struct ctdb_client_id);
1273         if (cid == NULL) {
1274                 return ENOMEM;
1275         }
1276
1277         ret = ctdb_client_id_pull_elems(buf, buflen, cid, cid);
1278         if (ret != 0) {
1279                 TALLOC_FREE(cid);
1280         }
1281
1282         *out = cid;
1283         return ret;
1284 }
1285
1286 struct ctdb_client_id_list_wire {
1287         uint32_t num;
1288         struct ctdb_client_id cid[1];
1289 };
1290
1291 size_t ctdb_client_id_list_len(struct ctdb_client_id_list *cid_list)
1292 {
1293         return sizeof(uint32_t) +
1294                cid_list->num * sizeof(struct ctdb_client_id);
1295 }
1296
1297 void ctdb_client_id_list_push(struct ctdb_client_id_list *cid_list,
1298                               uint8_t *buf)
1299 {
1300         struct ctdb_client_id_list_wire *wire =
1301                 (struct ctdb_client_id_list_wire *)buf;
1302         size_t offset;
1303         int i;
1304
1305         wire->num = cid_list->num;
1306
1307         offset = offsetof(struct ctdb_client_id_list_wire, cid);
1308         for (i=0; i<cid_list->num; i++) {
1309                 ctdb_client_id_push(&cid_list->cid[i], &buf[offset]);
1310                 offset += ctdb_client_id_len(&cid_list->cid[i]);
1311         }
1312 }
1313
1314 static int ctdb_client_id_list_pull_elems(uint8_t *buf, size_t buflen,
1315                                           TALLOC_CTX *mem_ctx,
1316                                           struct ctdb_client_id_list *out)
1317 {
1318         struct ctdb_client_id_list_wire *wire =
1319                 (struct ctdb_client_id_list_wire *)buf;
1320         size_t offset;
1321         int i;
1322
1323         if (buflen < sizeof(uint32_t)) {
1324                 return EMSGSIZE;
1325         }
1326         if (buflen < sizeof(uint32_t) +
1327                      wire->num * sizeof(struct ctdb_client_id)) {
1328                 return EMSGSIZE;
1329         }
1330
1331         out->num = wire->num;
1332         out->cid = talloc_array(mem_ctx, struct ctdb_client_id,
1333                                 wire->num);
1334         if (out->cid == NULL) {
1335                 return ENOMEM;
1336         }
1337
1338         offset = offsetof(struct ctdb_client_id_list_wire, cid);
1339         for (i=0; i<wire->num; i++) {
1340                 bool ret;
1341                 ret = ctdb_client_id_pull_elems(&buf[offset], buflen-offset,
1342                                                 out->cid, &out->cid[i]);
1343                 if (ret != 0) {
1344                         return ret;
1345                 }
1346                 offset += ctdb_client_id_len(&out->cid[i]);
1347         }
1348
1349         return 0;
1350 }
1351
1352 int ctdb_client_id_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1353                              struct ctdb_client_id_list **out)
1354 {
1355         struct ctdb_client_id_list *cid_list;
1356         int ret;
1357
1358         cid_list = talloc(mem_ctx, struct ctdb_client_id_list);
1359         if (cid_list == NULL) {
1360                 return ENOMEM;
1361         }
1362
1363         ret = ctdb_client_id_list_pull_elems(buf, buflen, cid_list, cid_list);
1364         if (ret != 0) {
1365                 TALLOC_FREE(cid_list);
1366         }
1367
1368         *out = cid_list;
1369         return ret;
1370 }
1371
1372 struct ctdb_client_id_map_wire {
1373         int count;
1374         struct ctdb_client_id_list list[1];
1375 };
1376
1377 size_t ctdb_client_id_map_len(struct ctdb_client_id_map *cid_map)
1378 {
1379         int i;
1380         size_t len;
1381
1382         len = sizeof(int);
1383         for (i=0; i<cid_map->count; i++) {
1384                 len += ctdb_client_id_list_len(&cid_map->list[i]);
1385         }
1386         return len;
1387 }
1388
1389 void ctdb_client_id_map_push(struct ctdb_client_id_map *cid_map, uint8_t *buf)
1390 {
1391         struct ctdb_client_id_map_wire *wire =
1392                 (struct ctdb_client_id_map_wire *)buf;
1393         size_t offset;
1394         int i;
1395
1396         wire->count = cid_map->count;
1397
1398         offset = sizeof(int);
1399         for (i=0; i<cid_map->count; i++) {
1400                 ctdb_client_id_list_push(&cid_map->list[i], &buf[offset]);
1401                 offset += ctdb_client_id_list_len(&cid_map->list[i]);
1402         }
1403 }
1404
1405 int ctdb_client_id_map_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1406                             struct ctdb_client_id_map **out)
1407 {
1408         struct ctdb_client_id_map *cid_map;
1409         struct ctdb_client_id_map_wire *wire =
1410                 (struct ctdb_client_id_map_wire *)buf;
1411         size_t offset;
1412         int i;
1413         bool ret;
1414
1415         if (buflen < sizeof(int)) {
1416                 return EMSGSIZE;
1417         }
1418         if (buflen < sizeof(int) +
1419                      wire->count * sizeof(struct ctdb_client_id_list)) {
1420                 return EMSGSIZE;
1421         }
1422
1423         cid_map = talloc(mem_ctx, struct ctdb_client_id_map);
1424         if (cid_map == NULL) {
1425                 return ENOMEM;
1426         }
1427
1428         cid_map->count = wire->count;
1429         cid_map->list = talloc_array(cid_map, struct ctdb_client_id_list,
1430                                      wire->count);
1431         if (cid_map->list == NULL) {
1432                 return ENOMEM;
1433         }
1434
1435         offset = sizeof(int);
1436         for (i=0; i<wire->count; i++) {
1437                 ret = ctdb_client_id_list_pull_elems(&buf[offset],
1438                                                      buflen-offset,
1439                                                      cid_map->list,
1440                                                      &cid_map->list[i]);
1441                 if (ret != 0) {
1442                         talloc_free(cid_map);
1443                         return ret;
1444                 }
1445                 offset += ctdb_client_id_list_len(&cid_map->list[i]);
1446         }
1447
1448         *out = cid_map;
1449         return 0;
1450 }
1451
1452 struct ctdb_addr_info_wire {
1453         ctdb_sock_addr addr;
1454         uint32_t mask;
1455         uint32_t len;
1456         char iface[1];
1457 };
1458
1459 size_t ctdb_addr_info_len(struct ctdb_addr_info *arp)
1460 {
1461         uint32_t len;
1462
1463         len = offsetof(struct ctdb_addr_info_wire, iface);
1464         if (arp->iface != NULL) {
1465                len += strlen(arp->iface)+1;
1466         }
1467
1468         return len;
1469 }
1470
1471 void ctdb_addr_info_push(struct ctdb_addr_info *addr_info, uint8_t *buf)
1472 {
1473         struct ctdb_addr_info_wire *wire = (struct ctdb_addr_info_wire *)buf;
1474
1475         wire->addr = addr_info->addr;
1476         wire->mask = addr_info->mask;
1477         if (addr_info->iface == NULL) {
1478                 wire->len = 0;
1479         } else {
1480                 wire->len = strlen(addr_info->iface)+1;
1481                 memcpy(wire->iface, addr_info->iface, wire->len);
1482         }
1483 }
1484
1485 int ctdb_addr_info_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1486                         struct ctdb_addr_info **out)
1487 {
1488         struct ctdb_addr_info *addr_info;
1489         struct ctdb_addr_info_wire *wire = (struct ctdb_addr_info_wire *)buf;
1490
1491         if (buflen < offsetof(struct ctdb_addr_info_wire, iface)) {
1492                 return EMSGSIZE;
1493         }
1494         if (buflen < offsetof(struct ctdb_addr_info_wire, iface) + wire->len) {
1495                 return EMSGSIZE;
1496         }
1497
1498         addr_info = talloc(mem_ctx, struct ctdb_addr_info);
1499         if (addr_info == NULL) {
1500                 return ENOMEM;
1501         }
1502
1503         addr_info->addr = wire->addr;
1504         addr_info->mask = wire->mask;
1505
1506         if (wire->len == 0) {
1507                 addr_info->iface = NULL;
1508         } else {
1509                 addr_info->iface = talloc_strndup(addr_info, wire->iface,
1510                                                   wire->len);
1511                 if (addr_info->iface == NULL) {
1512                         talloc_free(addr_info);
1513                         return ENOMEM;
1514                 }
1515         }
1516
1517         *out = addr_info;
1518         return 0;
1519 }
1520
1521 size_t ctdb_transdb_len(struct ctdb_transdb *transdb)
1522 {
1523         return sizeof(struct ctdb_transdb);
1524 }
1525
1526 void ctdb_transdb_push(struct ctdb_transdb *transdb, uint8_t *buf)
1527 {
1528         memcpy(buf, transdb, sizeof(struct ctdb_transdb));
1529 }
1530
1531 int ctdb_transdb_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1532                      struct ctdb_transdb **out)
1533 {
1534         struct ctdb_transdb *transdb;
1535
1536         if (buflen < sizeof(struct ctdb_transdb)) {
1537                 return EMSGSIZE;
1538         }
1539
1540         transdb = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_transdb));
1541         if (transdb == NULL) {
1542                 return ENOMEM;
1543         }
1544
1545         *out = transdb;
1546         return 0;
1547 }
1548
1549 size_t ctdb_uptime_len(struct ctdb_uptime *uptime)
1550 {
1551         return sizeof(struct ctdb_uptime);
1552 }
1553
1554 void ctdb_uptime_push(struct ctdb_uptime *uptime, uint8_t *buf)
1555 {
1556         memcpy(buf, uptime, sizeof(struct ctdb_uptime));
1557 }
1558
1559 int ctdb_uptime_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1560                      struct ctdb_uptime **out)
1561 {
1562         struct ctdb_uptime *uptime;
1563
1564         if (buflen < sizeof(struct ctdb_uptime)) {
1565                 return EMSGSIZE;
1566         }
1567
1568         uptime = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_uptime));
1569         if (uptime == NULL) {
1570                 return ENOMEM;
1571         }
1572
1573         *out = uptime;
1574         return 0;
1575 }
1576
1577 size_t ctdb_public_ip_len(struct ctdb_public_ip *pubip)
1578 {
1579         return sizeof(struct ctdb_public_ip);
1580 }
1581
1582 void ctdb_public_ip_push(struct ctdb_public_ip *pubip, uint8_t *buf)
1583 {
1584         memcpy(buf, pubip, sizeof(struct ctdb_public_ip));
1585 }
1586
1587 static int ctdb_public_ip_pull_elems(uint8_t *buf, size_t buflen,
1588                                      TALLOC_CTX *mem_ctx,
1589                                      struct ctdb_public_ip *out)
1590 {
1591         if (buflen < sizeof(struct ctdb_public_ip)) {
1592                 return EMSGSIZE;
1593         }
1594
1595         memcpy(out, buf, sizeof(struct ctdb_public_ip));
1596
1597         return 0;
1598 }
1599
1600 int ctdb_public_ip_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1601                         struct ctdb_public_ip **out)
1602 {
1603         struct ctdb_public_ip *pubip;
1604         int ret;
1605
1606         pubip = talloc(mem_ctx, struct ctdb_public_ip);
1607         if (pubip == NULL) {
1608                 return ENOMEM;
1609         }
1610
1611         ret = ctdb_public_ip_pull_elems(buf, buflen, pubip, pubip);
1612         if (ret != 0) {
1613                 TALLOC_FREE(pubip);
1614         }
1615
1616         *out = pubip;
1617         return ret;
1618 }
1619
1620 struct ctdb_public_ip_list_wire {
1621         uint32_t num;
1622         struct ctdb_public_ip ip[1];
1623 };
1624
1625 size_t ctdb_public_ip_list_len(struct ctdb_public_ip_list *pubip_list)
1626 {
1627         int i;
1628         size_t len;
1629
1630         len = sizeof(uint32_t);
1631         for (i=0; i<pubip_list->num; i++) {
1632                 len += ctdb_public_ip_len(&pubip_list->ip[i]);
1633         }
1634         return len;
1635 }
1636
1637 void ctdb_public_ip_list_push(struct ctdb_public_ip_list *pubip_list,
1638                               uint8_t *buf)
1639 {
1640         struct ctdb_public_ip_list_wire *wire =
1641                 (struct ctdb_public_ip_list_wire *)buf;
1642         size_t offset;
1643         int i;
1644
1645         wire->num = pubip_list->num;
1646
1647         offset = offsetof(struct ctdb_public_ip_list_wire, ip);
1648         for (i=0; i<pubip_list->num; i++) {
1649                 ctdb_public_ip_push(&pubip_list->ip[i], &buf[offset]);
1650                 offset += ctdb_public_ip_len(&pubip_list->ip[i]);
1651         }
1652 }
1653
1654 int ctdb_public_ip_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1655                              struct ctdb_public_ip_list **out)
1656 {
1657         struct ctdb_public_ip_list *pubip_list;
1658         struct ctdb_public_ip_list_wire *wire =
1659                 (struct ctdb_public_ip_list_wire *)buf;
1660         size_t offset;
1661         int i;
1662         bool ret;
1663
1664         if (buflen < sizeof(uint32_t)) {
1665                 return EMSGSIZE;
1666         }
1667         if (buflen < sizeof(uint32_t) +
1668                      wire->num * sizeof(struct ctdb_public_ip)) {
1669                 return EMSGSIZE;
1670         }
1671
1672         pubip_list = talloc(mem_ctx, struct ctdb_public_ip_list);
1673         if (pubip_list == NULL) {
1674                 return ENOMEM;
1675         }
1676
1677         pubip_list->num = wire->num;
1678         pubip_list->ip = talloc_array(pubip_list, struct ctdb_public_ip,
1679                                       wire->num);
1680         if (pubip_list->ip == NULL) {
1681                 talloc_free(pubip_list);
1682                 return ENOMEM;
1683         }
1684
1685         offset = offsetof(struct ctdb_public_ip_list_wire, ip);
1686         for (i=0; i<wire->num; i++) {
1687                 ret = ctdb_public_ip_pull_elems(&buf[offset], buflen-offset,
1688                                                 pubip_list->ip,
1689                                                 &pubip_list->ip[i]);
1690                 if (ret != 0) {
1691                         talloc_free(pubip_list);
1692                         return ret;
1693                 }
1694                 offset += ctdb_public_ip_len(&pubip_list->ip[i]);
1695         }
1696
1697         *out = pubip_list;
1698         return 0;
1699 }
1700
1701 size_t ctdb_node_and_flags_len(struct ctdb_node_and_flags *node)
1702 {
1703         return sizeof(struct ctdb_node_and_flags);
1704 }
1705
1706 void ctdb_node_and_flags_push(struct ctdb_node_and_flags *node, uint8_t *buf)
1707 {
1708         memcpy(buf, node, sizeof(struct ctdb_node_and_flags));
1709 }
1710
1711 static int ctdb_node_and_flags_pull_elems(TALLOC_CTX *mem_ctx,
1712                                           uint8_t *buf, size_t buflen,
1713                                           struct ctdb_node_and_flags *out)
1714 {
1715         if (buflen < sizeof(struct ctdb_node_and_flags)) {
1716                 return EMSGSIZE;
1717         }
1718
1719         memcpy(out, buf, sizeof(struct ctdb_node_and_flags));
1720
1721         return 0;
1722 }
1723
1724 int ctdb_node_and_flags_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1725                               struct ctdb_node_and_flags **out)
1726 {
1727         struct ctdb_node_and_flags *node;
1728         int ret;
1729
1730         node = talloc(mem_ctx, struct ctdb_node_and_flags);
1731         if (node == NULL) {
1732                 return ENOMEM;
1733         }
1734
1735         ret = ctdb_node_and_flags_pull_elems(node, buf, buflen, node);
1736         if (ret != 0) {
1737                 TALLOC_FREE(node);
1738         }
1739
1740         *out = node;
1741         return ret;
1742 }
1743
1744 struct ctdb_node_map_wire {
1745         uint32_t num;
1746         struct ctdb_node_and_flags node[1];
1747 };
1748
1749 size_t ctdb_node_map_len(struct ctdb_node_map *nodemap)
1750 {
1751         return sizeof(uint32_t) +
1752                nodemap->num * sizeof(struct ctdb_node_and_flags);
1753 }
1754
1755 void ctdb_node_map_push(struct ctdb_node_map *nodemap, uint8_t *buf)
1756 {
1757         struct ctdb_node_map_wire *wire = (struct ctdb_node_map_wire *)buf;
1758         size_t offset;
1759         int i;
1760
1761         wire->num = nodemap->num;
1762
1763         offset = offsetof(struct ctdb_node_map_wire, node);
1764         for (i=0; i<nodemap->num; i++) {
1765                 ctdb_node_and_flags_push(&nodemap->node[i], &buf[offset]);
1766                 offset += ctdb_node_and_flags_len(&nodemap->node[i]);
1767         }
1768 }
1769
1770 int ctdb_node_map_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1771                        struct ctdb_node_map **out)
1772 {
1773         struct ctdb_node_map *nodemap;
1774         struct ctdb_node_map_wire *wire = (struct ctdb_node_map_wire *)buf;
1775         size_t offset;
1776         int i;
1777         bool ret;
1778
1779         nodemap = talloc(mem_ctx, struct ctdb_node_map);
1780         if (nodemap == NULL) {
1781                 return ENOMEM;
1782         }
1783
1784         nodemap->num = wire->num;
1785         nodemap->node = talloc_array(nodemap, struct ctdb_node_and_flags,
1786                                      wire->num);
1787         if (nodemap->node == NULL) {
1788                 talloc_free(nodemap);
1789                 return ENOMEM;
1790         }
1791
1792         offset = offsetof(struct ctdb_node_map_wire, node);
1793         for (i=0; i<wire->num; i++) {
1794                 ret = ctdb_node_and_flags_pull_elems(nodemap->node,
1795                                                      &buf[offset],
1796                                                      buflen-offset,
1797                                                      &nodemap->node[i]);
1798                 if (ret != 0) {
1799                         talloc_free(nodemap);
1800                         return ret;
1801                 }
1802                 offset += ctdb_node_and_flags_len(&nodemap->node[i]);
1803         }
1804
1805         *out = nodemap;
1806         return 0;
1807 }
1808
1809 size_t ctdb_script_len(struct ctdb_script *script)
1810 {
1811         return sizeof(struct ctdb_script);
1812 }
1813
1814 void ctdb_script_push(struct ctdb_script *script, uint8_t *buf)
1815 {
1816         memcpy(buf, script, sizeof(struct ctdb_script));
1817 }
1818
1819 static int ctdb_script_pull_elems(uint8_t *buf, size_t buflen,
1820                                   TALLOC_CTX *mem_ctx,
1821                                   struct ctdb_script *out)
1822 {
1823         if (buflen < sizeof(struct ctdb_script)) {
1824                 return EMSGSIZE;
1825         }
1826
1827         memcpy(out, buf, sizeof(struct ctdb_script));
1828
1829         return 0;
1830 }
1831
1832 int ctdb_script_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1833                      struct ctdb_script **out)
1834 {
1835         struct ctdb_script *script;
1836         int ret;
1837
1838         script = talloc(mem_ctx, struct ctdb_script);
1839         if (script == NULL) {
1840                 return ENOMEM;
1841         }
1842
1843         ret = ctdb_script_pull_elems(buf, buflen, script, script);
1844         if (ret != 0) {
1845                 TALLOC_FREE(script);
1846         }
1847
1848         *out = script;
1849         return ret;
1850 }
1851
1852 struct ctdb_script_list_wire {
1853         uint32_t num_scripts;
1854         struct ctdb_script script[1];
1855 };
1856
1857 size_t ctdb_script_list_len(struct ctdb_script_list *script_list)
1858 {
1859         int i;
1860         size_t len;
1861
1862         if (script_list == NULL) {
1863                 return 0;
1864         }
1865
1866         len = offsetof(struct ctdb_script_list_wire, script);
1867         for (i=0; i<script_list->num_scripts; i++) {
1868                 len += ctdb_script_len(&script_list->script[i]);
1869         }
1870         return len;
1871 }
1872
1873 void ctdb_script_list_push(struct ctdb_script_list *script_list, uint8_t *buf)
1874 {
1875         struct ctdb_script_list_wire *wire =
1876                 (struct ctdb_script_list_wire *)buf;
1877         size_t offset;
1878         int i;
1879
1880         if (script_list == NULL) {
1881                 return;
1882         }
1883
1884         wire->num_scripts = script_list->num_scripts;
1885
1886         offset = offsetof(struct ctdb_script_list_wire, script);
1887         for (i=0; i<script_list->num_scripts; i++) {
1888                 ctdb_script_push(&script_list->script[i], &buf[offset]);
1889                 offset += ctdb_script_len(&script_list->script[i]);
1890         }
1891 }
1892
1893 int ctdb_script_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1894                           struct ctdb_script_list **out)
1895 {
1896         struct ctdb_script_list *script_list;
1897         struct ctdb_script_list_wire *wire =
1898                 (struct ctdb_script_list_wire *)buf;
1899         size_t offset;
1900         int i;
1901         bool ret;
1902
1903         /* If event scripts have never been run, the result will be NULL */
1904         if (buflen == 0) {
1905                 *out = NULL;
1906                 return 0;
1907         }
1908
1909         offset = offsetof(struct ctdb_script_list_wire, script);
1910
1911         if (buflen < offset) {
1912                 return EMSGSIZE;
1913         }
1914         if (buflen < offset + wire->num_scripts * sizeof(struct ctdb_script)) {
1915                 return EMSGSIZE;
1916         }
1917
1918         script_list = talloc(mem_ctx, struct ctdb_script_list);
1919         if (script_list == NULL) {
1920                 return ENOMEM;
1921
1922         }
1923
1924         script_list->num_scripts = wire->num_scripts;
1925         script_list->script = talloc_array(script_list, struct ctdb_script,
1926                                            wire->num_scripts);
1927         if (script_list->script == NULL) {
1928                 talloc_free(script_list);
1929                 return ENOMEM;
1930         }
1931
1932         for (i=0; i<wire->num_scripts; i++) {
1933                 ret = ctdb_script_pull_elems(&buf[offset], buflen-offset,
1934                                              script_list->script,
1935                                              &script_list->script[i]);
1936                 if (ret != 0) {
1937                         talloc_free(script_list);
1938                         return ret;
1939                 }
1940                 offset += ctdb_script_len(&script_list->script[i]);
1941         }
1942
1943         *out = script_list;
1944         return 0;
1945 }
1946
1947 size_t ctdb_ban_state_len(struct ctdb_ban_state *ban_state)
1948 {
1949         return sizeof(struct ctdb_ban_state);
1950 }
1951
1952 void ctdb_ban_state_push(struct ctdb_ban_state *ban_state, uint8_t *buf)
1953 {
1954         memcpy(buf, ban_state, sizeof(struct ctdb_ban_state));
1955 }
1956
1957 int ctdb_ban_state_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1958                         struct ctdb_ban_state **out)
1959 {
1960         struct ctdb_ban_state *ban_state;
1961
1962         if (buflen < sizeof(struct ctdb_ban_state)) {
1963                 return EMSGSIZE;
1964         }
1965
1966         ban_state = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_ban_state));
1967         if (ban_state == NULL) {
1968                 return ENOMEM;
1969         }
1970
1971         *out = ban_state;
1972         return 0;
1973 }
1974
1975 size_t ctdb_db_priority_len(struct ctdb_db_priority *db_prio)
1976 {
1977         return sizeof(struct ctdb_db_priority);
1978 }
1979
1980 void ctdb_db_priority_push(struct ctdb_db_priority *db_prio, uint8_t *buf)
1981 {
1982         memcpy(buf, db_prio, sizeof(struct ctdb_db_priority));
1983 }
1984
1985 int ctdb_db_priority_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1986                           struct ctdb_db_priority **out)
1987 {
1988         struct ctdb_db_priority *db_prio;
1989
1990         if (buflen < sizeof(struct ctdb_db_priority)) {
1991                 return EMSGSIZE;
1992         }
1993
1994         db_prio = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_db_priority));
1995         if (db_prio == NULL) {
1996                 return ENOMEM;
1997         }
1998
1999         *out = db_prio;
2000         return 0;
2001 }
2002
2003 struct ctdb_notify_data_wire {
2004         uint64_t srvid;
2005         uint32_t len;
2006         uint8_t data[1];
2007 };
2008
2009 size_t ctdb_notify_data_len(struct ctdb_notify_data *notify)
2010 {
2011         return offsetof(struct ctdb_notify_data_wire, data) +
2012                notify->data.dsize;
2013 }
2014
2015 void ctdb_notify_data_push(struct ctdb_notify_data *notify, uint8_t *buf)
2016 {
2017         struct ctdb_notify_data_wire *wire =
2018                 (struct ctdb_notify_data_wire *)buf;
2019
2020         wire->srvid = notify->srvid;
2021         wire->len = notify->data.dsize;
2022         memcpy(wire->data, notify->data.dptr, notify->data.dsize);
2023 }
2024
2025 int ctdb_notify_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2026                           struct ctdb_notify_data **out)
2027 {
2028         struct ctdb_notify_data *notify;
2029         struct ctdb_notify_data_wire *wire =
2030                 (struct ctdb_notify_data_wire *)buf;
2031
2032         if (buflen < offsetof(struct ctdb_notify_data_wire, data)) {
2033                 return EMSGSIZE;
2034         }
2035         if (buflen < offsetof(struct ctdb_notify_data_wire, data) + wire->len) {
2036                 return EMSGSIZE;
2037         }
2038
2039         notify = talloc(mem_ctx, struct ctdb_notify_data);
2040         if (notify == NULL) {
2041                 return ENOMEM;
2042         }
2043
2044         notify->srvid = wire->srvid;
2045         notify->data.dsize = wire->len;
2046         notify->data.dptr = talloc_memdup(notify, wire->data, wire->len);
2047         if (notify->data.dptr == NULL) {
2048                 talloc_free(notify);
2049                 return ENOMEM;
2050         }
2051
2052         *out = notify;
2053         return 0;
2054 }
2055
2056 size_t ctdb_iface_len(struct ctdb_iface *iface)
2057 {
2058         return sizeof(struct ctdb_iface);
2059 }
2060
2061 void ctdb_iface_push(struct ctdb_iface *iface, uint8_t *buf)
2062 {
2063         memcpy(buf, iface, sizeof(struct ctdb_iface));
2064 }
2065
2066 static int ctdb_iface_pull_elems(uint8_t *buf, size_t buflen,
2067                                  TALLOC_CTX *mem_ctx,
2068                                  struct ctdb_iface *out)
2069 {
2070         if (buflen < sizeof(struct ctdb_iface)) {
2071                 return EMSGSIZE;
2072         }
2073
2074         memcpy(out, buf, sizeof(struct ctdb_iface));
2075
2076         return 0;
2077 }
2078
2079 int ctdb_iface_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2080                     struct ctdb_iface **out)
2081 {
2082         struct ctdb_iface *iface;
2083         int ret;
2084
2085         iface = talloc(mem_ctx, struct ctdb_iface);
2086         if (iface == NULL) {
2087                 return ENOMEM;
2088         }
2089
2090         ret = ctdb_iface_pull_elems(buf, buflen, iface, iface);
2091         if (ret != 0) {
2092                 TALLOC_FREE(iface);
2093         }
2094
2095         *out = iface;
2096         return ret;
2097 }
2098
2099 struct ctdb_iface_list_wire {
2100         uint32_t num;
2101         struct ctdb_iface iface[1];
2102 };
2103
2104 size_t ctdb_iface_list_len(struct ctdb_iface_list *iface_list)
2105 {
2106         return sizeof(uint32_t) +
2107                iface_list->num * sizeof(struct ctdb_iface);
2108 }
2109
2110 void ctdb_iface_list_push(struct ctdb_iface_list *iface_list, uint8_t *buf)
2111 {
2112         struct ctdb_iface_list_wire *wire =
2113                 (struct ctdb_iface_list_wire *)buf;
2114
2115         wire->num = iface_list->num;
2116         memcpy(wire->iface, iface_list->iface,
2117                iface_list->num * sizeof(struct ctdb_iface));
2118 }
2119
2120 int ctdb_iface_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2121                          struct ctdb_iface_list **out)
2122 {
2123         struct ctdb_iface_list *iface_list;
2124         struct ctdb_iface_list_wire *wire =
2125                 (struct ctdb_iface_list_wire *)buf;
2126
2127         if (buflen < sizeof(uint32_t)) {
2128                 return EMSGSIZE;
2129         }
2130         if (buflen < sizeof(uint32_t) + wire->num * sizeof(struct ctdb_iface)) {
2131                 return EMSGSIZE;
2132         }
2133
2134         iface_list = talloc(mem_ctx, struct ctdb_iface_list);
2135         if (iface_list == NULL) {
2136                 return ENOMEM;
2137         }
2138
2139         iface_list->num = wire->num;
2140         iface_list->iface = talloc_array(iface_list, struct ctdb_iface,
2141                                          wire->num);
2142         if (iface_list->iface == NULL) {
2143                 talloc_free(iface_list);
2144                 return ENOMEM;
2145         }
2146
2147         memcpy(iface_list->iface, wire->iface,
2148                wire->num * sizeof(struct ctdb_iface));
2149
2150         *out = iface_list;
2151         return 0;
2152 }
2153
2154 struct ctdb_public_ip_info_wire {
2155         struct ctdb_public_ip ip;
2156         uint32_t active_idx;
2157         uint32_t num;
2158         struct ctdb_iface ifaces[1];
2159 };
2160
2161 size_t ctdb_public_ip_info_len(struct ctdb_public_ip_info *ipinfo)
2162 {
2163         return offsetof(struct ctdb_public_ip_info_wire, num) +
2164                ctdb_iface_list_len(ipinfo->ifaces);
2165 }
2166
2167 void ctdb_public_ip_info_push(struct ctdb_public_ip_info *ipinfo, uint8_t *buf)
2168 {
2169         struct ctdb_public_ip_info_wire *wire =
2170                 (struct ctdb_public_ip_info_wire *)buf;
2171         size_t offset;
2172
2173         offset = offsetof(struct ctdb_public_ip_info_wire, num);
2174         memcpy(wire, ipinfo, offset);
2175         wire->num = ipinfo->ifaces->num;
2176         memcpy(wire->ifaces, ipinfo->ifaces->iface,
2177                ipinfo->ifaces->num * sizeof(struct ctdb_iface));
2178 }
2179
2180 int ctdb_public_ip_info_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2181                              struct ctdb_public_ip_info **out)
2182 {
2183         struct ctdb_public_ip_info *ipinfo;
2184         struct ctdb_public_ip_info_wire *wire =
2185                 (struct ctdb_public_ip_info_wire *)buf;
2186
2187         if (buflen < offsetof(struct ctdb_public_ip_info_wire, ifaces)) {
2188                 return EMSGSIZE;
2189         }
2190
2191         ipinfo = talloc(mem_ctx, struct ctdb_public_ip_info);
2192         if (ipinfo == NULL) {
2193                 return ENOMEM;
2194         }
2195
2196         memcpy(ipinfo, wire, offsetof(struct ctdb_public_ip_info_wire, num));
2197
2198         ipinfo->ifaces = talloc(ipinfo, struct ctdb_iface_list);
2199         if (ipinfo->ifaces == NULL) {
2200                 talloc_free(ipinfo);
2201                 return ENOMEM;
2202         }
2203
2204         ipinfo->ifaces->num = wire->num;
2205         ipinfo->ifaces->iface = talloc_array(ipinfo->ifaces, struct ctdb_iface,
2206                                              wire->num);
2207         if (ipinfo->ifaces->iface == NULL) {
2208                 talloc_free(ipinfo);
2209                 return ENOMEM;
2210         }
2211
2212         memcpy(ipinfo->ifaces->iface, wire->ifaces,
2213                wire->num * sizeof(struct ctdb_iface));
2214
2215         *out = ipinfo;
2216         return 0;
2217 }
2218
2219 struct ctdb_key_data_wire {
2220         uint32_t db_id;
2221         struct ctdb_ltdb_header header;
2222         uint32_t keylen;
2223         uint8_t key[1];
2224 };
2225
2226 size_t ctdb_key_data_len(struct ctdb_key_data *key)
2227 {
2228         return offsetof(struct ctdb_key_data_wire, key) + key->key.dsize;
2229 }
2230
2231 void ctdb_key_data_push(struct ctdb_key_data *key, uint8_t *buf)
2232 {
2233         struct ctdb_key_data_wire *wire = (struct ctdb_key_data_wire *)buf;
2234
2235         memcpy(wire, key, offsetof(struct ctdb_key_data, key));
2236         wire->keylen = key->key.dsize;
2237         memcpy(wire->key, key->key.dptr, key->key.dsize);
2238 }
2239
2240 int ctdb_key_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2241                        struct ctdb_key_data **out)
2242 {
2243         struct ctdb_key_data *key_data;
2244         struct ctdb_key_data_wire *wire = (struct ctdb_key_data_wire *)buf;
2245
2246         if (buflen < offsetof(struct ctdb_key_data_wire, key)) {
2247                 return EMSGSIZE;
2248         }
2249         if (buflen < offsetof(struct ctdb_key_data_wire, key) + wire->keylen) {
2250                 return EMSGSIZE;
2251         }
2252
2253         key_data = talloc(mem_ctx, struct ctdb_key_data);
2254         if (key_data == NULL) {
2255                 return ENOMEM;
2256         }
2257
2258         memcpy(key_data, wire, offsetof(struct ctdb_key_data, key));
2259
2260         key_data->key.dsize = wire->keylen;
2261         key_data->key.dptr = talloc_memdup(key_data, wire->key, wire->keylen);
2262         if (key_data->key.dptr == NULL) {
2263                 talloc_free(key_data);
2264                 return ENOMEM;
2265         }
2266
2267         *out = key_data;
2268         return 0;
2269 }
2270
2271 struct ctdb_db_statistics_wire {
2272         struct ctdb_db_statistics dbstats;
2273         char hot_keys_wire[1];
2274 };
2275
2276 size_t ctdb_db_statistics_len(struct ctdb_db_statistics *dbstats)
2277 {
2278         size_t len;
2279         int i;
2280
2281         len = sizeof(struct ctdb_db_statistics);
2282         for (i=0; i<MAX_HOT_KEYS; i++) {
2283                 len += dbstats->hot_keys[i].key.dsize;
2284         }
2285         return len;
2286 }
2287
2288 void ctdb_db_statistics_push(struct ctdb_db_statistics *dbstats, void *buf)
2289 {
2290         struct ctdb_db_statistics_wire *wire =
2291                 (struct ctdb_db_statistics_wire *)buf;
2292         size_t offset;
2293         int i;
2294
2295         dbstats->num_hot_keys = MAX_HOT_KEYS;
2296         memcpy(wire, dbstats, sizeof(struct ctdb_db_statistics));
2297
2298         offset = 0;
2299         for (i=0; i<MAX_HOT_KEYS; i++) {
2300                 memcpy(&wire->hot_keys_wire[offset],
2301                        dbstats->hot_keys[i].key.dptr,
2302                        dbstats->hot_keys[i].key.dsize);
2303                 offset += dbstats->hot_keys[i].key.dsize;
2304         }
2305 }
2306
2307 int ctdb_db_statistics_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2308                             struct ctdb_db_statistics **out)
2309 {
2310         struct ctdb_db_statistics *dbstats;
2311         struct ctdb_db_statistics_wire *wire =
2312                 (struct ctdb_db_statistics_wire *)buf;
2313         size_t offset;
2314         int i;
2315
2316         if (buflen < sizeof(struct ctdb_db_statistics)) {
2317                 return EMSGSIZE;
2318         }
2319         offset = 0;
2320         for (i=0; i<wire->dbstats.num_hot_keys; i++) {
2321                 offset += wire->dbstats.hot_keys[i].key.dsize;
2322         }
2323         if (buflen < sizeof(struct ctdb_db_statistics) + offset) {
2324                 return EMSGSIZE;
2325         }
2326
2327         dbstats = talloc(mem_ctx, struct ctdb_db_statistics);
2328         if (dbstats == NULL) {
2329                 return ENOMEM;
2330         }
2331
2332         memcpy(dbstats, wire, sizeof(struct ctdb_db_statistics));
2333
2334         offset = 0;
2335         for (i=0; i<wire->dbstats.num_hot_keys; i++) {
2336                 uint8_t *ptr;
2337                 size_t key_size;
2338
2339                 key_size = dbstats->hot_keys[i].key.dsize;
2340                 ptr = talloc_memdup(mem_ctx, &wire->hot_keys_wire[offset],
2341                                     key_size);
2342                 if (ptr == NULL) {
2343                         talloc_free(dbstats);
2344                         return ENOMEM;
2345                 }
2346                 dbstats->hot_keys[i].key.dptr = ptr;
2347                 offset += key_size;
2348         }
2349
2350         *out = dbstats;
2351         return 0;
2352 }
2353
2354 size_t ctdb_election_message_len(struct ctdb_election_message *election)
2355 {
2356         return sizeof(struct ctdb_election_message);
2357 }
2358
2359 void ctdb_election_message_push(struct ctdb_election_message *election,
2360                                 uint8_t *buf)
2361 {
2362         memcpy(buf, election, sizeof(struct ctdb_election_message));
2363 }
2364
2365 int ctdb_election_message_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2366                                struct ctdb_election_message **out)
2367 {
2368         struct ctdb_election_message *election;
2369
2370         if (buflen < sizeof(struct ctdb_election_message)) {
2371                 return EMSGSIZE;
2372         }
2373
2374         election = talloc_memdup(mem_ctx, buf,
2375                                  sizeof(struct ctdb_election_message));
2376         if (election == NULL) {
2377                 return ENOMEM;
2378         }
2379
2380         *out = election;
2381         return 0;
2382 }
2383
2384 size_t ctdb_srvid_message_len(struct ctdb_srvid_message *msg)
2385 {
2386         return sizeof(struct ctdb_srvid_message);
2387 }
2388
2389 void ctdb_srvid_message_push(struct ctdb_srvid_message *msg, uint8_t *buf)
2390 {
2391         memcpy(buf, msg, sizeof(struct ctdb_srvid_message));
2392 }
2393
2394 int ctdb_srvid_message_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2395                             struct ctdb_srvid_message **out)
2396 {
2397         struct ctdb_srvid_message *msg;
2398
2399         if (buflen < sizeof(struct ctdb_srvid_message)) {
2400                 return EMSGSIZE;
2401         }
2402
2403         msg = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_srvid_message));
2404         if (msg == NULL) {
2405                 return ENOMEM;
2406         }
2407
2408         *out = msg;
2409         return 0;
2410 }
2411
2412 size_t ctdb_disable_message_len(struct ctdb_disable_message *disable)
2413 {
2414         return sizeof(struct ctdb_disable_message);
2415 }
2416
2417 void ctdb_disable_message_push(struct ctdb_disable_message *disable,
2418                                uint8_t *buf)
2419 {
2420         memcpy(buf, disable, sizeof(struct ctdb_disable_message));
2421 }
2422
2423 int ctdb_disable_message_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2424                               struct ctdb_disable_message **out)
2425 {
2426         struct ctdb_disable_message *disable;
2427
2428         if (buflen < sizeof(struct ctdb_disable_message)) {
2429                 return EMSGSIZE;
2430         }
2431
2432         disable = talloc_memdup(mem_ctx, buf,
2433                                 sizeof(struct ctdb_disable_message));
2434         if (disable == NULL) {
2435                 return ENOMEM;
2436         }
2437
2438         *out = disable;
2439         return 0;
2440 }
2441
2442 size_t ctdb_tdb_data_len(TDB_DATA data)
2443 {
2444         return data.dsize;
2445 }
2446
2447 void ctdb_tdb_data_push(TDB_DATA data, uint8_t *buf)
2448 {
2449         memcpy(buf, data.dptr, data.dsize);
2450 }
2451
2452 int ctdb_tdb_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2453                        TDB_DATA *out)
2454 {
2455         TDB_DATA data;
2456
2457         data.dsize = buflen;
2458         if (data.dsize > 0) {
2459                 data.dptr = talloc_memdup(mem_ctx, buf, buflen);
2460                 if (data.dptr == NULL) {
2461                         return ENOMEM;
2462                 }
2463         } else {
2464                 data.dptr = NULL;
2465         }
2466
2467         *out = data;
2468         return 0;
2469 }
2470
2471 size_t ctdb_server_id_len(struct ctdb_server_id *sid)
2472 {
2473         return sizeof(struct ctdb_server_id);
2474 }
2475
2476 void ctdb_server_id_push(struct ctdb_server_id *sid, uint8_t *buf)
2477 {
2478         memcpy(buf, sid, sizeof(struct ctdb_server_id));
2479 }
2480
2481 int ctdb_server_id_pull(uint8_t *buf, size_t buflen,
2482                         struct ctdb_server_id *sid)
2483 {
2484         if (buflen < sizeof(struct ctdb_server_id)) {
2485                 return EMSGSIZE;
2486         }
2487
2488         memcpy(sid, buf, sizeof(struct ctdb_server_id));
2489         return 0;
2490 }
2491
2492 size_t ctdb_g_lock_len(struct ctdb_g_lock *lock)
2493 {
2494         return sizeof(struct ctdb_g_lock);
2495 }
2496
2497 void ctdb_g_lock_push(struct ctdb_g_lock *lock, uint8_t *buf)
2498 {
2499         memcpy(buf, lock, sizeof(struct ctdb_g_lock));
2500 }
2501
2502 int ctdb_g_lock_pull(uint8_t *buf, size_t buflen, struct ctdb_g_lock *lock)
2503 {
2504         if (buflen < sizeof(struct ctdb_g_lock)) {
2505                 return EMSGSIZE;
2506         }
2507
2508         memcpy(lock, buf, sizeof(struct ctdb_g_lock));
2509         return 0;
2510 }
2511
2512 size_t ctdb_g_lock_list_len(struct ctdb_g_lock_list *lock_list)
2513 {
2514         return lock_list->num * sizeof(struct ctdb_g_lock);
2515 }
2516
2517 void ctdb_g_lock_list_push(struct ctdb_g_lock_list *lock_list, uint8_t *buf)
2518 {
2519         size_t offset = 0;
2520         int i;
2521
2522         for (i=0; i<lock_list->num; i++) {
2523                 ctdb_g_lock_push(&lock_list->lock[i], &buf[offset]);
2524                 offset += sizeof(struct ctdb_g_lock);
2525         }
2526 }
2527
2528 int ctdb_g_lock_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2529                           struct ctdb_g_lock_list **out)
2530 {
2531         struct ctdb_g_lock_list *lock_list;
2532         unsigned count;
2533         size_t offset;
2534         int ret, i;
2535
2536         lock_list = talloc_zero(mem_ctx, struct ctdb_g_lock_list);
2537         if (lock_list == NULL) {
2538                 return ENOMEM;
2539         }
2540
2541         count = buflen / sizeof(struct ctdb_g_lock);
2542         lock_list->lock = talloc_array(lock_list, struct ctdb_g_lock, count);
2543         if (lock_list->lock == NULL) {
2544                 talloc_free(lock_list);
2545                 return ENOMEM;
2546         }
2547
2548         offset = 0;
2549         for (i=0; i<count; i++) {
2550                 ret = ctdb_g_lock_pull(&buf[offset], buflen-offset,
2551                                        &lock_list->lock[i]);
2552                 if (ret != 0) {
2553                         talloc_free(lock_list);
2554                         return ret;
2555                 }
2556                 offset += sizeof(struct ctdb_g_lock);
2557         }
2558
2559         lock_list->num = count;
2560
2561         *out = lock_list;
2562         return 0;
2563 }