summaryrefslogtreecommitdiff
path: root/minecproxy/server-rcon.c
blob: 81a526cf8438177d15662c5448dcec31d6d68fe0 (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
/* SPDX-License-Identifier: GPL-2.0 */
#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 "minecproxy.h"
#include "uring.h"
#include "server.h"
#include "server-rcon.h"
#include "shared/rcon-protocol.h"

static int rcon_packet_complete(struct uring_task *task, _unused_ int res)
{
	assert_return(task, -EINVAL);
	assert_task_alive_or(DBG_RCON, task, return -EINTR);

	return rcon_protocol_packet_complete(task->tbuf->buf, task->tbuf->len);
}

static bool rcon_check_reply(struct uring_task *task, int res, int32_t *id,
			     int32_t *type, const char **msg)
{
	struct server *server = container_of(task, struct server, rcon_task);
	const char *error;

	if (res < 0) {
		error("rcon(%s): reading reply failed, res: %i",
		      server->scfg.name, res);
		goto error;
	}

	if (!rcon_protocol_read_packet(task->tbuf->buf, task->tbuf->len, id,
				       type, msg, &error)) {
		error("rcon(%s): failed to parse packet: %s", server->scfg.name,
		      error);
		goto error;
	}

	return true;

error:
	uring_task_close_fd(task);
	return false;
}

static void rcon_stop_reply(struct uring_task *task, int res)
{
	struct server *server = container_of(task, struct server, rcon_task);
	int32_t id;
	int32_t type;
	const char *msg, *error;

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

	if (!rcon_check_reply(task, res, &id, &type, &msg))
		return;

	if (!rcon_protocol_verify_response(2, id, RCON_PACKET_COMMAND, type,
					   &error))
		error("rcon(%s): stop cmd failed - %s", server->scfg.name, error);
	else
		verbose("rcon(%s): stop command sent, reply: %s",
			server->scfg.name, msg);

	uring_task_close_fd(task);
}

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

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

	if (res != task->tbuf->len) {
		error("rcon(%s): sending stop cmd failed, res: %i",
		      server->scfg.name, res);
		uring_task_close_fd(task);
		return;
	}

	debug(DBG_RCON, "rcon(%s): stop cmd sent", server->scfg.name);
	uring_tbuf_read_until(task, rcon_packet_complete, rcon_stop_reply);
}

static void rcon_login_reply(struct uring_task *task, int res)
{
	struct server *server = container_of(task, struct server, rcon_task);
	int32_t id;
	int32_t type;
	const char *msg, *error;

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

	if (!rcon_check_reply(task, res, &id, &type, &msg))
		return;

	if (!rcon_protocol_verify_response(1, id, RCON_PACKET_LOGIN, type,
					   &error)) {
		error("rcon(%s): login failed - %s", server->scfg.name, error);
		goto error;
	}

	debug(DBG_RCON, "rcon(%s): login successful", server->scfg.name);
	rcon_protocol_create_packet(task->tbuf->buf, sizeof(task->tbuf->buf),
				    &task->tbuf->len, 2, RCON_PACKET_COMMAND,
				    "stop");
	uring_tbuf_write(task, rcon_stop_sent);
	return;

error:
	uring_task_close_fd(task);
}

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

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

	if (res != task->tbuf->len) {
		error("rcon(%s): sending login failed, res: %i", server->scfg.name,
		      res);
		uring_task_close_fd(task);
		return;
	}

	debug(DBG_RCON, "rcon(%s): login sent", server->scfg.name);
	uring_tbuf_read_until(task, rcon_packet_complete, rcon_login_reply);
}

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

	assert_return(conn);
	assert_task_alive(DBG_RCON, &server->rcon_task);

	if (!connected) {
		error("rcon (%s): connection failed", server->scfg.name);
		return;
	}

	rcon_protocol_create_packet(server->rcon_tbuf.buf,
				    sizeof(server->rcon_tbuf.buf),
				    &server->rcon_tbuf.len, 1,
				    RCON_PACKET_LOGIN,
				    server->scfg.rcon_password);
	uring_tbuf_write(&server->rcon_task, rcon_login_sent);
}

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

	assert_return(task);

	debug(DBG_RCON, "task %p, server %s (%p)", task, server->scfg.name, server);
}

void rcon_stop(struct server *server)
{
	assert_return(server && !list_empty(&server->scfg.rcons) &&
		      !empty_str(server->scfg.rcon_password));
	assert_task_alive(DBG_RCON, &server->rcon_task);

	connect_any(&server->rcon_task, &server->scfg.rcons, &server->rcon_conn,
		    rcon_connected_cb);
}

void rcon_refdump(struct server *server)
{
	assert_return(server);

	uring_task_refdump(&server->rcon_task);
}

void rcon_delete(struct server *server)
{
	assert_return(server);

	debug(DBG_RCON, "closing fd %i", server->rcon_task.fd);
	uring_task_destroy(&server->rcon_task);
}

void rcon_init(struct server *server)
{
	assert_return(server);

	uring_task_init(&server->rcon_task, "rcon", &server->task, rcon_free);
	uring_task_set_buf(&server->rcon_task, &server->rcon_tbuf);
}