blob: bc0691531dbba55056e912ab768b3ecd0c20c8b9 [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
Denis Vlasenko14579152006-10-26 01:09:46 +000052void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
53{
54 if (connect(s, s_addr, addrlen) < 0) {
55 if (ENABLE_FEATURE_CLEAN_UP) close(s);
56 if (s_addr->sa_family == AF_INET)
Denis Vlasenko1dc1b372006-12-23 02:48:44 +000057 bb_perror_msg_and_die("%s (%s)",
58 "cannot connect to remote host",
Denis Vlasenko14579152006-10-26 01:09:46 +000059 inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
Denis Vlasenko89f0b342006-11-18 22:04:09 +000060 bb_perror_msg_and_die("cannot connect to remote host");
Denis Vlasenko14579152006-10-26 01:09:46 +000061 }
62}
63
64int xconnect_tcp_v4(struct sockaddr_in *s_addr)
Eric Andersene6dc4392003-10-31 09:31:46 +000065{
Rob Landleyd921b2e2006-08-03 15:41:12 +000066 int s = xsocket(AF_INET, SOCK_STREAM, 0);
Denis Vlasenko14579152006-10-26 01:09:46 +000067 xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
Eric Andersen0b315862002-07-03 11:51:44 +000068 return s;
Eric Andersen0b315862002-07-03 11:51:44 +000069}
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000070
Denis Vlasenko48237b02006-11-22 23:22:06 +000071static const int one = 1;
72int setsockopt_reuseaddr(int fd)
73{
74 return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
75}
76int setsockopt_broadcast(int fd)
77{
78 return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
79}
80
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000081int dotted2sockaddr(const char *dotted, struct sockaddr* sp, int socklen)
82{
83 union {
84 struct in_addr a4;
85#if ENABLE_FEATURE_IPV6
86 struct in6_addr a6;
87#endif
88 } a;
89
90 /* TODO maybe: port spec? like n.n.n.n:nn */
91
92#if ENABLE_FEATURE_IPV6
93 if (socklen >= sizeof(struct sockaddr_in6)
94 && inet_pton(AF_INET6, dotted, &a.a6) > 0
95 ) {
96 ((struct sockaddr_in6*)sp)->sin6_family = AF_INET6;
97 ((struct sockaddr_in6*)sp)->sin6_addr = a.a6;
98 /* ((struct sockaddr_in6*)sp)->sin6_port = */
99 return 0; /* success */
100 }
101#endif
102 if (socklen >= sizeof(struct sockaddr_in)
103 && inet_pton(AF_INET, dotted, &a.a4) > 0
104 ) {
105 ((struct sockaddr_in*)sp)->sin_family = AF_INET;
106 ((struct sockaddr_in*)sp)->sin_addr = a.a4;
107 /* ((struct sockaddr_in*)sp)->sin_port = */
108 return 0; /* success */
109 }
110 return 1;
111}
112
113int xsocket_stream_ip4or6(sa_family_t *fp)
114{
115 int fd;
116#if ENABLE_FEATURE_IPV6
117 fd = socket(AF_INET6, SOCK_STREAM, 0);
118 if (fp) *fp = AF_INET6;
119 if (fd < 0)
120#endif
121 {
122 fd = xsocket(AF_INET, SOCK_STREAM, 0);
123 if (fp) *fp = AF_INET;
124 }
125 return fd;
126}
127
128int create_and_bind_socket_ip4or6(const char *hostaddr, int port)
129{
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000130 int fd;
131 sockaddr_inet sa;
132
133 memset(&sa, 0, sizeof(sa));
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000134 if (hostaddr) {
135 if (dotted2sockaddr(hostaddr, &sa.sa, sizeof(sa)))
136 bb_error_msg_and_die("bad address '%s'", hostaddr);
137 /* user specified bind addr dictates family */
138 fd = xsocket(sa.sa.sa_family, SOCK_STREAM, 0);
139 } else
140 fd = xsocket_stream_ip4or6(&sa.sa.sa_family);
Denis Vlasenko48237b02006-11-22 23:22:06 +0000141 setsockopt_reuseaddr(fd);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000142
143 /* if (port >= 0) { */
144#if ENABLE_FEATURE_IPV6
145 if (sa.sa.sa_family == AF_INET6 /* && !sa.sin6.sin6_port */)
146 sa.sin6.sin6_port = htons(port);
147#endif
148 if (sa.sa.sa_family == AF_INET /* && !sa.sin.sin_port */)
149 sa.sin.sin_port = htons(port);
150 /* } */
151
152 xbind(fd, &sa.sa, sizeof(sa));
153 return fd;
154}