summaryrefslogtreecommitdiff
path: root/minecctl/minecctl-rcon.c
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-25 19:14:50 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-25 19:14:50 +0200
commita71d847b0a752375e8fa2c82c634a800bede2f9b (patch)
tree3b31c7f6eac81ec320e56fba03cf7aebc3d44356 /minecctl/minecctl-rcon.c
parent28606456dd6b110326c3027ea078b0409afeba3b (diff)
Only check for password if/when required
Diffstat (limited to 'minecctl/minecctl-rcon.c')
-rw-r--r--minecctl/minecctl-rcon.c84
1 files changed, 52 insertions, 32 deletions
diff --git a/minecctl/minecctl-rcon.c b/minecctl/minecctl-rcon.c
index f5a9bb5..6ee8b50 100644
--- a/minecctl/minecctl-rcon.c
+++ b/minecctl/minecctl-rcon.c
@@ -100,16 +100,25 @@ send_msg(int sfd, char *buf, size_t len, enum rcon_packet_type type,
rcon_packet_id++;
}
-static void
-send_login(struct cfg *cfg)
+static int
+rcon_login(struct cfg *cfg)
{
char buf[4096];
int32_t rtype;
const char *reply;
+ int fd;
+
+ assert_die(cfg, "invalid arguments");
+
+ if (!cfg->password)
+ cfg->password = ask_password();
- assert_die(cfg && cfg->fd >= 0 && cfg->password, "invalid arguments");
+ if (!cfg->password)
+ die("Can't login - no password");
- send_msg(cfg->fd, buf, sizeof(buf), RCON_PACKET_LOGIN, cfg->password,
+ fd = connect_any(&cfg->addrs, false);
+
+ send_msg(fd, buf, sizeof(buf), RCON_PACKET_LOGIN, cfg->password,
&rtype, &reply);
/* An rcon password isn't exactly super-secret, but can't hurt */
@@ -124,6 +133,8 @@ send_login(struct cfg *cfg)
die("Login failure, invalid password?");
else
die("Invalid return code: %" PRIi32, rtype);
+
+ return fd;
}
static void
@@ -195,13 +206,13 @@ mctime_mm(unsigned mctime) {
}
static bool
-get_one_status(struct cfg *cfg, char *buf, size_t len, const char *cmd,
+get_one_status(int fd, char *buf, size_t len, const char *cmd,
size_t argc, const char *replyscan, const char **reply, ...)
{
va_list ap;
int r;
- get_info(cfg->fd, buf, len, cmd, reply);
+ get_info(fd, buf, len, cmd, reply);
va_start(ap, reply);
r = vsscanf(*reply, replyscan, ap);
@@ -221,53 +232,56 @@ do_status(struct cfg *cfg) {
unsigned cplayers, maxplayers, gtime;
unsigned epacks, apacks;
unsigned bannedplayers, bannedips;
+ int fd;
- send_login(cfg);
+ assert_die(cfg, "invalid arguments");
- if (get_one_status(cfg, buf, sizeof(buf), "seed", 1,
+ fd = rcon_login(cfg);
+
+ if (get_one_status(fd, buf, sizeof(buf), "seed", 1,
"Seed : [ %[^]]]", &reply, tbuf))
info("Seed: %s", tbuf);
- if (get_one_status(cfg, buf, sizeof(buf), "difficulty", 1,
+ if (get_one_status(fd, buf, sizeof(buf), "difficulty", 1,
"The difficulty is %s", &reply, tbuf))
info("Difficulty: %s", tbuf);
- if (get_one_status(cfg, buf, sizeof(buf), "list", 2,
+ if (get_one_status(fd, buf, sizeof(buf), "list", 2,
"There are %u of a max %u players online",
&reply, &cplayers, &maxplayers))
info("Players: %u/%u", cplayers, maxplayers);
- if (get_one_status(cfg, buf, sizeof(buf), "time query day", 1,
+ if (get_one_status(fd, buf, sizeof(buf), "time query day", 1,
"The time is %u", &reply, &gtime))
info("In-game days: %u", gtime);
- if (get_one_status(cfg, buf, sizeof(buf), "time query gametime", 1,
+ if (get_one_status(fd, buf, sizeof(buf), "time query gametime", 1,
"The time is %u", &reply, &gtime))
info("World age: %ud:%02uh:%02um",
mctime_days(gtime), mctime_hh(gtime), mctime_mm(gtime));
- if (get_one_status(cfg, buf, sizeof(buf), "time query daytime", 1,
+ if (get_one_status(fd, buf, sizeof(buf), "time query daytime", 1,
"The time is %u", &reply, &gtime))
info("Current in-game time: %02uh:%02um",
mctime_hh(gtime + MCTIME_OFFSET), mctime_mm(gtime + MCTIME_OFFSET));
- if (get_one_status(cfg, buf, sizeof(buf), "datapack list enabled", 2,
+ if (get_one_status(fd, buf, sizeof(buf), "datapack list enabled", 2,
"There are %u data packs enabled: %[^\n]", &reply, &epacks, tbuf))
info("Enabled data packs (%u): %s", epacks, tbuf);
- if (get_one_status(cfg, buf, sizeof(buf), "datapack list available", 2,
+ if (get_one_status(fd, buf, sizeof(buf), "datapack list available", 2,
"There are %u data packs available : %[^\n]", &reply, &apacks, tbuf))
info("Available data packs (%u): %s", apacks, tbuf);
else if (streq(reply, "There are no more data packs available"))
info("Available data packs: none");
- if (get_one_status(cfg, buf, sizeof(buf), "banlist players", 1,
+ if (get_one_status(fd, buf, sizeof(buf), "banlist players", 1,
"There are %u bans", &reply, &bannedplayers))
info("Banned players: %u", bannedplayers);
else if (streq(reply, "There are no bans"))
info("Banned players: 0");
- if (get_one_status(cfg, buf, sizeof(buf), "banlist ips", 1,
+ if (get_one_status(fd, buf, sizeof(buf), "banlist ips", 1,
"There are %u bans", &reply, &bannedips))
info("Banned IPs: %u", bannedips);
else if (streq(reply, "There are no bans"))
@@ -280,12 +294,13 @@ do_ping(_unused_ struct cfg *cfg) {
}
void
-do_stop(_unused_ struct cfg *cfg) {
- assert_die(cfg && cfg->fd >= 0, "invalid arguments");
+do_stop(struct cfg *cfg) {
+ int fd;
- send_login(cfg);
+ assert_die(cfg, "invalid arguments");
- send_cmd(cfg->fd, "stop");
+ fd = rcon_login(cfg);
+ send_cmd(fd, "stop");
}
void
@@ -294,10 +309,13 @@ do_stop_all(_unused_ struct cfg *cfg) {
}
void
-do_pcount(_unused_ struct cfg *cfg) {
- send_login(cfg);
+do_pcount(struct cfg *cfg) {
+ int fd;
+
+ assert_die(cfg, "invalid arguments");
- send_cmd(cfg->fd, "list");
+ fd = rcon_login(cfg);
+ send_cmd(fd, "list");
}
void
@@ -306,8 +324,11 @@ do_console(struct cfg *cfg)
char *prompt;
char *cmd;
const char *sname;
+ int fd;
- assert_die(cfg && cfg->fd >= 0, "invalid arguments");
+ assert_die(cfg, "invalid arguments");
+
+ fd = rcon_login(cfg);
if (cfg->server)
sname = cfg->server->shortname;
@@ -320,8 +341,6 @@ do_console(struct cfg *cfg)
STRLEN(" (") + strlen(sname) + STRLEN("): ") + 1);
sprintf(prompt, "%s (%s): ", program_invocation_short_name, sname);
- send_login(cfg);
-
while (true) {
char *tmp;
@@ -340,7 +359,7 @@ do_console(struct cfg *cfg)
streq(tmp, "/q") || streq(tmp, "/quit"))
break;
- send_cmd(cfg->fd, tmp);
+ send_cmd(fd, tmp);
if (streq(tmp, "stop") || streq(tmp, "/stop"))
/* The server waits for us to close the connection */
@@ -353,11 +372,12 @@ do_console(struct cfg *cfg)
}
void
-do_command(_unused_ struct cfg *cfg) {
- assert_die(cfg && cfg->fd >= 0, "invalid arguments");
+do_command(struct cfg *cfg) {
+ int fd;
- send_login(cfg);
+ assert_die(cfg, "invalid arguments");
- send_cmd(cfg->fd, cfg->cmdstr);
+ fd = rcon_login(cfg);
+ send_cmd(fd, cfg->cmdstr);
}