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
|
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include "rcon-protocol.h"
static int32_t
read_int(const char **pos, size_t *len)
{
uint32_t val;
const char *p;
if (!pos || !*pos)
return 0;
if (len && *len < RCON_INT_LEN)
return 0;
p = *pos;
val = ((uint8_t)p[0] << 0);
val += ((uint8_t)p[1] << 8);
val += ((uint8_t)p[2] << 16);
val += ((uint8_t)p[3] << 24);
*pos += RCON_INT_LEN;
if (len)
*len -= RCON_INT_LEN;
return (int32_t)val;
}
static void
write_int(char **pos, size_t *len, int32_t orig)
{
uint32_t val = (uint32_t)orig;
char *p;
if (!pos || !*pos)
return;
p = *pos;
p[0] = (val >> 0) & 0xff;
p[1] = (val >> 8) & 0xff;
p[2] = (val >> 16) & 0xff;
p[3] = (val >> 24) & 0xff;
*pos += RCON_INT_LEN;
if (len)
*len += RCON_INT_LEN;
}
static void
write_str(char **pos, size_t *len, const char *str, size_t slen)
{
if (!pos || !*pos || !str || *str == '\0' || slen < 1)
return;
memcpy(*pos, str, slen);
*pos += slen;
if (len)
*len += slen;
}
static void
write_end(char **pos, size_t *len)
{
char *p;
if (!pos || !*pos)
return;
p = *pos;
p[0] = 0x00;
p[1] = 0x00;
*pos += RCON_END_LEN;
if (len)
*len += RCON_END_LEN;
}
bool
rcon_protocol_create_packet(char *buf, size_t len, size_t *rlen, int32_t reqid,
enum rcon_packet_type type, const char *msg)
{
size_t plen, msglen;
char *pos;
if (!buf || !rlen || !msg || msg[0] == '\0')
return false;
msglen = strlen(msg);
if (len < rcon_protocol_packet_len(msglen))
return false;
/* Body */
pos = buf + RCON_HDR_LEN;
plen = RCON_HDR_LEN;
write_int(&pos, &plen, reqid);
write_int(&pos, &plen, type);
write_str(&pos, &plen, msg, msglen);
write_end(&pos, &plen);
/* Header (length of body) */
pos = buf;
write_int(&pos, NULL, plen - RCON_HDR_LEN);
*rlen = plen;
return true;
}
/*
* Returns:
* < 0 = error
* 0 = packet not complete
* > 0 = packet complete (length)
*/
int32_t
rcon_protocol_packet_complete(const char *buf, size_t len)
{
const char *pos;
int32_t plen;
if (!buf)
return -1;
if (len < RCON_PKT_MIN_LEN)
return 0;
pos = buf;
plen = read_int(&pos, NULL) + RCON_HDR_LEN;
if (plen < RCON_HDR_LEN)
/* Read a negative int */
return -1;
if (len < plen)
return 0;
else
return plen;
}
bool
rcon_protocol_read_packet(const char *buf, size_t len, int32_t *id, int32_t *type,
const char **rmsg, const char **error)
{
const char *pos;
int32_t plen;
if (!error)
return false;
if (!buf || !id || !type || !rmsg) {
*error = "invalid arguments";
return false;
}
plen = rcon_protocol_packet_complete(buf, len);
*error = "invalid packet length";
if (plen < RCON_PKT_MIN_LEN)
return false;
pos = buf + RCON_HDR_LEN;
len -= RCON_HDR_LEN;
*id = read_int(&pos, &len);
*type = read_int(&pos, &len);
*rmsg = NULL;
if (len < RCON_END_LEN)
return false;
else if (len > RCON_END_LEN) {
*rmsg = pos;
pos += len - RCON_END_LEN;
len = RCON_END_LEN;
}
if (pos[0] != '\0' || pos[1] != '\0') {
*error = "invalid trailer";
return false;
}
*error = NULL;
return true;
}
|