diff options
author | David Härdeman <david@hardeman.nu> | 2020-07-01 21:29:06 +0200 |
---|---|---|
committer | David Härdeman <david@hardeman.nu> | 2020-07-01 21:29:06 +0200 |
commit | 7e728d971b95f115c7e9c2def21815034d9bde54 (patch) | |
tree | ef719c2cc2a42ff765b43e888fddc2f5f54ed9d7 /minecctl | |
parent | 2ca520b06a1a80497dd39c98533ae21c165752c2 (diff) |
Reduce some rcon duplication by using a common function
Diffstat (limited to 'minecctl')
-rw-r--r-- | minecctl/rcon-commands.c | 34 |
1 files changed, 12 insertions, 22 deletions
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; |