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_IPREALLOCATED
:
63 case CTDB_SRVID_START_IPREALLOCATE
:
66 case CTDB_SRVID_SET_NODE_FLAGS
:
67 len
= ctdb_node_flag_change_len(mdata
->flag_change
);
70 case CTDB_SRVID_RECD_UPDATE_IP
:
71 len
= ctdb_public_ip_len(mdata
->pubip
);
74 case CTDB_SRVID_VACUUM_FETCH
:
75 len
= ctdb_rec_buffer_len(mdata
->recbuf
);
78 case CTDB_SRVID_DETACH_DATABASE
:
79 len
= ctdb_uint32_len(&mdata
->db_id
);
82 case CTDB_SRVID_MEM_DUMP
:
83 len
= ctdb_srvid_message_len(mdata
->msg
);
86 case CTDB_SRVID_GETLOG
:
89 case CTDB_SRVID_CLEARLOG
:
92 case CTDB_SRVID_PUSH_NODE_FLAGS
:
93 len
= ctdb_node_flag_change_len(mdata
->flag_change
);
96 case CTDB_SRVID_RELOAD_NODES
:
99 case CTDB_SRVID_TAKEOVER_RUN
:
100 len
= ctdb_srvid_message_len(mdata
->msg
);
103 case CTDB_SRVID_REBALANCE_NODE
:
104 len
= ctdb_uint32_len(&mdata
->pnn
);
107 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS
:
108 len
= ctdb_disable_message_len(mdata
->disable
);
111 case CTDB_SRVID_DISABLE_RECOVERIES
:
112 len
= ctdb_disable_message_len(mdata
->disable
);
115 case CTDB_SRVID_DISABLE_IP_CHECK
:
116 len
= ctdb_uint32_len(&mdata
->timeout
);
120 len
= ctdb_tdb_data_len(&mdata
->data
);
127 static void ctdb_message_data_push(union ctdb_message_data
*mdata
,
128 uint64_t srvid
, uint8_t *buf
,
134 case CTDB_SRVID_BANNING
:
135 ctdb_uint32_push(&mdata
->pnn
, buf
, &np
);
138 case CTDB_SRVID_ELECTION
:
139 ctdb_election_message_push(mdata
->election
, buf
, &np
);
142 case CTDB_SRVID_LEADER
:
143 ctdb_uint32_push(&mdata
->pnn
, buf
, &np
);
146 case CTDB_SRVID_RECONFIGURE
:
149 case CTDB_SRVID_RELEASE_IP
:
150 ctdb_string_push(&mdata
->ipaddr
, buf
, &np
);
153 case CTDB_SRVID_TAKE_IP
:
154 ctdb_string_push(&mdata
->ipaddr
, buf
, &np
);
157 case CTDB_SRVID_IPREALLOCATED
:
160 case CTDB_SRVID_START_IPREALLOCATE
:
163 case CTDB_SRVID_SET_NODE_FLAGS
:
164 ctdb_node_flag_change_push(mdata
->flag_change
, buf
, &np
);
167 case CTDB_SRVID_RECD_UPDATE_IP
:
168 ctdb_public_ip_push(mdata
->pubip
, buf
, &np
);
171 case CTDB_SRVID_VACUUM_FETCH
:
172 ctdb_rec_buffer_push(mdata
->recbuf
, buf
, &np
);
175 case CTDB_SRVID_DETACH_DATABASE
:
176 ctdb_uint32_push(&mdata
->db_id
, buf
, &np
);
179 case CTDB_SRVID_MEM_DUMP
:
180 ctdb_srvid_message_push(mdata
->msg
, buf
, &np
);
183 case CTDB_SRVID_GETLOG
:
186 case CTDB_SRVID_CLEARLOG
:
189 case CTDB_SRVID_PUSH_NODE_FLAGS
:
190 ctdb_node_flag_change_push(mdata
->flag_change
, buf
, &np
);
193 case CTDB_SRVID_RELOAD_NODES
:
196 case CTDB_SRVID_TAKEOVER_RUN
:
197 ctdb_srvid_message_push(mdata
->msg
, buf
, &np
);
200 case CTDB_SRVID_REBALANCE_NODE
:
201 ctdb_uint32_push(&mdata
->pnn
, buf
, &np
);
204 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS
:
205 ctdb_disable_message_push(mdata
->disable
, buf
, &np
);
208 case CTDB_SRVID_DISABLE_RECOVERIES
:
209 ctdb_disable_message_push(mdata
->disable
, buf
, &np
);
212 case CTDB_SRVID_DISABLE_IP_CHECK
:
213 ctdb_uint32_push(&mdata
->timeout
, buf
, &np
);
217 ctdb_tdb_data_push(&mdata
->data
, buf
, &np
);
224 static int ctdb_message_data_pull(uint8_t *buf
, size_t buflen
,
225 uint64_t srvid
, TALLOC_CTX
*mem_ctx
,
226 union ctdb_message_data
*mdata
,
233 case CTDB_SRVID_BANNING
:
234 ret
= ctdb_uint32_pull(buf
, buflen
, &mdata
->pnn
, &np
);
237 case CTDB_SRVID_ELECTION
:
238 ret
= ctdb_election_message_pull(buf
, buflen
, mem_ctx
,
239 &mdata
->election
, &np
);
242 case CTDB_SRVID_LEADER
:
243 ret
= ctdb_uint32_pull(buf
, buflen
, &mdata
->pnn
, &np
);
246 case CTDB_SRVID_RECONFIGURE
:
249 case CTDB_SRVID_RELEASE_IP
:
250 ret
= ctdb_string_pull(buf
, buflen
, mem_ctx
, &mdata
->ipaddr
,
254 case CTDB_SRVID_TAKE_IP
:
255 ret
= ctdb_string_pull(buf
, buflen
, mem_ctx
, &mdata
->ipaddr
,
259 case CTDB_SRVID_IPREALLOCATED
:
262 case CTDB_SRVID_START_IPREALLOCATE
:
265 case CTDB_SRVID_SET_NODE_FLAGS
:
266 ret
= ctdb_node_flag_change_pull(buf
, buflen
, mem_ctx
,
267 &mdata
->flag_change
, &np
);
270 case CTDB_SRVID_RECD_UPDATE_IP
:
271 ret
= ctdb_public_ip_pull(buf
, buflen
, mem_ctx
,
275 case CTDB_SRVID_VACUUM_FETCH
:
276 ret
= ctdb_rec_buffer_pull(buf
, buflen
, mem_ctx
,
277 &mdata
->recbuf
, &np
);
280 case CTDB_SRVID_DETACH_DATABASE
:
281 ret
= ctdb_uint32_pull(buf
, buflen
, &mdata
->db_id
, &np
);
284 case CTDB_SRVID_MEM_DUMP
:
285 ret
= ctdb_srvid_message_pull(buf
, buflen
, mem_ctx
,
289 case CTDB_SRVID_GETLOG
:
292 case CTDB_SRVID_CLEARLOG
:
295 case CTDB_SRVID_PUSH_NODE_FLAGS
:
296 ret
= ctdb_node_flag_change_pull(buf
, buflen
, mem_ctx
,
297 &mdata
->flag_change
, &np
);
300 case CTDB_SRVID_RELOAD_NODES
:
303 case CTDB_SRVID_TAKEOVER_RUN
:
304 ret
= ctdb_srvid_message_pull(buf
, buflen
, mem_ctx
,
308 case CTDB_SRVID_REBALANCE_NODE
:
309 ret
= ctdb_uint32_pull(buf
, buflen
, &mdata
->pnn
, &np
);
312 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS
:
313 ret
= ctdb_disable_message_pull(buf
, buflen
, mem_ctx
,
314 &mdata
->disable
, &np
);
317 case CTDB_SRVID_DISABLE_RECOVERIES
:
318 ret
= ctdb_disable_message_pull(buf
, buflen
, mem_ctx
,
319 &mdata
->disable
, &np
);
322 case CTDB_SRVID_DISABLE_IP_CHECK
:
323 ret
= ctdb_uint32_pull(buf
, buflen
, &mdata
->timeout
, &np
);
327 ret
= ctdb_tdb_data_pull(buf
, buflen
, mem_ctx
, &mdata
->data
,
340 size_t ctdb_req_message_len(struct ctdb_req_header
*h
,
341 struct ctdb_req_message
*c
)
343 uint32_t u32
= ctdb_message_data_len(&c
->data
, c
->srvid
);
345 return ctdb_req_header_len(h
) +
346 ctdb_uint64_len(&c
->srvid
) +
347 ctdb_uint32_len(&u32
) + u32
;
350 int ctdb_req_message_push(struct ctdb_req_header
*h
,
351 struct ctdb_req_message
*c
,
352 uint8_t *buf
, size_t *buflen
)
354 size_t offset
= 0, np
;
358 length
= ctdb_req_message_len(h
, c
);
359 if (*buflen
< length
) {
365 ctdb_req_header_push(h
, buf
+offset
, &np
);
368 ctdb_uint64_push(&c
->srvid
, buf
+offset
, &np
);
371 u32
= ctdb_message_data_len(&c
->data
, c
->srvid
);
372 ctdb_uint32_push(&u32
, buf
+offset
, &np
);
375 ctdb_message_data_push(&c
->data
, c
->srvid
, buf
+offset
, &np
);
381 int ctdb_req_message_pull(uint8_t *buf
, size_t buflen
,
382 struct ctdb_req_header
*h
,
384 struct ctdb_req_message
*c
)
386 struct ctdb_req_header header
;
387 size_t offset
= 0, np
;
391 ret
= ctdb_req_header_pull(buf
+offset
, buflen
-offset
, &header
, &np
);
401 ret
= ctdb_uint64_pull(buf
+offset
, buflen
-offset
, &c
->srvid
, &np
);
407 ret
= ctdb_uint32_pull(buf
+offset
, buflen
-offset
, &u32
, &np
);
413 if (buflen
-offset
< u32
) {
417 ret
= ctdb_message_data_pull(buf
+offset
, u32
, c
->srvid
,
418 mem_ctx
, &c
->data
, &np
);
427 size_t ctdb_req_message_data_len(struct ctdb_req_header
*h
,
428 struct ctdb_req_message_data
*c
)
430 return ctdb_req_header_len(h
) +
431 ctdb_uint64_len(&c
->srvid
) +
432 ctdb_tdb_datan_len(&c
->data
);
435 int ctdb_req_message_data_push(struct ctdb_req_header
*h
,
436 struct ctdb_req_message_data
*c
,
437 uint8_t *buf
, size_t *buflen
)
439 size_t offset
= 0, np
;
442 length
= ctdb_req_message_data_len(h
, c
);
443 if (*buflen
< length
) {
449 ctdb_req_header_push(h
, buf
+offset
, &np
);
452 ctdb_uint64_push(&c
->srvid
, buf
+offset
, &np
);
455 ctdb_tdb_datan_push(&c
->data
, buf
+offset
, &np
);
461 int ctdb_req_message_data_pull(uint8_t *buf
, size_t buflen
,
462 struct ctdb_req_header
*h
,
464 struct ctdb_req_message_data
*c
)
466 struct ctdb_req_header header
;
467 size_t offset
= 0, np
;
470 ret
= ctdb_req_header_pull(buf
+offset
, buflen
-offset
, &header
, &np
);
480 ret
= ctdb_uint64_pull(buf
+offset
, buflen
-offset
, &c
->srvid
, &np
);
486 ret
= ctdb_tdb_datan_pull(buf
+offset
, buflen
-offset
,
487 mem_ctx
, &c
->data
, &np
);