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>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
21 int bb_getport(char *port)
25 struct servent *tserv;
30 port_nr=strtol(port, &endptr, 10);
31 if (errno != 0 || *endptr!='\0' || endptr==port || port_nr < 1 || port_nr > 65536)
33 if (port_nr==0 && (tserv = getservbyname(port, "tcp")) != NULL) {
34 port_nr = tserv->s_port;
39 port_nr = htons(port_nr);
44 void bb_lookup_host(struct sockaddr_in *s_in, char *host, char *port)
48 memset(s_in, 0, sizeof(struct sockaddr_in));
49 s_in->sin_family = AF_INET;
50 he = xgethostbyname(host);
51 memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
54 s_in->sin_port=bb_getport(port);
58 int xconnect(struct sockaddr_in *s_addr)
60 int s = socket(AF_INET, SOCK_STREAM, 0);
61 if (connect(s, (struct sockaddr_in *)s_addr, sizeof(struct sockaddr_in)) < 0)
63 bb_perror_msg_and_die("Unable to connect to remote host (%s)",
64 inet_ntoa(s_addr->sin_addr));