gawk

[old] Sed-like interface to the Gopher protocol
Log | Files | Refs | LICENSE

commit e317fe283e2869fffd7413dcd9dfb306717fd475
parent 69069fe9e1d69ef1a8bb10728fb7ac05da7670af
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Sun, 20 Dec 2020 16:58:48 -0800

Replace connect_to() with resolve()

Diffstat:
Mmain.c | 44+++++++++++++++++---------------------------
1 file changed, 17 insertions(+), 27 deletions(-)

diff --git a/main.c b/main.c @@ -64,50 +64,40 @@ wfopen(const char *path, const char *mode) } int -resolve(struct addrinfo **ai, const char *host, const char *proto) +resolve(const char *host, const char *port) { - int error; + int error, s; struct addrinfo hints; + struct addrinfo *ai, *ai0; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; - error = getaddrinfo(host, proto, &hints, ai); - if (error) - return error; - return 0; -} - -int -connect_to(const char *host, const char *proto) -{ - int error; - int s; - struct addrinfo *ai, *ai0; - - error = resolve(&ai0, host, proto); + error = getaddrinfo(host, port, &hints, &ai0); if (error) { - warnx("Unable to resolve: '%s' at '%s': %s", host, proto, gai_strerror(error)); + warnx("Unable to resolve: '%s' at '%s': %s", host, port, gai_strerror(error)); return -1; } s = -1; - for (ai = ai0; ai && s == -1; ai = ai->ai_next) { + for (ai = ai0; ai && s == -1;) { s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); - if (s == -1) - continue; - if (connect(s, ai->ai_addr, ai->ai_addrlen) == -1) { - close(s); - error = errno; - s = -1; - errno = error; + 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; + } } } freeaddrinfo(ai0); + if (s == -1) { - warnx("Unable to connect '%s'", host); + warnx("unable to connect to '%s' at '%s'", host, port); return -1; } @@ -267,7 +257,7 @@ gphcache(const char *cache, const char *host, const char *path, const char *port { int sock; - sock = connect_to(host, port); + sock = resolve(host, port); if (sock == -1) return 1;