aboutsummaryrefslogtreecommitdiff
path: root/lib/net.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2015-08-03 14:34:01 -0500
committerRob Landley <rob@landley.net>2015-08-03 14:34:01 -0500
commitea75e752f930df7b740a773294b997f46927c716 (patch)
treedb12eb92ad24fdbd3defa7e1f6b2993402e25d83 /lib/net.c
parent7a3f53ba446ae2600763ee37b7f8dcc91de3ec5f (diff)
downloadtoybox-ea75e752f930df7b740a773294b997f46927c716.tar.gz
Factor out xconnect(), plus some other small cleanups to telnet.c.
Diffstat (limited to 'lib/net.c')
-rw-r--r--lib/net.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/net.c b/lib/net.c
index 5d3ea4a8..fc97a262 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -12,3 +12,30 @@ 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, int port, int family, int socktype, int protocol,
+ int flags)
+{
+ char buf[32];
+ struct addrinfo info, *ai;
+ int fd;
+
+ memset(&info, 0, sizeof(struct addrinfo));
+ info.ai_family = family;
+ info.ai_socktype = socktype;
+ info.ai_protocol = protocol;
+ info.ai_flags = flags;
+
+ sprintf(buf, "%d", port);
+ fd = getaddrinfo(host, port ? buf : 0, &info, &ai);
+
+ if (fd || !ai)
+ error_exit("Connect '%s:%d': %s", host, port,
+ fd ? gai_strerror(fd) : "not found");
+
+ fd = xsocket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+ if (connect(fd, ai->ai_addr, ai->ai_addrlen)) perror_exit("connect");
+ freeaddrinfo(ai);
+
+ return fd;
+}