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_LEADER
:
46 len
= ctdb_uint32_len(&mdata
->pnn
);
49 case CTDB_SRVID_RECONFIGURE
:
52 case CTDB_SRVID_RELEASE_IP
:
53 len
= ctdb_string_len(&mdata
->ipaddr
);
56 case CTDB_SRVID_TAKE_IP
:
57 len
= ctdb_string_len(&mdata
->ipaddr
);
60 case CTDB_SRVID_SET_NODE_FLAGS
:
61 len
= ctdb_node_flag_change_len(mdata
->flag_change
);
64 case CTDB_SRVID_RECD_UPDATE_IP
:
65 len
= ctdb_public_ip_len(mdata
->pubip
);
68 case CTDB_SRVID_VACUUM_FETCH
:
69 len
= ctdb_rec_buffer_len(mdata
->recbuf
);
72 case CTDB_SRVID_DETACH_DATABASE
:
73 len
= ctdb_uint32_len(&mdata
->db_id
);
76 case CTDB_SRVID_MEM_DUMP
:
77 len
= ctdb_srvid_message_len(mdata
->msg
);
80 case CTDB_SRVID_GETLOG
:
83 case CTDB_SRVID_CLEARLOG
:
86 case CTDB_SRVID_PUSH_NODE_FLAGS
:
87 len
= ctdb_node_flag_change_len(mdata
->flag_change
);
90 case CTDB_SRVID_RELOAD_NODES
:
93 case CTDB_SRVID_TAKEOVER_RUN
:
94 len
= ctdb_srvid_message_len(mdata
->msg
);
97 case CTDB_SRVID_REBALANCE_NODE
:
98 len
= ctdb_uint32_len(&mdata
->pnn
);
101 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS
:
102 len
= ctdb_disable_message_len(mdata
->disable
);
105 case CTDB_SRVID_DISABLE_RECOVERIES
:
106 len
= ctdb_disable_message_len(mdata
->disable
);
109 case CTDB_SRVID_DISABLE_IP_CHECK
:
110 len
= ctdb_uint32_len(&mdata
->timeout
);
114 len
= ctdb_tdb_data_len(&mdata
->data
);
121 static void ctdb_message_data_push(union ctdb_message_data
*mdata
,
122 uint64_t srvid
, uint8_t *buf
,
128 case CTDB_SRVID_BANNING
:
129 ctdb_uint32_push(&mdata
->pnn
, buf
, &np
);
132 case CTDB_SRVID_ELECTION
:
133 ctdb_election_message_push(mdata
->election
, buf
, &np
);
136 case CTDB_SRVID_LEADER
:
137 ctdb_uint32_push(&mdata
->pnn
, buf
, &np
);
140 case CTDB_SRVID_RECONFIGURE
:
143 case CTDB_SRVID_RELEASE_IP
:
144 ctdb_string_push(&mdata
->ipaddr
, buf
, &np
);
147 case CTDB_SRVID_TAKE_IP
:
148 ctdb_string_push(&mdata
->ipaddr
, buf
, &np
);
151 case CTDB_SRVID_SET_NODE_FLAGS
:
152 ctdb_node_flag_change_push(mdata
->flag_change
, buf
, &np
);
155 case CTDB_SRVID_RECD_UPDATE_IP
:
156 ctdb_public_ip_push(mdata
->pubip
, buf
, &np
);
159 case CTDB_SRVID_VACUUM_FETCH
:
160 ctdb_rec_buffer_push(mdata
->recbuf
, buf
, &np
);
163 case CTDB_SRVID_DETACH_DATABASE
:
164 ctdb_uint32_push(&mdata
->db_id
, buf
, &np
);
167 case CTDB_SRVID_MEM_DUMP
:
168 ctdb_srvid_message_push(mdata
->msg
, buf
, &np
);
171 case CTDB_SRVID_GETLOG
:
174 case CTDB_SRVID_CLEARLOG
:
177 case CTDB_SRVID_PUSH_NODE_FLAGS
:
178 ctdb_node_flag_change_push(mdata
->flag_change
, buf
, &np
);
181 case CTDB_SRVID_RELOAD_NODES
:
184 case CTDB_SRVID_TAKEOVER_RUN
:
185 ctdb_srvid_message_push(mdata
->msg
, buf
, &np
);
188 case CTDB_SRVID_REBALANCE_NODE
:
189 ctdb_uint32_push(&mdata
->pnn
, buf
, &np
);
192 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS
:
193 ctdb_disable_message_push(mdata
->disable
, buf
, &np
);
196 case CTDB_SRVID_DISABLE_RECOVERIES
:
197 ctdb_disable_message_push(mdata
->disable
, buf
, &np
);
200 case CTDB_SRVID_DISABLE_IP_CHECK
:
201 ctdb_uint32_push(&mdata
->timeout
, buf
, &np
);
205 ctdb_tdb_data_push(&mdata
->data
, buf
, &np
);
212 static int ctdb_message_data_pull(uint8_t *buf
, size_t buflen
,
213 uint64_t srvid
, TALLOC_CTX
*mem_ctx
,
214 union ctdb_message_data
*mdata
,
221 case CTDB_SRVID_BANNING
:
222 ret
= ctdb_uint32_pull(buf
, buflen
, &mdata
->pnn
, &np
);
225 case CTDB_SRVID_ELECTION
:
226 ret
= ctdb_election_message_pull(buf
, buflen
, mem_ctx
,
227 &mdata
->election
, &np
);
230 case CTDB_SRVID_LEADER
:
231 ret
= ctdb_uint32_pull(buf
, buflen
, &mdata
->pnn
, &np
);
234 case CTDB_SRVID_RECONFIGURE
:
237 case CTDB_SRVID_RELEASE_IP
:
238 ret
= ctdb_string_pull(buf
, buflen
, mem_ctx
, &mdata
->ipaddr
,
242 case CTDB_SRVID_TAKE_IP
:
243 ret
= ctdb_string_pull(buf
, buflen
, mem_ctx
, &mdata
->ipaddr
,
247 case CTDB_SRVID_SET_NODE_FLAGS
:
248 ret
= ctdb_node_flag_change_pull(buf
, buflen
, mem_ctx
,
249 &mdata
->flag_change
, &np
);
252 case CTDB_SRVID_RECD_UPDATE_IP
:
253 ret
= ctdb_public_ip_pull(buf
, buflen
, mem_ctx
,
257 case CTDB_SRVID_VACUUM_FETCH
:
258 ret
= ctdb_rec_buffer_pull(buf
, buflen
, mem_ctx
,
259 &mdata
->recbuf
, &np
);
262 case CTDB_SRVID_DETACH_DATABASE
:
263 ret
= ctdb_uint32_pull(buf
, buflen
, &mdata
->db_id
, &np
);
266 case CTDB_SRVID_MEM_DUMP
:
267 ret
= ctdb_srvid_message_pull(buf
, buflen
, mem_ctx
,
271 case CTDB_SRVID_GETLOG
:
274 case CTDB_SRVID_CLEARLOG
:
277 case CTDB_SRVID_PUSH_NODE_FLAGS
:
278 ret
= ctdb_node_flag_change_pull(buf
, buflen
, mem_ctx
,
279 &mdata
->flag_change
, &np
);
282 case CTDB_SRVID_RELOAD_NODES
:
285 case CTDB_SRVID_TAKEOVER_RUN
:
286 ret
= ctdb_srvid_message_pull(buf
, buflen
, mem_ctx
,
290 case CTDB_SRVID_REBALANCE_NODE
:
291 ret
= ctdb_uint32_pull(buf
, buflen
, &mdata
->pnn
, &np
);
294 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS
:
295 ret
= ctdb_disable_message_pull(buf
, buflen
, mem_ctx
,
296 &mdata
->disable
, &np
);
299 case CTDB_SRVID_DISABLE_RECOVERIES
:
300 ret
= ctdb_disable_message_pull(buf
, buflen
, mem_ctx
,
301 &mdata
->disable
, &np
);
304 case CTDB_SRVID_DISABLE_IP_CHECK
:
305 ret
= ctdb_uint32_pull(buf
, buflen
, &mdata
->timeout
, &np
);
309 ret
= ctdb_tdb_data_pull(buf
, buflen
, mem_ctx
, &mdata
->data
,
322 size_t ctdb_req_message_len(struct ctdb_req_header
*h
,
323 struct ctdb_req_message
*c
)
325 uint32_t u32
= ctdb_message_data_len(&c
->data
, c
->srvid
);
327 return ctdb_req_header_len(h
) +
328 ctdb_uint64_len(&c
->srvid
) +
329 ctdb_uint32_len(&u32
) + u32
;
332 int ctdb_req_message_push(struct ctdb_req_header
*h
,
333 struct ctdb_req_message
*c
,
334 uint8_t *buf
, size_t *buflen
)
336 size_t offset
= 0, np
;
340 length
= ctdb_req_message_len(h
, c
);
341 if (*buflen
< length
) {
347 ctdb_req_header_push(h
, buf
+offset
, &np
);
350 ctdb_uint64_push(&c
->srvid
, buf
+offset
, &np
);
353 u32
= ctdb_message_data_len(&c
->data
, c
->srvid
);
354 ctdb_uint32_push(&u32
, buf
+offset
, &np
);
357 ctdb_message_data_push(&c
->data
, c
->srvid
, buf
+offset
, &np
);
363 int ctdb_req_message_pull(uint8_t *buf
, size_t buflen
,
364 struct ctdb_req_header
*h
,
366 struct ctdb_req_message
*c
)
368 struct ctdb_req_header header
;
369 size_t offset
= 0, np
;
373 ret
= ctdb_req_header_pull(buf
+offset
, buflen
-offset
, &header
, &np
);
383 ret
= ctdb_uint64_pull(buf
+offset
, buflen
-offset
, &c
->srvid
, &np
);
389 ret
= ctdb_uint32_pull(buf
+offset
, buflen
-offset
, &u32
, &np
);
395 if (buflen
-offset
< u32
) {
399 ret
= ctdb_message_data_pull(buf
+offset
, u32
, c
->srvid
,
400 mem_ctx
, &c
->data
, &np
);
409 size_t ctdb_req_message_data_len(struct ctdb_req_header
*h
,
410 struct ctdb_req_message_data
*c
)
412 return ctdb_req_header_len(h
) +
413 ctdb_uint64_len(&c
->srvid
) +
414 ctdb_tdb_datan_len(&c
->data
);
417 int ctdb_req_message_data_push(struct ctdb_req_header
*h
,
418 struct ctdb_req_message_data
*c
,
419 uint8_t *buf
, size_t *buflen
)
421 size_t offset
= 0, np
;
424 length
= ctdb_req_message_data_len(h
, c
);
425 if (*buflen
< length
) {
431 ctdb_req_header_push(h
, buf
+offset
, &np
);
434 ctdb_uint64_push(&c
->srvid
, buf
+offset
, &np
);
437 ctdb_tdb_datan_push(&c
->data
, buf
+offset
, &np
);
443 int ctdb_req_message_data_pull(uint8_t *buf
, size_t buflen
,
444 struct ctdb_req_header
*h
,
446 struct ctdb_req_message_data
*c
)
448 struct ctdb_req_header header
;
449 size_t offset
= 0, np
;
452 ret
= ctdb_req_header_pull(buf
+offset
, buflen
-offset
, &header
, &np
);
462 ret
= ctdb_uint64_pull(buf
+offset
, buflen
-offset
, &c
->srvid
, &np
);
468 ret
= ctdb_tdb_datan_pull(buf
+offset
, buflen
-offset
,
469 mem_ctx
, &c
->data
, &np
);