summaryrefslogtreecommitdiff
path: root/minecctl
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-30 08:10:04 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-30 08:10:04 +0200
commita89a0f918925a662503c1bcb28bdb06ab9b7ef25 (patch)
tree733b4c1ff841f99eeb3840c5948fef6c5bf76109 /minecctl
parent8f29a4d23dd13a80aa26951b17e2a71f4e651cb1 (diff)
Share config parsing fully between server and cmdline tool
Diffstat (limited to 'minecctl')
-rw-r--r--minecctl/mc-commands.c4
-rw-r--r--minecctl/minecctl.c16
-rw-r--r--minecctl/misc-commands.c4
-rw-r--r--minecctl/rcon-commands.c14
-rw-r--r--minecctl/server.c87
-rw-r--r--minecctl/server.h7
6 files changed, 39 insertions, 93 deletions
diff --git a/minecctl/mc-commands.c b/minecctl/mc-commands.c
index d4d2846..424eae9 100644
--- a/minecctl/mc-commands.c
+++ b/minecctl/mc-commands.c
@@ -19,14 +19,14 @@ bool do_mc_pcount(struct cfg *cfg, unsigned *online, unsigned *max)
server = server_get_default(cfg);
- fd = connect_any(&server->mc_addrs, true);
+ fd = connect_any(&server->scfg.remotes, true);
if (fd < 0) {
error("%s: unable to connect", server->name);
return false;
}
/* FIXME: connect_any needs to indicate the address it used */
- saddr = list_first_entry(&server->mc_addrs, struct saddr, list);
+ saddr = list_first_entry(&server->scfg.remotes, struct saddr, list);
if (!saddr) {
error("No saddr");
goto out;
diff --git a/minecctl/minecctl.c b/minecctl/minecctl.c
index 3d21ae6..231b150 100644
--- a/minecctl/minecctl.c
+++ b/minecctl/minecctl.c
@@ -44,13 +44,13 @@ static void dump_config(struct cfg *cfg)
list_for_each_entry(server, &cfg->servers, list) {
info(" * server");
info(" name : %s", server->name);
- info(" filename : %s", server->filename);
- info(" rcon_password : %s", server->rcon_password);
+ info(" filename : %s", server->scfg.filename);
+ info(" rcon_password : %s", server->scfg.rcon_password);
info(" file_read : %s",
server->file_read ? "yes" : "no");
- list_for_each_entry(saddr, &server->rcon_addrs, list)
+ list_for_each_entry(saddr, &server->scfg.rcons, list)
info(" * rcon addr : %s", saddr->addrstr);
- list_for_each_entry(saddr, &server->mc_addrs, list)
+ list_for_each_entry(saddr, &server->scfg.remotes, list)
info(" * mc addr : %s", saddr->addrstr);
}
@@ -133,10 +133,10 @@ static bool create_server_from_cmdline_args(struct cfg *cfg)
if (!cfg->rcon_addrstr && !cfg->mc_addrstr)
return false;
- server = server_new();
+ server = server_new(NULL);
if (cfg->rcon_addrstr) {
- if (!str_to_addrs(cfg->rcon_addrstr, &server->rcon_addrs))
+ if (!str_to_addrs(cfg->rcon_addrstr, &server->scfg.rcons))
goto error;
server->name = cfg->rcon_addrstr;
@@ -144,7 +144,7 @@ static bool create_server_from_cmdline_args(struct cfg *cfg)
}
if (cfg->mc_addrstr) {
- if (!str_to_addrs(cfg->mc_addrstr, &server->mc_addrs))
+ if (!str_to_addrs(cfg->mc_addrstr, &server->scfg.remotes))
goto error;
if (!server->name)
@@ -156,7 +156,7 @@ static bool create_server_from_cmdline_args(struct cfg *cfg)
}
if (cfg->rcon_password) {
- server->rcon_password = cfg->rcon_password;
+ server->scfg.rcon_password = cfg->rcon_password;
cfg->rcon_password = NULL;
}
diff --git a/minecctl/misc-commands.c b/minecctl/misc-commands.c
index 52797da..f20cac5 100644
--- a/minecctl/misc-commands.c
+++ b/minecctl/misc-commands.c
@@ -9,9 +9,9 @@ bool do_list(struct cfg *cfg)
{
struct server *server;
- /* server->filename check excludes servers created from cmdline */
+ /* server->scfg.filename check excludes servers created from cmdline */
list_for_each_entry(server, &cfg->servers, list)
- if (server->filename)
+ if (server->scfg.filename)
info("%s", server->name);
return true;
diff --git a/minecctl/rcon-commands.c b/minecctl/rcon-commands.c
index 5f98b72..2f1687b 100644
--- a/minecctl/rcon-commands.c
+++ b/minecctl/rcon-commands.c
@@ -110,30 +110,30 @@ static int rcon_login(struct cfg *cfg, struct server *server)
assert_die(cfg && server, "invalid arguments");
- if (list_empty(&server->rcon_addrs)) {
+ if (list_empty(&server->scfg.rcons)) {
error("%s: rcon address unknown", server->name);
goto error;
}
- fd = connect_any(&server->rcon_addrs, true);
+ fd = connect_any(&server->scfg.rcons, true);
if (fd < 0) {
error("%s: unable to connect", server->name);
goto error;
}
- if (!server->rcon_password)
- server->rcon_password = ask_password();
+ if (!server->scfg.rcon_password)
+ server->scfg.rcon_password = ask_password();
- if (!server->rcon_password) {
+ if (!server->scfg.rcon_password) {
error("%s: can't login - password missing", server->name);
goto error;
}
- send_msg(fd, buf, sizeof(buf), RCON_PACKET_LOGIN, server->rcon_password,
+ send_msg(fd, buf, sizeof(buf), RCON_PACKET_LOGIN, server->scfg.rcon_password,
&rtype, &reply);
explicit_bzero(buf, sizeof(buf));
- free_password(&server->rcon_password);
+ free_password(&server->scfg.rcon_password);
if (rtype == RCON_PACKET_LOGIN_OK)
verbose("%s: login ok", server->name);
diff --git a/minecctl/server.c b/minecctl/server.c
index aca69a0..21df7f4 100644
--- a/minecctl/server.c
+++ b/minecctl/server.c
@@ -7,8 +7,6 @@
#include "minecctl.h"
#include "server.h"
#include "misc.h"
-#include "config-parser.h"
-#include "server-config-options.h"
void server_read_config(struct cfg *cfg, struct server *server)
{
@@ -17,9 +15,8 @@ void server_read_config(struct cfg *cfg, struct server *server)
ssize_t r;
int dfd;
int fd;
- char *pos = buf;
- if (!server || !server->filename || server->file_read)
+ if (!server || !server->scfg.filename || server->file_read)
return;
server->file_read = true;
@@ -28,73 +25,39 @@ void server_read_config(struct cfg *cfg, struct server *server)
if (dfd < 0)
die("Failed to open %s: %m", cfg->cfgdir);
- fd = openat(dfd, server->filename, O_RDONLY | O_CLOEXEC);
+ fd = openat(dfd, server->scfg.filename, O_RDONLY | O_CLOEXEC);
if (fd < 0)
- die("Failed to open %s: %m", server->filename);
+ die("Failed to open %s: %m", server->scfg.filename);
close(dfd);
while (true) {
r = read(fd, buf + off, sizeof(buf) - off - 1);
if (r < 0)
- die("Failed to read %s: %m", server->filename);
+ die("Failed to read %s: %m", server->scfg.filename);
else if (r == 0)
break;
off += r;
if (off == sizeof(buf) - 1)
die("Failed to read %s: file too large",
- server->filename);
+ server->scfg.filename);
}
buf[off] = '\0';
close(fd);
- if (!config_parse_header(SERVER_CFG_HEADER, &pos))
- die("Unable to parse %s: invalid/missing header",
- server->filename);
+ if (!scfg_parse(&server->scfg, buf, NULL))
+ die("Unable to parse %s", server->scfg.filename);
- /* FIXME: this will cause superflous DNS lookups of other cfg entries */
- while (true) {
- int key;
- const char *keyname;
- struct cfg_value value;
-
- if (!config_parse_line(server->filename, &pos, scfg_key_map,
- &key, &keyname, &value, false))
- break;
-
- switch (key) {
- case SCFG_KEY_RCON:
- if (!list_empty(&server->rcon_addrs))
- die("rcon address defined twice in %s",
- server->filename);
- list_replace(&value.saddrs, &server->rcon_addrs);
- break;
- case SCFG_KEY_RCON_PASSWORD:
- if (server->rcon_password)
- die("rcon password defined twice in %s",
- server->filename);
- server->rcon_password = xstrdup(value.str);
- break;
- case SCFG_KEY_REMOTE:
- if (!list_empty(&server->mc_addrs))
- die("rcon address defined twice in %s",
- server->filename);
- list_replace(&value.saddrs, &server->mc_addrs);
- default:
- continue;
- }
- }
-
- if (!server->rcon_password)
- verbose("rcon password not found in %s", server->filename);
+ if (!server->scfg.rcon_password)
+ verbose("rcon password not found in %s", server->scfg.filename);
- if (list_empty(&server->rcon_addrs))
- verbose("rcon address not found in %s", server->filename);
+ if (list_empty(&server->scfg.rcons))
+ verbose("rcon address not found in %s", server->scfg.filename);
- if (list_empty(&server->mc_addrs))
- verbose("mc server address not found in %s", server->filename);
+ if (list_empty(&server->scfg.remotes))
+ verbose("mc server address not found in %s", server->scfg.filename);
}
struct server *server_get_default(struct cfg *cfg)
@@ -144,8 +107,7 @@ void server_load_all_known(struct cfg *cfg)
if (!is_valid_server_config_filename(dent, NULL))
continue;
- server = server_new();
- server->filename = xstrdup(dent->d_name);
+ server = server_new(dent->d_name);
suffix = strrchr(dent->d_name, '.');
assert_die(suffix, "Error parsing filename");
@@ -168,32 +130,17 @@ void server_free_all(struct cfg *cfg)
void server_free(struct server *server)
{
- struct saddr *saddr, *tmp;
-
+ scfg_delete(&server->scfg);
xfree(server->name);
- xfree(server->filename);
- free_password(&server->rcon_password);
-
- list_for_each_entry_safe(saddr, tmp, &server->rcon_addrs, list) {
- list_del(&saddr->list);
- xfree(saddr);
- }
-
- list_for_each_entry_safe(saddr, tmp, &server->mc_addrs, list) {
- list_del(&saddr->list);
- xfree(saddr);
- }
-
xfree(server);
}
-struct server *server_new()
+struct server *server_new(const char *filename)
{
struct server *server;
server = zmalloc(sizeof(*server));
- INIT_LIST_HEAD(&server->rcon_addrs);
- INIT_LIST_HEAD(&server->mc_addrs);
+ scfg_init(&server->scfg, filename);
INIT_LIST_HEAD(&server->list);
return server;
diff --git a/minecctl/server.h b/minecctl/server.h
index 89ee44c..a0c0a76 100644
--- a/minecctl/server.h
+++ b/minecctl/server.h
@@ -3,13 +3,12 @@
#include <stdbool.h>
+#include "config-parser.h"
+
struct server {
bool file_read;
char *name;
- char *filename;
- char *rcon_password;
- struct list_head rcon_addrs;
- struct list_head mc_addrs;
+ struct server_config scfg;
struct list_head list;
};