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"
31 static size_t ctdb_message_data_len(union ctdb_message_data
*mdata
,
37 case CTDB_SRVID_BANNING
:
38 len
= ctdb_uint32_len(&mdata
->pnn
);
41 case CTDB_SRVID_ELECTION
:
42 len
= ctdb_election_message_len(mdata
->election
);
45 case CTDB_SRVID_RECONFIGURE
:
48 case CTDB_SRVID_RELEASE_IP
:
49 len
= ctdb_string_len(&mdata
->ipaddr
);
52 case CTDB_SRVID_TAKE_IP
:
53 len
= ctdb_string_len(&mdata
->ipaddr
);
56 case CTDB_SRVID_SET_NODE_FLAGS
:
57 len
= ctdb_node_flag_change_len(mdata
->flag_change
);
60 case CTDB_SRVID_RECD_UPDATE_IP
:
61 len
= ctdb_public_ip_len(mdata
->pubip
);
64 case CTDB_SRVID_VACUUM_FETCH
:
65 len
= ctdb_rec_buffer_len(mdata
->recbuf
);
68 case CTDB_SRVID_DETACH_DATABASE
:
69 len
= ctdb_uint32_len(&mdata
->db_id
);
72 case CTDB_SRVID_MEM_DUMP
:
73 len
= ctdb_srvid_message_len(mdata
->msg
);
76 case CTDB_SRVID_GETLOG
:
79 case CTDB_SRVID_CLEARLOG
:
82 case CTDB_SRVID_PUSH_NODE_FLAGS
:
83 len
= ctdb_node_flag_change_len(mdata
->flag_change
);
86 case CTDB_SRVID_RELOAD_NODES
:
89 case CTDB_SRVID_TAKEOVER_RUN
:
90 len
= ctdb_srvid_message_len(mdata
->msg
);
93 case CTDB_SRVID_REBALANCE_NODE
:
94 len
= ctdb_uint32_len(&mdata
->pnn
);
97 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS
:
98 len
= ctdb_disable_message_len(mdata
->disable
);
101 case CTDB_SRVID_DISABLE_RECOVERIES
:
102 len
= ctdb_disable_message_len(mdata
->disable
);
105 case CTDB_SRVID_DISABLE_IP_CHECK
:
106 len
= ctdb_uint32_len(&mdata
->timeout
);
110 len
= ctdb_tdb_data_len(&mdata
->data
);
117 static void ctdb_message_data_push(union ctdb_message_data
*mdata
,
118 uint64_t srvid
, uint8_t *buf
,
124 case CTDB_SRVID_BANNING
:
125 ctdb_uint32_push(&mdata
->pnn
, buf
, &np
);
128 case CTDB_SRVID_ELECTION
:
129 ctdb_election_message_push(mdata
->election
, buf
, &np
);
132 case CTDB_SRVID_RECONFIGURE
:
135 case CTDB_SRVID_RELEASE_IP
:
136 ctdb_string_push(&mdata
->ipaddr
, buf
, &np
);
139 case CTDB_SRVID_TAKE_IP
:
140 ctdb_string_push(&mdata
->ipaddr
, buf
, &np
);
143 case CTDB_SRVID_SET_NODE_FLAGS
:
144 ctdb_node_flag_change_push(mdata
->flag_change
, buf
, &np
);
147 case CTDB_SRVID_RECD_UPDATE_IP
:
148 ctdb_public_ip_push(mdata
->pubip
, buf
, &np
);
151 case CTDB_SRVID_VACUUM_FETCH
:
152 ctdb_rec_buffer_push(mdata
->recbuf
, buf
, &np
);
155 case CTDB_SRVID_DETACH_DATABASE
:
156 ctdb_uint32_push(&mdata
->db_id
, buf
, &np
);
159 case CTDB_SRVID_MEM_DUMP
:
160 ctdb_srvid_message_push(mdata
->msg
, buf
, &np
);
163 case CTDB_SRVID_GETLOG
:
166 case CTDB_SRVID_CLEARLOG
:
169 case CTDB_SRVID_PUSH_NODE_FLAGS
:
170 ctdb_node_flag_change_push(mdata
->flag_change
, buf
, &np
);
173 case CTDB_SRVID_RELOAD_NODES
:
176 case CTDB_SRVID_TAKEOVER_RUN
:
177 ctdb_srvid_message_push(mdata
->msg
, buf
, &np
);
180 case CTDB_SRVID_REBALANCE_NODE
:
181 ctdb_uint32_push(&mdata
->pnn
, buf
, &np
);
184 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS
:
185 ctdb_disable_message_push(mdata
->disable
, buf
, &np
);
188 case CTDB_SRVID_DISABLE_RECOVERIES
:
189 ctdb_disable_message_push(mdata
->disable
, buf
, &np
);
192 case CTDB_SRVID_DISABLE_IP_CHECK
:
193 ctdb_uint32_push(&mdata
->timeout
, buf
, &np
);
197 ctdb_tdb_data_push(&mdata
->data
, buf
, &np
);
204 static int ctdb_message_data_pull(uint8_t *buf
, size_t buflen
,
205 uint64_t srvid
, TALLOC_CTX
*mem_ctx
,
206 union ctdb_message_data
*mdata
,
213 case CTDB_SRVID_BANNING
:
214 ret
= ctdb_uint32_pull(buf
, buflen
, &mdata
->pnn
, &np
);
217 case CTDB_SRVID_ELECTION
:
218 ret
= ctdb_election_message_pull(buf
, buflen
, mem_ctx
,
219 &mdata
->election
, &np
);
222 case CTDB_SRVID_RECONFIGURE
:
225 case CTDB_SRVID_RELEASE_IP
:
226 ret
= ctdb_string_pull(buf
, buflen
, mem_ctx
, &mdata
->ipaddr
,
230 case CTDB_SRVID_TAKE_IP
:
231 ret
= ctdb_string_pull(buf
, buflen
, mem_ctx
, &mdata
->ipaddr
,
235 case CTDB_SRVID_SET_NODE_FLAGS
:
236 ret
= ctdb_node_flag_change_pull(buf
, buflen
, mem_ctx
,
237 &mdata
->flag_change
, &np
);
240 case CTDB_SRVID_RECD_UPDATE_IP
:
241 ret
= ctdb_public_ip_pull(buf
, buflen
, mem_ctx
,
245 case CTDB_SRVID_VACUUM_FETCH
:
246 ret
= ctdb_rec_buffer_pull(buf
, buflen
, mem_ctx
,
247 &mdata
->recbuf
, &np
);
250 case CTDB_SRVID_DETACH_DATABASE
:
251 ret
= ctdb_uint32_pull(buf
, buflen
, &mdata
->db_id
, &np
);
254 case CTDB_SRVID_MEM_DUMP
:
255 ret
= ctdb_srvid_message_pull(buf
, buflen
, mem_ctx
,
259 case CTDB_SRVID_GETLOG
:
262 case CTDB_SRVID_CLEARLOG
:
265 case CTDB_SRVID_PUSH_NODE_FLAGS
:
266 ret
= ctdb_node_flag_change_pull(buf
, buflen
, mem_ctx
,
267 &mdata
->flag_change
, &np
);
270 case CTDB_SRVID_RELOAD_NODES
:
273 case CTDB_SRVID_TAKEOVER_RUN
:
274 ret
= ctdb_srvid_message_pull(buf
, buflen
, mem_ctx
,
278 case CTDB_SRVID_REBALANCE_NODE
:
279 ret
= ctdb_uint32_pull(buf
, buflen
, &mdata
->pnn
, &np
);
282 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS
:
283 ret
= ctdb_disable_message_pull(buf
, buflen
, mem_ctx
,
284 &mdata
->disable
, &np
);
287 case CTDB_SRVID_DISABLE_RECOVERIES
:
288 ret
= ctdb_disable_message_pull(buf
, buflen
, mem_ctx
,
289 &mdata
->disable
, &np
);
292 case CTDB_SRVID_DISABLE_IP_CHECK
:
293 ret
= ctdb_uint32_pull(buf
, buflen
, &mdata
->timeout
, &np
);
297 ret
= ctdb_tdb_data_pull(buf
, buflen
, mem_ctx
, &mdata
->data
,
310 size_t ctdb_req_message_len(struct ctdb_req_header
*h
,
311 struct ctdb_req_message
*c
)
313 uint32_t u32
= ctdb_message_data_len(&c
->data
, c
->srvid
);
315 return ctdb_req_header_len(h
) +
316 ctdb_uint64_len(&c
->srvid
) +
317 ctdb_uint32_len(&u32
) + u32
;
320 int ctdb_req_message_push(struct ctdb_req_header
*h
,
321 struct ctdb_req_message
*c
,
322 uint8_t *buf
, size_t *buflen
)
324 size_t offset
= 0, np
;
328 length
= ctdb_req_message_len(h
, c
);
329 if (*buflen
< length
) {
335 ctdb_req_header_push(h
, buf
+offset
, &np
);
338 ctdb_uint64_push(&c
->srvid
, buf
+offset
, &np
);
341 u32
= ctdb_message_data_len(&c
->data
, c
->srvid
);
342 ctdb_uint32_push(&u32
, buf
+offset
, &np
);
345 ctdb_message_data_push(&c
->data
, c
->srvid
, buf
+offset
, &np
);
351 int ctdb_req_message_pull(uint8_t *buf
, size_t buflen
,
352 struct ctdb_req_header
*h
,
354 struct ctdb_req_message
*c
)
356 struct ctdb_req_header header
;
357 size_t offset
= 0, np
;
361 ret
= ctdb_req_header_pull(buf
+offset
, buflen
-offset
, &header
, &np
);
371 ret
= ctdb_uint64_pull(buf
+offset
, buflen
-offset
, &c
->srvid
, &np
);
377 ret
= ctdb_uint32_pull(buf
+offset
, buflen
-offset
, &u32
, &np
);
383 if (buflen
-offset
< u32
) {
387 ret
= ctdb_message_data_pull(buf
+offset
, u32
, c
->srvid
,
388 mem_ctx
, &c
->data
, &np
);
397 size_t ctdb_req_message_data_len(struct ctdb_req_header
*h
,
398 struct ctdb_req_message_data
*c
)
400 return ctdb_req_header_len(h
) +
401 ctdb_uint64_len(&c
->srvid
) +
402 ctdb_tdb_datan_len(&c
->data
);
405 int ctdb_req_message_data_push(struct ctdb_req_header
*h
,
406 struct ctdb_req_message_data
*c
,
407 uint8_t *buf
, size_t *buflen
)
409 size_t offset
= 0, np
;
412 length
= ctdb_req_message_data_len(h
, c
);
413 if (*buflen
< length
) {
419 ctdb_req_header_push(h
, buf
+offset
, &np
);
422 ctdb_uint64_push(&c
->srvid
, buf
+offset
, &np
);
425 ctdb_tdb_datan_push(&c
->data
, buf
+offset
, &np
);
431 int ctdb_req_message_data_pull(uint8_t *buf
, size_t buflen
,
432 struct ctdb_req_header
*h
,
434 struct ctdb_req_message_data
*c
)
436 struct ctdb_req_header header
;
437 size_t offset
= 0, np
;
440 ret
= ctdb_req_header_pull(buf
+offset
, buflen
-offset
, &header
, &np
);
450 ret
= ctdb_uint64_pull(buf
+offset
, buflen
-offset
, &c
->srvid
, &np
);
456 ret
= ctdb_tdb_datan_pull(buf
+offset
, buflen
-offset
,
457 mem_ctx
, &c
->data
, &np
);