summaryrefslogtreecommitdiff
path: root/rcon.c
blob: dc29364f32f7dab92fbe758568580183213b3859 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <inttypes.h>
#include <errno.h>

#include "main.h"
#include "uring.h"
#include "server.h"
#include "rcon.h"

struct rcon {
	struct server *server;
	struct connection conn;
	struct uring_task task;
	struct uring_task_buf tbuf;
};

static void
rcon_free(struct uring_task *task)
{
	struct rcon *rcon = container_of(task, struct rcon, task);

	assert_return(task);

	debug(DBG_RCON, "task %p, idle %p", task, rcon);
	rcon->server->rcon = NULL;
	xfree(rcon);
}

void
rcon_refdump(struct rcon *rcon)
{
	assert_return_silent(rcon);

	uring_task_refdump(&rcon->task);
}

void
rcon_delete(struct cfg *cfg, struct server *server)
{
	assert_return_silent(server->rcon);

	debug(DBG_RCON, "closing fd %i", server->rcon->task.fd);
	uring_task_destroy(cfg, &server->rcon->task);
	server->rcon = NULL;
}

static int32_t
read_int(char **pos, size_t *len)
{
	uint32_t val;
	char *p;

	assert_return(pos && *pos, 0);

	if (len && *len < 4)
		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 += 4;
	if (len)
		*len -= 4;

	return (int32_t)val;
}

static void
write_int(char **pos, size_t *len, int32_t orig)
{
	uint32_t val = (uint32_t)orig;
	char *p;

	assert_return(pos && *pos);

	p = *pos;
	p[0] = (val >>  0) & 0xff;
	p[1] = (val >>  8) & 0xff;
	p[2] = (val >> 16) & 0xff;
	p[3] = (val >> 24) & 0xff;

	*pos += 4;
	if (len)
		*len += 4;
}

static void
write_str(char **pos, size_t *len, const char *str)
{
	size_t towrite;

	assert_return(pos && *pos && !empty_str(str));

	towrite = strlen(str);
	memcpy(*pos, str, towrite);
	*pos += towrite;
	if (len)
		*len += towrite;
}

static void
write_end(char **pos, size_t *len)
{
	char *p;

	assert_return(pos && *pos);

	p = *pos;
	p[0] = 0x00;
	p[1] = 0x00;

	*pos += 2;
	if (len)
		*len += 2;
}

enum rcon_packet_type {
	RCON_PACKET_LOGIN	= 3,
	RCON_PACKET_LOGIN_OK	= 2,
	RCON_PACKET_LOGIN_FAIL	= -1,
	RCON_PACKET_COMMAND	= 2,
	RCON_PACKET_RESPONSE	= 0,
};

static void
create_packet(struct cfg *cfg, struct rcon *rcon, int32_t reqid,
	      enum rcon_packet_type type, const char *msg)
{
	char *pos;

	assert_return(cfg && rcon && !empty_str(msg));

	/* Body */
	pos = &rcon->tbuf.buf[4];
	rcon->tbuf.len = 4;
	write_int(&pos, &rcon->tbuf.len, reqid);
	write_int(&pos, &rcon->tbuf.len, type);
	write_str(&pos, &rcon->tbuf.len, msg);
	write_end(&pos, &rcon->tbuf.len);

	/* Header (length of body) */
	pos = &rcon->tbuf.buf[0];
	write_int(&pos, NULL, rcon->tbuf.len - 4);

	debug(DBG_RCON, "created packet (reqid: %" PRIi32 ", type %" PRIi32 
	      ", len %zu, payload: %s)",
	      reqid, type, rcon->tbuf.len, msg);
}

static int
packet_complete(struct cfg *cfg, struct uring_task *task, int res)
{
	char *pos;
	size_t len;
	int32_t plen;

	assert_return(cfg && task, 0);
	assert_task_alive_or(DBG_RCON, task, return -EINTR);

	pos = task->tbuf->buf;
	len = task->tbuf->len;
	if (task->tbuf->len < 14)
		return 0;

	plen = read_int(&pos, &len);
	debug(DBG_RCON, "reply size: %zu bytes, packet size %" PRIi32,
	      task->tbuf->len, plen + 4);

	if (task->tbuf->len < plen + 4)
		return 0;
	else
		return 1;
}

static bool
rcon_read_packet(struct cfg *cfg, struct rcon *rcon, int32_t *id,
		 int32_t *type, char **rmsg)
{
	char *pos;
	size_t len;
	int32_t plen;

	assert_return(cfg && rcon && id && type && rmsg, false);

	pos = rcon->tbuf.buf;
	len = rcon->tbuf.len;
	plen = read_int(&pos, &len);
	*id = read_int(&pos, &len);
	*type = read_int(&pos, &len);
	*rmsg = NULL;

	if (plen < 10) {
		error("invalid packet length: %" PRIi32, plen);
		return false;
	}

	if (len > 2) {
		*rmsg = pos;
		pos += len - 2;
		len = 2;
	}

	if (len < 2) {
		error("short message");
		return false;
	}

	if (pos[0] != '\0' || pos[1] != '\0') {
		error("invalid trailer");
		return false;
	}

	debug(DBG_RCON, "response - len: %" PRIi32 ", id: %" PRIi32
	      ", type: %" PRIi32 ", msg: %s",
	      plen, *id, *type, *rmsg);

	return true;
}

static void
rcon_stop_reply(struct cfg *cfg, struct uring_task *task, int res)
{
	struct rcon *rcon = container_of(task, struct rcon, task);
	int32_t id;
	int32_t type;
	char *msg;

	assert_return(cfg && task);
	assert_task_alive(DBG_RCON, task);

	if (res < 0) {
		debug(DBG_RCON, "res: %i", res);
		goto out;
	}

	debug(DBG_RCON, "packet complete");
	rcon_read_packet(cfg, rcon, &id, &type, &msg);

	if (id != 2) {
		error("rcon stop failed - reply id (%" PRIi32 ")", id);
		goto out;
	} else if (type != RCON_PACKET_RESPONSE) {
		error("rcon stop failed - reply type (%" PRIi32 ")", type);
		goto out;
	}

	verbose("rcon stop successful (%s)", msg);

out:
	uring_task_put(cfg, &rcon->task);
}

static void
rcon_stop_sent(struct cfg *cfg, struct uring_task *task, int res)
{
	struct rcon *rcon = container_of(task, struct rcon, task);

	assert_return(cfg && task);
	assert_task_alive(DBG_RCON, task);

	if (res < 0) {
		debug(DBG_RCON, "res: %i", res);
		uring_task_put(cfg, &rcon->task);
		return;
	}

	debug(DBG_RCON, "stop cmd sent");
	uring_tbuf_read_until(cfg, &rcon->task, packet_complete, rcon_stop_reply);
}

static void
rcon_login_reply(struct cfg *cfg, struct uring_task *task, int res)
{
	struct rcon *rcon = container_of(task, struct rcon, task);
	int32_t id;
	int32_t type;
	char *msg;

	assert_return(cfg && task);
	assert_task_alive(DBG_RCON, task);

	if (res < 0) {
		debug(DBG_RCON, "res: %i", res);
		goto error;
	}

	debug(DBG_RCON, "packet complete");
	rcon_read_packet(cfg, rcon, &id, &type, &msg);

	if (id != 1) {
		error("rcon login failed - unexpected reply id (%" PRIi32 ")", id);
		goto error;
	} else if (type == RCON_PACKET_LOGIN_FAIL) {
		error("rcon login failed - incorrect password");
		goto error;
	} else if (type != RCON_PACKET_LOGIN_OK) {
		error("rcon login failed - unexpected reply type (%" PRIi32 ")", type);
		goto error;
	}

	debug(DBG_RCON, "rcon login successful");
	create_packet(cfg, rcon, 2, RCON_PACKET_COMMAND, "stop");
	uring_tbuf_write(cfg, &rcon->task, rcon_stop_sent);
	return;

error:
	uring_task_put(cfg, &rcon->task);
}

static void
rcon_login_sent(struct cfg *cfg, struct uring_task *task, int res)
{
	struct rcon *rcon = container_of(task, struct rcon, task);

	assert_return(cfg && task);
	assert_task_alive(DBG_RCON, task);

	if (res < 0) {
		debug(DBG_RCON, "res: %i", res);
		uring_task_put(cfg, &rcon->task);
		return;
	}

	debug(DBG_RCON, "login sent");
	uring_tbuf_read_until(cfg, &rcon->task, packet_complete, rcon_login_reply);
}

static void
rcon_connected_cb(struct cfg *cfg, struct connection *conn, bool connected)
{
	struct rcon *rcon = container_of(conn, struct rcon, conn);

	assert_return(cfg && conn);
	assert_task_alive(DBG_RCON, &rcon->task);

	if (!connected) {
		error("rcon connection to remote server (%s) failed",
		      rcon->server->name);
		uring_task_put(cfg, &rcon->task);
		return;
	}

	create_packet(cfg, rcon, 1, RCON_PACKET_LOGIN, rcon->server->rcon_password);
	uring_tbuf_write(cfg, &rcon->task, rcon_login_sent);
}

void
rcon_init(struct cfg *cfg, struct server *server)
{
	struct rcon *rcon;

	assert_return(cfg && server && !list_empty(&server->rcons) && !empty_str(server->rcon_password));

	rcon = zmalloc(sizeof(*rcon));
	if (!rcon)
		die("malloc: %m");

	uring_task_init(cfg, &rcon->task, "rcon", &server->task, rcon_free);
	uring_task_set_buf(&rcon->task, &rcon->tbuf);
	rcon->server = server;
	server->rcon = rcon;

	connect_any(cfg, &rcon->task, &server->rcons, &rcon->conn, rcon_connected_cb);
}