From 7e728d971b95f115c7e9c2def21815034d9bde54 Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Wed, 1 Jul 2020 21:29:06 +0200 Subject: Reduce some rcon duplication by using a common function --- minecctl/rcon-commands.c | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) (limited to 'minecctl/rcon-commands.c') diff --git a/minecctl/rcon-commands.c b/minecctl/rcon-commands.c index dfe8647..be18bba 100644 --- a/minecctl/rcon-commands.c +++ b/minecctl/rcon-commands.c @@ -69,10 +69,11 @@ static void read_packet(int sfd, char *buf, size_t len, int32_t *id, } static void send_msg(int sfd, char *buf, size_t len, enum rcon_packet_type type, - const char *msg, enum rcon_packet_type *rtype, + const char *msg, int32_t *rtype, const char **reply) { static uint32_t rcon_packet_id = 1; + const char *error; size_t plen; int32_t id; @@ -84,25 +85,17 @@ static void send_msg(int sfd, char *buf, size_t len, enum rcon_packet_type type, read_packet(sfd, buf, len, &id, rtype, reply); - /* FIXME: this should be shared */ - if (type == RCON_PACKET_LOGIN) { - if (*rtype != RCON_PACKET_LOGIN_OK) - die("Invalid reply id"); - - if (id == RCON_PACKET_LOGIN_FAIL) - *rtype = RCON_PACKET_LOGIN_FAIL; - else if (id != rcon_packet_id) - die("Invalid reply id"); - } else { - if (id != rcon_packet_id) - die("Invalid reply"); - } + /* FIXME: Shouldn't die */ + if (!rcon_protocol_verify_response(rcon_packet_id, id, type, + *rtype, &error)) + die("Invalid response: %s", error); rcon_packet_id++; } static int rcon_login(struct cfg *cfg, struct server *server) { + const char *error; char buf[4096]; int32_t rtype; const char *reply; @@ -135,15 +128,12 @@ static int rcon_login(struct cfg *cfg, struct server *server) explicit_bzero(buf, sizeof(buf)); free_password(&server->scfg.rcon_password); - if (rtype == RCON_PACKET_LOGIN_OK) - verbose("%s: login ok", server->name); - else if (rtype == RCON_PACKET_LOGIN_FAIL) { - info("%s: login failure, invalid password?", server->name); + if (!rcon_protocol_verify_response(1, 1, RCON_PACKET_LOGIN, + rtype, &error)) { + error("%s: invalid response - %s", server->name, error); goto error; - } else { - error("%s: invalid return code: %" PRIi32, server->name, rtype); - goto error; - } + } else + verbose("%s: login ok", server->name); return fd; -- cgit v1.2.3