blob: 04259f47b88a7aa649196a4bb0a22805c56f0bb1 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc223ced2001-11-10 12:18:42 +00002/*
3 * stolen from net-tools-1.59 and stripped down for busybox by
Eric Andersencb81e642003-07-14 21:21:08 +00004 * Erik Andersen <andersen@codepoet.org>
Eric Andersenc223ced2001-11-10 12:18:42 +00005 *
6 * Heavily modified by Manuel Novoa III Mar 12, 2001
7 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersenc223ced2001-11-10 12:18:42 +00009 */
10
Rob Landleyea224be2006-06-18 20:20:07 +000011#include "libbb.h"
Eric Andersenc223ced2001-11-10 12:18:42 +000012#include "inet_common.h"
Eric Andersen77d22c42002-06-06 12:11:55 +000013
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +020014#if 0
15# define dbg(...) bb_error_msg(__VA_ARGS__)
16#else
17# define dbg(...) ((void)0)
18#endif
19
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000020int FAST_FUNC INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
Eric Andersenc223ced2001-11-10 12:18:42 +000021{
Glenn L McGrath30db4232002-11-28 09:52:23 +000022 struct hostent *hp;
Denis Vlasenkoa61cb922007-06-19 11:24:47 +000023#if ENABLE_FEATURE_ETC_NETWORKS
Glenn L McGrath30db4232002-11-28 09:52:23 +000024 struct netent *np;
Denis Vlasenkoa61cb922007-06-19 11:24:47 +000025#endif
Eric Andersenc223ced2001-11-10 12:18:42 +000026
Glenn L McGrath30db4232002-11-28 09:52:23 +000027 /* Grmpf. -FvK */
28 s_in->sin_family = AF_INET;
29 s_in->sin_port = 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000030
Glenn L McGrath30db4232002-11-28 09:52:23 +000031 /* Default is special, meaning 0.0.0.0. */
Denys Vlasenko0004e992010-09-01 12:01:17 +020032 if (strcmp(name, "default") == 0) {
Glenn L McGrath30db4232002-11-28 09:52:23 +000033 s_in->sin_addr.s_addr = INADDR_ANY;
Denis Vlasenko7ca39212006-11-21 20:34:21 +000034 return 1;
Glenn L McGrath30db4232002-11-28 09:52:23 +000035 }
36 /* Look to see if it's a dotted quad. */
37 if (inet_aton(name, &s_in->sin_addr)) {
38 return 0;
39 }
40 /* If we expect this to be a hostname, try hostname database first */
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010041 if (hostfirst) {
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +020042 dbg("gethostbyname(%s)", name);
Denis Vlasenko703e2022007-01-22 14:12:08 +000043 hp = gethostbyname(name);
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010044 if (hp) {
Denis Vlasenko703e2022007-01-22 14:12:08 +000045 memcpy(&s_in->sin_addr, hp->h_addr_list[0],
46 sizeof(struct in_addr));
47 return 0;
48 }
Glenn L McGrath30db4232002-11-28 09:52:23 +000049 }
Denis Vlasenkoa61cb922007-06-19 11:24:47 +000050#if ENABLE_FEATURE_ETC_NETWORKS
Glenn L McGrath30db4232002-11-28 09:52:23 +000051 /* Try the NETWORKS database to see if this is a known network. */
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +020052 dbg("getnetbyname(%s)", name);
Denis Vlasenko703e2022007-01-22 14:12:08 +000053 np = getnetbyname(name);
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010054 if (np) {
Glenn L McGrath30db4232002-11-28 09:52:23 +000055 s_in->sin_addr.s_addr = htonl(np->n_net);
56 return 1;
57 }
Denis Vlasenkoa61cb922007-06-19 11:24:47 +000058#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +000059 if (hostfirst) {
60 /* Don't try again */
Glenn L McGrath30db4232002-11-28 09:52:23 +000061 return -1;
62 }
63#ifdef DEBUG
64 res_init();
65 _res.options |= RES_DEBUG;
Glenn L McGrath30db4232002-11-28 09:52:23 +000066#endif
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +020067 dbg("gethostbyname(%s)", name);
Denis Vlasenko703e2022007-01-22 14:12:08 +000068 hp = gethostbyname(name);
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010069 if (!hp) {
Glenn L McGrath30db4232002-11-28 09:52:23 +000070 return -1;
71 }
Denis Vlasenko703e2022007-01-22 14:12:08 +000072 memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
Eric Andersenc223ced2001-11-10 12:18:42 +000073 return 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000074}
75
Eric Andersenc223ced2001-11-10 12:18:42 +000076
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010077/* numeric: & 0x8000: "default" instead of "*",
Eric Andersenc223ced2001-11-10 12:18:42 +000078 * & 0x4000: host instead of net,
79 * & 0x0fff: don't resolve
80 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000081char* FAST_FUNC INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask)
Eric Andersenc223ced2001-11-10 12:18:42 +000082{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +000083 /* addr-to-name cache */
84 struct addr {
85 struct addr *next;
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010086 uint32_t nip;
87 smallint is_host;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +000088 char name[1];
89 };
90 static struct addr *cache = NULL;
91
Glenn L McGrath30db4232002-11-28 09:52:23 +000092 struct addr *pn;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +000093 char *name;
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010094 uint32_t nip;
95 smallint is_host;
Eric Andersenc223ced2001-11-10 12:18:42 +000096
Glenn L McGrath30db4232002-11-28 09:52:23 +000097 if (s_in->sin_family != AF_INET) {
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +020098 dbg("rresolve: unsupported address family %d!", s_in->sin_family);
Glenn L McGrath30db4232002-11-28 09:52:23 +000099 errno = EAFNOSUPPORT;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000100 return NULL;
Eric Andersenc223ced2001-11-10 12:18:42 +0000101 }
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100102 nip = s_in->sin_addr.s_addr;
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +0200103 dbg("rresolve: %08x mask:%08x num:%08x", (unsigned)nip, netmask, numeric);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000104 if (numeric & 0x0FFF)
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100105 return xmalloc_sockaddr2dotted_noport((void*)s_in);
106 if (nip == INADDR_ANY) {
107 if (numeric & 0x8000)
108 return xstrdup("default");
109 return xstrdup("*");
110 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000111
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100112 is_host = ((nip & (~netmask)) != 0 || (numeric & 0x4000));
113
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000114 pn = cache;
115 while (pn) {
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100116 if (pn->nip == nip && pn->is_host == is_host) {
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +0200117 dbg("rresolve: found %s %08x in cache",
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100118 (is_host ? "host" : "net"), (unsigned)nip);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000119 return xstrdup(pn->name);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000120 }
121 pn = pn->next;
Eric Andersenc223ced2001-11-10 12:18:42 +0000122 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000123
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000124 name = NULL;
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100125 if (is_host) {
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +0200126 dbg("sockaddr2host_noport(%08x)", (unsigned)nip);
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100127 name = xmalloc_sockaddr2host_noport((void*)s_in);
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +0200128 }
129#if ENABLE_FEATURE_ETC_NETWORKS
130 else {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000131 struct netent *np;
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +0200132 dbg("getnetbyaddr(%08x)", (unsigned)ntohl(nip));
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100133 np = getnetbyaddr(ntohl(nip), AF_INET);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000134 if (np)
135 name = xstrdup(np->n_name);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000136 }
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +0200137#endif
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000138 if (!name)
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100139 name = xmalloc_sockaddr2dotted_noport((void*)s_in);
140
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000141 pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
142 pn->next = cache;
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100143 pn->nip = nip;
144 pn->is_host = is_host;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000145 strcpy(pn->name, name);
146 cache = pn;
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100147
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000148 return name;
Eric Andersenc223ced2001-11-10 12:18:42 +0000149}
Eric Andersen51b8bd62002-07-03 11:46:38 +0000150
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000151#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000152
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000153int FAST_FUNC INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000154{
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100155 struct addrinfo req, *ai = NULL;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000156 int s;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000157
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100158 memset(&req, 0, sizeof(req));
Glenn L McGrath30db4232002-11-28 09:52:23 +0000159 req.ai_family = AF_INET6;
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000160 s = getaddrinfo(name, NULL, &req, &ai);
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100161 if (s != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000162 bb_error_msg("getaddrinfo: %s: %d", name, s);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000163 return -1;
164 }
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100165 memcpy(sin6, ai->ai_addr, sizeof(*sin6));
Denys Vlasenko1a7256a2013-11-29 16:39:28 +0100166 freeaddrinfo(ai);
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000167 return 0;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000168}
169
170#ifndef IN6_IS_ADDR_UNSPECIFIED
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000171# define IN6_IS_ADDR_UNSPECIFIED(a) \
Bernhard Reutner-Fischer38583fd2006-05-19 12:44:16 +0000172 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
173 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000174#endif
175
176
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000177char* FAST_FUNC INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000178{
Glenn L McGrath30db4232002-11-28 09:52:23 +0000179 if (sin6->sin6_family != AF_INET6) {
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +0200180 dbg("rresolve: unsupported address family %d!",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100181 sin6->sin6_family);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000182 errno = EAFNOSUPPORT;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000183 return NULL;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000184 }
185 if (numeric & 0x7FFF) {
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100186 return xmalloc_sockaddr2dotted_noport((void*)sin6);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000187 }
188 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000189 if (numeric & 0x8000)
Denys Vlasenko0004e992010-09-01 12:01:17 +0200190 return xstrdup("default");
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000191 return xstrdup("*");
Glenn L McGrath30db4232002-11-28 09:52:23 +0000192 }
Eric Andersen51b8bd62002-07-03 11:46:38 +0000193
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100194 return xmalloc_sockaddr2host_noport((void*)sin6);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000195}
196
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200197#endif /* CONFIG_FEATURE_IPV6 */