summaryrefslogtreecommitdiff
path: root/ctdb/protocol/protocol_sock.c
blob: e32f08767e75d406c4e044db378824590362b693 (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
/*
   CTDB generic sock packet marshalling

   Copyright (C) Amitay Isaacs  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.h"
#include "protocol_private.h"
#include "protocol_api.h"

size_t sock_packet_header_len(struct sock_packet_header *in)
{
	return ctdb_uint32_len(&in->length) +
		ctdb_uint32_len(&in->reqid);
}

void sock_packet_header_push(struct sock_packet_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->reqid, buf+offset, &np);
	offset += np;

	*npush = offset;
}

int sock_packet_header_pull(uint8_t *buf, size_t buflen,
			    struct sock_packet_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->reqid, &np);
	if (ret != 0) {
		return ret;
	}
	offset += np;

	*npull = offset;
	return 0;
}

void sock_packet_header_set_reqid(struct sock_packet_header *h,
				  uint32_t reqid)
{
	h->reqid = reqid;
}

void sock_packet_header_set_length(struct sock_packet_header *h,
				   uint32_t length)
{
	h->length = length;
}