diff options
author | Eric Andersen <andersen@codepoet.org> | 2002-07-03 15:51:44 +0400 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2002-07-03 15:51:44 +0400 |
commit | 0b31586c7113b9b26ca0aeee9247a831b55b308c (patch) | |
tree | 7071145eeeea4dd92d18e05fce6d8e710dc67d52 /libbb | |
parent | 51b8bd68bb22b1cc5d95e418813c2f08a194ec2b (diff) |
A patch from Bart Visscher <magick@linux-fan.com> to add an
xconnect helper routine which does:
-address and port resolving
-tries to connect to all resolved addresses until connected
-uses getaddrinfo, so works for IPv6 too
This patch also ports rdate, telnet, and wget to use the new
xconnect function. Thanks Bart!
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/xconnect.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/libbb/xconnect.c b/libbb/xconnect.c new file mode 100644 index 000000000..0d670f286 --- /dev/null +++ b/libbb/xconnect.c @@ -0,0 +1,78 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Connect to host at port using address resolusion from getaddrinfo + * + */ + +#include <unistd.h> +#include <string.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netdb.h> +#include <stdlib.h> +#include "libbb.h" + +int xconnect(const char *host, const char *port) +{ +#if CONFIG_FEATURE_IPV6 + struct addrinfo hints; + struct addrinfo *res; + struct addrinfo *addr_info; + int error; + int s; + + memset(&hints, 0, sizeof(hints)); + /* set-up hints structure */ + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + error = getaddrinfo(host, port, &hints, &res); + if (error||!res) + perror_msg_and_die(gai_strerror(error)); + addr_info=res; + while (res) { + s=socket(res->ai_family, res->ai_socktype, res->ai_protocol); + if (s<0) + { + error=s; + res=res->ai_next; + continue; + } + /* try to connect() to res->ai_addr */ + error = connect(s, res->ai_addr, res->ai_addrlen); + if (error >= 0) + break; + close(s); + res=res->ai_next; + } + freeaddrinfo(addr_info); + if (error < 0) + { + perror_msg_and_die("Unable to connect to remote host (%s)", host); + } + return s; +#else + struct sockaddr_in s_addr; + int s = socket(AF_INET, SOCK_STREAM, 0); + struct servent *tserv; + int port_nr=atoi(port); + struct hostent * he; + + if (port_nr==0 && (tserv = getservbyname(port, "tcp")) != NULL) + port_nr = tserv->s_port; + + memset(&s_addr, 0, sizeof(struct sockaddr_in)); + s_addr.sin_family = AF_INET; + s_addr.sin_port = htons(port_nr); + + he = xgethostbyname(host); + memcpy(&s_addr.sin_addr, he->h_addr, sizeof s_addr.sin_addr); + + if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0) + { + perror_msg_and_die("Unable to connect to remote host (%s)", host); + } + return s; +#endif +} |