f868d767d079911d1b259a312c5ffeeb9bf0f0cc
[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         return offsetof(struct ctdb_addr_info_wire, iface) +
1462                strlen(arp->iface)+1;
1463 }
1464
1465 void ctdb_addr_info_push(struct ctdb_addr_info *addr_info, uint8_t *buf)
1466 {
1467         struct ctdb_addr_info_wire *wire = (struct ctdb_addr_info_wire *)buf;
1468
1469         wire->addr = addr_info->addr;
1470         wire->mask = addr_info->mask;
1471         wire->len = strlen(addr_info->iface)+1;
1472         memcpy(wire->iface, addr_info->iface, wire->len);
1473 }
1474
1475 int ctdb_addr_info_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1476                         struct ctdb_addr_info **out)
1477 {
1478         struct ctdb_addr_info *addr_info;
1479         struct ctdb_addr_info_wire *wire = (struct ctdb_addr_info_wire *)buf;
1480
1481         if (buflen < offsetof(struct ctdb_addr_info_wire, iface)) {
1482                 return EMSGSIZE;
1483         }
1484         if (buflen < offsetof(struct ctdb_addr_info_wire, iface) + wire->len) {
1485                 return EMSGSIZE;
1486         }
1487
1488         addr_info = talloc(mem_ctx, struct ctdb_addr_info);
1489         if (addr_info == NULL) {
1490                 return ENOMEM;
1491         }
1492
1493         addr_info->addr = wire->addr;
1494         addr_info->mask = wire->mask;
1495
1496         addr_info->iface = talloc_strndup(addr_info, wire->iface, wire->len);
1497         if (addr_info->iface == NULL) {
1498                 talloc_free(addr_info);
1499                 return ENOMEM;
1500         }
1501
1502         *out = addr_info;
1503         return 0;
1504 }
1505
1506 size_t ctdb_transdb_len(struct ctdb_transdb *transdb)
1507 {
1508         return sizeof(struct ctdb_transdb);
1509 }
1510
1511 void ctdb_transdb_push(struct ctdb_transdb *transdb, uint8_t *buf)
1512 {
1513         memcpy(buf, transdb, sizeof(struct ctdb_transdb));
1514 }
1515
1516 int ctdb_transdb_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1517                      struct ctdb_transdb **out)
1518 {
1519         struct ctdb_transdb *transdb;
1520
1521         if (buflen < sizeof(struct ctdb_transdb)) {
1522                 return EMSGSIZE;
1523         }
1524
1525         transdb = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_transdb));
1526         if (transdb == NULL) {
1527                 return ENOMEM;
1528         }
1529
1530         *out = transdb;
1531         return 0;
1532 }
1533
1534 size_t ctdb_uptime_len(struct ctdb_uptime *uptime)
1535 {
1536         return sizeof(struct ctdb_uptime);
1537 }
1538
1539 void ctdb_uptime_push(struct ctdb_uptime *uptime, uint8_t *buf)
1540 {
1541         memcpy(buf, uptime, sizeof(struct ctdb_uptime));
1542 }
1543
1544 int ctdb_uptime_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1545                      struct ctdb_uptime **out)
1546 {
1547         struct ctdb_uptime *uptime;
1548
1549         if (buflen < sizeof(struct ctdb_uptime)) {
1550                 return EMSGSIZE;
1551         }
1552
1553         uptime = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_uptime));
1554         if (uptime == NULL) {
1555                 return ENOMEM;
1556         }
1557
1558         *out = uptime;
1559         return 0;
1560 }
1561
1562 size_t ctdb_public_ip_len(struct ctdb_public_ip *pubip)
1563 {
1564         return sizeof(struct ctdb_public_ip);
1565 }
1566
1567 void ctdb_public_ip_push(struct ctdb_public_ip *pubip, uint8_t *buf)
1568 {
1569         memcpy(buf, pubip, sizeof(struct ctdb_public_ip));
1570 }
1571
1572 static int ctdb_public_ip_pull_elems(uint8_t *buf, size_t buflen,
1573                                      TALLOC_CTX *mem_ctx,
1574                                      struct ctdb_public_ip *out)
1575 {
1576         if (buflen < sizeof(struct ctdb_public_ip)) {
1577                 return EMSGSIZE;
1578         }
1579
1580         memcpy(out, buf, sizeof(struct ctdb_public_ip));
1581
1582         return 0;
1583 }
1584
1585 int ctdb_public_ip_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1586                         struct ctdb_public_ip **out)
1587 {
1588         struct ctdb_public_ip *pubip;
1589         int ret;
1590
1591         pubip = talloc(mem_ctx, struct ctdb_public_ip);
1592         if (pubip == NULL) {
1593                 return ENOMEM;
1594         }
1595
1596         ret = ctdb_public_ip_pull_elems(buf, buflen, pubip, pubip);
1597         if (ret != 0) {
1598                 TALLOC_FREE(pubip);
1599         }
1600
1601         *out = pubip;
1602         return ret;
1603 }
1604
1605 struct ctdb_public_ip_list_wire {
1606         uint32_t num;
1607         struct ctdb_public_ip ip[1];
1608 };
1609
1610 size_t ctdb_public_ip_list_len(struct ctdb_public_ip_list *pubip_list)
1611 {
1612         int i;
1613         size_t len;
1614
1615         len = sizeof(uint32_t);
1616         for (i=0; i<pubip_list->num; i++) {
1617                 len += ctdb_public_ip_len(&pubip_list->ip[i]);
1618         }
1619         return len;
1620 }
1621
1622 void ctdb_public_ip_list_push(struct ctdb_public_ip_list *pubip_list,
1623                               uint8_t *buf)
1624 {
1625         struct ctdb_public_ip_list_wire *wire =
1626                 (struct ctdb_public_ip_list_wire *)buf;
1627         size_t offset;
1628         int i;
1629
1630         wire->num = pubip_list->num;
1631
1632         offset = offsetof(struct ctdb_public_ip_list_wire, ip);
1633         for (i=0; i<pubip_list->num; i++) {
1634                 ctdb_public_ip_push(&pubip_list->ip[i], &buf[offset]);
1635                 offset += ctdb_public_ip_len(&pubip_list->ip[i]);
1636         }
1637 }
1638
1639 int ctdb_public_ip_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1640                              struct ctdb_public_ip_list **out)
1641 {
1642         struct ctdb_public_ip_list *pubip_list;
1643         struct ctdb_public_ip_list_wire *wire =
1644                 (struct ctdb_public_ip_list_wire *)buf;
1645         size_t offset;
1646         int i;
1647         bool ret;
1648
1649         if (buflen < sizeof(uint32_t)) {
1650                 return EMSGSIZE;
1651         }
1652         if (buflen < sizeof(uint32_t) +
1653                      wire->num * sizeof(struct ctdb_public_ip)) {
1654                 return EMSGSIZE;
1655         }
1656
1657         pubip_list = talloc(mem_ctx, struct ctdb_public_ip_list);
1658         if (pubip_list == NULL) {
1659                 return ENOMEM;
1660         }
1661
1662         pubip_list->num = wire->num;
1663         pubip_list->ip = talloc_array(pubip_list, struct ctdb_public_ip,
1664                                       wire->num);
1665         if (pubip_list->ip == NULL) {
1666                 talloc_free(pubip_list);
1667                 return ENOMEM;
1668         }
1669
1670         offset = offsetof(struct ctdb_public_ip_list_wire, ip);
1671         for (i=0; i<wire->num; i++) {
1672                 ret = ctdb_public_ip_pull_elems(&buf[offset], buflen-offset,
1673                                                 pubip_list->ip,
1674                                                 &pubip_list->ip[i]);
1675                 if (ret != 0) {
1676                         talloc_free(pubip_list);
1677                         return ret;
1678                 }
1679                 offset += ctdb_public_ip_len(&pubip_list->ip[i]);
1680         }
1681
1682         *out = pubip_list;
1683         return 0;
1684 }
1685
1686 size_t ctdb_node_and_flags_len(struct ctdb_node_and_flags *node)
1687 {
1688         return sizeof(struct ctdb_node_and_flags);
1689 }
1690
1691 void ctdb_node_and_flags_push(struct ctdb_node_and_flags *node, uint8_t *buf)
1692 {
1693         memcpy(buf, node, sizeof(struct ctdb_node_and_flags));
1694 }
1695
1696 static int ctdb_node_and_flags_pull_elems(TALLOC_CTX *mem_ctx,
1697                                           uint8_t *buf, size_t buflen,
1698                                           struct ctdb_node_and_flags *out)
1699 {
1700         if (buflen < sizeof(struct ctdb_node_and_flags)) {
1701                 return EMSGSIZE;
1702         }
1703
1704         memcpy(out, buf, sizeof(struct ctdb_node_and_flags));
1705
1706         return 0;
1707 }
1708
1709 int ctdb_node_and_flags_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1710                               struct ctdb_node_and_flags **out)
1711 {
1712         struct ctdb_node_and_flags *node;
1713         int ret;
1714
1715         node = talloc(mem_ctx, struct ctdb_node_and_flags);
1716         if (node == NULL) {
1717                 return ENOMEM;
1718         }
1719
1720         ret = ctdb_node_and_flags_pull_elems(node, buf, buflen, node);
1721         if (ret != 0) {
1722                 TALLOC_FREE(node);
1723         }
1724
1725         *out = node;
1726         return ret;
1727 }
1728
1729 struct ctdb_node_map_wire {
1730         uint32_t num;
1731         struct ctdb_node_and_flags node[1];
1732 };
1733
1734 size_t ctdb_node_map_len(struct ctdb_node_map *nodemap)
1735 {
1736         return sizeof(uint32_t) +
1737                nodemap->num * sizeof(struct ctdb_node_and_flags);
1738 }
1739
1740 void ctdb_node_map_push(struct ctdb_node_map *nodemap, uint8_t *buf)
1741 {
1742         struct ctdb_node_map_wire *wire = (struct ctdb_node_map_wire *)buf;
1743         size_t offset;
1744         int i;
1745
1746         wire->num = nodemap->num;
1747
1748         offset = offsetof(struct ctdb_node_map_wire, node);
1749         for (i=0; i<nodemap->num; i++) {
1750                 ctdb_node_and_flags_push(&nodemap->node[i], &buf[offset]);
1751                 offset += ctdb_node_and_flags_len(&nodemap->node[i]);
1752         }
1753 }
1754
1755 int ctdb_node_map_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1756                        struct ctdb_node_map **out)
1757 {
1758         struct ctdb_node_map *nodemap;
1759         struct ctdb_node_map_wire *wire = (struct ctdb_node_map_wire *)buf;
1760         size_t offset;
1761         int i;
1762         bool ret;
1763
1764         nodemap = talloc(mem_ctx, struct ctdb_node_map);
1765         if (nodemap == NULL) {
1766                 return ENOMEM;
1767         }
1768
1769         nodemap->num = wire->num;
1770         nodemap->node = talloc_array(nodemap, struct ctdb_node_and_flags,
1771                                      wire->num);
1772         if (nodemap->node == NULL) {
1773                 talloc_free(nodemap);
1774                 return ENOMEM;
1775         }
1776
1777         offset = offsetof(struct ctdb_node_map_wire, node);
1778         for (i=0; i<wire->num; i++) {
1779                 ret = ctdb_node_and_flags_pull_elems(nodemap->node,
1780                                                      &buf[offset],
1781                                                      buflen-offset,
1782                                                      &nodemap->node[i]);
1783                 if (ret != 0) {
1784                         talloc_free(nodemap);
1785                         return ret;
1786                 }
1787                 offset += ctdb_node_and_flags_len(&nodemap->node[i]);
1788         }
1789
1790         *out = nodemap;
1791         return 0;
1792 }
1793
1794 size_t ctdb_script_len(struct ctdb_script *script)
1795 {
1796         return sizeof(struct ctdb_script);
1797 }
1798
1799 void ctdb_script_push(struct ctdb_script *script, uint8_t *buf)
1800 {
1801         memcpy(buf, script, sizeof(struct ctdb_script));
1802 }
1803
1804 static int ctdb_script_pull_elems(uint8_t *buf, size_t buflen,
1805                                   TALLOC_CTX *mem_ctx,
1806                                   struct ctdb_script *out)
1807 {
1808         if (buflen < sizeof(struct ctdb_script)) {
1809                 return EMSGSIZE;
1810         }
1811
1812         memcpy(out, buf, sizeof(struct ctdb_script));
1813
1814         return 0;
1815 }
1816
1817 int ctdb_script_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1818                      struct ctdb_script **out)
1819 {
1820         struct ctdb_script *script;
1821         int ret;
1822
1823         script = talloc(mem_ctx, struct ctdb_script);
1824         if (script == NULL) {
1825                 return ENOMEM;
1826         }
1827
1828         ret = ctdb_script_pull_elems(buf, buflen, script, script);
1829         if (ret != 0) {
1830                 TALLOC_FREE(script);
1831         }
1832
1833         *out = script;
1834         return ret;
1835 }
1836
1837 struct ctdb_script_list_wire {
1838         uint32_t num_scripts;
1839         struct ctdb_script script[1];
1840 };
1841
1842 size_t ctdb_script_list_len(struct ctdb_script_list *script_list)
1843 {
1844         int i;
1845         size_t len;
1846
1847         if (script_list == NULL) {
1848                 return 0;
1849         }
1850
1851         len = offsetof(struct ctdb_script_list_wire, script);
1852         for (i=0; i<script_list->num_scripts; i++) {
1853                 len += ctdb_script_len(&script_list->script[i]);
1854         }
1855         return len;
1856 }
1857
1858 void ctdb_script_list_push(struct ctdb_script_list *script_list, uint8_t *buf)
1859 {
1860         struct ctdb_script_list_wire *wire =
1861                 (struct ctdb_script_list_wire *)buf;
1862         size_t offset;
1863         int i;
1864
1865         if (script_list == NULL) {
1866                 return;
1867         }
1868
1869         wire->num_scripts = script_list->num_scripts;
1870
1871         offset = offsetof(struct ctdb_script_list_wire, script);
1872         for (i=0; i<script_list->num_scripts; i++) {
1873                 ctdb_script_push(&script_list->script[i], &buf[offset]);
1874                 offset += ctdb_script_len(&script_list->script[i]);
1875         }
1876 }
1877
1878 int ctdb_script_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1879                           struct ctdb_script_list **out)
1880 {
1881         struct ctdb_script_list *script_list;
1882         struct ctdb_script_list_wire *wire =
1883                 (struct ctdb_script_list_wire *)buf;
1884         size_t offset;
1885         int i;
1886         bool ret;
1887
1888         /* If event scripts have never been run, the result will be NULL */
1889         if (buflen == 0) {
1890                 *out = NULL;
1891                 return 0;
1892         }
1893
1894         offset = offsetof(struct ctdb_script_list_wire, script);
1895
1896         if (buflen < offset) {
1897                 return EMSGSIZE;
1898         }
1899         if (buflen < offset + wire->num_scripts * sizeof(struct ctdb_script)) {
1900                 return EMSGSIZE;
1901         }
1902
1903         script_list = talloc(mem_ctx, struct ctdb_script_list);
1904         if (script_list == NULL) {
1905                 return ENOMEM;
1906
1907         }
1908
1909         script_list->num_scripts = wire->num_scripts;
1910         script_list->script = talloc_array(script_list, struct ctdb_script,
1911                                            wire->num_scripts);
1912         if (script_list->script == NULL) {
1913                 talloc_free(script_list);
1914                 return ENOMEM;
1915         }
1916
1917         for (i=0; i<wire->num_scripts; i++) {
1918                 ret = ctdb_script_pull_elems(&buf[offset], buflen-offset,
1919                                              script_list->script,
1920                                              &script_list->script[i]);
1921                 if (ret != 0) {
1922                         talloc_free(script_list);
1923                         return ret;
1924                 }
1925                 offset += ctdb_script_len(&script_list->script[i]);
1926         }
1927
1928         *out = script_list;
1929         return 0;
1930 }
1931
1932 size_t ctdb_ban_state_len(struct ctdb_ban_state *ban_state)
1933 {
1934         return sizeof(struct ctdb_ban_state);
1935 }
1936
1937 void ctdb_ban_state_push(struct ctdb_ban_state *ban_state, uint8_t *buf)
1938 {
1939         memcpy(buf, ban_state, sizeof(struct ctdb_ban_state));
1940 }
1941
1942 int ctdb_ban_state_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1943                         struct ctdb_ban_state **out)
1944 {
1945         struct ctdb_ban_state *ban_state;
1946
1947         if (buflen < sizeof(struct ctdb_ban_state)) {
1948                 return EMSGSIZE;
1949         }
1950
1951         ban_state = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_ban_state));
1952         if (ban_state == NULL) {
1953                 return ENOMEM;
1954         }
1955
1956         *out = ban_state;
1957         return 0;
1958 }
1959
1960 size_t ctdb_db_priority_len(struct ctdb_db_priority *db_prio)
1961 {
1962         return sizeof(struct ctdb_db_priority);
1963 }
1964
1965 void ctdb_db_priority_push(struct ctdb_db_priority *db_prio, uint8_t *buf)
1966 {
1967         memcpy(buf, db_prio, sizeof(struct ctdb_db_priority));
1968 }
1969
1970 int ctdb_db_priority_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
1971                           struct ctdb_db_priority **out)
1972 {
1973         struct ctdb_db_priority *db_prio;
1974
1975         if (buflen < sizeof(struct ctdb_db_priority)) {
1976                 return EMSGSIZE;
1977         }
1978
1979         db_prio = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_db_priority));
1980         if (db_prio == NULL) {
1981                 return ENOMEM;
1982         }
1983
1984         *out = db_prio;
1985         return 0;
1986 }
1987
1988 struct ctdb_notify_data_wire {
1989         uint64_t srvid;
1990         uint32_t len;
1991         uint8_t data[1];
1992 };
1993
1994 size_t ctdb_notify_data_len(struct ctdb_notify_data *notify)
1995 {
1996         return offsetof(struct ctdb_notify_data_wire, data) +
1997                notify->data.dsize;
1998 }
1999
2000 void ctdb_notify_data_push(struct ctdb_notify_data *notify, uint8_t *buf)
2001 {
2002         struct ctdb_notify_data_wire *wire =
2003                 (struct ctdb_notify_data_wire *)buf;
2004
2005         wire->srvid = notify->srvid;
2006         wire->len = notify->data.dsize;
2007         memcpy(wire->data, notify->data.dptr, notify->data.dsize);
2008 }
2009
2010 int ctdb_notify_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2011                           struct ctdb_notify_data **out)
2012 {
2013         struct ctdb_notify_data *notify;
2014         struct ctdb_notify_data_wire *wire =
2015                 (struct ctdb_notify_data_wire *)buf;
2016
2017         if (buflen < offsetof(struct ctdb_notify_data_wire, data)) {
2018                 return EMSGSIZE;
2019         }
2020         if (buflen < offsetof(struct ctdb_notify_data_wire, data) + wire->len) {
2021                 return EMSGSIZE;
2022         }
2023
2024         notify = talloc(mem_ctx, struct ctdb_notify_data);
2025         if (notify == NULL) {
2026                 return ENOMEM;
2027         }
2028
2029         notify->srvid = wire->srvid;
2030         notify->data.dsize = wire->len;
2031         notify->data.dptr = talloc_memdup(notify, wire->data, wire->len);
2032         if (notify->data.dptr == NULL) {
2033                 talloc_free(notify);
2034                 return ENOMEM;
2035         }
2036
2037         *out = notify;
2038         return 0;
2039 }
2040
2041 size_t ctdb_iface_len(struct ctdb_iface *iface)
2042 {
2043         return sizeof(struct ctdb_iface);
2044 }
2045
2046 void ctdb_iface_push(struct ctdb_iface *iface, uint8_t *buf)
2047 {
2048         memcpy(buf, iface, sizeof(struct ctdb_iface));
2049 }
2050
2051 static int ctdb_iface_pull_elems(uint8_t *buf, size_t buflen,
2052                                  TALLOC_CTX *mem_ctx,
2053                                  struct ctdb_iface *out)
2054 {
2055         if (buflen < sizeof(struct ctdb_iface)) {
2056                 return EMSGSIZE;
2057         }
2058
2059         memcpy(out, buf, sizeof(struct ctdb_iface));
2060
2061         return 0;
2062 }
2063
2064 int ctdb_iface_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2065                     struct ctdb_iface **out)
2066 {
2067         struct ctdb_iface *iface;
2068         int ret;
2069
2070         iface = talloc(mem_ctx, struct ctdb_iface);
2071         if (iface == NULL) {
2072                 return ENOMEM;
2073         }
2074
2075         ret = ctdb_iface_pull_elems(buf, buflen, iface, iface);
2076         if (ret != 0) {
2077                 TALLOC_FREE(iface);
2078         }
2079
2080         *out = iface;
2081         return ret;
2082 }
2083
2084 struct ctdb_iface_list_wire {
2085         uint32_t num;
2086         struct ctdb_iface iface[1];
2087 };
2088
2089 size_t ctdb_iface_list_len(struct ctdb_iface_list *iface_list)
2090 {
2091         return sizeof(uint32_t) +
2092                iface_list->num * sizeof(struct ctdb_iface);
2093 }
2094
2095 void ctdb_iface_list_push(struct ctdb_iface_list *iface_list, uint8_t *buf)
2096 {
2097         struct ctdb_iface_list_wire *wire =
2098                 (struct ctdb_iface_list_wire *)buf;
2099
2100         wire->num = iface_list->num;
2101         memcpy(wire->iface, iface_list->iface,
2102                iface_list->num * sizeof(struct ctdb_iface));
2103 }
2104
2105 int ctdb_iface_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2106                          struct ctdb_iface_list **out)
2107 {
2108         struct ctdb_iface_list *iface_list;
2109         struct ctdb_iface_list_wire *wire =
2110                 (struct ctdb_iface_list_wire *)buf;
2111
2112         if (buflen < sizeof(uint32_t)) {
2113                 return EMSGSIZE;
2114         }
2115         if (buflen < sizeof(uint32_t) + wire->num * sizeof(struct ctdb_iface)) {
2116                 return EMSGSIZE;
2117         }
2118
2119         iface_list = talloc(mem_ctx, struct ctdb_iface_list);
2120         if (iface_list == NULL) {
2121                 return ENOMEM;
2122         }
2123
2124         iface_list->num = wire->num;
2125         iface_list->iface = talloc_array(iface_list, struct ctdb_iface,
2126                                          wire->num);
2127         if (iface_list->iface == NULL) {
2128                 talloc_free(iface_list);
2129                 return ENOMEM;
2130         }
2131
2132         memcpy(iface_list->iface, wire->iface,
2133                wire->num * sizeof(struct ctdb_iface));
2134
2135         *out = iface_list;
2136         return 0;
2137 }
2138
2139 struct ctdb_public_ip_info_wire {
2140         struct ctdb_public_ip ip;
2141         uint32_t active_idx;
2142         uint32_t num;
2143         struct ctdb_iface ifaces[1];
2144 };
2145
2146 size_t ctdb_public_ip_info_len(struct ctdb_public_ip_info *ipinfo)
2147 {
2148         return offsetof(struct ctdb_public_ip_info_wire, num) +
2149                ctdb_iface_list_len(ipinfo->ifaces);
2150 }
2151
2152 void ctdb_public_ip_info_push(struct ctdb_public_ip_info *ipinfo, uint8_t *buf)
2153 {
2154         struct ctdb_public_ip_info_wire *wire =
2155                 (struct ctdb_public_ip_info_wire *)buf;
2156         size_t offset;
2157
2158         offset = offsetof(struct ctdb_public_ip_info_wire, num);
2159         memcpy(wire, ipinfo, offset);
2160         wire->num = ipinfo->ifaces->num;
2161         memcpy(wire->ifaces, ipinfo->ifaces->iface,
2162                ipinfo->ifaces->num * sizeof(struct ctdb_iface));
2163 }
2164
2165 int ctdb_public_ip_info_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2166                              struct ctdb_public_ip_info **out)
2167 {
2168         struct ctdb_public_ip_info *ipinfo;
2169         struct ctdb_public_ip_info_wire *wire =
2170                 (struct ctdb_public_ip_info_wire *)buf;
2171
2172         if (buflen < offsetof(struct ctdb_public_ip_info_wire, ifaces)) {
2173                 return EMSGSIZE;
2174         }
2175
2176         ipinfo = talloc(mem_ctx, struct ctdb_public_ip_info);
2177         if (ipinfo == NULL) {
2178                 return ENOMEM;
2179         }
2180
2181         memcpy(ipinfo, wire, offsetof(struct ctdb_public_ip_info_wire, num));
2182
2183         ipinfo->ifaces = talloc(ipinfo, struct ctdb_iface_list);
2184         if (ipinfo->ifaces == NULL) {
2185                 talloc_free(ipinfo);
2186                 return ENOMEM;
2187         }
2188
2189         ipinfo->ifaces->num = wire->num;
2190         ipinfo->ifaces->iface = talloc_array(ipinfo->ifaces, struct ctdb_iface,
2191                                              wire->num);
2192         if (ipinfo->ifaces->iface == NULL) {
2193                 talloc_free(ipinfo);
2194                 return ENOMEM;
2195         }
2196
2197         memcpy(ipinfo->ifaces->iface, wire->ifaces,
2198                wire->num * sizeof(struct ctdb_iface));
2199
2200         *out = ipinfo;
2201         return 0;
2202 }
2203
2204 struct ctdb_key_data_wire {
2205         uint32_t db_id;
2206         struct ctdb_ltdb_header header;
2207         uint32_t keylen;
2208         uint8_t key[1];
2209 };
2210
2211 size_t ctdb_key_data_len(struct ctdb_key_data *key)
2212 {
2213         return offsetof(struct ctdb_key_data_wire, key) + key->key.dsize;
2214 }
2215
2216 void ctdb_key_data_push(struct ctdb_key_data *key, uint8_t *buf)
2217 {
2218         struct ctdb_key_data_wire *wire = (struct ctdb_key_data_wire *)buf;
2219
2220         memcpy(wire, key, offsetof(struct ctdb_key_data, key));
2221         wire->keylen = key->key.dsize;
2222         memcpy(wire->key, key->key.dptr, key->key.dsize);
2223 }
2224
2225 int ctdb_key_data_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2226                        struct ctdb_key_data **out)
2227 {
2228         struct ctdb_key_data *key_data;
2229         struct ctdb_key_data_wire *wire = (struct ctdb_key_data_wire *)buf;
2230
2231         if (buflen < offsetof(struct ctdb_key_data_wire, key)) {
2232                 return EMSGSIZE;
2233         }
2234         if (buflen < offsetof(struct ctdb_key_data_wire, key) + wire->keylen) {
2235                 return EMSGSIZE;
2236         }
2237
2238         key_data = talloc(mem_ctx, struct ctdb_key_data);
2239         if (key_data == NULL) {
2240                 return ENOMEM;
2241         }
2242
2243         memcpy(key_data, wire, offsetof(struct ctdb_key_data, key));
2244
2245         key_data->key.dsize = wire->keylen;
2246         key_data->key.dptr = talloc_memdup(key_data, wire->key, wire->keylen);
2247         if (key_data->key.dptr == NULL) {
2248                 talloc_free(key_data);
2249                 return ENOMEM;
2250         }
2251
2252         *out = key_data;
2253         return 0;
2254 }
2255
2256 struct ctdb_db_statistics_wire {
2257         struct ctdb_db_statistics dbstats;
2258         char hot_keys_wire[1];
2259 };
2260
2261 size_t ctdb_db_statistics_len(struct ctdb_db_statistics *dbstats)
2262 {
2263         size_t len;
2264         int i;
2265
2266         len = sizeof(struct ctdb_db_statistics);
2267         for (i=0; i<MAX_HOT_KEYS; i++) {
2268                 len += dbstats->hot_keys[i].key.dsize;
2269         }
2270         return len;
2271 }
2272
2273 void ctdb_db_statistics_push(struct ctdb_db_statistics *dbstats, void *buf)
2274 {
2275         struct ctdb_db_statistics_wire *wire =
2276                 (struct ctdb_db_statistics_wire *)buf;
2277         size_t offset;
2278         int i;
2279
2280         dbstats->num_hot_keys = MAX_HOT_KEYS;
2281         memcpy(wire, dbstats, sizeof(struct ctdb_db_statistics));
2282
2283         offset = 0;
2284         for (i=0; i<MAX_HOT_KEYS; i++) {
2285                 memcpy(&wire->hot_keys_wire[offset],
2286                        dbstats->hot_keys[i].key.dptr,
2287                        dbstats->hot_keys[i].key.dsize);
2288                 offset += dbstats->hot_keys[i].key.dsize;
2289         }
2290 }
2291
2292 int ctdb_db_statistics_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2293                             struct ctdb_db_statistics **out)
2294 {
2295         struct ctdb_db_statistics *dbstats;
2296         struct ctdb_db_statistics_wire *wire =
2297                 (struct ctdb_db_statistics_wire *)buf;
2298         size_t offset;
2299         int i;
2300
2301         if (buflen < sizeof(struct ctdb_db_statistics)) {
2302                 return EMSGSIZE;
2303         }
2304         offset = 0;
2305         for (i=0; i<wire->dbstats.num_hot_keys; i++) {
2306                 offset += wire->dbstats.hot_keys[i].key.dsize;
2307         }
2308         if (buflen < sizeof(struct ctdb_db_statistics) + offset) {
2309                 return EMSGSIZE;
2310         }
2311
2312         dbstats = talloc(mem_ctx, struct ctdb_db_statistics);
2313         if (dbstats == NULL) {
2314                 return ENOMEM;
2315         }
2316
2317         memcpy(dbstats, wire, sizeof(struct ctdb_db_statistics));
2318
2319         offset = 0;
2320         for (i=0; i<wire->dbstats.num_hot_keys; i++) {
2321                 uint8_t *ptr;
2322                 size_t key_size;
2323
2324                 key_size = dbstats->hot_keys[i].key.dsize;
2325                 ptr = talloc_memdup(mem_ctx, &wire->hot_keys_wire[offset],
2326                                     key_size);
2327                 if (ptr == NULL) {
2328                         talloc_free(dbstats);
2329                         return ENOMEM;
2330                 }
2331                 dbstats->hot_keys[i].key.dptr = ptr;
2332                 offset += key_size;
2333         }
2334
2335         *out = dbstats;
2336         return 0;
2337 }
2338
2339 size_t ctdb_election_message_len(struct ctdb_election_message *election)
2340 {
2341         return sizeof(struct ctdb_election_message);
2342 }
2343
2344 void ctdb_election_message_push(struct ctdb_election_message *election,
2345                                 uint8_t *buf)
2346 {
2347         memcpy(buf, election, sizeof(struct ctdb_election_message));
2348 }
2349
2350 int ctdb_election_message_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2351                                struct ctdb_election_message **out)
2352 {
2353         struct ctdb_election_message *election;
2354
2355         if (buflen < sizeof(struct ctdb_election_message)) {
2356                 return EMSGSIZE;
2357         }
2358
2359         election = talloc_memdup(mem_ctx, buf,
2360                                  sizeof(struct ctdb_election_message));
2361         if (election == NULL) {
2362                 return ENOMEM;
2363         }
2364
2365         *out = election;
2366         return 0;
2367 }
2368
2369 size_t ctdb_srvid_message_len(struct ctdb_srvid_message *msg)
2370 {
2371         return sizeof(struct ctdb_srvid_message);
2372 }
2373
2374 void ctdb_srvid_message_push(struct ctdb_srvid_message *msg, uint8_t *buf)
2375 {
2376         memcpy(buf, msg, sizeof(struct ctdb_srvid_message));
2377 }
2378
2379 int ctdb_srvid_message_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2380                             struct ctdb_srvid_message **out)
2381 {
2382         struct ctdb_srvid_message *msg;
2383
2384         if (buflen < sizeof(struct ctdb_srvid_message)) {
2385                 return EMSGSIZE;
2386         }
2387
2388         msg = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_srvid_message));
2389         if (msg == NULL) {
2390                 return ENOMEM;
2391         }
2392
2393         *out = msg;
2394         return 0;
2395 }
2396
2397 size_t ctdb_disable_message_len(struct ctdb_disable_message *disable)
2398 {
2399         return sizeof(struct ctdb_disable_message);
2400 }
2401
2402 void ctdb_disable_message_push(struct ctdb_disable_message *disable,
2403                                uint8_t *buf)
2404 {
2405         memcpy(buf, disable, sizeof(struct ctdb_disable_message));
2406 }
2407
2408 int ctdb_disable_message_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2409                               struct ctdb_disable_message **out)
2410 {
2411         struct ctdb_disable_message *disable;
2412
2413         if (buflen < sizeof(struct ctdb_disable_message)) {
2414                 return EMSGSIZE;
2415         }
2416
2417         disable = talloc_memdup(mem_ctx, buf,
2418                                 sizeof(struct ctdb_disable_message));
2419         if (disable == NULL) {
2420                 return ENOMEM;
2421         }
2422
2423         *out = disable;
2424         return 0;
2425 }
2426
2427 size_t ctdb_server_id_len(struct ctdb_server_id *sid)
2428 {
2429         return sizeof(struct ctdb_server_id);
2430 }
2431
2432 void ctdb_server_id_push(struct ctdb_server_id *sid, uint8_t *buf)
2433 {
2434         memcpy(buf, sid, sizeof(struct ctdb_server_id));
2435 }
2436
2437 int ctdb_server_id_pull(uint8_t *buf, size_t buflen,
2438                         struct ctdb_server_id *sid)
2439 {
2440         if (buflen < sizeof(struct ctdb_server_id)) {
2441                 return EMSGSIZE;
2442         }
2443
2444         memcpy(sid, buf, sizeof(struct ctdb_server_id));
2445         return 0;
2446 }
2447
2448 size_t ctdb_g_lock_len(struct ctdb_g_lock *lock)
2449 {
2450         return sizeof(struct ctdb_g_lock);
2451 }
2452
2453 void ctdb_g_lock_push(struct ctdb_g_lock *lock, uint8_t *buf)
2454 {
2455         memcpy(buf, lock, sizeof(struct ctdb_g_lock));
2456 }
2457
2458 int ctdb_g_lock_pull(uint8_t *buf, size_t buflen, struct ctdb_g_lock *lock)
2459 {
2460         if (buflen < sizeof(struct ctdb_g_lock)) {
2461                 return EMSGSIZE;
2462         }
2463
2464         memcpy(lock, buf, sizeof(struct ctdb_g_lock));
2465         return 0;
2466 }
2467
2468 size_t ctdb_g_lock_list_len(struct ctdb_g_lock_list *lock_list)
2469 {
2470         return lock_list->num * sizeof(struct ctdb_g_lock);
2471 }
2472
2473 void ctdb_g_lock_list_push(struct ctdb_g_lock_list *lock_list, uint8_t *buf)
2474 {
2475         size_t offset = 0;
2476         int i;
2477
2478         for (i=0; i<lock_list->num; i++) {
2479                 ctdb_g_lock_push(&lock_list->lock[i], &buf[offset]);
2480                 offset += sizeof(struct ctdb_g_lock);
2481         }
2482 }
2483
2484 int ctdb_g_lock_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
2485                           struct ctdb_g_lock_list **out)
2486 {
2487         struct ctdb_g_lock_list *lock_list;
2488         unsigned count;
2489         size_t offset;
2490         int ret, i;
2491
2492         lock_list = talloc_zero(mem_ctx, struct ctdb_g_lock_list);
2493         if (lock_list == NULL) {
2494                 return ENOMEM;
2495         }
2496
2497         count = buflen / sizeof(struct ctdb_g_lock);
2498         lock_list->lock = talloc_array(lock_list, struct ctdb_g_lock, count);
2499         if (lock_list->lock == NULL) {
2500                 talloc_free(lock_list);
2501                 return ENOMEM;
2502         }
2503
2504         offset = 0;
2505         for (i=0; i<count; i++) {
2506                 ret = ctdb_g_lock_pull(&buf[offset], buflen-offset,
2507                                        &lock_list->lock[i]);
2508                 if (ret != 0) {
2509                         talloc_free(lock_list);
2510                         return ret;
2511                 }
2512                 offset += sizeof(struct ctdb_g_lock);
2513         }
2514
2515         lock_list->num = count;
2516
2517         *out = lock_list;
2518         return 0;
2519 }