From 1454f8ec3946d36722dc71af91ae8fa7cee0f050 Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Fri, 26 Jun 2020 15:25:52 +0200 Subject: Implement stopall part 2 --- minecctl/minecctl-rcon.c | 65 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/minecctl/minecctl-rcon.c b/minecctl/minecctl-rcon.c index f11a20b..f074f3c 100644 --- a/minecctl/minecctl-rcon.c +++ b/minecctl/minecctl-rcon.c @@ -107,20 +107,28 @@ rcon_login(struct cfg *cfg, struct server *server) char buf[4096]; int32_t rtype; const char *reply; - int fd; + int fd = -1; assert_die(cfg && server, "invalid arguments"); - if (list_empty(&server->rcon_addrs)) - info("server rcon addrs empty"); + if (list_empty(&server->rcon_addrs)) { + error("%s: rcon address unknown", server->shortname); + goto error; + } - fd = connect_any(&server->rcon_addrs, false); + fd = connect_any(&server->rcon_addrs, true); + if (fd < 0) { + error("%s: unable to connect", server->shortname); + goto error; + } if (!server->rcon_password) server->rcon_password = ask_password(); - if (!server->rcon_password) - die("Can't login - no password"); + if (!server->rcon_password) { + error("%s: can't login - password missing", server->shortname); + goto error; + } send_msg(fd, buf, sizeof(buf), RCON_PACKET_LOGIN, server->rcon_password, &rtype, &reply); @@ -132,13 +140,22 @@ rcon_login(struct cfg *cfg, struct server *server) server->rcon_password = NULL; if (rtype == RCON_PACKET_LOGIN_OK) - info("Login ok"); - else if (rtype == RCON_PACKET_LOGIN_FAIL) - die("Login failure, invalid password?"); - else - die("Invalid return code: %" PRIi32, rtype); + verbose("%s: login ok", server->shortname); + else if (rtype == RCON_PACKET_LOGIN_FAIL) { + info("%s: login failure, invalid password?", + server->shortname); + goto error; + } else { + error("%s: invalid return code: %" PRIi32, + server->shortname, rtype); + goto error; + } return fd; + +error: + close(fd); + return -1; } static void @@ -178,16 +195,6 @@ eat_whitespace(char **pos) *end = '\0'; } -static void -get_info(int fd, char *buf, size_t buflen, const char *query, const char **reply) -{ - int32_t rtype; - - send_msg(fd, buf, buflen, RCON_PACKET_COMMAND, query, &rtype, reply); - if (rtype != RCON_PACKET_RESPONSE) - die("Invalid return code: %" PRIi32, rtype); -} - /* midnight = 18000 */ #define MCTIME_OFFSET 6000 #define MCTIME_PER_DAY 24000 @@ -213,10 +220,13 @@ static bool get_one_status(int fd, char *buf, size_t len, const char *cmd, size_t argc, const char *replyscan, const char **reply, ...) { + int32_t rtype; va_list ap; int r; - get_info(fd, buf, len, cmd, reply); + send_msg(fd, buf, buflen, RCON_PACKET_COMMAND, cmd, &rtype, reply); + if (rtype != RCON_PACKET_RESPONSE) + die("Invalid return code: %" PRIi32, rtype); va_start(ap, reply); r = vsscanf(*reply, replyscan, ap); @@ -255,6 +265,8 @@ do_status(struct cfg *cfg) { server = get_default_server(cfg); fd = rcon_login(cfg, server); + if (fd < 0) + return; if (get_one_status(fd, buf, sizeof(buf), "seed", 1, "Seed : [ %[^]]]", &reply, tbuf)) @@ -338,6 +350,8 @@ stop_one_server(struct cfg *cfg, struct server *server) int fd; fd = rcon_login(cfg, server); + if (fd < 0) + return; if (cfg->force_stop) { unsigned current; @@ -384,6 +398,8 @@ do_pcount(struct cfg *cfg) { server = get_default_server(cfg); fd = rcon_login(cfg, server); + if (fd < 0) + return; if (get_player_count(fd, ¤t, &max)) info("Players: %u/%u", current, max); @@ -401,6 +417,8 @@ do_console(struct cfg *cfg) server = get_default_server(cfg); fd = rcon_login(cfg, server); + if (fd < 0) + return; prompt = alloca(strlen(program_invocation_short_name) + STRLEN(" (") + strlen(server->shortname) + STRLEN("): ") + 1); @@ -443,6 +461,9 @@ do_command(struct cfg *cfg) { server = get_default_server(cfg); fd = rcon_login(cfg, server); + if (fd < 0) + return; + send_cmd(fd, cfg->cmdstr); } -- cgit v1.2.3