blob: 6e85322cf2fa132b947c4766d7f3f4b9cdf62acf [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
Denis Vlasenko9de420c2007-01-10 09:28:01 +00009#include <netinet/in.h>
Eric Andersen0b315862002-07-03 11:51:44 +000010#include "libbb.h"
11
Denis Vlasenko48237b02006-11-22 23:22:06 +000012static const int one = 1;
13int setsockopt_reuseaddr(int fd)
14{
15 return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
16}
17int setsockopt_broadcast(int fd)
18{
19 return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
20}
21
Denis Vlasenko9de420c2007-01-10 09:28:01 +000022void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000023{
Denis Vlasenko9de420c2007-01-10 09:28:01 +000024 if (connect(s, s_addr, addrlen) < 0) {
25 if (ENABLE_FEATURE_CLEAN_UP)
26 close(s);
27 if (s_addr->sa_family == AF_INET)
28 bb_perror_msg_and_die("%s (%s)",
29 "cannot connect to remote host",
30 inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
31 bb_perror_msg_and_die("cannot connect to remote host");
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000032 }
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000033}
34
Denis Vlasenko9de420c2007-01-10 09:28:01 +000035/* Return network byte ordered port number for a service.
36 * If "port" is a number use it as the port.
37 * If "port" is a name it is looked up in /etc/services, if it isnt found return
38 * default_port */
39unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
40{
41 unsigned port_nr = htons(default_port);
42 if (port) {
43 int old_errno;
44
45 /* Since this is a lib function, we're not allowed to reset errno to 0.
46 * Doing so could break an app that is deferring checking of errno. */
47 old_errno = errno;
48 port_nr = bb_strtou(port, NULL, 10);
49 if (errno || port_nr > 65535) {
50 struct servent *tserv = getservbyname(port, protocol);
51 if (tserv)
52 port_nr = tserv->s_port;
53 } else {
54 port_nr = htons(port_nr);
55 }
56 errno = old_errno;
57 }
58 return port_nr;
59}
60
61
62/* "Old" networking API - only IPv4 */
63
64
65void bb_lookup_host(struct sockaddr_in *s_in, const char *host)
66{
67 struct hostent *he;
68
69 memset(s_in, 0, sizeof(struct sockaddr_in));
70 s_in->sin_family = AF_INET;
71 he = xgethostbyname(host);
72 memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
73}
74
75int xconnect_tcp_v4(struct sockaddr_in *s_addr)
76{
77 int s = xsocket(AF_INET, SOCK_STREAM, 0);
78 xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
79 return s;
80}
81
82
83/* "New" networking API */
84
85
Denis Vlasenko9de420c2007-01-10 09:28:01 +000086//extern int xsocket_stream_ip4or6(sa_family_t *fp);
Denis Vlasenko9de420c2007-01-10 09:28:01 +000087//extern len_and_sockaddr* dotted2sockaddr(const char *dotted, int def_port);
88
89/* peer: "1.2.3.4[:port]", "www.google.com[:port]"
Denis Vlasenkobbe51462007-01-10 20:19:56 +000090 * port: if neither of above specifies port #
Denis Vlasenko9de420c2007-01-10 09:28:01 +000091 */
Denis Vlasenkobbe51462007-01-10 20:19:56 +000092static len_and_sockaddr* str2sockaddr(const char *host, int port, int ai_flags)
Denis Vlasenko9de420c2007-01-10 09:28:01 +000093{
94 int rc;
95 len_and_sockaddr *r; // = NULL;
96 struct addrinfo *result = NULL;
97 const char *org_host = host; /* only for error msg */
98 const char *cp;
99 char service[sizeof(int)*3 + 1];
100 struct addrinfo hint;
101
102 /* Ugly parsing of host:addr */
103 if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
104 host++;
105 cp = strchr(host, ']');
106 if (!cp || cp[1] != ':') /* Malformed: must have [xx]:nn */
107 bb_error_msg_and_die("bad address '%s'", org_host);
108 //return r; /* return NULL */
109 } else {
110 cp = strrchr(host, ':');
111 if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
112 /* There is more than one ':' (e.g. "::1") */
113 cp = NULL; /* it's not a port spec */
114 }
115 }
116 if (cp) {
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000117 int sz = cp - host + 1;
118 host = safe_strncpy(alloca(sz), host, sz);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000119 if (ENABLE_FEATURE_IPV6 && *cp != ':')
120 cp++; /* skip ']' */
121 cp++; /* skip ':' */
122 } else {
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000123 utoa_to_buf(port, service, sizeof(service));
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000124 cp = service;
125 }
126
127 memset(&hint, 0 , sizeof(hint));
128 /* hint.ai_family = AF_UNSPEC; - zero anyway */
129#if !ENABLE_FEATURE_IPV6
130 hint.ai_family = AF_INET; /* do not try to find IPv6 */
131#endif
132 /* Needed. Or else we will get each address thrice (or more)
133 * for each possible socket type (tcp,udp,raw...): */
134 hint.ai_socktype = SOCK_STREAM;
135 hint.ai_flags = ai_flags | AI_NUMERICSERV;
136 rc = getaddrinfo(host, cp, &hint, &result);
137 if (rc || !result)
138 bb_error_msg_and_die("bad address '%s'", org_host);
139 r = xmalloc(offsetof(len_and_sockaddr, sa) + result->ai_addrlen);
140 r->len = result->ai_addrlen;
141 memcpy(&r->sa, result->ai_addr, result->ai_addrlen);
142 freeaddrinfo(result);
143 return r;
144}
145
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000146len_and_sockaddr* host2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000147{
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000148 return str2sockaddr(host, port, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000149}
150
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000151static len_and_sockaddr* dotted2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000152{
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000153 return str2sockaddr(host, port, NI_NUMERICHOST);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000154}
155
156static int xsocket_stream_ip4or6(len_and_sockaddr *lsa)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000157{
158 int fd;
159#if ENABLE_FEATURE_IPV6
160 fd = socket(AF_INET6, SOCK_STREAM, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000161 lsa->sa.sa_family = AF_INET6;
162 lsa->len = sizeof(struct sockaddr_in6);
163 if (fd >= 0)
164 return fd;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000165#endif
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000166 fd = xsocket(AF_INET, SOCK_STREAM, 0);
167 lsa->sa.sa_family = AF_INET;
168 lsa->len = sizeof(struct sockaddr_in);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000169 return fd;
170}
171
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000172int create_and_bind_stream_or_die(const char *bindaddr, int port)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000173{
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000174 int fd;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000175 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000176
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000177 if (bindaddr && bindaddr[0]) {
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000178 lsa = dotted2sockaddr(bindaddr, port);
179 /* currently NULL check is in str2sockaddr */
180 //if (!lsa)
181 // bb_error_msg_and_die("bad address '%s'", bindaddr);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000182 /* user specified bind addr dictates family */
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000183 fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
184 } else {
185 lsa = xzalloc(offsetof(len_and_sockaddr, sa) +
186 USE_FEATURE_IPV6(sizeof(struct sockaddr_in6))
187 SKIP_FEATURE_IPV6(sizeof(struct sockaddr_in))
188 );
189 fd = xsocket_stream_ip4or6(lsa);
190 }
Denis Vlasenko48237b02006-11-22 23:22:06 +0000191 setsockopt_reuseaddr(fd);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000192 xbind(fd, &lsa->sa, lsa->len);
193 free(lsa);
194 return fd;
195}
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000196
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000197int create_and_connect_stream_or_die(const char *peer, int port)
198{
199 int fd;
200 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000201
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000202 lsa = host2sockaddr(peer, port);
203 /* currently NULL check is in str2sockaddr */
204 //if (!lsa)
205 // bb_error_msg_and_die("bad address '%s'", peer);
206 fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
207 setsockopt_reuseaddr(fd);
208 xconnect(fd, &lsa->sa, lsa->len);
209 free(lsa);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000210 return fd;
211}