diff options
author | Eric Andersen <andersen@codepoet.org> | 2002-07-03 11:51:44 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2002-07-03 11:51:44 +0000 |
commit | 0b31586c7113b9b26ca0aeee9247a831b55b308c (patch) | |
tree | 7071145eeeea4dd92d18e05fce6d8e710dc67d52 /libbb/xconnect.c | |
parent | 51b8bd68bb22b1cc5d95e418813c2f08a194ec2b (diff) | |
download | busybox-w32-0b31586c7113b9b26ca0aeee9247a831b55b308c.tar.gz busybox-w32-0b31586c7113b9b26ca0aeee9247a831b55b308c.tar.bz2 busybox-w32-0b31586c7113b9b26ca0aeee9247a831b55b308c.zip |
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/xconnect.c')
-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 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Connect to host at port using address resolusion from getaddrinfo | ||
6 | * | ||
7 | */ | ||
8 | |||
9 | #include <unistd.h> | ||
10 | #include <string.h> | ||
11 | #include <sys/types.h> | ||
12 | #include <sys/socket.h> | ||
13 | #include <netdb.h> | ||
14 | #include <stdlib.h> | ||
15 | #include "libbb.h" | ||
16 | |||
17 | int xconnect(const char *host, const char *port) | ||
18 | { | ||
19 | #if CONFIG_FEATURE_IPV6 | ||
20 | struct addrinfo hints; | ||
21 | struct addrinfo *res; | ||
22 | struct addrinfo *addr_info; | ||
23 | int error; | ||
24 | int s; | ||
25 | |||
26 | memset(&hints, 0, sizeof(hints)); | ||
27 | /* set-up hints structure */ | ||
28 | hints.ai_family = PF_UNSPEC; | ||
29 | hints.ai_socktype = SOCK_STREAM; | ||
30 | error = getaddrinfo(host, port, &hints, &res); | ||
31 | if (error||!res) | ||
32 | perror_msg_and_die(gai_strerror(error)); | ||
33 | addr_info=res; | ||
34 | while (res) { | ||
35 | s=socket(res->ai_family, res->ai_socktype, res->ai_protocol); | ||
36 | if (s<0) | ||
37 | { | ||
38 | error=s; | ||
39 | res=res->ai_next; | ||
40 | continue; | ||
41 | } | ||
42 | /* try to connect() to res->ai_addr */ | ||
43 | error = connect(s, res->ai_addr, res->ai_addrlen); | ||
44 | if (error >= 0) | ||
45 | break; | ||
46 | close(s); | ||
47 | res=res->ai_next; | ||
48 | } | ||
49 | freeaddrinfo(addr_info); | ||
50 | if (error < 0) | ||
51 | { | ||
52 | perror_msg_and_die("Unable to connect to remote host (%s)", host); | ||
53 | } | ||
54 | return s; | ||
55 | #else | ||
56 | struct sockaddr_in s_addr; | ||
57 | int s = socket(AF_INET, SOCK_STREAM, 0); | ||
58 | struct servent *tserv; | ||
59 | int port_nr=atoi(port); | ||
60 | struct hostent * he; | ||
61 | |||
62 | if (port_nr==0 && (tserv = getservbyname(port, "tcp")) != NULL) | ||
63 | port_nr = tserv->s_port; | ||
64 | |||
65 | memset(&s_addr, 0, sizeof(struct sockaddr_in)); | ||
66 | s_addr.sin_family = AF_INET; | ||
67 | s_addr.sin_port = htons(port_nr); | ||
68 | |||
69 | he = xgethostbyname(host); | ||
70 | memcpy(&s_addr.sin_addr, he->h_addr, sizeof s_addr.sin_addr); | ||
71 | |||
72 | if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0) | ||
73 | { | ||
74 | perror_msg_and_die("Unable to connect to remote host (%s)", host); | ||
75 | } | ||
76 | return s; | ||
77 | #endif | ||
78 | } | ||