1 /* vi: set sw=4 ts=4: */
5 * Connect to host at port using address resolution from getaddrinfo
11 /* Return network byte ordered port number for a service.
12 * If "port" is a number use it as the port.
13 * If "port" is a name it is looked up in /etc/services, if it isnt found return
16 unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port)
18 unsigned short port_nr = htons(default_port);
24 /* Since this is a lib function, we're not allowed to reset errno to 0.
25 * Doing so could break an app that is deferring checking of errno. */
28 port_long = strtol(port, &endptr, 10);
29 if (errno != 0 || *endptr!='\0' || endptr==port || port_long < 0 || port_long > 65535) {
30 struct servent *tserv = getservbyname(port, protocol);
32 port_nr = tserv->s_port;
35 port_nr = htons(port_long);
42 void bb_lookup_host(struct sockaddr_in *s_in, const char *host)
46 memset(s_in, 0, sizeof(struct sockaddr_in));
47 s_in->sin_family = AF_INET;
48 he = xgethostbyname(host);
49 memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
52 int xconnect(struct sockaddr_in *s_addr)
54 int s = xsocket(AF_INET, SOCK_STREAM, 0);
55 if (connect(s, (struct sockaddr *)s_addr, sizeof(struct sockaddr_in)) < 0)
57 if (ENABLE_FEATURE_CLEAN_UP) close(s);
58 bb_perror_msg_and_die("unable to connect to remote host (%s)",
59 inet_ntoa(s_addr->sin_addr));