2 CTDB protocol marshalling
4 Copyright (C) Amitay Isaacs 2015
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/network.h"
27 #include "protocol_api.h"
28 #include "protocol_private.h"
30 struct ctdb_req_message_wire {
31 struct ctdb_req_header hdr;
37 static size_t ctdb_message_data_len(union ctdb_message_data *mdata,
43 case CTDB_SRVID_BANNING:
44 len = ctdb_uint32_len(&mdata->pnn);
47 case CTDB_SRVID_ELECTION:
48 len = ctdb_election_message_len(mdata->election);
51 case CTDB_SRVID_RECONFIGURE:
54 case CTDB_SRVID_RELEASE_IP:
55 len = ctdb_string_len(&mdata->ipaddr);
58 case CTDB_SRVID_TAKE_IP:
59 len = ctdb_string_len(&mdata->ipaddr);
62 case CTDB_SRVID_SET_NODE_FLAGS:
63 len = ctdb_node_flag_change_len(mdata->flag_change);
66 case CTDB_SRVID_RECD_UPDATE_IP:
67 len = ctdb_public_ip_len(mdata->pubip);
70 case CTDB_SRVID_VACUUM_FETCH:
71 len = ctdb_rec_buffer_len(mdata->recbuf);
74 case CTDB_SRVID_DETACH_DATABASE:
75 len = ctdb_uint32_len(&mdata->db_id);
78 case CTDB_SRVID_MEM_DUMP:
79 len = ctdb_srvid_message_len(mdata->msg);
82 case CTDB_SRVID_GETLOG:
85 case CTDB_SRVID_CLEARLOG:
88 case CTDB_SRVID_PUSH_NODE_FLAGS:
89 len = ctdb_node_flag_change_len(mdata->flag_change);
92 case CTDB_SRVID_RELOAD_NODES:
95 case CTDB_SRVID_TAKEOVER_RUN:
96 len = ctdb_srvid_message_len(mdata->msg);
99 case CTDB_SRVID_REBALANCE_NODE:
100 len = ctdb_uint32_len(&mdata->pnn);
103 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS:
104 len = ctdb_disable_message_len(mdata->disable);
107 case CTDB_SRVID_DISABLE_RECOVERIES:
108 len = ctdb_disable_message_len(mdata->disable);
111 case CTDB_SRVID_DISABLE_IP_CHECK:
112 len = ctdb_uint32_len(&mdata->timeout);
116 len = ctdb_tdb_data_len(&mdata->data);
123 static void ctdb_message_data_push(union ctdb_message_data *mdata,
124 uint64_t srvid, uint8_t *buf,
130 case CTDB_SRVID_BANNING:
131 ctdb_uint32_push(&mdata->pnn, buf, &np);
134 case CTDB_SRVID_ELECTION:
135 ctdb_election_message_push(mdata->election, buf, &np);
138 case CTDB_SRVID_RECONFIGURE:
141 case CTDB_SRVID_RELEASE_IP:
142 ctdb_string_push(&mdata->ipaddr, buf, &np);
145 case CTDB_SRVID_TAKE_IP:
146 ctdb_string_push(&mdata->ipaddr, buf, &np);
149 case CTDB_SRVID_SET_NODE_FLAGS:
150 ctdb_node_flag_change_push(mdata->flag_change, buf, &np);
153 case CTDB_SRVID_RECD_UPDATE_IP:
154 ctdb_public_ip_push(mdata->pubip, buf, &np);
157 case CTDB_SRVID_VACUUM_FETCH:
158 ctdb_rec_buffer_push(mdata->recbuf, buf, &np);
161 case CTDB_SRVID_DETACH_DATABASE:
162 ctdb_uint32_push(&mdata->db_id, buf, &np);
165 case CTDB_SRVID_MEM_DUMP:
166 ctdb_srvid_message_push(mdata->msg, buf, &np);
169 case CTDB_SRVID_GETLOG:
172 case CTDB_SRVID_CLEARLOG:
175 case CTDB_SRVID_PUSH_NODE_FLAGS:
176 ctdb_node_flag_change_push(mdata->flag_change, buf, &np);
179 case CTDB_SRVID_RELOAD_NODES:
182 case CTDB_SRVID_TAKEOVER_RUN:
183 ctdb_srvid_message_push(mdata->msg, buf, &np);
186 case CTDB_SRVID_REBALANCE_NODE:
187 ctdb_uint32_push(&mdata->pnn, buf, &np);
190 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS:
191 ctdb_disable_message_push(mdata->disable, buf, &np);
194 case CTDB_SRVID_DISABLE_RECOVERIES:
195 ctdb_disable_message_push(mdata->disable, buf, &np);
198 case CTDB_SRVID_DISABLE_IP_CHECK:
199 ctdb_uint32_push(&mdata->timeout, buf, &np);
203 ctdb_tdb_data_push(&mdata->data, buf, &np);
210 static int ctdb_message_data_pull(uint8_t *buf, size_t buflen,
211 uint64_t srvid, TALLOC_CTX *mem_ctx,
212 union ctdb_message_data *mdata,
219 case CTDB_SRVID_BANNING:
220 ret = ctdb_uint32_pull(buf, buflen, &mdata->pnn, &np);
223 case CTDB_SRVID_ELECTION:
224 ret = ctdb_election_message_pull(buf, buflen, mem_ctx,
225 &mdata->election, &np);
228 case CTDB_SRVID_RECONFIGURE:
231 case CTDB_SRVID_RELEASE_IP:
232 ret = ctdb_string_pull(buf, buflen, mem_ctx, &mdata->ipaddr,
236 case CTDB_SRVID_TAKE_IP:
237 ret = ctdb_string_pull(buf, buflen, mem_ctx, &mdata->ipaddr,
241 case CTDB_SRVID_SET_NODE_FLAGS:
242 ret = ctdb_node_flag_change_pull(buf, buflen, mem_ctx,
243 &mdata->flag_change, &np);
246 case CTDB_SRVID_RECD_UPDATE_IP:
247 ret = ctdb_public_ip_pull(buf, buflen, mem_ctx,
251 case CTDB_SRVID_VACUUM_FETCH:
252 ret = ctdb_rec_buffer_pull(buf, buflen, mem_ctx,
253 &mdata->recbuf, &np);
256 case CTDB_SRVID_DETACH_DATABASE:
257 ret = ctdb_uint32_pull(buf, buflen, &mdata->db_id, &np);
260 case CTDB_SRVID_MEM_DUMP:
261 ret = ctdb_srvid_message_pull(buf, buflen, mem_ctx,
265 case CTDB_SRVID_GETLOG:
268 case CTDB_SRVID_CLEARLOG:
271 case CTDB_SRVID_PUSH_NODE_FLAGS:
272 ret = ctdb_node_flag_change_pull(buf, buflen, mem_ctx,
273 &mdata->flag_change, &np);
276 case CTDB_SRVID_RELOAD_NODES:
279 case CTDB_SRVID_TAKEOVER_RUN:
280 ret = ctdb_srvid_message_pull(buf, buflen, mem_ctx,
284 case CTDB_SRVID_REBALANCE_NODE:
285 ret = ctdb_uint32_pull(buf, buflen, &mdata->pnn, &np);
288 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS:
289 ret = ctdb_disable_message_pull(buf, buflen, mem_ctx,
290 &mdata->disable, &np);
293 case CTDB_SRVID_DISABLE_RECOVERIES:
294 ret = ctdb_disable_message_pull(buf, buflen, mem_ctx,
295 &mdata->disable, &np);
298 case CTDB_SRVID_DISABLE_IP_CHECK:
299 ret = ctdb_uint32_pull(buf, buflen, &mdata->timeout, &np);
303 ret = ctdb_tdb_data_pull(buf, buflen, mem_ctx, &mdata->data,
316 size_t ctdb_req_message_len(struct ctdb_req_header *h,
317 struct ctdb_req_message *c)
319 uint32_t u32 = ctdb_message_data_len(&c->data, c->srvid);
321 return ctdb_req_header_len(h) +
322 ctdb_uint64_len(&c->srvid) +
323 ctdb_uint32_len(&u32) + u32;
326 int ctdb_req_message_push(struct ctdb_req_header *h,
327 struct ctdb_req_message *c,
328 uint8_t *buf, size_t *buflen)
330 size_t offset = 0, np;
334 length = ctdb_req_message_len(h, c);
335 if (*buflen < length) {
341 ctdb_req_header_push(h, buf+offset, &np);
344 ctdb_uint64_push(&c->srvid, buf+offset, &np);
347 u32 = ctdb_message_data_len(&c->data, c->srvid);
348 ctdb_uint32_push(&u32, buf+offset, &np);
351 ctdb_message_data_push(&c->data, c->srvid, buf+offset, &np);
357 int ctdb_req_message_pull(uint8_t *buf, size_t buflen,
358 struct ctdb_req_header *h,
360 struct ctdb_req_message *c)
362 struct ctdb_req_header header;
363 size_t offset = 0, np;
367 ret = ctdb_req_header_pull(buf+offset, buflen-offset, &header, &np);
377 ret = ctdb_uint64_pull(buf+offset, buflen-offset, &c->srvid, &np);
383 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
389 if (buflen-offset < u32) {
393 ret = ctdb_message_data_pull(buf+offset, u32, c->srvid,
394 mem_ctx, &c->data, &np);
403 size_t ctdb_req_message_data_len(struct ctdb_req_header *h,
404 struct ctdb_req_message_data *c)
406 return offsetof(struct ctdb_req_message_wire, data) +
407 ctdb_tdb_data_len(&c->data);
410 int ctdb_req_message_data_push(struct ctdb_req_header *h,
411 struct ctdb_req_message_data *message,
412 uint8_t *buf, size_t *buflen)
414 struct ctdb_req_message_wire *wire =
415 (struct ctdb_req_message_wire *)buf;
418 length = ctdb_req_message_data_len(h, message);
419 if (*buflen < length) {
425 ctdb_req_header_push(h, (uint8_t *)&wire->hdr, &np);
427 wire->srvid = message->srvid;
428 wire->datalen = ctdb_tdb_data_len(&message->data);
429 ctdb_tdb_data_push(&message->data, wire->data, &np);
434 int ctdb_req_message_data_pull(uint8_t *buf, size_t buflen,
435 struct ctdb_req_header *h,
437 struct ctdb_req_message_data *c)
439 struct ctdb_req_message_wire *wire =
440 (struct ctdb_req_message_wire *)buf;
444 length = offsetof(struct ctdb_req_message_wire, data);
445 if (buflen < length) {
448 if (wire->datalen > buflen) {
451 if (length + wire->datalen < length) {
454 if (buflen < length + wire->datalen) {
459 ret = ctdb_req_header_pull((uint8_t *)&wire->hdr, buflen, h,
466 c->srvid = wire->srvid;
468 ret = ctdb_tdb_data_pull(wire->data, wire->datalen,
469 mem_ctx, &c->data, &np);