ctdb-build: Split protocol-util as a separate subsystem
[vlendec/samba-autobuild/.git] / ctdb / protocol / protocol_header.c
1 /*
2    CTDB protocol marshalling
3
4    Copyright (C) Amitay Isaacs  2015
5
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.
10
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.
15
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/>.
18 */
19
20 #include "replace.h"
21 #include "system/network.h"
22
23 #include <talloc.h>
24 #include <tdb.h>
25
26 #include "protocol.h"
27 #include "protocol_api.h"
28 #include "protocol_private.h"
29
30 int ctdb_req_header_verify(struct ctdb_req_header *h, uint32_t operation)
31 {
32         if (h->length < sizeof(struct ctdb_req_header)) {
33                 return EMSGSIZE;
34         }
35
36         if (h->ctdb_magic != CTDB_MAGIC) {
37                 return EPROTO;
38         }
39
40         if (h->ctdb_version != CTDB_PROTOCOL) {
41                 return EPROTO;
42         }
43
44         if (operation != 0 && h->operation != operation) {
45                 return EPROTO;
46         }
47
48         return 0;
49 }
50
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)
54 {
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;
63 }
64
65 size_t ctdb_req_header_len(struct ctdb_req_header *in)
66 {
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);
75 }
76
77 void ctdb_req_header_push(struct ctdb_req_header *in, uint8_t *buf,
78                           size_t *npush)
79 {
80         size_t offset = 0, np;
81
82         ctdb_uint32_push(&in->length, buf+offset, &np);
83         offset += np;
84
85         ctdb_uint32_push(&in->ctdb_magic, buf+offset, &np);
86         offset += np;
87
88         ctdb_uint32_push(&in->ctdb_version, buf+offset, &np);
89         offset += np;
90
91         ctdb_uint32_push(&in->generation, buf+offset, &np);
92         offset += np;
93
94         ctdb_uint32_push(&in->operation, buf+offset, &np);
95         offset += np;
96
97         ctdb_uint32_push(&in->destnode, buf+offset, &np);
98         offset += np;
99
100         ctdb_uint32_push(&in->srcnode, buf+offset, &np);
101         offset += np;
102
103         ctdb_uint32_push(&in->reqid, buf+offset, &np);
104         offset += np;
105
106         *npush = offset;
107 }
108
109 int ctdb_req_header_pull(uint8_t *buf, size_t buflen,
110                          struct ctdb_req_header *out, size_t *npull)
111 {
112         size_t offset = 0, np;
113         int ret;
114
115         ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->length, &np);
116         if (ret != 0) {
117                 return ret;
118         }
119         offset += np;
120
121         ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->ctdb_magic,
122                                &np);
123         if (ret != 0) {
124                 return ret;
125         }
126         offset += np;
127
128         ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->ctdb_version,
129                                &np);
130         if (ret != 0) {
131                 return ret;
132         }
133         offset += np;
134
135         ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->generation,
136                                &np);
137         if (ret != 0) {
138                 return ret;
139         }
140         offset += np;
141
142         ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->operation,
143                                &np);
144         if (ret != 0) {
145                 return ret;
146         }
147         offset += np;
148
149         ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->destnode, &np);
150         if (ret != 0) {
151                 return ret;
152         }
153         offset += np;
154
155         ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->srcnode, &np);
156         if (ret != 0) {
157                 return ret;
158         }
159         offset += np;
160
161         ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->reqid, &np);
162         if (ret != 0) {
163                 return ret;
164         }
165         offset += np;
166
167         *npull = offset;
168         return 0;
169 }