blob: 7208db9ea9b254d6f185b23c64eb56b2ab97b95c [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 */
Eric Andersenc223ced2001-11-10 12:18:42 +000035#ifdef DEBUG
Glenn L McGrath30db4232002-11-28 09:52:23 +000036 if (hostfirst) {
Denis Vlasenko703e2022007-01-22 14:12:08 +000037 bb_error_msg("gethostbyname(%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000038 }
Eric Andersenc223ced2001-11-10 12:18:42 +000039#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +000040 if (hostfirst) {
41 hp = gethostbyname(name);
42 if (hp != NULL) {
43 memcpy(&s_in->sin_addr, hp->h_addr_list[0],
44 sizeof(struct in_addr));
45 return 0;
46 }
Glenn L McGrath30db4232002-11-28 09:52:23 +000047 }
Denis Vlasenkoa61cb922007-06-19 11:24:47 +000048#if ENABLE_FEATURE_ETC_NETWORKS
Glenn L McGrath30db4232002-11-28 09:52:23 +000049 /* Try the NETWORKS database to see if this is a known network. */
50#ifdef DEBUG
Denis Vlasenko703e2022007-01-22 14:12:08 +000051 bb_error_msg("getnetbyname(%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000052#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +000053 np = getnetbyname(name);
54 if (np != NULL) {
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;
Denis Vlasenko703e2022007-01-22 14:12:08 +000066 bb_error_msg("gethostbyname(%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000067#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +000068 hp = gethostbyname(name);
69 if (hp == NULL) {
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
77/* numeric: & 0x8000: default instead of *,
78 * & 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;
86 struct sockaddr_in addr;
87 int host;
88 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;
Denis Vlasenko703e2022007-01-22 14:12:08 +000094 uint32_t ad, host_ad;
Glenn L McGrath30db4232002-11-28 09:52:23 +000095 int host = 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000096
Glenn L McGrath30db4232002-11-28 09:52:23 +000097 if (s_in->sin_family != AF_INET) {
Eric Andersenc223ced2001-11-10 12:18:42 +000098#ifdef DEBUG
Denis Vlasenko1b16bda2007-06-19 11:10:02 +000099 bb_error_msg("rresolve: unsupported address family %d!",
Glenn L McGrath30db4232002-11-28 09:52:23 +0000100 s_in->sin_family);
Eric Andersenc223ced2001-11-10 12:18:42 +0000101#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000102 errno = EAFNOSUPPORT;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000103 return NULL;
Eric Andersenc223ced2001-11-10 12:18:42 +0000104 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000105 ad = s_in->sin_addr.s_addr;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000106#ifdef DEBUG
Denis Vlasenko703e2022007-01-22 14:12:08 +0000107 bb_error_msg("rresolve: %08x, mask %08x, num %08x", (unsigned)ad, netmask, numeric);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000108#endif
109 if (ad == INADDR_ANY) {
110 if ((numeric & 0x0FFF) == 0) {
111 if (numeric & 0x8000)
Denys Vlasenko0004e992010-09-01 12:01:17 +0200112 return xstrdup("default");
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000113 return xstrdup("*");
Glenn L McGrath30db4232002-11-28 09:52:23 +0000114 }
115 }
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000116 if (numeric & 0x0FFF)
117 return xstrdup(inet_ntoa(s_in->sin_addr));
Eric Andersenc223ced2001-11-10 12:18:42 +0000118
Glenn L McGrath30db4232002-11-28 09:52:23 +0000119 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
120 host = 1;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000121 pn = cache;
122 while (pn) {
Glenn L McGrath30db4232002-11-28 09:52:23 +0000123 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
Eric Andersenc223ced2001-11-10 12:18:42 +0000124#ifdef DEBUG
Denis Vlasenko703e2022007-01-22 14:12:08 +0000125 bb_error_msg("rresolve: found %s %08x in cache",
126 (host ? "host" : "net"), (unsigned)ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000127#endif
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000128 return xstrdup(pn->name);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000129 }
130 pn = pn->next;
Eric Andersenc223ced2001-11-10 12:18:42 +0000131 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000132
Glenn L McGrath30db4232002-11-28 09:52:23 +0000133 host_ad = ntohl(ad);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000134 name = NULL;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000135 if (host) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000136 struct hostent *ent;
Eric Andersenc223ced2001-11-10 12:18:42 +0000137#ifdef DEBUG
Denis Vlasenko703e2022007-01-22 14:12:08 +0000138 bb_error_msg("gethostbyaddr (%08x)", (unsigned)ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000139#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000140 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000141 if (ent)
142 name = xstrdup(ent->h_name);
Denis Vlasenkoa61cb922007-06-19 11:24:47 +0000143 } else if (ENABLE_FEATURE_ETC_NETWORKS) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000144 struct netent *np;
Eric Andersenc223ced2001-11-10 12:18:42 +0000145#ifdef DEBUG
Denis Vlasenko703e2022007-01-22 14:12:08 +0000146 bb_error_msg("getnetbyaddr (%08x)", (unsigned)host_ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000147#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000148 np = getnetbyaddr(host_ad, AF_INET);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000149 if (np)
150 name = xstrdup(np->n_name);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000151 }
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000152 if (!name)
153 name = xstrdup(inet_ntoa(s_in->sin_addr));
154 pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
155 pn->next = cache;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000156 pn->addr = *s_in;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000157 pn->host = host;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000158 strcpy(pn->name, name);
159 cache = pn;
160 return name;
Eric Andersenc223ced2001-11-10 12:18:42 +0000161}
Eric Andersen51b8bd62002-07-03 11:46:38 +0000162
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000163#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000164
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000165int FAST_FUNC INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000166{
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100167 struct addrinfo req, *ai = NULL;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000168 int s;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000169
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100170 memset(&req, 0, sizeof(req));
Glenn L McGrath30db4232002-11-28 09:52:23 +0000171 req.ai_family = AF_INET6;
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000172 s = getaddrinfo(name, NULL, &req, &ai);
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100173 if (s != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000174 bb_error_msg("getaddrinfo: %s: %d", name, s);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000175 return -1;
176 }
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100177 memcpy(sin6, ai->ai_addr, sizeof(*sin6));
Denys Vlasenko8f6ce092011-07-08 08:34:28 +0200178 if (ai)
179 freeaddrinfo(ai);
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000180 return 0;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000181}
182
183#ifndef IN6_IS_ADDR_UNSPECIFIED
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000184# define IN6_IS_ADDR_UNSPECIFIED(a) \
Bernhard Reutner-Fischer38583fd2006-05-19 12:44:16 +0000185 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
186 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000187#endif
188
189
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000190char* FAST_FUNC INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000191{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000192 char name[128];
Glenn L McGrath30db4232002-11-28 09:52:23 +0000193 int s;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000194
Glenn L McGrath30db4232002-11-28 09:52:23 +0000195 if (sin6->sin6_family != AF_INET6) {
Eric Andersen51b8bd62002-07-03 11:46:38 +0000196#ifdef DEBUG
Denys Vlasenkobcbd37d2009-06-18 13:23:58 +0200197 bb_error_msg("rresolve: unsupported address family %d!",
Glenn L McGrath30db4232002-11-28 09:52:23 +0000198 sin6->sin6_family);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000199#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000200 errno = EAFNOSUPPORT;
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000201 return NULL;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000202 }
203 if (numeric & 0x7FFF) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000204 inet_ntop(AF_INET6, &sin6->sin6_addr, name, sizeof(name));
205 return xstrdup(name);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000206 }
207 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000208 if (numeric & 0x8000)
Denys Vlasenko0004e992010-09-01 12:01:17 +0200209 return xstrdup("default");
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000210 return xstrdup("*");
Glenn L McGrath30db4232002-11-28 09:52:23 +0000211 }
Eric Andersen51b8bd62002-07-03 11:46:38 +0000212
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100213 s = getnameinfo((struct sockaddr *) sin6, sizeof(*sin6),
214 name, sizeof(name),
215 /*serv,servlen:*/ NULL, 0,
216 0);
217 if (s != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000218 bb_error_msg("getnameinfo failed");
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000219 return NULL;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000220 }
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000221 return xstrdup(name);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000222}
223
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200224#endif /* CONFIG_FEATURE_IPV6 */