blob: e9fbde7dcb171fcc13df6a784983f1a48084643c [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 */
Rob Landleyea224be2006-06-18 20:20:07 +000010#include "libbb.h"
Eric Andersenc223ced2001-11-10 12:18:42 +000011#include "inet_common.h"
Eric Andersen77d22c42002-06-06 12:11:55 +000012
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +020013#if 0
14# define dbg(...) bb_error_msg(__VA_ARGS__)
15#else
16# define dbg(...) ((void)0)
17#endif
18
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000019int FAST_FUNC INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
Eric Andersenc223ced2001-11-10 12:18:42 +000020{
Glenn L McGrath30db4232002-11-28 09:52:23 +000021 struct hostent *hp;
Denis Vlasenkoa61cb922007-06-19 11:24:47 +000022#if ENABLE_FEATURE_ETC_NETWORKS
Glenn L McGrath30db4232002-11-28 09:52:23 +000023 struct netent *np;
Denis Vlasenkoa61cb922007-06-19 11:24:47 +000024#endif
Eric Andersenc223ced2001-11-10 12:18:42 +000025
Glenn L McGrath30db4232002-11-28 09:52:23 +000026 /* Grmpf. -FvK */
27 s_in->sin_family = AF_INET;
28 s_in->sin_port = 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000029
Glenn L McGrath30db4232002-11-28 09:52:23 +000030 /* Default is special, meaning 0.0.0.0. */
Denys Vlasenko0004e992010-09-01 12:01:17 +020031 if (strcmp(name, "default") == 0) {
Glenn L McGrath30db4232002-11-28 09:52:23 +000032 s_in->sin_addr.s_addr = INADDR_ANY;
Denis Vlasenko7ca39212006-11-21 20:34:21 +000033 return 1;
Glenn L McGrath30db4232002-11-28 09:52:23 +000034 }
35 /* Look to see if it's a dotted quad. */
36 if (inet_aton(name, &s_in->sin_addr)) {
37 return 0;
38 }
39 /* If we expect this to be a hostname, try hostname database first */
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010040 if (hostfirst) {
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +020041 dbg("gethostbyname(%s)", name);
Denis Vlasenko703e2022007-01-22 14:12:08 +000042 hp = gethostbyname(name);
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010043 if (hp) {
Denis Vlasenko703e2022007-01-22 14:12:08 +000044 memcpy(&s_in->sin_addr, hp->h_addr_list[0],
45 sizeof(struct in_addr));
46 return 0;
47 }
Glenn L McGrath30db4232002-11-28 09:52:23 +000048 }
Denis Vlasenkoa61cb922007-06-19 11:24:47 +000049#if ENABLE_FEATURE_ETC_NETWORKS
Glenn L McGrath30db4232002-11-28 09:52:23 +000050 /* Try the NETWORKS database to see if this is a known network. */
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +020051 dbg("getnetbyname(%s)", name);
Denis Vlasenko703e2022007-01-22 14:12:08 +000052 np = getnetbyname(name);
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010053 if (np) {
Glenn L McGrath30db4232002-11-28 09:52:23 +000054 s_in->sin_addr.s_addr = htonl(np->n_net);
55 return 1;
56 }
Denis Vlasenkoa61cb922007-06-19 11:24:47 +000057#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +000058 if (hostfirst) {
59 /* Don't try again */
Glenn L McGrath30db4232002-11-28 09:52:23 +000060 return -1;
61 }
62#ifdef DEBUG
63 res_init();
64 _res.options |= RES_DEBUG;
Glenn L McGrath30db4232002-11-28 09:52:23 +000065#endif
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +020066 dbg("gethostbyname(%s)", name);
Denis Vlasenko703e2022007-01-22 14:12:08 +000067 hp = gethostbyname(name);
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010068 if (!hp) {
Glenn L McGrath30db4232002-11-28 09:52:23 +000069 return -1;
70 }
Denis Vlasenko703e2022007-01-22 14:12:08 +000071 memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
Eric Andersenc223ced2001-11-10 12:18:42 +000072 return 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000073}
74
Eric Andersenc223ced2001-11-10 12:18:42 +000075
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010076/* numeric: & 0x8000: "default" instead of "*",
Eric Andersenc223ced2001-11-10 12:18:42 +000077 * & 0x4000: host instead of net,
78 * & 0x0fff: don't resolve
79 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000080char* FAST_FUNC INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask)
Eric Andersenc223ced2001-11-10 12:18:42 +000081{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +000082 /* addr-to-name cache */
83 struct addr {
84 struct addr *next;
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010085 uint32_t nip;
86 smallint is_host;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +000087 char name[1];
88 };
89 static struct addr *cache = NULL;
90
Glenn L McGrath30db4232002-11-28 09:52:23 +000091 struct addr *pn;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +000092 char *name;
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010093 uint32_t nip;
94 smallint is_host;
Eric Andersenc223ced2001-11-10 12:18:42 +000095
Glenn L McGrath30db4232002-11-28 09:52:23 +000096 if (s_in->sin_family != AF_INET) {
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +020097 dbg("rresolve: unsupported address family %d!", s_in->sin_family);
Glenn L McGrath30db4232002-11-28 09:52:23 +000098 errno = EAFNOSUPPORT;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +000099 return NULL;
Eric Andersenc223ced2001-11-10 12:18:42 +0000100 }
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100101 nip = s_in->sin_addr.s_addr;
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +0200102 dbg("rresolve: %08x mask:%08x num:%08x", (unsigned)nip, netmask, numeric);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000103 if (numeric & 0x0FFF)
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100104 return xmalloc_sockaddr2dotted_noport((void*)s_in);
105 if (nip == INADDR_ANY) {
106 if (numeric & 0x8000)
107 return xstrdup("default");
108 return xstrdup("*");
109 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000110
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100111 is_host = ((nip & (~netmask)) != 0 || (numeric & 0x4000));
112
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000113 pn = cache;
114 while (pn) {
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100115 if (pn->nip == nip && pn->is_host == is_host) {
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +0200116 dbg("rresolve: found %s %08x in cache",
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100117 (is_host ? "host" : "net"), (unsigned)nip);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000118 return xstrdup(pn->name);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000119 }
120 pn = pn->next;
Eric Andersenc223ced2001-11-10 12:18:42 +0000121 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000122
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000123 name = NULL;
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100124 if (is_host) {
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +0200125 dbg("sockaddr2host_noport(%08x)", (unsigned)nip);
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100126 name = xmalloc_sockaddr2host_noport((void*)s_in);
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +0200127 }
128#if ENABLE_FEATURE_ETC_NETWORKS
129 else {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000130 struct netent *np;
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +0200131 dbg("getnetbyaddr(%08x)", (unsigned)ntohl(nip));
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100132 np = getnetbyaddr(ntohl(nip), AF_INET);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000133 if (np)
134 name = xstrdup(np->n_name);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000135 }
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +0200136#endif
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000137 if (!name)
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100138 name = xmalloc_sockaddr2dotted_noport((void*)s_in);
139
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000140 pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
141 pn->next = cache;
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100142 pn->nip = nip;
143 pn->is_host = is_host;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000144 strcpy(pn->name, name);
145 cache = pn;
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100146
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000147 return name;
Eric Andersenc223ced2001-11-10 12:18:42 +0000148}
Eric Andersen51b8bd62002-07-03 11:46:38 +0000149
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000150#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000151
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000152int FAST_FUNC INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000153{
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100154 struct addrinfo req, *ai = NULL;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000155 int s;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000156
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100157 memset(&req, 0, sizeof(req));
Glenn L McGrath30db4232002-11-28 09:52:23 +0000158 req.ai_family = AF_INET6;
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000159 s = getaddrinfo(name, NULL, &req, &ai);
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100160 if (s != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000161 bb_error_msg("getaddrinfo: %s: %d", name, s);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000162 return -1;
163 }
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100164 memcpy(sin6, ai->ai_addr, sizeof(*sin6));
Denys Vlasenko1a7256a2013-11-29 16:39:28 +0100165 freeaddrinfo(ai);
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000166 return 0;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000167}
168
169#ifndef IN6_IS_ADDR_UNSPECIFIED
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000170# define IN6_IS_ADDR_UNSPECIFIED(a) \
Bernhard Reutner-Fischer38583fd2006-05-19 12:44:16 +0000171 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
172 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000173#endif
174
175
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000176char* FAST_FUNC INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000177{
Glenn L McGrath30db4232002-11-28 09:52:23 +0000178 if (sin6->sin6_family != AF_INET6) {
Denys Vlasenko7d7c7bb2017-07-14 20:17:17 +0200179 dbg("rresolve: unsupported address family %d!",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100180 sin6->sin6_family);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000181 errno = EAFNOSUPPORT;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000182 return NULL;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000183 }
184 if (numeric & 0x7FFF) {
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100185 return xmalloc_sockaddr2dotted_noport((void*)sin6);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000186 }
187 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000188 if (numeric & 0x8000)
Denys Vlasenko0004e992010-09-01 12:01:17 +0200189 return xstrdup("default");
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000190 return xstrdup("*");
Glenn L McGrath30db4232002-11-28 09:52:23 +0000191 }
Eric Andersen51b8bd62002-07-03 11:46:38 +0000192
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100193 return xmalloc_sockaddr2host_noport((void*)sin6);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000194}
195
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200196#endif /* CONFIG_FEATURE_IPV6 */