python:tests: Store keys as bytes rather than as tuples
[Samba.git] / ctdb / protocol / protocol_header.c
bloba6be7f515477f711ad32e3f7270e5d80920eacf0
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"
30 int ctdb_req_header_verify(struct ctdb_req_header *h, uint32_t operation)
32 if (h->length < sizeof(struct ctdb_req_header)) {
33 return EMSGSIZE;
36 if (h->ctdb_magic != CTDB_MAGIC) {
37 return EPROTO;
40 if (h->ctdb_version != CTDB_PROTOCOL) {
41 return EPROTO;
44 if (operation != 0 && h->operation != operation) {
45 return EPROTO;
48 return 0;
51 void ctdb_req_header_fill(struct ctdb_req_header *h, uint32_t generation,
52 uint32_t operation, uint32_t destnode,
53 uint32_t srcnode, uint32_t reqid)
55 h->length = sizeof(struct ctdb_req_header);
56 h->ctdb_magic = CTDB_MAGIC;
57 h->ctdb_version = CTDB_PROTOCOL;
58 h->generation = generation;
59 h->operation = operation;
60 h->destnode = destnode;
61 h->srcnode = srcnode;
62 h->reqid = reqid;
65 size_t ctdb_req_header_len(struct ctdb_req_header *in)
67 return ctdb_uint32_len(&in->length) +
68 ctdb_uint32_len(&in->ctdb_magic) +
69 ctdb_uint32_len(&in->ctdb_version) +
70 ctdb_uint32_len(&in->generation) +
71 ctdb_uint32_len(&in->operation) +
72 ctdb_uint32_len(&in->destnode) +
73 ctdb_uint32_len(&in->srcnode) +
74 ctdb_uint32_len(&in->reqid);
77 void ctdb_req_header_push(struct ctdb_req_header *in, uint8_t *buf,
78 size_t *npush)
80 size_t offset = 0, np;
82 ctdb_uint32_push(&in->length, buf+offset, &np);
83 offset += np;
85 ctdb_uint32_push(&in->ctdb_magic, buf+offset, &np);
86 offset += np;
88 ctdb_uint32_push(&in->ctdb_version, buf+offset, &np);
89 offset += np;
91 ctdb_uint32_push(&in->generation, buf+offset, &np);
92 offset += np;
94 ctdb_uint32_push(&in->operation, buf+offset, &np);
95 offset += np;
97 ctdb_uint32_push(&in->destnode, buf+offset, &np);
98 offset += np;
100 ctdb_uint32_push(&in->srcnode, buf+offset, &np);
101 offset += np;
103 ctdb_uint32_push(&in->reqid, buf+offset, &np);
104 offset += np;
106 *npush = offset;
109 int ctdb_req_header_pull(uint8_t *buf, size_t buflen,
110 struct ctdb_req_header *out, size_t *npull)
112 size_t offset = 0, np;
113 int ret;
115 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->length, &np);
116 if (ret != 0) {
117 return ret;
119 offset += np;
121 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->ctdb_magic,
122 &np);
123 if (ret != 0) {
124 return ret;
126 offset += np;
128 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->ctdb_version,
129 &np);
130 if (ret != 0) {
131 return ret;
133 offset += np;
135 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->generation,
136 &np);
137 if (ret != 0) {
138 return ret;
140 offset += np;
142 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->operation,
143 &np);
144 if (ret != 0) {
145 return ret;
147 offset += np;
149 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->destnode, &np);
150 if (ret != 0) {
151 return ret;
153 offset += np;
155 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->srcnode, &np);
156 if (ret != 0) {
157 return ret;
159 offset += np;
161 ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->reqid, &np);
162 if (ret != 0) {
163 return ret;
165 offset += np;
167 *npull = offset;
168 return 0;