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
|
/*
* Usurpation – clinet-server protocol implementation.
*
* Copyright (C) 2019 Ramūnas Mažeikis
* Copyright (C) 2019 Gediminas Jakutis
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1
* of the License.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef USURPATION_PROTOCOL_H_INCLUDED
#define USURPATION_PROTOCOL_H_INCLUDED
#include <errno.h>
#include <stdint.h>
#include <stddef.h>
#include <limits.h>
#define TLV_SZ_MAX (MTU - 64)
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/**
* Message sequence number since beggining of sesssion.
*
* Mainly used for identifying lost messages.
*/
typedef unsigned int msg_idx_t;
enum tlv_type {
/**
* Explicitly states that this tlv is no longer valid for reading.
*/
INVALID = 0,
/**
* NULL-terminated string. To be put in a queue to display on the
* screen.
*/
TEXT,
/** Fixed point. 1 decimal digit of precision. */
FPI1,
/** Literally time_t */
TIMESTAMP,
/** Represents a request for lost message. Data is unsigned integer
* that uniquely identifies the message.
*/
REQUEST,
/**
* Response to request. Begins with unsigned integer that represents
* which message is being repeated and the actual null-terminated
* message after that.
*/
REPLY,
/**
* UUID that represents a particular device.
*/
UUID,
/**
* Says "I'm not dead yet.".
*/
HEARTBEAT,
};
struct tlv_header {
int32_t type;
uint32_t size;
};
struct tlv {
struct tlv_header head;
char *data;
};
/* for tlv to "raw" conversion */
struct tlv_packet {
size_t size;
ptrdiff_t cursor;
char *data;
};
/* used as a termninator */
#define tlv_none ((struct tlv) {{INVALID, 0}, NULL})
/**
* Fills tlv structure to represent the next tlv in the packet.
* If saveptr is non-NULL, saves the offset to the next field in saveptr.
* Returns NONEWDATA if once all tlvs were read.
*
* tlv data fields obtained through this function MUST NOT be given
* to free(), as they point to the offsets in the "in" buffer.
*/
int tlv_get(char *in, struct tlv *tlv, char **saveptr);
/* host<->network endianess conversion for the header */
struct tlv_header ntoh_tlvh(const struct tlv_header * const in);
struct tlv_header hton_tlvh(const struct tlv_header * const in);
/**
* Forms (packs up) a raw data packet from the given tlv by appending it to the
* packet. Returns E_TLV_OVERFLOW if pushing data would cause the final size
* (including the terminator tlv) to be greater than TLV_SZ_MAX_RAW. In case of
* such error the data is left untouched.
*/
int tlv_pack(struct tlv_packet *pack, struct tlv *tlv);
/* finalized the packet by placing tlv_none at the end */
int tlv_packet_finalize(struct tlv_packet *pack);
struct tlv *tlv_init(struct tlv *in, enum tlv_type type);
void tlv_destroy(struct tlv *in);
struct tlv_packet *tlv_packet_init(struct tlv_packet *in);
void tlv_packet_destroy(struct tlv_packet *in);
/* resets the packet to form a new one, without deallocating buffers */
struct tlv_packet *tlv_packet_reset(struct tlv_packet *in);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* USURPATION_PROTOCOL_H_INCLUDED */
|