summaryrefslogtreecommitdiff
path: root/ctdb/protocol/protocol_header.c
blob: a6be7f515477f711ad32e3f7270e5d80920eacf0 (plain)
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
/*
   CTDB protocol marshalling

   Copyright (C) Amitay Isaacs  2015

   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 <tdb.h>

#include "protocol.h"
#include "protocol_api.h"
#include "protocol_private.h"

int ctdb_req_header_verify(struct ctdb_req_header *h, uint32_t operation)
{
	if (h->length < sizeof(struct ctdb_req_header)) {
		return EMSGSIZE;
	}

	if (h->ctdb_magic != CTDB_MAGIC) {
		return EPROTO;
	}

	if (h->ctdb_version != CTDB_PROTOCOL) {
		return EPROTO;
	}

	if (operation != 0 && h->operation != operation) {
		return EPROTO;
	}

	return 0;
}

void ctdb_req_header_fill(struct ctdb_req_header *h, uint32_t generation,
			  uint32_t operation, uint32_t destnode,
			  uint32_t srcnode, uint32_t reqid)
{
	h->length = sizeof(struct ctdb_req_header);
	h->ctdb_magic = CTDB_MAGIC;
	h->ctdb_version = CTDB_PROTOCOL;
	h->generation = generation;
	h->operation = operation;
	h->destnode = destnode;
	h->srcnode = srcnode;
	h->reqid = reqid;
}

size_t ctdb_req_header_len(struct ctdb_req_header *in)
{
	return ctdb_uint32_len(&in->length) +
		ctdb_uint32_len(&in->ctdb_magic) +
		ctdb_uint32_len(&in->ctdb_version) +
		ctdb_uint32_len(&in->generation) +
		ctdb_uint32_len(&in->operation) +
		ctdb_uint32_len(&in->destnode) +
		ctdb_uint32_len(&in->srcnode) +
		ctdb_uint32_len(&in->reqid);
}

void ctdb_req_header_push(struct ctdb_req_header *in, uint8_t *buf,
			  size_t *npush)
{
	size_t offset = 0, np;

	ctdb_uint32_push(&in->length, buf+offset, &np);
	offset += np;

	ctdb_uint32_push(&in->ctdb_magic, buf+offset, &np);
	offset += np;

	ctdb_uint32_push(&in->ctdb_version, buf+offset, &np);
	offset += np;

	ctdb_uint32_push(&in->generation, buf+offset, &np);
	offset += np;

	ctdb_uint32_push(&in->operation, buf+offset, &np);
	offset += np;

	ctdb_uint32_push(&in->destnode, buf+offset, &np);
	offset += np;

	ctdb_uint32_push(&in->srcnode, buf+offset, &np);
	offset += np;

	ctdb_uint32_push(&in->reqid, buf+offset, &np);
	offset += np;

	*npush = offset;
}

int ctdb_req_header_pull(uint8_t *buf, size_t buflen,
			 struct ctdb_req_header *out, size_t *npull)
{
	size_t offset = 0, np;
	int ret;

	ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->length, &np);
	if (ret != 0) {
		return ret;
	}
	offset += np;

	ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->ctdb_magic,
			       &np);
	if (ret != 0) {
		return ret;
	}
	offset += np;

	ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->ctdb_version,
			       &np);
	if (ret != 0) {
		return ret;
	}
	offset += np;

	ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->generation,
			       &np);
	if (ret != 0) {
		return ret;
	}
	offset += np;

	ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->operation,
			       &np);
	if (ret != 0) {
		return ret;
	}
	offset += np;

	ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->destnode, &np);
	if (ret != 0) {
		return ret;
	}
	offset += np;

	ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->srcnode, &np);
	if (ret != 0) {
		return ret;
	}
	offset += np;

	ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->reqid, &np);
	if (ret != 0) {
		return ret;
	}
	offset += np;

	*npull = offset;
	return 0;
}