summaryrefslogtreecommitdiff
path: root/minecctl
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-07-07 18:41:38 +0200
committerDavid Härdeman <david@hardeman.nu>2020-07-07 18:41:38 +0200
commit90e27b4356f2a6ab98e812c4096b0b76f94a8fb3 (patch)
tree7c900e498c426611c90ef8b2ba80d141456d9552 /minecctl
parent0e2db6328946180c47bc09a486fe38ca0364e2af (diff)
Flesh out the ping implementation
Diffstat (limited to 'minecctl')
-rw-r--r--minecctl/mc-commands.c31
-rw-r--r--minecctl/mc-commands.h3
-rw-r--r--minecctl/misc-commands.c63
-rw-r--r--minecctl/misc-commands.h2
-rw-r--r--minecctl/rcon-commands.c20
-rw-r--r--minecctl/rcon-commands.h5
6 files changed, 84 insertions, 40 deletions
diff --git a/minecctl/mc-commands.c b/minecctl/mc-commands.c
index 7d44451..c65f166 100644
--- a/minecctl/mc-commands.c
+++ b/minecctl/mc-commands.c
@@ -8,32 +8,23 @@
#include "misc.h"
#include "shared/mc-protocol.h"
-bool do_mc_pcount(struct cfg *cfg, unsigned *online, unsigned *max)
+bool do_mc_pcount(struct cfg *cfg, struct server *server, unsigned *online,
+ unsigned *max, const char **error)
{
- struct server *server;
struct saddr *saddr;
- const char *error;
char buf[4096];
size_t plen, off;
ssize_t r;
bool rv = false;
int fd;
- server = server_get_default(cfg);
- if (!server) {
- error("failed to get default server");
- return false;
- }
-
fd = connect_any(&server->scfg.remotes, &saddr, &error);
- if (fd < 0) {
- error("%s: unable to connect - %s", server->name, error);
+ if (fd < 0)
return false;
- }
if (!mc_protocol_create_status_request(buf, sizeof(buf), &plen,
saddr)) {
- error("Failed to create req");
+ *error = "failed to create request";
goto out;
}
@@ -42,7 +33,7 @@ bool do_mc_pcount(struct cfg *cfg, unsigned *online, unsigned *max)
while (off < plen) {
r = write(fd, buf + off, plen - off);
if (r <= 0) {
- error("write failed: %zi (%m)", r);
+ *error = "write failed";
goto out;
}
off += r;
@@ -52,23 +43,23 @@ bool do_mc_pcount(struct cfg *cfg, unsigned *online, unsigned *max)
while (off < sizeof(buf)) {
r = read(fd, buf + off, sizeof(buf) - off);
if (r <= 0) {
- error("Read failed %zi: %m", r);
+ *error = "read failed";
goto out;
}
off += r;
- if (mc_is_handshake_complete(buf, off)) {
- rv = true;
+ if (mc_is_handshake_complete(buf, off))
break;
- }
}
if (!mc_protocol_parse_status_reply(buf, off, online, max)) {
- error("Failed to get player count");
- return false;
+ *error = "failed to get player count";
+ goto out;
}
+ rv = true;
+
out:
close(fd);
return rv;
diff --git a/minecctl/mc-commands.h b/minecctl/mc-commands.h
index c6e2e6f..493a976 100644
--- a/minecctl/mc-commands.h
+++ b/minecctl/mc-commands.h
@@ -2,6 +2,7 @@
#ifndef foomccommandshfoo
#define foomccomanndshfoo
-bool do_mc_pcount(struct cfg *cfg, unsigned *online, unsigned *max);
+bool do_mc_pcount(struct cfg *cfg, struct server *server, unsigned *online,
+ unsigned *max, const char **error);
#endif
diff --git a/minecctl/misc-commands.c b/minecctl/misc-commands.c
index f89afb6..1870e07 100644
--- a/minecctl/misc-commands.c
+++ b/minecctl/misc-commands.c
@@ -5,6 +5,7 @@
#include "misc-commands.h"
#include "rcon-commands.h"
#include "mc-commands.h"
+#include "shared/systemd.h"
bool do_list(struct cfg *cfg)
{
@@ -135,13 +136,65 @@ bool do_lint(struct cfg *cfg)
bool do_pcount(struct cfg *cfg)
{
- unsigned x, y;
+ unsigned online, max;
+ struct server *server;
+ const char *error;
+
+ server = server_get_default(cfg);
+ if (!server) {
+ error("failed to get default server");
+ return false;
+ }
- if (do_rcon_pcount(cfg, &y, &x))
- error("Rcon says %u/%u", y, x);
+ if (do_rcon_pcount(cfg, server, &online, &max, &error))
+ info("Rcon says %u/%u", online, max);
- if (do_mc_pcount(cfg, &y, &x))
- error("MC says %u/%u", y, x);
+ if (do_mc_pcount(cfg, server, &online, &max, &error))
+ info("MC says %u/%u", online, max);
return true;
}
+
+bool do_ping(struct cfg *cfg)
+{
+ struct server *server;
+ const char *error;
+ unsigned online, max;
+
+ /* FIXME: Do all servers when default not given */
+ server = server_get_default(cfg);
+ if (!server) {
+ error("failed to get default server");
+ return false;
+ }
+
+ info("• %s", server->name);
+
+ if (list_empty(&server->scfg.rcons))
+ info(" rcon : not configured");
+ else if (!do_rcon_pcount(cfg, server, &online, &max, &error))
+ info(" rcon : %sfail%s (%s)",
+ ansi_red, ansi_normal, error);
+ else
+ info(" rcon : %sok%s", ansi_green, ansi_normal);
+
+ if (list_empty(&server->scfg.remotes))
+ info(" mc : not configured");
+ else if (!do_mc_pcount(cfg, server, &online, &max, &error))
+ info(" mc : %sfail%s (%s)",
+ ansi_red, ansi_normal, error);
+ else
+ info(" mc : %sok%s", ansi_green, ansi_normal);
+
+ if (!server->scfg.systemd_service || !server->scfg.systemd_obj)
+ info(" systemd service : not configured");
+ else if (!systemd_service_running(&server->scfg, &error))
+ info(" systemd service : %sfail%s (%s)",
+ ansi_red, ansi_normal, error);
+ else
+ info(" systemd service : %sactive%s", ansi_green, ansi_normal);
+ systemd_delete();
+
+ return true;
+}
+
diff --git a/minecctl/misc-commands.h b/minecctl/misc-commands.h
index 8ea4127..ac2fd54 100644
--- a/minecctl/misc-commands.h
+++ b/minecctl/misc-commands.h
@@ -8,4 +8,6 @@ bool do_lint(struct cfg *cfg);
bool do_pcount(struct cfg *cfg);
+bool do_ping(struct cfg *cfg);
+
#endif
diff --git a/minecctl/rcon-commands.c b/minecctl/rcon-commands.c
index f18f082..61a682c 100644
--- a/minecctl/rcon-commands.c
+++ b/minecctl/rcon-commands.c
@@ -12,8 +12,8 @@
#include "shared/utils.h"
#include "shared/rcon-protocol.h"
#include "minecctl.h"
-#include "rcon-commands.h"
#include "server.h"
+#include "rcon-commands.h"
#include "misc.h"
static void send_packet(int sfd, const char *buf, size_t len)
@@ -93,6 +93,7 @@ static void send_msg(int sfd, char *buf, size_t len, enum rcon_packet_type type,
rcon_packet_id++;
}
+/* FIXME: return error message */
static int rcon_login(struct cfg *cfg, struct server *server)
{
const char *error;
@@ -296,12 +297,6 @@ bool do_status(struct cfg *cfg)
return true;
}
-bool do_ping(_unused_ struct cfg *cfg)
-{
- die("Not implemented");
- return false;
-}
-
static bool get_player_count(int fd, unsigned *current, unsigned *max)
{
char buf[4096];
@@ -382,18 +377,21 @@ bool do_stop_all(struct cfg *cfg)
return rv;
}
-bool do_rcon_pcount(struct cfg *cfg, unsigned *online, unsigned *max)
+bool do_rcon_pcount(struct cfg *cfg, struct server *server, unsigned *online,
+ unsigned *max, const char **error)
{
- struct server *server;
bool rv;
int fd;
- server = server_get_default(cfg);
fd = rcon_login(cfg, server);
- if (fd < 0)
+ if (fd < 0) {
+ *error = "failed to login";
return false;
+ }
rv = get_player_count(fd, online, max);
+ if (!rv)
+ *error = "failed to get player count";
close(fd);
diff --git a/minecctl/rcon-commands.h b/minecctl/rcon-commands.h
index b2720b4..84d5529 100644
--- a/minecctl/rcon-commands.h
+++ b/minecctl/rcon-commands.h
@@ -4,13 +4,12 @@
bool do_status(struct cfg *cfg);
-bool do_ping(struct cfg *cfg);
-
bool do_stop(struct cfg *cfg);
bool do_stop_all(struct cfg *cfg);
-bool do_rcon_pcount(struct cfg *cfg, unsigned *online, unsigned *max);
+bool do_rcon_pcount(struct cfg *cfg, struct server *server, unsigned *online,
+ unsigned *max, const char **error);
bool do_console(struct cfg *cfg);