blob: cb5ac2c8ec00324c5a71f13fb80612e5bf673f15 [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 Vlasenkoa27a11b2007-08-18 14:16:39 +000012void setsockopt_reuseaddr(int fd)
Denis Vlasenko48237b02006-11-22 23:22:06 +000013{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +000014 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1));
Denis Vlasenko48237b02006-11-22 23:22:06 +000015}
16int setsockopt_broadcast(int fd)
17{
Denis Vlasenko703e2022007-01-22 14:12:08 +000018 return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
Denis Vlasenko48237b02006-11-22 23:22:06 +000019}
20
Denis Vlasenko9de420c2007-01-10 09:28:01 +000021void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000022{
Denis Vlasenko9de420c2007-01-10 09:28:01 +000023 if (connect(s, s_addr, addrlen) < 0) {
24 if (ENABLE_FEATURE_CLEAN_UP)
25 close(s);
26 if (s_addr->sa_family == AF_INET)
27 bb_perror_msg_and_die("%s (%s)",
28 "cannot connect to remote host",
29 inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
30 bb_perror_msg_and_die("cannot connect to remote host");
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000031 }
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000032}
33
Denis Vlasenko5d687242007-01-12 20:59:31 +000034/* Return port number for a service.
Denis Vlasenko9de420c2007-01-10 09:28:01 +000035 * If "port" is a number use it as the port.
36 * If "port" is a name it is looked up in /etc/services, if it isnt found return
37 * default_port */
38unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
39{
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000040 unsigned port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +000041 if (port) {
42 int old_errno;
43
44 /* Since this is a lib function, we're not allowed to reset errno to 0.
45 * Doing so could break an app that is deferring checking of errno. */
46 old_errno = errno;
47 port_nr = bb_strtou(port, NULL, 10);
48 if (errno || port_nr > 65535) {
49 struct servent *tserv = getservbyname(port, protocol);
Denis Vlasenko24182882007-01-13 21:05:38 +000050 port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +000051 if (tserv)
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000052 port_nr = ntohs(tserv->s_port);
Denis Vlasenko9de420c2007-01-10 09:28:01 +000053 }
54 errno = old_errno;
55 }
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000056 return (uint16_t)port_nr;
Denis Vlasenko9de420c2007-01-10 09:28:01 +000057}
58
59
60/* "Old" networking API - only IPv4 */
61
Denis Vlasenko85629f02007-01-22 09:36:41 +000062/*
Denis Vlasenko9de420c2007-01-10 09:28:01 +000063void bb_lookup_host(struct sockaddr_in *s_in, const char *host)
64{
65 struct hostent *he;
66
67 memset(s_in, 0, sizeof(struct sockaddr_in));
68 s_in->sin_family = AF_INET;
69 he = xgethostbyname(host);
70 memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
71}
72
Denis Vlasenko85629f02007-01-22 09:36:41 +000073
Denis Vlasenko9de420c2007-01-10 09:28:01 +000074int xconnect_tcp_v4(struct sockaddr_in *s_addr)
75{
76 int s = xsocket(AF_INET, SOCK_STREAM, 0);
77 xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
78 return s;
79}
Denis Vlasenko85629f02007-01-22 09:36:41 +000080*/
Denis Vlasenko9de420c2007-01-10 09:28:01 +000081
82/* "New" networking API */
83
84
Denis Vlasenko2856dab2007-04-01 01:18:20 +000085int get_nport(const struct sockaddr *sa)
Denis Vlasenko5d687242007-01-12 20:59:31 +000086{
87#if ENABLE_FEATURE_IPV6
Denis Vlasenko2856dab2007-04-01 01:18:20 +000088 if (sa->sa_family == AF_INET6) {
89 return ((struct sockaddr_in6*)sa)->sin6_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +000090 }
91#endif
Denis Vlasenko2856dab2007-04-01 01:18:20 +000092 if (sa->sa_family == AF_INET) {
93 return ((struct sockaddr_in*)sa)->sin_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +000094 }
Denis Vlasenko5d687242007-01-12 20:59:31 +000095 /* What? UNIX socket? IPX?? :) */
Denis Vlasenko85629f02007-01-22 09:36:41 +000096 return -1;
Denis Vlasenko5d687242007-01-12 20:59:31 +000097}
98
99void set_nport(len_and_sockaddr *lsa, unsigned port)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000100{
101#if ENABLE_FEATURE_IPV6
102 if (lsa->sa.sa_family == AF_INET6) {
103 lsa->sin6.sin6_port = port;
104 return;
105 }
106#endif
107 if (lsa->sa.sa_family == AF_INET) {
108 lsa->sin.sin_port = port;
109 return;
110 }
111 /* What? UNIX socket? IPX?? :) */
112}
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000113
Denis Vlasenko42823d52007-02-04 02:39:08 +0000114/* We hijack this constant to mean something else */
115/* It doesn't hurt because we will remove this bit anyway */
116#define DIE_ON_ERROR AI_CANONNAME
117
Denis Vlasenko85629f02007-01-22 09:36:41 +0000118/* host: "1.2.3.4[:port]", "www.google.com[:port]"
Denis Vlasenko42823d52007-02-04 02:39:08 +0000119 * port: if neither of above specifies port # */
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000120static len_and_sockaddr* str2sockaddr(
121 const char *host, int port,
122USE_FEATURE_IPV6(sa_family_t af,)
123 int ai_flags)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000124{
125 int rc;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000126 len_and_sockaddr *r = NULL;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000127 struct addrinfo *result = NULL;
128 const char *org_host = host; /* only for error msg */
129 const char *cp;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000130 struct addrinfo hint;
131
132 /* Ugly parsing of host:addr */
133 if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
134 host++;
135 cp = strchr(host, ']');
136 if (!cp || cp[1] != ':') /* Malformed: must have [xx]:nn */
137 bb_error_msg_and_die("bad address '%s'", org_host);
138 //return r; /* return NULL */
139 } else {
140 cp = strrchr(host, ':');
141 if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
142 /* There is more than one ':' (e.g. "::1") */
143 cp = NULL; /* it's not a port spec */
144 }
145 }
146 if (cp) {
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000147 int sz = cp - host + 1;
148 host = safe_strncpy(alloca(sz), host, sz);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000149 if (ENABLE_FEATURE_IPV6 && *cp != ':')
150 cp++; /* skip ']' */
151 cp++; /* skip ':' */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000152 port = xatou16(cp);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000153 }
154
155 memset(&hint, 0 , sizeof(hint));
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000156#if !ENABLE_FEATURE_IPV6
157 hint.ai_family = AF_INET; /* do not try to find IPv6 */
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000158#else
159 hint.ai_family = af;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000160#endif
161 /* Needed. Or else we will get each address thrice (or more)
162 * for each possible socket type (tcp,udp,raw...): */
163 hint.ai_socktype = SOCK_STREAM;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000164 hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000165 rc = getaddrinfo(host, NULL, &hint, &result);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000166 if (rc || !result) {
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000167 bb_error_msg("bad address '%s'", org_host);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000168 if (ai_flags & DIE_ON_ERROR)
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000169 xfunc_die();
Denis Vlasenko42823d52007-02-04 02:39:08 +0000170 goto ret;
171 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000172 r = xmalloc(offsetof(len_and_sockaddr, sa) + result->ai_addrlen);
173 r->len = result->ai_addrlen;
174 memcpy(&r->sa, result->ai_addr, result->ai_addrlen);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000175 set_nport(r, htons(port));
Denis Vlasenko42823d52007-02-04 02:39:08 +0000176 ret:
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000177 freeaddrinfo(result);
178 return r;
179}
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000180#if !ENABLE_FEATURE_IPV6
181#define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
182#endif
183
184#if ENABLE_FEATURE_IPV6
Denis Vlasenkoe9913032007-02-04 02:41:57 +0000185len_and_sockaddr* host_and_af2sockaddr(const char *host, int port, sa_family_t af)
186{
187 return str2sockaddr(host, port, af, 0);
188}
189
Denis Vlasenko42823d52007-02-04 02:39:08 +0000190len_and_sockaddr* xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000191{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000192 return str2sockaddr(host, port, af, DIE_ON_ERROR);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000193}
194#endif
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000195
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000196len_and_sockaddr* host2sockaddr(const char *host, int port)
197{
198 return str2sockaddr(host, port, AF_UNSPEC, 0);
199}
200
Denis Vlasenko42823d52007-02-04 02:39:08 +0000201len_and_sockaddr* xhost2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000202{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000203 return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000204}
205
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000206len_and_sockaddr* xdotted2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000207{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000208 return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000209}
210
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000211int xsocket_type(len_and_sockaddr **lsap, USE_FEATURE_IPV6(int family,) int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000212{
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000213 SKIP_FEATURE_IPV6(enum { family = AF_INET };)
Denis Vlasenko5d687242007-01-12 20:59:31 +0000214 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000215 int fd;
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000216 int len;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000217
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000218#if ENABLE_FEATURE_IPV6
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000219 if (family == AF_UNSPEC) {
220 fd = socket(AF_INET6, sock_type, 0);
221 if (fd >= 0) {
222 family = AF_INET6;
223 goto done;
224 }
225 family = AF_INET;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000226 }
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000227#endif
228 fd = xsocket(family, sock_type, 0);
229 len = sizeof(struct sockaddr_in);
230#if ENABLE_FEATURE_IPV6
231 if (family == AF_INET6) {
232 done:
233 len = sizeof(struct sockaddr_in6);
234 }
235#endif
Denis Vlasenko5d687242007-01-12 20:59:31 +0000236 lsa = xzalloc(offsetof(len_and_sockaddr, sa) + len);
237 lsa->len = len;
238 lsa->sa.sa_family = family;
239 *lsap = lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000240 return fd;
241}
242
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000243int xsocket_stream(len_and_sockaddr **lsap)
244{
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000245 return xsocket_type(lsap, USE_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000246}
247
248static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000249{
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000250 int fd;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000251 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000252
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000253 if (bindaddr && bindaddr[0]) {
Denis Vlasenko42823d52007-02-04 02:39:08 +0000254 lsa = xdotted2sockaddr(bindaddr, port);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000255 /* user specified bind addr dictates family */
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000256 fd = xsocket(lsa->sa.sa_family, sock_type, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000257 } else {
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000258 fd = xsocket_type(&lsa, USE_FEATURE_IPV6(AF_UNSPEC,) sock_type);
Denis Vlasenko24182882007-01-13 21:05:38 +0000259 set_nport(lsa, htons(port));
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000260 }
Denis Vlasenko48237b02006-11-22 23:22:06 +0000261 setsockopt_reuseaddr(fd);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000262 xbind(fd, &lsa->sa, lsa->len);
263 free(lsa);
264 return fd;
265}
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000266
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000267int create_and_bind_stream_or_die(const char *bindaddr, int port)
268{
269 return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
270}
271
272int create_and_bind_dgram_or_die(const char *bindaddr, int port)
273{
274 return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
275}
276
277
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000278int create_and_connect_stream_or_die(const char *peer, int port)
279{
280 int fd;
281 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000282
Denis Vlasenko42823d52007-02-04 02:39:08 +0000283 lsa = xhost2sockaddr(peer, port);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000284 fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
285 setsockopt_reuseaddr(fd);
286 xconnect(fd, &lsa->sa, lsa->len);
287 free(lsa);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000288 return fd;
289}
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000290
291int xconnect_stream(const len_and_sockaddr *lsa)
292{
293 int fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
294 xconnect(fd, &lsa->sa, lsa->len);
295 return fd;
296}
297
Denis Vlasenko448f0242007-01-22 22:43:05 +0000298/* We hijack this constant to mean something else */
299/* It doesn't hurt because we will add this bit anyway */
300#define IGNORE_PORT NI_NUMERICSERV
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000301static char* sockaddr2str(const struct sockaddr *sa, int flags)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000302{
303 char host[128];
304 char serv[16];
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000305 int rc;
306 socklen_t salen;
307
308 salen = LSA_SIZEOF_SA;
309#if ENABLE_FEATURE_IPV6
310 if (sa->sa_family == AF_INET)
311 salen = sizeof(struct sockaddr_in);
312 if (sa->sa_family == AF_INET6)
313 salen = sizeof(struct sockaddr_in6);
314#endif
315 rc = getnameinfo(sa, salen,
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000316 host, sizeof(host),
Denis Vlasenko448f0242007-01-22 22:43:05 +0000317 /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000318 serv, sizeof(serv),
Denis Vlasenko85629f02007-01-22 09:36:41 +0000319 /* do not resolve port# into service _name_ */
320 flags | NI_NUMERICSERV
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000321 );
Denis Vlasenko85629f02007-01-22 09:36:41 +0000322 if (rc)
323 return NULL;
Denis Vlasenko448f0242007-01-22 22:43:05 +0000324 if (flags & IGNORE_PORT)
325 return xstrdup(host);
Denis Vlasenko85629f02007-01-22 09:36:41 +0000326#if ENABLE_FEATURE_IPV6
Denis Vlasenko448f0242007-01-22 22:43:05 +0000327 if (sa->sa_family == AF_INET6) {
328 if (strchr(host, ':')) /* heh, it's not a resolved hostname */
329 return xasprintf("[%s]:%s", host, serv);
330 /*return xasprintf("%s:%s", host, serv);*/
331 /* - fall through instead */
332 }
Denis Vlasenko85629f02007-01-22 09:36:41 +0000333#endif
334 /* For now we don't support anything else, so it has to be INET */
335 /*if (sa->sa_family == AF_INET)*/
336 return xasprintf("%s:%s", host, serv);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000337 /*return xstrdup(host);*/
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000338}
339
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000340char* xmalloc_sockaddr2host(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000341{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000342 return sockaddr2str(sa, 0);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000343}
344
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000345char* xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000346{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000347 return sockaddr2str(sa, IGNORE_PORT);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000348}
349
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000350char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000351{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000352 return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000353}
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000354char* xmalloc_sockaddr2dotted(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000355{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000356 return sockaddr2str(sa, NI_NUMERICHOST);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000357}
Denis Vlasenko448f0242007-01-22 22:43:05 +0000358
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000359char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000360{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000361 return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000362}