smbd: Move get_posix_fsp() to smb1_trans2.c
[Samba.git] / ctdb / protocol / protocol_message.c
blobe2202bb78d509920d21550f66124ec675f8194ad
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_SET_NODE_FLAGS:
61 len = ctdb_node_flag_change_len(mdata->flag_change);
62 break;
64 case CTDB_SRVID_RECD_UPDATE_IP:
65 len = ctdb_public_ip_len(mdata->pubip);
66 break;
68 case CTDB_SRVID_VACUUM_FETCH:
69 len = ctdb_rec_buffer_len(mdata->recbuf);
70 break;
72 case CTDB_SRVID_DETACH_DATABASE:
73 len = ctdb_uint32_len(&mdata->db_id);
74 break;
76 case CTDB_SRVID_MEM_DUMP:
77 len = ctdb_srvid_message_len(mdata->msg);
78 break;
80 case CTDB_SRVID_GETLOG:
81 break;
83 case CTDB_SRVID_CLEARLOG:
84 break;
86 case CTDB_SRVID_PUSH_NODE_FLAGS:
87 len = ctdb_node_flag_change_len(mdata->flag_change);
88 break;
90 case CTDB_SRVID_RELOAD_NODES:
91 break;
93 case CTDB_SRVID_TAKEOVER_RUN:
94 len = ctdb_srvid_message_len(mdata->msg);
95 break;
97 case CTDB_SRVID_REBALANCE_NODE:
98 len = ctdb_uint32_len(&mdata->pnn);
99 break;
101 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS:
102 len = ctdb_disable_message_len(mdata->disable);
103 break;
105 case CTDB_SRVID_DISABLE_RECOVERIES:
106 len = ctdb_disable_message_len(mdata->disable);
107 break;
109 case CTDB_SRVID_DISABLE_IP_CHECK:
110 len = ctdb_uint32_len(&mdata->timeout);
111 break;
113 default:
114 len = ctdb_tdb_data_len(&mdata->data);
115 break;
118 return len;
121 static void ctdb_message_data_push(union ctdb_message_data *mdata,
122 uint64_t srvid, uint8_t *buf,
123 size_t *npush)
125 size_t np = 0;
127 switch (srvid) {
128 case CTDB_SRVID_BANNING:
129 ctdb_uint32_push(&mdata->pnn, buf, &np);
130 break;
132 case CTDB_SRVID_ELECTION:
133 ctdb_election_message_push(mdata->election, buf, &np);
134 break;
136 case CTDB_SRVID_LEADER:
137 ctdb_uint32_push(&mdata->pnn, buf, &np);
138 break;
140 case CTDB_SRVID_RECONFIGURE:
141 break;
143 case CTDB_SRVID_RELEASE_IP:
144 ctdb_string_push(&mdata->ipaddr, buf, &np);
145 break;
147 case CTDB_SRVID_TAKE_IP:
148 ctdb_string_push(&mdata->ipaddr, buf, &np);
149 break;
151 case CTDB_SRVID_SET_NODE_FLAGS:
152 ctdb_node_flag_change_push(mdata->flag_change, buf, &np);
153 break;
155 case CTDB_SRVID_RECD_UPDATE_IP:
156 ctdb_public_ip_push(mdata->pubip, buf, &np);
157 break;
159 case CTDB_SRVID_VACUUM_FETCH:
160 ctdb_rec_buffer_push(mdata->recbuf, buf, &np);
161 break;
163 case CTDB_SRVID_DETACH_DATABASE:
164 ctdb_uint32_push(&mdata->db_id, buf, &np);
165 break;
167 case CTDB_SRVID_MEM_DUMP:
168 ctdb_srvid_message_push(mdata->msg, buf, &np);
169 break;
171 case CTDB_SRVID_GETLOG:
172 break;
174 case CTDB_SRVID_CLEARLOG:
175 break;
177 case CTDB_SRVID_PUSH_NODE_FLAGS:
178 ctdb_node_flag_change_push(mdata->flag_change, buf, &np);
179 break;
181 case CTDB_SRVID_RELOAD_NODES:
182 break;
184 case CTDB_SRVID_TAKEOVER_RUN:
185 ctdb_srvid_message_push(mdata->msg, buf, &np);
186 break;
188 case CTDB_SRVID_REBALANCE_NODE:
189 ctdb_uint32_push(&mdata->pnn, buf, &np);
190 break;
192 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS:
193 ctdb_disable_message_push(mdata->disable, buf, &np);
194 break;
196 case CTDB_SRVID_DISABLE_RECOVERIES:
197 ctdb_disable_message_push(mdata->disable, buf, &np);
198 break;
200 case CTDB_SRVID_DISABLE_IP_CHECK:
201 ctdb_uint32_push(&mdata->timeout, buf, &np);
202 break;
204 default:
205 ctdb_tdb_data_push(&mdata->data, buf, &np);
206 break;
209 *npush = 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,
215 size_t *npull)
217 int ret = 0;
218 size_t np = 0;
220 switch (srvid) {
221 case CTDB_SRVID_BANNING:
222 ret = ctdb_uint32_pull(buf, buflen, &mdata->pnn, &np);
223 break;
225 case CTDB_SRVID_ELECTION:
226 ret = ctdb_election_message_pull(buf, buflen, mem_ctx,
227 &mdata->election, &np);
228 break;
230 case CTDB_SRVID_LEADER:
231 ret = ctdb_uint32_pull(buf, buflen, &mdata->pnn, &np);
232 break;
234 case CTDB_SRVID_RECONFIGURE:
235 break;
237 case CTDB_SRVID_RELEASE_IP:
238 ret = ctdb_string_pull(buf, buflen, mem_ctx, &mdata->ipaddr,
239 &np);
240 break;
242 case CTDB_SRVID_TAKE_IP:
243 ret = ctdb_string_pull(buf, buflen, mem_ctx, &mdata->ipaddr,
244 &np);
245 break;
247 case CTDB_SRVID_SET_NODE_FLAGS:
248 ret = ctdb_node_flag_change_pull(buf, buflen, mem_ctx,
249 &mdata->flag_change, &np);
250 break;
252 case CTDB_SRVID_RECD_UPDATE_IP:
253 ret = ctdb_public_ip_pull(buf, buflen, mem_ctx,
254 &mdata->pubip, &np);
255 break;
257 case CTDB_SRVID_VACUUM_FETCH:
258 ret = ctdb_rec_buffer_pull(buf, buflen, mem_ctx,
259 &mdata->recbuf, &np);
260 break;
262 case CTDB_SRVID_DETACH_DATABASE:
263 ret = ctdb_uint32_pull(buf, buflen, &mdata->db_id, &np);
264 break;
266 case CTDB_SRVID_MEM_DUMP:
267 ret = ctdb_srvid_message_pull(buf, buflen, mem_ctx,
268 &mdata->msg, &np);
269 break;
271 case CTDB_SRVID_GETLOG:
272 break;
274 case CTDB_SRVID_CLEARLOG:
275 break;
277 case CTDB_SRVID_PUSH_NODE_FLAGS:
278 ret = ctdb_node_flag_change_pull(buf, buflen, mem_ctx,
279 &mdata->flag_change, &np);
280 break;
282 case CTDB_SRVID_RELOAD_NODES:
283 break;
285 case CTDB_SRVID_TAKEOVER_RUN:
286 ret = ctdb_srvid_message_pull(buf, buflen, mem_ctx,
287 &mdata->msg, &np);
288 break;
290 case CTDB_SRVID_REBALANCE_NODE:
291 ret = ctdb_uint32_pull(buf, buflen, &mdata->pnn, &np);
292 break;
294 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS:
295 ret = ctdb_disable_message_pull(buf, buflen, mem_ctx,
296 &mdata->disable, &np);
297 break;
299 case CTDB_SRVID_DISABLE_RECOVERIES:
300 ret = ctdb_disable_message_pull(buf, buflen, mem_ctx,
301 &mdata->disable, &np);
302 break;
304 case CTDB_SRVID_DISABLE_IP_CHECK:
305 ret = ctdb_uint32_pull(buf, buflen, &mdata->timeout, &np);
306 break;
308 default:
309 ret = ctdb_tdb_data_pull(buf, buflen, mem_ctx, &mdata->data,
310 &np);
311 break;
314 if (ret != 0) {
315 return ret;
318 *npull = np;
319 return 0;
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;
337 size_t length;
338 uint32_t u32;
340 length = ctdb_req_message_len(h, c);
341 if (*buflen < length) {
342 *buflen = length;
343 return EMSGSIZE;
346 h->length = *buflen;
347 ctdb_req_header_push(h, buf+offset, &np);
348 offset += np;
350 ctdb_uint64_push(&c->srvid, buf+offset, &np);
351 offset += np;
353 u32 = ctdb_message_data_len(&c->data, c->srvid);
354 ctdb_uint32_push(&u32, buf+offset, &np);
355 offset += np;
357 ctdb_message_data_push(&c->data, c->srvid, buf+offset, &np);
358 offset += np;
360 return 0;
363 int ctdb_req_message_pull(uint8_t *buf, size_t buflen,
364 struct ctdb_req_header *h,
365 TALLOC_CTX *mem_ctx,
366 struct ctdb_req_message *c)
368 struct ctdb_req_header header;
369 size_t offset = 0, np;
370 uint32_t u32;
371 int ret;
373 ret = ctdb_req_header_pull(buf+offset, buflen-offset, &header, &np);
374 if (ret != 0) {
375 return ret;
377 offset += np;
379 if (h != NULL) {
380 *h = header;
383 ret = ctdb_uint64_pull(buf+offset, buflen-offset, &c->srvid, &np);
384 if (ret != 0) {
385 return ret;
387 offset += np;
389 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
390 if (ret != 0) {
391 return ret;
393 offset += np;
395 if (buflen-offset < u32) {
396 return EMSGSIZE;
399 ret = ctdb_message_data_pull(buf+offset, u32, c->srvid,
400 mem_ctx, &c->data, &np);
401 if (ret != 0) {
402 return ret;
404 offset += np;
406 return ret;
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;
422 size_t length;
424 length = ctdb_req_message_data_len(h, c);
425 if (*buflen < length) {
426 *buflen = length;
427 return EMSGSIZE;
430 h->length = *buflen;
431 ctdb_req_header_push(h, buf+offset, &np);
432 offset += np;
434 ctdb_uint64_push(&c->srvid, buf+offset, &np);
435 offset += np;
437 ctdb_tdb_datan_push(&c->data, buf+offset, &np);
438 offset += np;
440 return 0;
443 int ctdb_req_message_data_pull(uint8_t *buf, size_t buflen,
444 struct ctdb_req_header *h,
445 TALLOC_CTX *mem_ctx,
446 struct ctdb_req_message_data *c)
448 struct ctdb_req_header header;
449 size_t offset = 0, np;
450 int ret;
452 ret = ctdb_req_header_pull(buf+offset, buflen-offset, &header, &np);
453 if (ret != 0) {
454 return ret;
456 offset += np;
458 if (h != NULL) {
459 *h = header;
462 ret = ctdb_uint64_pull(buf+offset, buflen-offset, &c->srvid, &np);
463 if (ret != 0) {
464 return ret;
466 offset += np;
468 ret = ctdb_tdb_datan_pull(buf+offset, buflen-offset,
469 mem_ctx, &c->data, &np);
470 if (ret != 0) {
471 return ret;
473 offset += np;
475 return 0;