blob: 5b4a4a10b7b41c7cdc8aae43d37219a93bda8f24 [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
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000014int FAST_FUNC INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
Eric Andersenc223ced2001-11-10 12:18:42 +000015{
Glenn L McGrath30db4232002-11-28 09:52:23 +000016 struct hostent *hp;
Denis Vlasenkoa61cb922007-06-19 11:24:47 +000017#if ENABLE_FEATURE_ETC_NETWORKS
Glenn L McGrath30db4232002-11-28 09:52:23 +000018 struct netent *np;
Denis Vlasenkoa61cb922007-06-19 11:24:47 +000019#endif
Eric Andersenc223ced2001-11-10 12:18:42 +000020
Glenn L McGrath30db4232002-11-28 09:52:23 +000021 /* Grmpf. -FvK */
22 s_in->sin_family = AF_INET;
23 s_in->sin_port = 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000024
Glenn L McGrath30db4232002-11-28 09:52:23 +000025 /* Default is special, meaning 0.0.0.0. */
Denys Vlasenko0004e992010-09-01 12:01:17 +020026 if (strcmp(name, "default") == 0) {
Glenn L McGrath30db4232002-11-28 09:52:23 +000027 s_in->sin_addr.s_addr = INADDR_ANY;
Denis Vlasenko7ca39212006-11-21 20:34:21 +000028 return 1;
Glenn L McGrath30db4232002-11-28 09:52:23 +000029 }
30 /* Look to see if it's a dotted quad. */
31 if (inet_aton(name, &s_in->sin_addr)) {
32 return 0;
33 }
34 /* If we expect this to be a hostname, try hostname database first */
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010035 if (hostfirst) {
Eric Andersenc223ced2001-11-10 12:18:42 +000036#ifdef DEBUG
Denis Vlasenko703e2022007-01-22 14:12:08 +000037 bb_error_msg("gethostbyname(%s)", name);
Eric Andersenc223ced2001-11-10 12:18:42 +000038#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +000039 hp = gethostbyname(name);
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010040 if (hp) {
Denis Vlasenko703e2022007-01-22 14:12:08 +000041 memcpy(&s_in->sin_addr, hp->h_addr_list[0],
42 sizeof(struct in_addr));
43 return 0;
44 }
Glenn L McGrath30db4232002-11-28 09:52:23 +000045 }
Denis Vlasenkoa61cb922007-06-19 11:24:47 +000046#if ENABLE_FEATURE_ETC_NETWORKS
Glenn L McGrath30db4232002-11-28 09:52:23 +000047 /* Try the NETWORKS database to see if this is a known network. */
48#ifdef DEBUG
Denis Vlasenko703e2022007-01-22 14:12:08 +000049 bb_error_msg("getnetbyname(%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000050#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +000051 np = getnetbyname(name);
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010052 if (np) {
Glenn L McGrath30db4232002-11-28 09:52:23 +000053 s_in->sin_addr.s_addr = htonl(np->n_net);
54 return 1;
55 }
Denis Vlasenkoa61cb922007-06-19 11:24:47 +000056#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +000057 if (hostfirst) {
58 /* Don't try again */
Glenn L McGrath30db4232002-11-28 09:52:23 +000059 return -1;
60 }
61#ifdef DEBUG
62 res_init();
63 _res.options |= RES_DEBUG;
Denis Vlasenko703e2022007-01-22 14:12:08 +000064 bb_error_msg("gethostbyname(%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000065#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +000066 hp = gethostbyname(name);
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010067 if (!hp) {
Glenn L McGrath30db4232002-11-28 09:52:23 +000068 return -1;
69 }
Denis Vlasenko703e2022007-01-22 14:12:08 +000070 memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
Eric Andersenc223ced2001-11-10 12:18:42 +000071 return 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000072}
73
Eric Andersenc223ced2001-11-10 12:18:42 +000074
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010075/* numeric: & 0x8000: "default" instead of "*",
Eric Andersenc223ced2001-11-10 12:18:42 +000076 * & 0x4000: host instead of net,
77 * & 0x0fff: don't resolve
78 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000079char* FAST_FUNC INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask)
Eric Andersenc223ced2001-11-10 12:18:42 +000080{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +000081 /* addr-to-name cache */
82 struct addr {
83 struct addr *next;
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010084 uint32_t nip;
85 smallint is_host;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +000086 char name[1];
87 };
88 static struct addr *cache = NULL;
89
Glenn L McGrath30db4232002-11-28 09:52:23 +000090 struct addr *pn;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +000091 char *name;
Denys Vlasenko8e74ada2015-02-03 12:07:40 +010092 uint32_t nip;
93 smallint is_host;
Eric Andersenc223ced2001-11-10 12:18:42 +000094
Glenn L McGrath30db4232002-11-28 09:52:23 +000095 if (s_in->sin_family != AF_INET) {
Eric Andersenc223ced2001-11-10 12:18:42 +000096#ifdef DEBUG
Denis Vlasenko1b16bda2007-06-19 11:10:02 +000097 bb_error_msg("rresolve: unsupported address family %d!",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010098 s_in->sin_family);
Eric Andersenc223ced2001-11-10 12:18:42 +000099#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000100 errno = EAFNOSUPPORT;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000101 return NULL;
Eric Andersenc223ced2001-11-10 12:18:42 +0000102 }
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100103 nip = s_in->sin_addr.s_addr;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000104#ifdef DEBUG
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100105 bb_error_msg("rresolve: %08x mask:%08x num:%08x", (unsigned)nip, netmask, numeric);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000106#endif
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000107 if (numeric & 0x0FFF)
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100108 return xmalloc_sockaddr2dotted_noport((void*)s_in);
109 if (nip == INADDR_ANY) {
110 if (numeric & 0x8000)
111 return xstrdup("default");
112 return xstrdup("*");
113 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000114
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100115 is_host = ((nip & (~netmask)) != 0 || (numeric & 0x4000));
116
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000117 pn = cache;
118 while (pn) {
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100119 if (pn->nip == nip && pn->is_host == is_host) {
Eric Andersenc223ced2001-11-10 12:18:42 +0000120#ifdef DEBUG
Denis Vlasenko703e2022007-01-22 14:12:08 +0000121 bb_error_msg("rresolve: found %s %08x in cache",
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100122 (is_host ? "host" : "net"), (unsigned)nip);
Eric Andersenc223ced2001-11-10 12:18:42 +0000123#endif
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000124 return xstrdup(pn->name);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000125 }
126 pn = pn->next;
Eric Andersenc223ced2001-11-10 12:18:42 +0000127 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000128
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000129 name = NULL;
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100130 if (is_host) {
Eric Andersenc223ced2001-11-10 12:18:42 +0000131#ifdef DEBUG
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100132 bb_error_msg("sockaddr2host_noport(%08x)", (unsigned)nip);
Eric Andersenc223ced2001-11-10 12:18:42 +0000133#endif
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100134 name = xmalloc_sockaddr2host_noport((void*)s_in);
Denis Vlasenkoa61cb922007-06-19 11:24:47 +0000135 } else if (ENABLE_FEATURE_ETC_NETWORKS) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000136 struct netent *np;
Eric Andersenc223ced2001-11-10 12:18:42 +0000137#ifdef DEBUG
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100138 bb_error_msg("getnetbyaddr(%08x)", (unsigned)ntohl(nip));
Eric Andersenc223ced2001-11-10 12:18:42 +0000139#endif
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100140 np = getnetbyaddr(ntohl(nip), AF_INET);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000141 if (np)
142 name = xstrdup(np->n_name);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000143 }
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000144 if (!name)
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100145 name = xmalloc_sockaddr2dotted_noport((void*)s_in);
146
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000147 pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
148 pn->next = cache;
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100149 pn->nip = nip;
150 pn->is_host = is_host;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000151 strcpy(pn->name, name);
152 cache = pn;
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100153
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000154 return name;
Eric Andersenc223ced2001-11-10 12:18:42 +0000155}
Eric Andersen51b8bd62002-07-03 11:46:38 +0000156
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000157#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000158
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000159int FAST_FUNC INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000160{
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100161 struct addrinfo req, *ai = NULL;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000162 int s;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000163
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100164 memset(&req, 0, sizeof(req));
Glenn L McGrath30db4232002-11-28 09:52:23 +0000165 req.ai_family = AF_INET6;
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000166 s = getaddrinfo(name, NULL, &req, &ai);
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100167 if (s != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000168 bb_error_msg("getaddrinfo: %s: %d", name, s);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000169 return -1;
170 }
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100171 memcpy(sin6, ai->ai_addr, sizeof(*sin6));
Denys Vlasenko1a7256a2013-11-29 16:39:28 +0100172 freeaddrinfo(ai);
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000173 return 0;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000174}
175
176#ifndef IN6_IS_ADDR_UNSPECIFIED
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000177# define IN6_IS_ADDR_UNSPECIFIED(a) \
Bernhard Reutner-Fischer38583fd2006-05-19 12:44:16 +0000178 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
179 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000180#endif
181
182
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000183char* FAST_FUNC INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000184{
Glenn L McGrath30db4232002-11-28 09:52:23 +0000185 if (sin6->sin6_family != AF_INET6) {
Eric Andersen51b8bd62002-07-03 11:46:38 +0000186#ifdef DEBUG
Denys Vlasenkobcbd37d2009-06-18 13:23:58 +0200187 bb_error_msg("rresolve: unsupported address family %d!",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100188 sin6->sin6_family);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000189#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000190 errno = EAFNOSUPPORT;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000191 return NULL;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000192 }
193 if (numeric & 0x7FFF) {
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100194 return xmalloc_sockaddr2dotted_noport((void*)sin6);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000195 }
196 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000197 if (numeric & 0x8000)
Denys Vlasenko0004e992010-09-01 12:01:17 +0200198 return xstrdup("default");
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000199 return xstrdup("*");
Glenn L McGrath30db4232002-11-28 09:52:23 +0000200 }
Eric Andersen51b8bd62002-07-03 11:46:38 +0000201
Denys Vlasenko8e74ada2015-02-03 12:07:40 +0100202 return xmalloc_sockaddr2host_noport((void*)sin6);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000203}
204
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200205#endif /* CONFIG_FEATURE_IPV6 */