blob: f88136a1a0eb92b9fc5187779725082a2f65288c [file] [log] [blame]
Eric Andersen0b315862002-07-03 11:51:44 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenaff114c2004-04-14 17:51:38 +00005 * Connect to host at port using address resolution from getaddrinfo
Eric Andersen0b315862002-07-03 11:51:44 +00006 *
7 */
8
Eric Andersen0b315862002-07-03 11:51:44 +00009#include "libbb.h"
10
Glenn L McGrathffccf6e2003-12-20 01:47:18 +000011/* 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
14 * default_port
15 */
Glenn L McGrath036dbaa2004-01-17 05:03:31 +000016unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port)
Eric Andersen0b315862002-07-03 11:51:44 +000017{
Glenn L McGrathffccf6e2003-12-20 01:47:18 +000018 unsigned short port_nr = htons(default_port);
19 if (port) {
Eric Andersen9cccc182003-12-23 20:37:23 +000020 char *endptr;
21 int old_errno;
22 long port_long;
Eric Andersen0b315862002-07-03 11:51:44 +000023
Eric Andersen9cccc182003-12-23 20:37:23 +000024 /* 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. */
26 old_errno = errno;
27 errno = 0;
28 port_long = strtol(port, &endptr, 10);
Glenn L McGrathffccf6e2003-12-20 01:47:18 +000029 if (errno != 0 || *endptr!='\0' || endptr==port || port_long < 0 || port_long > 65535) {
Glenn L McGrath036dbaa2004-01-17 05:03:31 +000030 struct servent *tserv = getservbyname(port, protocol);
Glenn L McGrathffccf6e2003-12-20 01:47:18 +000031 if (tserv) {
Eric Andersen3a5ed562003-12-23 20:37:54 +000032 port_nr = tserv->s_port;
33 }
34 } else {
Glenn L McGrathffccf6e2003-12-20 01:47:18 +000035 port_nr = htons(port_long);
36 }
Eric Andersen9cccc182003-12-23 20:37:23 +000037 errno = old_errno;
Eric Andersene6dc4392003-10-31 09:31:46 +000038 }
39 return port_nr;
40}
41
Glenn L McGrathffccf6e2003-12-20 01:47:18 +000042void bb_lookup_host(struct sockaddr_in *s_in, const char *host)
Eric Andersene6dc4392003-10-31 09:31:46 +000043{
44 struct hostent *he;
45
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);
Eric Andersene6dc4392003-10-31 09:31:46 +000050}
51
52int xconnect(struct sockaddr_in *s_addr)
53{
Rob Landleyd921b2e2006-08-03 15:41:12 +000054 int s = xsocket(AF_INET, SOCK_STREAM, 0);
"Vladimir N. Oleynik"06776b32006-02-17 09:33:22 +000055 if (connect(s, (struct sockaddr *)s_addr, sizeof(struct sockaddr_in)) < 0)
Eric Andersene6dc4392003-10-31 09:31:46 +000056 {
Rob Landley64175642005-08-22 15:57:50 +000057 if (ENABLE_FEATURE_CLEAN_UP) close(s);
Denis Vlasenkoa94554d2006-09-23 17:49:09 +000058 bb_perror_msg_and_die("unable to connect to remote host (%s)",
Eric Andersene6dc4392003-10-31 09:31:46 +000059 inet_ntoa(s_addr->sin_addr));
Eric Andersen0b315862002-07-03 11:51:44 +000060 }
61 return s;
Eric Andersen0b315862002-07-03 11:51:44 +000062}