1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
/*
CTDB protocol marshalling
Copyright (C) Amitay Isaacs 2015-2017
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "replace.h"
#include "system/network.h"
#include <talloc.h>
#include "protocol_basic.h"
/*
* Basic data types
*/
size_t ctdb_uint8_len(uint8_t *in)
{
return sizeof(uint8_t);
}
void ctdb_uint8_push(uint8_t *in, uint8_t *buf, size_t *npush)
{
*buf = *in;
*npush = sizeof(uint8_t);
}
int ctdb_uint8_pull(uint8_t *buf, size_t buflen, uint8_t *out, size_t *npull)
{
if (buflen < sizeof(uint8_t)) {
return EMSGSIZE;
}
*out = *buf;
*npull = sizeof(uint8_t);
return 0;
}
size_t ctdb_uint16_len(uint16_t *in)
{
return sizeof(uint16_t);
}
void ctdb_uint16_push(uint16_t *in, uint8_t *buf, size_t *npush)
{
memcpy(buf, in, sizeof(uint16_t));
*npush = sizeof(uint16_t);
}
int ctdb_uint16_pull(uint8_t *buf, size_t buflen, uint16_t *out, size_t *npull)
{
if (buflen < sizeof(uint16_t)) {
return EMSGSIZE;
}
memcpy(out, buf, sizeof(uint16_t));
*npull = sizeof(uint16_t);
return 0;
}
size_t ctdb_int32_len(int32_t *in)
{
return sizeof(int32_t);
}
void ctdb_int32_push(int32_t *in, uint8_t *buf, size_t *npush)
{
memcpy(buf, in, sizeof(int32_t));
*npush = sizeof(int32_t);
}
int ctdb_int32_pull(uint8_t *buf, size_t buflen, int32_t *out, size_t *npull)
{
if (buflen < sizeof(int32_t)) {
return EMSGSIZE;
}
memcpy(out, buf, sizeof(int32_t));
*npull = sizeof(int32_t);
return 0;
}
size_t ctdb_uint32_len(uint32_t *in)
{
return sizeof(uint32_t);
}
void ctdb_uint32_push(uint32_t *in, uint8_t *buf, size_t *npush)
{
memcpy(buf, in, sizeof(uint32_t));
*npush = sizeof(uint32_t);
}
int ctdb_uint32_pull(uint8_t *buf, size_t buflen, uint32_t *out, size_t *npull)
{
if (buflen < sizeof(uint32_t)) {
return EMSGSIZE;
}
memcpy(out, buf, sizeof(uint32_t));
*npull = sizeof(uint32_t);
return 0;
}
size_t ctdb_uint64_len(uint64_t *in)
{
return sizeof(uint64_t);
}
void ctdb_uint64_push(uint64_t *in, uint8_t *buf, size_t *npush)
{
memcpy(buf, in, sizeof(uint64_t));
*npush = sizeof(uint64_t);
}
int ctdb_uint64_pull(uint8_t *buf, size_t buflen, uint64_t *out, size_t *npull)
{
if (buflen < sizeof(uint64_t)) {
return EMSGSIZE;
}
memcpy(out, buf, sizeof(uint64_t));
*npull = sizeof(uint64_t);
return 0;
}
size_t ctdb_double_len(double *in)
{
return sizeof(double);
}
void ctdb_double_push(double *in, uint8_t *buf, size_t *npush)
{
memcpy(buf, in, sizeof(double));
*npush = sizeof(double);
}
int ctdb_double_pull(uint8_t *buf, size_t buflen, double *out, size_t *npull)
{
if (buflen < sizeof(double)) {
return EMSGSIZE;
}
memcpy(out, buf, sizeof(double));
*npull = sizeof(double);
return 0;
}
size_t ctdb_bool_len(bool *in)
{
uint8_t u8 = 0;
return ctdb_uint8_len(&u8);
}
void ctdb_bool_push(bool *in, uint8_t *buf, size_t *npush)
{
size_t np;
uint8_t u8 = *in;
ctdb_uint8_push(&u8, buf, &np);
*npush = np;
}
int ctdb_bool_pull(uint8_t *buf, size_t buflen, bool *out, size_t *npull)
{
size_t np;
uint8_t u8;
int ret;
ret = ctdb_uint8_pull(buf, buflen, &u8, &np);
if (ret != 0) {
return ret;
}
if (u8 == 0) {
*out = false;
} else if (u8 == 1) {
*out = true;
} else {
return EINVAL;
}
*npull = np;
return 0;
}
size_t ctdb_chararray_len(char *in, size_t len)
{
return len;
}
void ctdb_chararray_push(char *in, size_t len, uint8_t *buf, size_t *npush)
{
memcpy(buf, in, len);
*npush = len;
}
int ctdb_chararray_pull(uint8_t *buf, size_t buflen, char *out, size_t len,
size_t *npull)
{
if (buflen < len) {
return EMSGSIZE;
}
memcpy(out, buf, len);
out[len-1] = '\0';
*npull = len;
return 0;
}
size_t ctdb_string_len(const char **in)
{
if (*in == NULL) {
return 0;
}
return strlen(*in) + 1;
}
void ctdb_string_push(const char **in, uint8_t *buf, size_t *npush)
{
size_t len;
len = ctdb_string_len(in);
if (len > 0) {
memcpy(buf, *in, len);
}
*npush = len;
}
int ctdb_string_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
const char **out, size_t *npull)
{
const char *str;
if (buflen > UINT32_MAX) {
return EMSGSIZE;
}
if (buflen == 0) {
*out = NULL;
*npull = 0;
return 0;
}
str = talloc_strndup(mem_ctx, (char *)buf, buflen);
if (str == NULL) {
return ENOMEM;
}
*out = str;
*npull = ctdb_string_len(&str);
return 0;
}
size_t ctdb_stringn_len(const char **in)
{
uint32_t u32 = ctdb_string_len(in);
return ctdb_uint32_len(&u32) + u32;
}
void ctdb_stringn_push(const char **in, uint8_t *buf, size_t *npush)
{
size_t offset = 0, np;
uint32_t u32 = ctdb_string_len(in);
ctdb_uint32_push(&u32, buf+offset, &np);
offset += np;
ctdb_string_push(in, buf+offset, &np);
offset += np;
*npush = offset;
}
int ctdb_stringn_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
const char **out, size_t *npull)
{
size_t offset = 0, np;
uint32_t u32;
int ret;
ret = ctdb_uint32_pull(buf+offset, buflen-offset, &u32, &np);
if (ret != 0) {
return ret;
}
offset += np;
if (buflen-offset < u32) {
return EMSGSIZE;
}
ret = ctdb_string_pull(buf+offset, u32, mem_ctx, out, &np);
if (ret != 0) {
return ret;
}
offset += np;
*npull = offset;
return 0;
}
/*
* System defined data types
*/
size_t ctdb_pid_len(pid_t *in)
{
return sizeof(pid_t);
}
void ctdb_pid_push(pid_t *in, uint8_t *buf, size_t *npush)
{
memcpy(buf, in, sizeof(pid_t));
*npush = sizeof(pid_t);
}
int ctdb_pid_pull(uint8_t *buf, size_t buflen, pid_t *out, size_t *npull)
{
if (buflen < sizeof(pid_t)) {
return EMSGSIZE;
}
memcpy(out, buf, sizeof(pid_t));
*npull = sizeof(pid_t);
return 0;
}
size_t ctdb_timeval_len(struct timeval *in)
{
return sizeof(struct timeval);
}
void ctdb_timeval_push(struct timeval *in, uint8_t *buf, size_t *npush)
{
memcpy(buf, in, sizeof(struct timeval));
*npush = sizeof(struct timeval);
}
int ctdb_timeval_pull(uint8_t *buf, size_t buflen, struct timeval *out,
size_t *npull)
{
if (buflen < sizeof(struct timeval)) {
return EMSGSIZE;
}
memcpy(out, buf, sizeof(struct timeval));
*npull = sizeof(struct timeval);
return 0;
}
/*
* Dummy type to tackle structure padding
*/
size_t ctdb_padding_len(int count)
{
return count % SIZEOF_VOID_P;
}
void ctdb_padding_push(int count, uint8_t *buf, size_t *npush)
{
uint8_t padding[count];
size_t aligned_count = count % SIZEOF_VOID_P;
if (aligned_count > 0) {
memset(padding, 0, aligned_count);
memcpy(buf, padding, aligned_count);
}
*npush = aligned_count;
}
int ctdb_padding_pull(uint8_t *buf, size_t buflen, int count, size_t *npull)
{
size_t aligned_count = count % SIZEOF_VOID_P;
if (buflen < aligned_count) {
return EMSGSIZE;
}
*npull = aligned_count;
return 0;
}
|