s4:rpc_server: only share assoc group ids on the same transport
[Samba.git] / ctdb / protocol / protocol_message.c
blob19c1f162d0c17446d1d21c2cbff6588fb3f1f5f2
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_RECONFIGURE:
46 break;
48 case CTDB_SRVID_RELEASE_IP:
49 len = ctdb_string_len(&mdata->ipaddr);
50 break;
52 case CTDB_SRVID_TAKE_IP:
53 len = ctdb_string_len(&mdata->ipaddr);
54 break;
56 case CTDB_SRVID_SET_NODE_FLAGS:
57 len = ctdb_node_flag_change_len(mdata->flag_change);
58 break;
60 case CTDB_SRVID_RECD_UPDATE_IP:
61 len = ctdb_public_ip_len(mdata->pubip);
62 break;
64 case CTDB_SRVID_VACUUM_FETCH:
65 len = ctdb_rec_buffer_len(mdata->recbuf);
66 break;
68 case CTDB_SRVID_DETACH_DATABASE:
69 len = ctdb_uint32_len(&mdata->db_id);
70 break;
72 case CTDB_SRVID_MEM_DUMP:
73 len = ctdb_srvid_message_len(mdata->msg);
74 break;
76 case CTDB_SRVID_GETLOG:
77 break;
79 case CTDB_SRVID_CLEARLOG:
80 break;
82 case CTDB_SRVID_PUSH_NODE_FLAGS:
83 len = ctdb_node_flag_change_len(mdata->flag_change);
84 break;
86 case CTDB_SRVID_RELOAD_NODES:
87 break;
89 case CTDB_SRVID_TAKEOVER_RUN:
90 len = ctdb_srvid_message_len(mdata->msg);
91 break;
93 case CTDB_SRVID_REBALANCE_NODE:
94 len = ctdb_uint32_len(&mdata->pnn);
95 break;
97 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS:
98 len = ctdb_disable_message_len(mdata->disable);
99 break;
101 case CTDB_SRVID_DISABLE_RECOVERIES:
102 len = ctdb_disable_message_len(mdata->disable);
103 break;
105 case CTDB_SRVID_DISABLE_IP_CHECK:
106 len = ctdb_uint32_len(&mdata->timeout);
107 break;
109 default:
110 len = ctdb_tdb_data_len(&mdata->data);
111 break;
114 return len;
117 static void ctdb_message_data_push(union ctdb_message_data *mdata,
118 uint64_t srvid, uint8_t *buf,
119 size_t *npush)
121 size_t np = 0;
123 switch (srvid) {
124 case CTDB_SRVID_BANNING:
125 ctdb_uint32_push(&mdata->pnn, buf, &np);
126 break;
128 case CTDB_SRVID_ELECTION:
129 ctdb_election_message_push(mdata->election, buf, &np);
130 break;
132 case CTDB_SRVID_RECONFIGURE:
133 break;
135 case CTDB_SRVID_RELEASE_IP:
136 ctdb_string_push(&mdata->ipaddr, buf, &np);
137 break;
139 case CTDB_SRVID_TAKE_IP:
140 ctdb_string_push(&mdata->ipaddr, buf, &np);
141 break;
143 case CTDB_SRVID_SET_NODE_FLAGS:
144 ctdb_node_flag_change_push(mdata->flag_change, buf, &np);
145 break;
147 case CTDB_SRVID_RECD_UPDATE_IP:
148 ctdb_public_ip_push(mdata->pubip, buf, &np);
149 break;
151 case CTDB_SRVID_VACUUM_FETCH:
152 ctdb_rec_buffer_push(mdata->recbuf, buf, &np);
153 break;
155 case CTDB_SRVID_DETACH_DATABASE:
156 ctdb_uint32_push(&mdata->db_id, buf, &np);
157 break;
159 case CTDB_SRVID_MEM_DUMP:
160 ctdb_srvid_message_push(mdata->msg, buf, &np);
161 break;
163 case CTDB_SRVID_GETLOG:
164 break;
166 case CTDB_SRVID_CLEARLOG:
167 break;
169 case CTDB_SRVID_PUSH_NODE_FLAGS:
170 ctdb_node_flag_change_push(mdata->flag_change, buf, &np);
171 break;
173 case CTDB_SRVID_RELOAD_NODES:
174 break;
176 case CTDB_SRVID_TAKEOVER_RUN:
177 ctdb_srvid_message_push(mdata->msg, buf, &np);
178 break;
180 case CTDB_SRVID_REBALANCE_NODE:
181 ctdb_uint32_push(&mdata->pnn, buf, &np);
182 break;
184 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS:
185 ctdb_disable_message_push(mdata->disable, buf, &np);
186 break;
188 case CTDB_SRVID_DISABLE_RECOVERIES:
189 ctdb_disable_message_push(mdata->disable, buf, &np);
190 break;
192 case CTDB_SRVID_DISABLE_IP_CHECK:
193 ctdb_uint32_push(&mdata->timeout, buf, &np);
194 break;
196 default:
197 ctdb_tdb_data_push(&mdata->data, buf, &np);
198 break;
201 *npush = 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,
207 size_t *npull)
209 int ret = 0;
210 size_t np = 0;
212 switch (srvid) {
213 case CTDB_SRVID_BANNING:
214 ret = ctdb_uint32_pull(buf, buflen, &mdata->pnn, &np);
215 break;
217 case CTDB_SRVID_ELECTION:
218 ret = ctdb_election_message_pull(buf, buflen, mem_ctx,
219 &mdata->election, &np);
220 break;
222 case CTDB_SRVID_RECONFIGURE:
223 break;
225 case CTDB_SRVID_RELEASE_IP:
226 ret = ctdb_string_pull(buf, buflen, mem_ctx, &mdata->ipaddr,
227 &np);
228 break;
230 case CTDB_SRVID_TAKE_IP:
231 ret = ctdb_string_pull(buf, buflen, mem_ctx, &mdata->ipaddr,
232 &np);
233 break;
235 case CTDB_SRVID_SET_NODE_FLAGS:
236 ret = ctdb_node_flag_change_pull(buf, buflen, mem_ctx,
237 &mdata->flag_change, &np);
238 break;
240 case CTDB_SRVID_RECD_UPDATE_IP:
241 ret = ctdb_public_ip_pull(buf, buflen, mem_ctx,
242 &mdata->pubip, &np);
243 break;
245 case CTDB_SRVID_VACUUM_FETCH:
246 ret = ctdb_rec_buffer_pull(buf, buflen, mem_ctx,
247 &mdata->recbuf, &np);
248 break;
250 case CTDB_SRVID_DETACH_DATABASE:
251 ret = ctdb_uint32_pull(buf, buflen, &mdata->db_id, &np);
252 break;
254 case CTDB_SRVID_MEM_DUMP:
255 ret = ctdb_srvid_message_pull(buf, buflen, mem_ctx,
256 &mdata->msg, &np);
257 break;
259 case CTDB_SRVID_GETLOG:
260 break;
262 case CTDB_SRVID_CLEARLOG:
263 break;
265 case CTDB_SRVID_PUSH_NODE_FLAGS:
266 ret = ctdb_node_flag_change_pull(buf, buflen, mem_ctx,
267 &mdata->flag_change, &np);
268 break;
270 case CTDB_SRVID_RELOAD_NODES:
271 break;
273 case CTDB_SRVID_TAKEOVER_RUN:
274 ret = ctdb_srvid_message_pull(buf, buflen, mem_ctx,
275 &mdata->msg, &np);
276 break;
278 case CTDB_SRVID_REBALANCE_NODE:
279 ret = ctdb_uint32_pull(buf, buflen, &mdata->pnn, &np);
280 break;
282 case CTDB_SRVID_DISABLE_TAKEOVER_RUNS:
283 ret = ctdb_disable_message_pull(buf, buflen, mem_ctx,
284 &mdata->disable, &np);
285 break;
287 case CTDB_SRVID_DISABLE_RECOVERIES:
288 ret = ctdb_disable_message_pull(buf, buflen, mem_ctx,
289 &mdata->disable, &np);
290 break;
292 case CTDB_SRVID_DISABLE_IP_CHECK:
293 ret = ctdb_uint32_pull(buf, buflen, &mdata->timeout, &np);
294 break;
296 default:
297 ret = ctdb_tdb_data_pull(buf, buflen, mem_ctx, &mdata->data,
298 &np);
299 break;
302 if (ret != 0) {
303 return ret;
306 *npull = np;
307 return 0;
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;
325 size_t length;
326 uint32_t u32;
328 length = ctdb_req_message_len(h, c);
329 if (*buflen < length) {
330 *buflen = length;
331 return EMSGSIZE;
334 h->length = *buflen;
335 ctdb_req_header_push(h, buf+offset, &np);
336 offset += np;
338 ctdb_uint64_push(&c->srvid, buf+offset, &np);
339 offset += np;
341 u32 = ctdb_message_data_len(&c->data, c->srvid);
342 ctdb_uint32_push(&u32, buf+offset, &np);
343 offset += np;
345 ctdb_message_data_push(&c->data, c->srvid, buf+offset, &np);
346 offset += np;
348 return 0;
351 int ctdb_req_message_pull(uint8_t *buf, size_t buflen,
352 struct ctdb_req_header *h,
353 TALLOC_CTX *mem_ctx,
354 struct ctdb_req_message *c)
356 struct ctdb_req_header header;
357 size_t offset = 0, np;
358 uint32_t u32;
359 int ret;
361 ret = ctdb_req_header_pull(buf+offset, buflen-offset, &header, &np);
362 if (ret != 0) {
363 return ret;
365 offset += np;
367 if (h != NULL) {
368 *h = header;
371 ret = ctdb_uint64_pull(buf+offset, buflen-offset, &c->srvid, &np);
372 if (ret != 0) {
373 return ret;
375 offset += np;
377 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
378 if (ret != 0) {
379 return ret;
381 offset += np;
383 if (buflen-offset < u32) {
384 return EMSGSIZE;
387 ret = ctdb_message_data_pull(buf+offset, u32, c->srvid,
388 mem_ctx, &c->data, &np);
389 if (ret != 0) {
390 return ret;
392 offset += np;
394 return ret;
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;
410 size_t length;
412 length = ctdb_req_message_data_len(h, c);
413 if (*buflen < length) {
414 *buflen = length;
415 return EMSGSIZE;
418 h->length = *buflen;
419 ctdb_req_header_push(h, buf+offset, &np);
420 offset += np;
422 ctdb_uint64_push(&c->srvid, buf+offset, &np);
423 offset += np;
425 ctdb_tdb_datan_push(&c->data, buf+offset, &np);
426 offset += np;
428 return 0;
431 int ctdb_req_message_data_pull(uint8_t *buf, size_t buflen,
432 struct ctdb_req_header *h,
433 TALLOC_CTX *mem_ctx,
434 struct ctdb_req_message_data *c)
436 struct ctdb_req_header header;
437 size_t offset = 0, np;
438 int ret;
440 ret = ctdb_req_header_pull(buf+offset, buflen-offset, &header, &np);
441 if (ret != 0) {
442 return ret;
444 offset += np;
446 if (h != NULL) {
447 *h = header;
450 ret = ctdb_uint64_pull(buf+offset, buflen-offset, &c->srvid, &np);
451 if (ret != 0) {
452 return ret;
454 offset += np;
456 ret = ctdb_tdb_datan_pull(buf+offset, buflen-offset,
457 mem_ctx, &c->data, &np);
458 if (ret != 0) {
459 return ret;
461 offset += np;
463 return 0;