diff options
author | Rob Landley <rob@landley.net> | 2017-07-17 05:23:45 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2017-07-17 05:23:45 -0500 |
commit | 92b6026fa55eb1a6d76dfa549e173fc7b863dda5 (patch) | |
tree | 93d148eee63cb4aea260f3faad60ba60b75fe43e /lib | |
parent | d4adb3f8e2ec39674e34ed518983ed6e85560c91 (diff) | |
download | toybox-92b6026fa55eb1a6d76dfa549e173fc7b863dda5.tar.gz |
Split out xgetaddrinfo() from xconnect()
Diffstat (limited to 'lib')
-rw-r--r-- | lib/lib.h | 5 | ||||
-rw-r--r-- | lib/net.c | 28 |
2 files changed, 21 insertions, 12 deletions
@@ -284,8 +284,9 @@ void tty_sigreset(int i); // net.c int xsocket(int domain, int type, int protocol); void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len); -int xconnect(char *host, char *port, int family, int socktype, int protocol, - int flags); +struct addrinfo *xgetaddrinfo(char *host, char *port, int family, int socktype, + int protocol, int flags); +int xconnect(struct addrinfo *ai_arg); int xpoll(struct pollfd *fds, int nfds, int timeout); int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout); @@ -15,11 +15,11 @@ void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len) if (-1 == setsockopt(fd, level, opt, val, len)) perror_exit("setsockopt"); } -int xconnect(char *host, char *port, int family, int socktype, int protocol, - int flags) +struct addrinfo *xgetaddrinfo(char *host, char *port, int family, int socktype, + int protocol, int flags) { - struct addrinfo info, *ai, *ai2; - int fd; + struct addrinfo info, *ai; + int rc; memset(&info, 0, sizeof(struct addrinfo)); info.ai_family = family; @@ -27,20 +27,28 @@ int xconnect(char *host, char *port, int family, int socktype, int protocol, info.ai_protocol = protocol; info.ai_flags = flags; - fd = getaddrinfo(host, port, &info, &ai); - if (fd || !ai) - error_exit("Connect '%s%s%s': %s", host, port ? ":" : "", port ? port : "", - fd ? gai_strerror(fd) : "not found"); + rc = getaddrinfo(host, port, &info, &ai); + if (rc || !ai) + error_exit("%s%s%s: %s", host, port ? ":" : "", port ? port : "", + rc ? gai_strerror(rc) : "not found"); + + return ai; +} + +int xconnect(struct addrinfo *ai_arg) +{ + struct addrinfo *ai; + int fd = -1; // Try all the returned addresses. Report errors if last entry can't connect. - for (ai2 = ai; ai; ai = ai->ai_next) { + for (ai = ai_arg; ai; ai = ai->ai_next) { fd = (ai->ai_next ? socket : xsocket)(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (!connect(fd, ai->ai_addr, ai->ai_addrlen)) break; else if (!ai->ai_next) perror_exit("connect"); close(fd); } - freeaddrinfo(ai2); + freeaddrinfo(ai_arg); return fd; } |