summaryrefslogtreecommitdiff
path: root/minecctl/misc.c
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-26 16:09:04 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-26 16:09:04 +0200
commit591b03bd3cfd52b33a5e8512fef466494cf329f6 (patch)
treed9d90783953b8ac6a98c7067d3f6c4f625264272 /minecctl/misc.c
parenta6d421f99e90739e60a60232dbe5b09a53545978 (diff)
Properly cleanup and free struct cfg and servers
Diffstat (limited to 'minecctl/misc.c')
-rw-r--r--minecctl/misc.c73
1 files changed, 42 insertions, 31 deletions
diff --git a/minecctl/misc.c b/minecctl/misc.c
index 13b4377..bb33161 100644
--- a/minecctl/misc.c
+++ b/minecctl/misc.c
@@ -61,6 +61,41 @@ strv_join(char * const *strv)
return r;
}
+int
+connect_any(struct list_head *addrs, bool may_fail)
+{
+ struct saddr *saddr;
+ bool connected = false;
+ int sfd;
+
+ if (list_empty(addrs))
+ die("No address to connect to");
+
+ list_for_each_entry(saddr, addrs, list) {
+ verbose("Attempting connection to %s", saddr->addrstr);
+ sfd = socket(saddr->storage.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ if (sfd < 0)
+ die("socket: %m");
+
+ socket_set_low_latency(sfd, true, true, true);
+
+ if (connect(sfd, (struct sockaddr *)&saddr->storage, saddr->addrlen) < 0) {
+ close(sfd);
+ continue;
+ }
+
+ connected = true;
+ break;
+ }
+
+ if (!connected && may_fail)
+ return -1;
+ else if (!connected)
+ die("Failed to connect to remote host");
+
+ return sfd;
+}
+
char *
ask_password()
{
@@ -99,39 +134,15 @@ ask_password()
return password;
}
-int
-connect_any(struct list_head *addrs, bool may_fail)
+void
+free_password(char **password)
{
- struct saddr *saddr;
- bool connected = false;
- int sfd;
-
- if (list_empty(addrs))
- die("No address to connect to");
-
- list_for_each_entry(saddr, addrs, list) {
- verbose("Attempting connection to %s", saddr->addrstr);
- sfd = socket(saddr->storage.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
- if (sfd < 0)
- die("socket: %m");
-
- socket_set_low_latency(sfd, true, true, true);
-
- if (connect(sfd, (struct sockaddr *)&saddr->storage, saddr->addrlen) < 0) {
- close(sfd);
- continue;
- }
-
- connected = true;
- break;
- }
-
- if (!connected && may_fail)
- return -1;
- else if (!connected)
- die("Failed to connect to remote host");
+ if (!password || !*password)
+ return;
- return sfd;
+ explicit_bzero(*password, strlen(*password));
+ xfree(*password);
+ *password = NULL;
}
void