summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-07-01 21:47:14 +0200
committerDavid Härdeman <david@hardeman.nu>2020-07-01 21:47:14 +0200
commit43830ce2bff25a9acca4268ffaa3a681ac3afec2 (patch)
treea6bfd06bbac5be2bff59a41a47a5ccdf169f3ad2
parent7e728d971b95f115c7e9c2def21815034d9bde54 (diff)
Teach connect_any to report the address it connected to in order to improve dbg messages
-rw-r--r--minecctl/mc-commands.c12
-rw-r--r--minecctl/misc.c30
-rw-r--r--minecctl/misc.h3
-rw-r--r--minecctl/rcon-commands.c12
4 files changed, 27 insertions, 30 deletions
diff --git a/minecctl/mc-commands.c b/minecctl/mc-commands.c
index 648af0e..49fea30 100644
--- a/minecctl/mc-commands.c
+++ b/minecctl/mc-commands.c
@@ -11,6 +11,7 @@ bool do_mc_pcount(struct cfg *cfg, unsigned *online, unsigned *max)
{
struct server *server;
struct saddr *saddr;
+ const char *error;
char buf[4096];
size_t plen, off;
ssize_t r;
@@ -19,19 +20,12 @@ bool do_mc_pcount(struct cfg *cfg, unsigned *online, unsigned *max)
server = server_get_default(cfg);
- fd = connect_any(&server->scfg.remotes, true);
+ fd = connect_any(&server->scfg.remotes, &saddr, &error);
if (fd < 0) {
- error("%s: unable to connect", server->name);
+ error("%s: unable to connect - %s", server->name, error);
return false;
}
- /* FIXME: connect_any needs to indicate the address it used */
- saddr = list_first_entry(&server->scfg.remotes, struct saddr, list);
- if (!saddr) {
- error("No saddr");
- goto out;
- }
-
if (!mc_protocol_create_status_request(buf, sizeof(buf), &plen,
saddr)) {
error("Failed to create req");
diff --git a/minecctl/misc.c b/minecctl/misc.c
index 6562b14..143b0cc 100644
--- a/minecctl/misc.c
+++ b/minecctl/misc.c
@@ -117,43 +117,43 @@ void strv_free(char **strv)
xfree(strv);
}
-int connect_any(struct list_head *addrs, bool may_fail)
+int connect_any(struct list_head *addrs, struct saddr **rsaddr,
+ const char **error)
{
struct saddr *saddr;
- bool connected = false;
int sfd;
- /* FIXME: check callers and coordinate debug msg */
if (list_empty(addrs)) {
- if (may_fail)
- return -1;
- else
- die("No address to connect to");
+ *error = "no address to connect to";
+ return -1;
}
list_for_each_entry(saddr, addrs, list) {
verbose("Attempting connection to %s", saddr->addrstr);
- sfd = socket(saddr->st.ss_family, SOCK_STREAM | SOCK_CLOEXEC,
- 0);
- if (sfd < 0)
- die("socket: %m");
+ sfd = socket(saddr->st.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ if (sfd < 0) {
+ *error = "failed to create socket";
+ return -1;
+ }
socket_set_low_latency(sfd, true, true, true);
if (connect(sfd, (struct sockaddr *)&saddr->st,
saddr->addrlen) < 0) {
close(sfd);
+ sfd = -1;
continue;
}
- connected = true;
+ if (rsaddr)
+ *rsaddr = saddr;
break;
}
- if (!connected && may_fail)
+ if (sfd < 0) {
+ *error = "failed to connect to remote host";
return -1;
- else if (!connected)
- die("Failed to connect to remote host");
+ }
return sfd;
}
diff --git a/minecctl/misc.h b/minecctl/misc.h
index f29422c..c91271d 100644
--- a/minecctl/misc.h
+++ b/minecctl/misc.h
@@ -11,7 +11,8 @@ char *strv_join(char *const *strv);
void strv_free(char **strv);
-int connect_any(struct list_head *addrs, bool may_fail);
+int connect_any(struct list_head *addrs, struct saddr **rsaddr,
+ const char **error);
char *ask_password();
diff --git a/minecctl/rcon-commands.c b/minecctl/rcon-commands.c
index be18bba..5e4ea08 100644
--- a/minecctl/rcon-commands.c
+++ b/minecctl/rcon-commands.c
@@ -99,6 +99,7 @@ static int rcon_login(struct cfg *cfg, struct server *server)
char buf[4096];
int32_t rtype;
const char *reply;
+ struct saddr *saddr;
int fd = -1;
assert_die(cfg && server, "invalid arguments");
@@ -108,11 +109,12 @@ static int rcon_login(struct cfg *cfg, struct server *server)
goto error;
}
- fd = connect_any(&server->scfg.rcons, true);
+ fd = connect_any(&server->scfg.rcons, &saddr, &error);
if (fd < 0) {
- error("%s: unable to connect", server->name);
+ error("%s: unable to connect - %s", server->name, error);
goto error;
- }
+ } else
+ verbose("%s: connected to %s", server->name, saddr->addrstr);
if (!server->scfg.rcon_password)
server->scfg.rcon_password = ask_password();
@@ -122,8 +124,8 @@ static int rcon_login(struct cfg *cfg, struct server *server)
goto error;
}
- send_msg(fd, buf, sizeof(buf), RCON_PACKET_LOGIN, server->scfg.rcon_password,
- &rtype, &reply);
+ send_msg(fd, buf, sizeof(buf), RCON_PACKET_LOGIN,
+ server->scfg.rcon_password, &rtype, &reply);
explicit_bzero(buf, sizeof(buf));
free_password(&server->scfg.rcon_password);