1 /* vi: set sw=4 ts=4: */
5 * Connect to host at port using address resolusion from getaddrinfo
12 #include <sys/types.h>
13 #include <sys/socket.h>
15 #include <netinet/in.h>
18 int xconnect(const char *host, const char *port)
20 #ifdef CONFIG_FEATURE_IPV6
21 struct addrinfo hints;
23 struct addrinfo *addr_info;
27 memset(&hints, 0, sizeof(hints));
28 /* set-up hints structure */
29 hints.ai_family = PF_UNSPEC;
30 hints.ai_socktype = SOCK_STREAM;
31 error = getaddrinfo(host, port, &hints, &res);
33 bb_perror_msg_and_die(gai_strerror(error));
36 s=socket(res->ai_family, res->ai_socktype, res->ai_protocol);
43 /* try to connect() to res->ai_addr */
44 error = connect(s, res->ai_addr, res->ai_addrlen);
50 freeaddrinfo(addr_info);
53 bb_perror_msg_and_die("Unable to connect to remote host (%s)", host);
57 struct sockaddr_in s_addr;
58 int s = socket(AF_INET, SOCK_STREAM, 0);
59 struct servent *tserv;
60 int port_nr=htons(atoi(port));
63 if (port_nr==0 && (tserv = getservbyname(port, "tcp")) != NULL)
64 port_nr = tserv->s_port;
66 memset(&s_addr, 0, sizeof(struct sockaddr_in));
67 s_addr.sin_family = AF_INET;
68 s_addr.sin_port = port_nr;
70 he = xgethostbyname(host);
71 memcpy(&s_addr.sin_addr, he->h_addr, sizeof s_addr.sin_addr);
73 if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0)
75 bb_perror_msg_and_die("Unable to connect to remote host (%s)", host);