2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2001-11-10 17:48:42 +05:30
|
|
|
/*
|
|
|
|
* stolen from net-tools-1.59 and stripped down for busybox by
|
2003-07-15 02:51:08 +05:30
|
|
|
* Erik Andersen <andersen@codepoet.org>
|
2001-11-10 17:48:42 +05:30
|
|
|
*
|
|
|
|
* Heavily modified by Manuel Novoa III Mar 12, 2001
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2001-11-10 17:48:42 +05:30
|
|
|
*/
|
|
|
|
|
2006-06-19 01:50:07 +05:30
|
|
|
#include "libbb.h"
|
2001-11-10 17:48:42 +05:30
|
|
|
#include "inet_common.h"
|
2002-06-06 17:41:55 +05:30
|
|
|
|
2017-07-14 23:47:17 +05:30
|
|
|
#if 0
|
|
|
|
# define dbg(...) bb_error_msg(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
# define dbg(...) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
int FAST_FUNC INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
|
2001-11-10 17:48:42 +05:30
|
|
|
{
|
2002-11-28 15:22:23 +05:30
|
|
|
struct hostent *hp;
|
2007-06-19 16:54:47 +05:30
|
|
|
#if ENABLE_FEATURE_ETC_NETWORKS
|
2002-11-28 15:22:23 +05:30
|
|
|
struct netent *np;
|
2007-06-19 16:54:47 +05:30
|
|
|
#endif
|
2002-11-28 15:22:23 +05:30
|
|
|
|
|
|
|
/* Grmpf. -FvK */
|
|
|
|
s_in->sin_family = AF_INET;
|
|
|
|
s_in->sin_port = 0;
|
|
|
|
|
|
|
|
/* Default is special, meaning 0.0.0.0. */
|
2010-09-01 15:31:17 +05:30
|
|
|
if (strcmp(name, "default") == 0) {
|
2002-11-28 15:22:23 +05:30
|
|
|
s_in->sin_addr.s_addr = INADDR_ANY;
|
2006-11-22 02:04:21 +05:30
|
|
|
return 1;
|
2002-11-28 15:22:23 +05:30
|
|
|
}
|
|
|
|
/* Look to see if it's a dotted quad. */
|
|
|
|
if (inet_aton(name, &s_in->sin_addr)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* If we expect this to be a hostname, try hostname database first */
|
|
|
|
if (hostfirst) {
|
2017-07-14 23:47:17 +05:30
|
|
|
dbg("gethostbyname(%s)", name);
|
2007-01-22 19:42:08 +05:30
|
|
|
hp = gethostbyname(name);
|
2015-02-03 16:37:40 +05:30
|
|
|
if (hp) {
|
2007-01-22 19:42:08 +05:30
|
|
|
memcpy(&s_in->sin_addr, hp->h_addr_list[0],
|
|
|
|
sizeof(struct in_addr));
|
|
|
|
return 0;
|
|
|
|
}
|
2002-11-28 15:22:23 +05:30
|
|
|
}
|
2007-06-19 16:54:47 +05:30
|
|
|
#if ENABLE_FEATURE_ETC_NETWORKS
|
2002-11-28 15:22:23 +05:30
|
|
|
/* Try the NETWORKS database to see if this is a known network. */
|
2017-07-14 23:47:17 +05:30
|
|
|
dbg("getnetbyname(%s)", name);
|
2007-01-22 19:42:08 +05:30
|
|
|
np = getnetbyname(name);
|
2015-02-03 16:37:40 +05:30
|
|
|
if (np) {
|
2002-11-28 15:22:23 +05:30
|
|
|
s_in->sin_addr.s_addr = htonl(np->n_net);
|
|
|
|
return 1;
|
|
|
|
}
|
2007-06-19 16:54:47 +05:30
|
|
|
#endif
|
2002-11-28 15:22:23 +05:30
|
|
|
if (hostfirst) {
|
|
|
|
/* Don't try again */
|
|
|
|
return -1;
|
|
|
|
}
|
2001-11-10 17:48:42 +05:30
|
|
|
#ifdef DEBUG
|
2002-11-28 15:22:23 +05:30
|
|
|
res_init();
|
|
|
|
_res.options |= RES_DEBUG;
|
2001-11-10 17:48:42 +05:30
|
|
|
#endif
|
2017-07-14 23:47:17 +05:30
|
|
|
dbg("gethostbyname(%s)", name);
|
2007-01-22 19:42:08 +05:30
|
|
|
hp = gethostbyname(name);
|
2015-02-03 16:37:40 +05:30
|
|
|
if (!hp) {
|
2002-11-28 15:22:23 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2007-01-22 19:42:08 +05:30
|
|
|
memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
|
2002-11-28 15:22:23 +05:30
|
|
|
return 0;
|
2001-11-10 17:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-03 16:37:40 +05:30
|
|
|
/* numeric: & 0x8000: "default" instead of "*",
|
2001-11-10 17:48:42 +05:30
|
|
|
* & 0x4000: host instead of net,
|
|
|
|
* & 0x0fff: don't resolve
|
|
|
|
*/
|
2008-06-27 08:22:20 +05:30
|
|
|
char* FAST_FUNC INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask)
|
2001-11-10 17:48:42 +05:30
|
|
|
{
|
2007-06-19 16:42:46 +05:30
|
|
|
/* addr-to-name cache */
|
|
|
|
struct addr {
|
|
|
|
struct addr *next;
|
2015-02-03 16:37:40 +05:30
|
|
|
uint32_t nip;
|
|
|
|
smallint is_host;
|
2007-06-19 16:42:46 +05:30
|
|
|
char name[1];
|
|
|
|
};
|
|
|
|
static struct addr *cache = NULL;
|
|
|
|
|
2002-11-28 15:22:23 +05:30
|
|
|
struct addr *pn;
|
2007-06-19 16:42:46 +05:30
|
|
|
char *name;
|
2015-02-03 16:37:40 +05:30
|
|
|
uint32_t nip;
|
|
|
|
smallint is_host;
|
2002-11-28 15:22:23 +05:30
|
|
|
|
|
|
|
if (s_in->sin_family != AF_INET) {
|
2017-07-14 23:47:17 +05:30
|
|
|
dbg("rresolve: unsupported address family %d!", s_in->sin_family);
|
2002-11-28 15:22:23 +05:30
|
|
|
errno = EAFNOSUPPORT;
|
2007-06-19 16:42:46 +05:30
|
|
|
return NULL;
|
2002-11-28 15:22:23 +05:30
|
|
|
}
|
2015-02-03 16:37:40 +05:30
|
|
|
nip = s_in->sin_addr.s_addr;
|
2017-07-14 23:47:17 +05:30
|
|
|
dbg("rresolve: %08x mask:%08x num:%08x", (unsigned)nip, netmask, numeric);
|
2007-06-19 16:42:46 +05:30
|
|
|
if (numeric & 0x0FFF)
|
2015-02-03 16:37:40 +05:30
|
|
|
return xmalloc_sockaddr2dotted_noport((void*)s_in);
|
|
|
|
if (nip == INADDR_ANY) {
|
|
|
|
if (numeric & 0x8000)
|
|
|
|
return xstrdup("default");
|
|
|
|
return xstrdup("*");
|
|
|
|
}
|
|
|
|
|
|
|
|
is_host = ((nip & (~netmask)) != 0 || (numeric & 0x4000));
|
2001-11-10 17:48:42 +05:30
|
|
|
|
2007-06-19 16:42:46 +05:30
|
|
|
pn = cache;
|
|
|
|
while (pn) {
|
2015-02-03 16:37:40 +05:30
|
|
|
if (pn->nip == nip && pn->is_host == is_host) {
|
2017-07-14 23:47:17 +05:30
|
|
|
dbg("rresolve: found %s %08x in cache",
|
2015-02-03 16:37:40 +05:30
|
|
|
(is_host ? "host" : "net"), (unsigned)nip);
|
2007-06-19 16:42:46 +05:30
|
|
|
return xstrdup(pn->name);
|
2002-11-28 15:22:23 +05:30
|
|
|
}
|
|
|
|
pn = pn->next;
|
2001-11-10 17:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-06-19 16:42:46 +05:30
|
|
|
name = NULL;
|
2015-02-03 16:37:40 +05:30
|
|
|
if (is_host) {
|
2017-07-14 23:47:17 +05:30
|
|
|
dbg("sockaddr2host_noport(%08x)", (unsigned)nip);
|
2015-02-03 16:37:40 +05:30
|
|
|
name = xmalloc_sockaddr2host_noport((void*)s_in);
|
2017-07-14 23:47:17 +05:30
|
|
|
}
|
|
|
|
#if ENABLE_FEATURE_ETC_NETWORKS
|
|
|
|
else {
|
2007-06-19 16:42:46 +05:30
|
|
|
struct netent *np;
|
2017-07-14 23:47:17 +05:30
|
|
|
dbg("getnetbyaddr(%08x)", (unsigned)ntohl(nip));
|
2015-02-03 16:37:40 +05:30
|
|
|
np = getnetbyaddr(ntohl(nip), AF_INET);
|
2007-06-19 16:42:46 +05:30
|
|
|
if (np)
|
|
|
|
name = xstrdup(np->n_name);
|
2002-11-28 15:22:23 +05:30
|
|
|
}
|
2017-07-14 23:47:17 +05:30
|
|
|
#endif
|
2007-06-19 16:42:46 +05:30
|
|
|
if (!name)
|
2015-02-03 16:37:40 +05:30
|
|
|
name = xmalloc_sockaddr2dotted_noport((void*)s_in);
|
|
|
|
|
2007-06-19 16:42:46 +05:30
|
|
|
pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
|
|
|
|
pn->next = cache;
|
2015-02-03 16:37:40 +05:30
|
|
|
pn->nip = nip;
|
|
|
|
pn->is_host = is_host;
|
2007-06-19 16:42:46 +05:30
|
|
|
strcpy(pn->name, name);
|
|
|
|
cache = pn;
|
2015-02-03 16:37:40 +05:30
|
|
|
|
2007-06-19 16:42:46 +05:30
|
|
|
return name;
|
2001-11-10 17:48:42 +05:30
|
|
|
}
|
2002-07-03 17:16:38 +05:30
|
|
|
|
2007-08-13 16:06:25 +05:30
|
|
|
#if ENABLE_FEATURE_IPV6
|
2002-07-03 17:16:38 +05:30
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
int FAST_FUNC INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
|
2002-07-03 17:16:38 +05:30
|
|
|
{
|
2011-03-23 00:44:26 +05:30
|
|
|
struct addrinfo req, *ai = NULL;
|
2002-11-28 15:22:23 +05:30
|
|
|
int s;
|
|
|
|
|
2011-03-23 00:44:26 +05:30
|
|
|
memset(&req, 0, sizeof(req));
|
2002-11-28 15:22:23 +05:30
|
|
|
req.ai_family = AF_INET6;
|
2006-11-22 02:04:21 +05:30
|
|
|
s = getaddrinfo(name, NULL, &req, &ai);
|
2011-03-23 00:44:26 +05:30
|
|
|
if (s != 0) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg("getaddrinfo: %s: %d", name, s);
|
2002-11-28 15:22:23 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2011-03-23 00:44:26 +05:30
|
|
|
memcpy(sin6, ai->ai_addr, sizeof(*sin6));
|
2013-11-29 21:09:28 +05:30
|
|
|
freeaddrinfo(ai);
|
2006-11-22 02:04:21 +05:30
|
|
|
return 0;
|
2002-07-03 17:16:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef IN6_IS_ADDR_UNSPECIFIED
|
2002-11-26 08:10:56 +05:30
|
|
|
# define IN6_IS_ADDR_UNSPECIFIED(a) \
|
2006-05-19 18:14:16 +05:30
|
|
|
(((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
|
|
|
|
((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
char* FAST_FUNC INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
|
2002-07-03 17:16:38 +05:30
|
|
|
{
|
2002-11-28 15:22:23 +05:30
|
|
|
if (sin6->sin6_family != AF_INET6) {
|
2017-07-14 23:47:17 +05:30
|
|
|
dbg("rresolve: unsupported address family %d!",
|
2013-01-14 20:27:44 +05:30
|
|
|
sin6->sin6_family);
|
2002-11-28 15:22:23 +05:30
|
|
|
errno = EAFNOSUPPORT;
|
2007-06-19 16:42:46 +05:30
|
|
|
return NULL;
|
2002-11-28 15:22:23 +05:30
|
|
|
}
|
|
|
|
if (numeric & 0x7FFF) {
|
2015-02-03 16:37:40 +05:30
|
|
|
return xmalloc_sockaddr2dotted_noport((void*)sin6);
|
2002-11-28 15:22:23 +05:30
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
|
2007-06-19 16:42:46 +05:30
|
|
|
if (numeric & 0x8000)
|
2010-09-01 15:31:17 +05:30
|
|
|
return xstrdup("default");
|
2007-06-19 16:42:46 +05:30
|
|
|
return xstrdup("*");
|
2002-11-28 15:22:23 +05:30
|
|
|
}
|
|
|
|
|
2015-02-03 16:37:40 +05:30
|
|
|
return xmalloc_sockaddr2host_noport((void*)sin6);
|
2002-07-03 17:16:38 +05:30
|
|
|
}
|
|
|
|
|
2010-10-28 22:27:19 +05:30
|
|
|
#endif /* CONFIG_FEATURE_IPV6 */
|