1 /* vi: set sw=4 ts=4: */
5 * Connect to host at port using address resolusion from getaddrinfo
9 #include "inet_common.h"
13 #include <sys/types.h>
14 #include <sys/socket.h>
16 #include <netinet/in.h>
19 int xconnect(const char *host, const char *port)
21 #if CONFIG_FEATURE_IPV6
22 struct addrinfo hints;
24 struct addrinfo *addr_info;
28 memset(&hints, 0, sizeof(hints));
29 /* set-up hints structure */
30 hints.ai_family = PF_UNSPEC;
31 hints.ai_socktype = SOCK_STREAM;
32 error = getaddrinfo(host, port, &hints, &res);
34 perror_msg_and_die(gai_strerror(error));
37 s=socket(res->ai_family, res->ai_socktype, res->ai_protocol);
44 /* try to connect() to res->ai_addr */
45 error = connect(s, res->ai_addr, res->ai_addrlen);
51 freeaddrinfo(addr_info);
54 perror_msg_and_die("Unable to connect to remote host (%s)", host);
58 struct sockaddr_in s_addr;
59 int s = socket(AF_INET, SOCK_STREAM, 0);
60 struct servent *tserv;
61 int port_nr=atoi(port);
64 if (port_nr==0 && (tserv = getservbyname(port, "tcp")) != NULL)
65 port_nr = tserv->s_port;
67 memset(&s_addr, 0, sizeof(struct sockaddr_in));
68 s_addr.sin_family = AF_INET;
69 s_addr.sin_port = htons(port_nr);
71 he = xgethostbyname(host);
72 memcpy(&s_addr.sin_addr, he->h_addr, sizeof s_addr.sin_addr);
74 if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0)
76 perror_msg_and_die("Unable to connect to remote host (%s)", host);