python:tests: Store keys as bytes rather than as tuples
[Samba.git] / ctdb / protocol / protocol_message.c
blob627d4f8317d47175695c3cb3ecb945b2d9db211a
1 /*
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/>.
20 #include "replace.h"
21 #include "system/network.h"
23 #include <talloc.h>
24 #include <tdb.h>
26 #include "protocol.h"
27 #include "protocol_api.h"
28 #include "protocol_private.h"
31 static size_t ctdb_message_data_len(union ctdb_message_data *mdata,
32 uint64_t srvid)
34 size_t len = 0;
36 switch (srvid) {
37 case CTDB_SRVID_BANNING:
38 len = ctdb_uint32_len(&mdata->pnn);
39 break;
41 case CTDB_SRVID_ELECTION:
42 len = ctdb_election_message_len(mdata->election);
43 break;
45 case CTDB_SRVID_LEADER:
46 len = ctdb_uint32_len(&mdata->pnn);
47 break;
49 case CTDB_SRVID_RECONFIGURE:
50 break;
52 case CTDB_SRVID_RELEASE_IP:
53 len = ctdb_string_len(&mdata->ipaddr);
54 break;
56 case CTDB_SRVID_TAKE_IP:
57 len = ctdb_string_len(&mdata->ipaddr);
58 break;
60 case CTDB_SRVID_IPREALLOCATED:
61 break;
63 case CTDB_SRVID_START_IPREALLOCATE:
64 break;
66 case CTDB_SRVID_SET_NODE_FLAGS:
67 len = ctdb_node_flag_change_len(mdata->flag_change);
68 break;
70 case CTDB_SRVID_RECD_UPDATE_IP:
71 len = ctdb_public_ip_len(mdata->pubip);
72 break;
74 case CTDB_SRVID_VACUUM_FETCH:
75 len = ctdb_rec_buffer_len(mdata->recbuf);
76 break;
78 case CTDB_SRVID_DETACH_DATABASE:
79 len = ctdb_uint32_len(&mdata->db_id);
80 break;
82 case CTDB_SRVID_MEM_DUMP:
83 len = ctdb_srvid_message_len(mdata->msg);
84 break;
86 case CTDB_SRVID_GETLOG:
87 break;
89 case CTDB_SRVID_CLEARLOG:
90 break;
92 case CTDB_SRVID_PUSH_NODE_FLAGS:
93 len = ctdb_node_flag_change_len(mdata->flag_change);
94 break;
96 case CTDB_SRVID_RELOAD_NODES:
97 break;
99 case CTDB_SRVID_TAKEOVER_RUN:
100 len = ctdb_srvid_message_len(mdata->msg);
101 break;
103 case CTDB_SRVID_REBALANCE_NODE:
104 len = ctdb_uint32_len(&mdata->pnn);
105 break;
107 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS:
108 len = ctdb_disable_message_len(mdata->disable);
109 break;
111 case CTDB_SRVID_DISABLE_RECOVERIES:
112 len = ctdb_disable_message_len(mdata->disable);
113 break;
115 case CTDB_SRVID_DISABLE_IP_CHECK:
116 len = ctdb_uint32_len(&mdata->timeout);
117 break;
119 default:
120 len = ctdb_tdb_data_len(&mdata->data);
121 break;
124 return len;
127 static void ctdb_message_data_push(union ctdb_message_data *mdata,
128 uint64_t srvid, uint8_t *buf,
129 size_t *npush)
131 size_t np = 0;
133 switch (srvid) {
134 case CTDB_SRVID_BANNING:
135 ctdb_uint32_push(&mdata->pnn, buf, &np);
136 break;
138 case CTDB_SRVID_ELECTION:
139 ctdb_election_message_push(mdata->election, buf, &np);
140 break;
142 case CTDB_SRVID_LEADER:
143 ctdb_uint32_push(&mdata->pnn, buf, &np);
144 break;
146 case CTDB_SRVID_RECONFIGURE:
147 break;
149 case CTDB_SRVID_RELEASE_IP:
150 ctdb_string_push(&mdata->ipaddr, buf, &np);
151 break;
153 case CTDB_SRVID_TAKE_IP:
154 ctdb_string_push(&mdata->ipaddr, buf, &np);
155 break;
157 case CTDB_SRVID_IPREALLOCATED:
158 break;
160 case CTDB_SRVID_START_IPREALLOCATE:
161 break;
163 case CTDB_SRVID_SET_NODE_FLAGS:
164 ctdb_node_flag_change_push(mdata->flag_change, buf, &np);
165 break;
167 case CTDB_SRVID_RECD_UPDATE_IP:
168 ctdb_public_ip_push(mdata->pubip, buf, &np);
169 break;
171 case CTDB_SRVID_VACUUM_FETCH:
172 ctdb_rec_buffer_push(mdata->recbuf, buf, &np);
173 break;
175 case CTDB_SRVID_DETACH_DATABASE:
176 ctdb_uint32_push(&mdata->db_id, buf, &np);
177 break;
179 case CTDB_SRVID_MEM_DUMP:
180 ctdb_srvid_message_push(mdata->msg, buf, &np);
181 break;
183 case CTDB_SRVID_GETLOG:
184 break;
186 case CTDB_SRVID_CLEARLOG:
187 break;
189 case CTDB_SRVID_PUSH_NODE_FLAGS:
190 ctdb_node_flag_change_push(mdata->flag_change, buf, &np);
191 break;
193 case CTDB_SRVID_RELOAD_NODES:
194 break;
196 case CTDB_SRVID_TAKEOVER_RUN:
197 ctdb_srvid_message_push(mdata->msg, buf, &np);
198 break;
200 case CTDB_SRVID_REBALANCE_NODE:
201 ctdb_uint32_push(&mdata->pnn, buf, &np);
202 break;
204 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS:
205 ctdb_disable_message_push(mdata->disable, buf, &np);
206 break;
208 case CTDB_SRVID_DISABLE_RECOVERIES:
209 ctdb_disable_message_push(mdata->disable, buf, &np);
210 break;
212 case CTDB_SRVID_DISABLE_IP_CHECK:
213 ctdb_uint32_push(&mdata->timeout, buf, &np);
214 break;
216 default:
217 ctdb_tdb_data_push(&mdata->data, buf, &np);
218 break;
221 *npush = 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,
227 size_t *npull)
229 int ret = 0;
230 size_t np = 0;
232 switch (srvid) {
233 case CTDB_SRVID_BANNING:
234 ret = ctdb_uint32_pull(buf, buflen, &mdata->pnn, &np);
235 break;
237 case CTDB_SRVID_ELECTION:
238 ret = ctdb_election_message_pull(buf, buflen, mem_ctx,
239 &mdata->election, &np);
240 break;
242 case CTDB_SRVID_LEADER:
243 ret = ctdb_uint32_pull(buf, buflen, &mdata->pnn, &np);
244 break;
246 case CTDB_SRVID_RECONFIGURE:
247 break;
249 case CTDB_SRVID_RELEASE_IP:
250 ret = ctdb_string_pull(buf, buflen, mem_ctx, &mdata->ipaddr,
251 &np);
252 break;
254 case CTDB_SRVID_TAKE_IP:
255 ret = ctdb_string_pull(buf, buflen, mem_ctx, &mdata->ipaddr,
256 &np);
257 break;
259 case CTDB_SRVID_IPREALLOCATED:
260 break;
262 case CTDB_SRVID_START_IPREALLOCATE:
263 break;
265 case CTDB_SRVID_SET_NODE_FLAGS:
266 ret = ctdb_node_flag_change_pull(buf, buflen, mem_ctx,
267 &mdata->flag_change, &np);
268 break;
270 case CTDB_SRVID_RECD_UPDATE_IP:
271 ret = ctdb_public_ip_pull(buf, buflen, mem_ctx,
272 &mdata->pubip, &np);
273 break;
275 case CTDB_SRVID_VACUUM_FETCH:
276 ret = ctdb_rec_buffer_pull(buf, buflen, mem_ctx,
277 &mdata->recbuf, &np);
278 break;
280 case CTDB_SRVID_DETACH_DATABASE:
281 ret = ctdb_uint32_pull(buf, buflen, &mdata->db_id, &np);
282 break;
284 case CTDB_SRVID_MEM_DUMP:
285 ret = ctdb_srvid_message_pull(buf, buflen, mem_ctx,
286 &mdata->msg, &np);
287 break;
289 case CTDB_SRVID_GETLOG:
290 break;
292 case CTDB_SRVID_CLEARLOG:
293 break;
295 case CTDB_SRVID_PUSH_NODE_FLAGS:
296 ret = ctdb_node_flag_change_pull(buf, buflen, mem_ctx,
297 &mdata->flag_change, &np);
298 break;
300 case CTDB_SRVID_RELOAD_NODES:
301 break;
303 case CTDB_SRVID_TAKEOVER_RUN:
304 ret = ctdb_srvid_message_pull(buf, buflen, mem_ctx,
305 &mdata->msg, &np);
306 break;
308 case CTDB_SRVID_REBALANCE_NODE:
309 ret = ctdb_uint32_pull(buf, buflen, &mdata->pnn, &np);
310 break;
312 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS:
313 ret = ctdb_disable_message_pull(buf, buflen, mem_ctx,
314 &mdata->disable, &np);
315 break;
317 case CTDB_SRVID_DISABLE_RECOVERIES:
318 ret = ctdb_disable_message_pull(buf, buflen, mem_ctx,
319 &mdata->disable, &np);
320 break;
322 case CTDB_SRVID_DISABLE_IP_CHECK:
323 ret = ctdb_uint32_pull(buf, buflen, &mdata->timeout, &np);
324 break;
326 default:
327 ret = ctdb_tdb_data_pull(buf, buflen, mem_ctx, &mdata->data,
328 &np);
329 break;
332 if (ret != 0) {
333 return ret;
336 *npull = np;
337 return 0;
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;
355 size_t length;
356 uint32_t u32;
358 length = ctdb_req_message_len(h, c);
359 if (*buflen < length) {
360 *buflen = length;
361 return EMSGSIZE;
364 h->length = *buflen;
365 ctdb_req_header_push(h, buf+offset, &np);
366 offset += np;
368 ctdb_uint64_push(&c->srvid, buf+offset, &np);
369 offset += np;
371 u32 = ctdb_message_data_len(&c->data, c->srvid);
372 ctdb_uint32_push(&u32, buf+offset, &np);
373 offset += np;
375 ctdb_message_data_push(&c->data, c->srvid, buf+offset, &np);
376 offset += np;
378 return 0;
381 int ctdb_req_message_pull(uint8_t *buf, size_t buflen,
382 struct ctdb_req_header *h,
383 TALLOC_CTX *mem_ctx,
384 struct ctdb_req_message *c)
386 struct ctdb_req_header header;
387 size_t offset = 0, np;
388 uint32_t u32;
389 int ret;
391 ret = ctdb_req_header_pull(buf+offset, buflen-offset, &header, &np);
392 if (ret != 0) {
393 return ret;
395 offset += np;
397 if (h != NULL) {
398 *h = header;
401 ret = ctdb_uint64_pull(buf+offset, buflen-offset, &c->srvid, &np);
402 if (ret != 0) {
403 return ret;
405 offset += np;
407 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
408 if (ret != 0) {
409 return ret;
411 offset += np;
413 if (buflen-offset < u32) {
414 return EMSGSIZE;
417 ret = ctdb_message_data_pull(buf+offset, u32, c->srvid,
418 mem_ctx, &c->data, &np);
419 if (ret != 0) {
420 return ret;
422 offset += np;
424 return ret;
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;
440 size_t length;
442 length = ctdb_req_message_data_len(h, c);
443 if (*buflen < length) {
444 *buflen = length;
445 return EMSGSIZE;
448 h->length = *buflen;
449 ctdb_req_header_push(h, buf+offset, &np);
450 offset += np;
452 ctdb_uint64_push(&c->srvid, buf+offset, &np);
453 offset += np;
455 ctdb_tdb_datan_push(&c->data, buf+offset, &np);
456 offset += np;
458 return 0;
461 int ctdb_req_message_data_pull(uint8_t *buf, size_t buflen,
462 struct ctdb_req_header *h,
463 TALLOC_CTX *mem_ctx,
464 struct ctdb_req_message_data *c)
466 struct ctdb_req_header header;
467 size_t offset = 0, np;
468 int ret;
470 ret = ctdb_req_header_pull(buf+offset, buflen-offset, &header, &np);
471 if (ret != 0) {
472 return ret;
474 offset += np;
476 if (h != NULL) {
477 *h = header;
480 ret = ctdb_uint64_pull(buf+offset, buflen-offset, &c->srvid, &np);
481 if (ret != 0) {
482 return ret;
484 offset += np;
486 ret = ctdb_tdb_datan_pull(buf+offset, buflen-offset,
487 mem_ctx, &c->data, &np);
488 if (ret != 0) {
489 return ret;
491 offset += np;
493 return 0;