1 /*
2    CTDB protocol marshalling
3 
4    Copyright (C) Amitay Isaacs  2017
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 
ctdb_req_keepalive_len(struct ctdb_req_header * h,struct ctdb_req_keepalive * c)30 size_t ctdb_req_keepalive_len(struct ctdb_req_header *h,
31 			      struct ctdb_req_keepalive *c)
32 {
33 	return ctdb_req_header_len(h) +
34 		ctdb_uint32_len(&c->version) +
35 		ctdb_uint32_len(&c->uptime);
36 }
37 
ctdb_req_keepalive_push(struct ctdb_req_header * h,struct ctdb_req_keepalive * c,uint8_t * buf,size_t * buflen)38 int ctdb_req_keepalive_push(struct ctdb_req_header *h,
39 			    struct ctdb_req_keepalive *c,
40 			    uint8_t *buf, size_t *buflen)
41 {
42 	size_t length, offset = 0, np;
43 
44 	length = ctdb_req_keepalive_len(h, c);
45 	if (*buflen < length) {
46 		*buflen = length;
47 		return EMSGSIZE;
48 	}
49 
50 	h->length = *buflen;
51 	ctdb_req_header_push(h, buf+offset, &np);
52 	offset += np;
53 
54 	ctdb_uint32_push(&c->version, buf+offset, &np);
55 	offset += np;
56 
57 	ctdb_uint32_push(&c->uptime, buf+offset, &np);
58 	offset += np;
59 
60 	return 0;
61 }
62 
ctdb_req_keepalive_pull(uint8_t * buf,size_t buflen,struct ctdb_req_header * h,TALLOC_CTX * mem_ctx,struct ctdb_req_keepalive * c)63 int ctdb_req_keepalive_pull(uint8_t *buf, size_t buflen,
64 			    struct ctdb_req_header *h,
65 			    TALLOC_CTX *mem_ctx,
66 			    struct ctdb_req_keepalive *c)
67 {
68 	struct ctdb_req_header header;
69 	size_t offset = 0, np;
70 	int ret;
71 
72 	ret = ctdb_req_header_pull(buf, buflen, &header, &np);
73 	if (ret != 0) {
74 		return ret;
75 	}
76 	offset += np;
77 
78 	if (h != NULL) {
79 		*h = header;
80 	}
81 
82 	ret = ctdb_uint32_pull(buf+offset, buflen-offset, &c->version, &np);
83 	if (ret != 0) {
84 		return ret;
85 	}
86 	offset += np;
87 
88 	ret = ctdb_uint32_pull(buf+offset, buflen-offset, &c->uptime, &np);
89 	if (ret != 0) {
90 		return ret;
91 	}
92 	offset += np;
93 
94 	return 0;
95 }
96