commit a7aa1d362d4f8a865bc685379276460ef7f9e186
parent e1ee7cd4aaf8da6811b25831075a590b52effaaf
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Wed, 23 Dec 2020 22:23:53 -0800
Cleanup resolve()
Don't bother saving errno (which I failed to do anyway), use the
for loop to increment ai, and combine multiple if statments into
one.
Diffstat:
1 file changed, 4 insertions(+), 11 deletions(-)
diff --git a/main.c b/main.c
@@ -167,26 +167,19 @@ resolve(const char *host, const char *port)
}
s = -1;
- for (ai = ai0; ai && s == -1;) {
+ for (ai = ai0; ai && s == -1; ai = ai->ai_next) {
s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
- if (s != -1) {
- if (connect(s, ai->ai_addr, ai->ai_addrlen) == -1) {
- close(s);
- error = errno;
- s = -1;
- errno = error;
- ai = ai->ai_next;
- }
+ if (s != -1 && connect(s, ai->ai_addr, ai->ai_addrlen) == -1) {
+ close(s);
+ s = -1;
}
}
-
freeaddrinfo(ai0);
if (s == -1) {
warnx("unable to connect to '%s' at '%s'", host, port);
return -1;
}
-
return s;
}