ctdb-protocol: Add protocol marshalling for CTDB_REQ_TUNNEL
[metze/samba/wip.git] / ctdb / protocol / protocol_tunnel.c
1 /*
2    CTDB protocol marshalling
3
4    Copyright (C) Amitay Isaacs  2017
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_api.h"
28 #include "protocol_private.h"
29
30 size_t ctdb_req_tunnel_len(struct ctdb_req_header *h,
31                            struct ctdb_req_tunnel *c)
32 {
33         return ctdb_req_header_len(h) +
34                 ctdb_uint64_len(&c->tunnel_id) +
35                 ctdb_uint32_len(&c->flags) +
36                 ctdb_tdb_datan_len(&c->data);
37 }
38
39 int ctdb_req_tunnel_push(struct ctdb_req_header *h,
40                          struct ctdb_req_tunnel *c,
41                          uint8_t *buf, size_t *buflen)
42 {
43         size_t length, offset = 0, np;
44
45         length = ctdb_req_tunnel_len(h, c);
46         if (*buflen < length) {
47                 *buflen = length;
48                 return EMSGSIZE;
49         }
50
51         h->length = *buflen;
52         ctdb_req_header_push(h, buf, &np);
53         offset += np;
54
55         ctdb_uint64_push(&c->tunnel_id, buf+offset, &np);
56         offset += np;
57
58         ctdb_uint32_push(&c->flags, buf+offset, &np);
59         offset += np;
60
61         ctdb_tdb_datan_push(&c->data, buf+offset, &np);
62         offset += np;
63
64         if (offset > *buflen) {
65                 return EMSGSIZE;
66         }
67
68         return 0;
69 }
70
71 int ctdb_req_tunnel_pull(uint8_t *buf, size_t buflen,
72                          struct ctdb_req_header *h,
73                          TALLOC_CTX *mem_ctx,
74                          struct ctdb_req_tunnel *c)
75 {
76         struct ctdb_req_header header;
77         size_t offset = 0, np;
78         int ret;
79
80         ret = ctdb_req_header_pull(buf, buflen, &header, &np);
81         if (ret != 0) {
82                 return ret;
83         }
84         offset += np;
85
86         if (h != NULL) {
87                 *h = header;
88         }
89
90         ret = ctdb_uint64_pull(buf+offset, buflen-offset, &c->tunnel_id, &np);
91         if (ret != 0) {
92                 return ret;
93         }
94         offset += np;
95
96         ret = ctdb_uint32_pull(buf+offset, buflen-offset, &c->flags, &np);
97         if (ret != 0) {
98                 return ret;
99         }
100         offset += np;
101
102         ret = ctdb_tdb_datan_pull(buf+offset, buflen-offset, mem_ctx,
103                                   &c->data, &np);
104         if (ret != 0) {
105                 return ret;
106         }
107         offset += np;
108
109         if (offset > buflen) {
110                 return EMSGSIZE;
111         }
112
113         return 0;
114 }