2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-10 07:03:55 +05:30
|
|
|
/*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2002-11-10 07:03:55 +05:30
|
|
|
*
|
2010-10-29 15:16:52 +05:30
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
2002-11-10 07:03:55 +05:30
|
|
|
*
|
|
|
|
* Changes:
|
|
|
|
*
|
2010-10-29 15:16:52 +05:30
|
|
|
* Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
|
2002-11-10 07:03:55 +05:30
|
|
|
*/
|
|
|
|
|
2006-06-03 02:23:38 +05:30
|
|
|
#include "libbb.h"
|
2002-11-10 07:03:55 +05:30
|
|
|
#include "utils.h"
|
2005-09-22 16:41:11 +05:30
|
|
|
#include "inet_common.h"
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2017-04-07 20:45:40 +05:30
|
|
|
unsigned FAST_FUNC get_hz(void)
|
2015-10-14 16:23:47 +05:30
|
|
|
{
|
|
|
|
static unsigned hz_internal;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if (hz_internal)
|
|
|
|
return hz_internal;
|
|
|
|
|
|
|
|
fp = fopen_for_read("/proc/net/psched");
|
|
|
|
if (fp) {
|
|
|
|
unsigned nom, denom;
|
|
|
|
|
|
|
|
if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
|
|
|
|
if (nom == 1000000)
|
|
|
|
hz_internal = denom;
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
if (!hz_internal)
|
|
|
|
hz_internal = bb_clk_tck();
|
|
|
|
return hz_internal;
|
|
|
|
}
|
|
|
|
|
2017-04-07 20:45:40 +05:30
|
|
|
unsigned FAST_FUNC get_unsigned(char *arg, const char *errmsg)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
unsigned long res;
|
|
|
|
char *ptr;
|
|
|
|
|
2009-03-05 14:51:57 +05:30
|
|
|
if (*arg) {
|
|
|
|
res = strtoul(arg, &ptr, 0);
|
2009-09-23 20:47:53 +05:30
|
|
|
//FIXME: "" will be accepted too, is it correct?!
|
2009-03-05 14:51:57 +05:30
|
|
|
if (!*ptr && res <= UINT_MAX) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2015-10-14 16:51:01 +05:30
|
|
|
invarg_1_to_2(arg, errmsg); /* does not return */
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2017-04-07 20:45:40 +05:30
|
|
|
uint32_t FAST_FUNC get_u32(char *arg, const char *errmsg)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
unsigned long res;
|
|
|
|
char *ptr;
|
|
|
|
|
2009-03-05 14:51:57 +05:30
|
|
|
if (*arg) {
|
|
|
|
res = strtoul(arg, &ptr, 0);
|
2009-09-23 20:47:53 +05:30
|
|
|
//FIXME: "" will be accepted too, is it correct?!
|
2009-03-05 14:51:57 +05:30
|
|
|
if (!*ptr && res <= 0xFFFFFFFFUL) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2015-10-14 16:51:01 +05:30
|
|
|
invarg_1_to_2(arg, errmsg); /* does not return */
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2017-04-07 20:45:40 +05:30
|
|
|
uint16_t FAST_FUNC get_u16(char *arg, const char *errmsg)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
unsigned long res;
|
|
|
|
char *ptr;
|
|
|
|
|
2009-03-05 14:51:57 +05:30
|
|
|
if (*arg) {
|
|
|
|
res = strtoul(arg, &ptr, 0);
|
2009-09-23 20:47:53 +05:30
|
|
|
//FIXME: "" will be accepted too, is it correct?!
|
2009-03-05 14:51:57 +05:30
|
|
|
if (!*ptr && res <= 0xFFFF) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2015-10-14 16:51:01 +05:30
|
|
|
invarg_1_to_2(arg, errmsg); /* does not return */
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2017-04-07 20:45:40 +05:30
|
|
|
int FAST_FUNC get_addr_1(inet_prefix *addr, char *name, int family)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
memset(addr, 0, sizeof(*addr));
|
|
|
|
|
2010-09-01 15:31:17 +05:30
|
|
|
if (strcmp(name, "default") == 0
|
2009-03-03 20:25:29 +05:30
|
|
|
|| strcmp(name, "all") == 0
|
|
|
|
|| strcmp(name, "any") == 0
|
|
|
|
) {
|
2002-11-10 07:03:55 +05:30
|
|
|
addr->family = family;
|
|
|
|
addr->bytelen = (family == AF_INET6 ? 16 : 4);
|
|
|
|
addr->bitlen = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strchr(name, ':')) {
|
|
|
|
addr->family = AF_INET6;
|
|
|
|
if (family != AF_UNSPEC && family != AF_INET6)
|
|
|
|
return -1;
|
|
|
|
if (inet_pton(AF_INET6, name, addr->data) <= 0)
|
|
|
|
return -1;
|
|
|
|
addr->bytelen = 16;
|
|
|
|
addr->bitlen = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (family != AF_UNSPEC && family != AF_INET)
|
|
|
|
return -1;
|
2011-02-23 05:50:44 +05:30
|
|
|
|
|
|
|
/* Try to parse it as IPv4 */
|
|
|
|
addr->family = AF_INET;
|
|
|
|
#if 0 /* Doesn't handle e.g. "10.10", for example, "ip r l root 10.10/16" */
|
2008-10-21 18:12:45 +05:30
|
|
|
if (inet_pton(AF_INET, name, addr->data) <= 0)
|
|
|
|
return -1;
|
2011-02-23 05:50:44 +05:30
|
|
|
#else
|
|
|
|
{
|
|
|
|
unsigned i = 0;
|
|
|
|
unsigned n = 0;
|
|
|
|
const char *cp = name - 1;
|
|
|
|
while (*++cp) {
|
|
|
|
if ((unsigned char)(*cp - '0') <= 9) {
|
|
|
|
n = 10 * n + (unsigned char)(*cp - '0');
|
|
|
|
if (n >= 256)
|
|
|
|
return -1;
|
|
|
|
((uint8_t*)addr->data)[i] = n;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*cp == '.' && ++i <= 3) {
|
|
|
|
n = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2002-11-10 07:03:55 +05:30
|
|
|
addr->bytelen = 4;
|
|
|
|
addr->bitlen = -1;
|
2011-02-23 05:50:44 +05:30
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-23 05:50:44 +05:30
|
|
|
static void get_prefix_1(inet_prefix *dst, char *arg, int family)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
char *slash;
|
|
|
|
|
|
|
|
memset(dst, 0, sizeof(*dst));
|
|
|
|
|
2010-09-01 15:31:17 +05:30
|
|
|
if (strcmp(arg, "default") == 0
|
2009-03-03 20:25:29 +05:30
|
|
|
|| strcmp(arg, "all") == 0
|
|
|
|
|| strcmp(arg, "any") == 0
|
|
|
|
) {
|
2002-11-10 07:03:55 +05:30
|
|
|
dst->family = family;
|
2009-03-05 14:51:57 +05:30
|
|
|
/*dst->bytelen = 0; - done by memset */
|
|
|
|
/*dst->bitlen = 0;*/
|
2011-02-23 05:50:44 +05:30
|
|
|
return;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
slash = strchr(arg, '/');
|
|
|
|
if (slash)
|
2007-01-30 04:21:58 +05:30
|
|
|
*slash = '\0';
|
2011-02-23 05:50:44 +05:30
|
|
|
|
|
|
|
if (get_addr_1(dst, arg, family) == 0) {
|
2008-10-21 18:12:45 +05:30
|
|
|
dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
|
2002-11-10 07:03:55 +05:30
|
|
|
if (slash) {
|
2011-02-23 05:50:44 +05:30
|
|
|
unsigned plen;
|
2008-10-21 18:12:45 +05:30
|
|
|
inet_prefix netmask_pfx;
|
|
|
|
|
|
|
|
netmask_pfx.family = AF_UNSPEC;
|
2009-03-05 14:51:57 +05:30
|
|
|
plen = bb_strtou(slash + 1, NULL, 0);
|
|
|
|
if ((errno || plen > dst->bitlen)
|
2011-02-23 05:50:44 +05:30
|
|
|
&& get_addr_1(&netmask_pfx, slash + 1, family) != 0
|
|
|
|
) {
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if (netmask_pfx.family == AF_INET) {
|
2008-10-21 18:12:45 +05:30
|
|
|
/* fill in prefix length of dotted quad */
|
|
|
|
uint32_t mask = ntohl(netmask_pfx.data[0]);
|
|
|
|
uint32_t host = ~mask;
|
|
|
|
|
|
|
|
/* a valid netmask must be 2^n - 1 */
|
2011-02-23 05:50:44 +05:30
|
|
|
if (host & (host + 1))
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
for (plen = 0; mask; mask <<= 1)
|
|
|
|
++plen;
|
|
|
|
if (plen > dst->bitlen)
|
|
|
|
goto bad;
|
|
|
|
/* dst->flags |= PREFIXLEN_SPECIFIED; */
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2011-02-23 05:50:44 +05:30
|
|
|
dst->bitlen = plen;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
}
|
2011-02-23 05:50:44 +05:30
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
if (slash)
|
|
|
|
*slash = '/';
|
2011-02-23 05:50:44 +05:30
|
|
|
return;
|
|
|
|
bad:
|
|
|
|
bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2017-04-07 20:45:40 +05:30
|
|
|
int FAST_FUNC get_addr(inet_prefix *dst, char *arg, int family)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
if (family == AF_PACKET) {
|
2008-10-21 18:12:45 +05:30
|
|
|
bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
if (get_addr_1(dst, arg, family)) {
|
2008-10-21 18:12:45 +05:30
|
|
|
bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-07 20:45:40 +05:30
|
|
|
void FAST_FUNC get_prefix(inet_prefix *dst, char *arg, int family)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
if (family == AF_PACKET) {
|
2008-10-21 18:12:45 +05:30
|
|
|
bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2011-02-23 05:50:44 +05:30
|
|
|
get_prefix_1(dst, arg, family);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2017-04-07 20:45:40 +05:30
|
|
|
uint32_t FAST_FUNC get_addr32(char *name)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
inet_prefix addr;
|
2002-11-18 12:56:42 +05:30
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
if (get_addr_1(&addr, name, AF_INET)) {
|
2008-10-21 18:12:45 +05:30
|
|
|
bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
return addr.data[0];
|
|
|
|
}
|
|
|
|
|
2017-04-07 20:45:40 +05:30
|
|
|
char** FAST_FUNC next_arg(char **argv)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2017-04-07 20:45:40 +05:30
|
|
|
if (!*++argv)
|
|
|
|
bb_error_msg_and_die("command line is not complete, try \"help\"");
|
|
|
|
return argv;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2017-04-07 20:45:40 +05:30
|
|
|
void FAST_FUNC invarg_1_to_2(const char *arg, const char *opt)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2015-10-14 16:51:01 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg_to, arg, opt);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2017-04-07 20:45:40 +05:30
|
|
|
void FAST_FUNC duparg(const char *key, const char *arg)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2007-01-30 04:21:58 +05:30
|
|
|
bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2017-04-07 20:45:40 +05:30
|
|
|
void FAST_FUNC duparg2(const char *key, const char *arg)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2007-01-30 04:21:58 +05:30
|
|
|
bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2017-04-07 20:45:40 +05:30
|
|
|
int FAST_FUNC inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2011-02-23 05:50:44 +05:30
|
|
|
const uint32_t *a1 = a->data;
|
|
|
|
const uint32_t *a2 = b->data;
|
2009-03-05 14:51:57 +05:30
|
|
|
int words = bits >> 5;
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
bits &= 0x1f;
|
|
|
|
|
|
|
|
if (words)
|
|
|
|
if (memcmp(a1, a2, words << 2))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (bits) {
|
2007-01-01 00:27:37 +05:30
|
|
|
uint32_t w1, w2;
|
|
|
|
uint32_t mask;
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
w1 = a1[words];
|
|
|
|
w2 = a2[words];
|
|
|
|
|
|
|
|
mask = htonl((0xffffffff) << (0x20 - bits));
|
|
|
|
|
|
|
|
if ((w1 ^ w2) & mask)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-07 20:45:40 +05:30
|
|
|
const char* FAST_FUNC rt_addr_n2a(int af, void *addr)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
switch (af) {
|
|
|
|
case AF_INET:
|
|
|
|
case AF_INET6:
|
2015-10-14 17:26:42 +05:30
|
|
|
return inet_ntop(af, addr,
|
|
|
|
auto_string(xzalloc(INET6_ADDRSTRLEN)), INET6_ADDRSTRLEN
|
|
|
|
);
|
2002-11-10 07:03:55 +05:30
|
|
|
default:
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-05 14:51:57 +05:30
|
|
|
#ifdef RESOLVE_HOSTNAMES
|
2017-04-07 20:45:40 +05:30
|
|
|
const char* FAST_FUNC format_host(int af, int len, void *addr)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
if (resolve_hosts) {
|
|
|
|
struct hostent *h_ent;
|
2002-11-18 12:56:42 +05:30
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
if (len <= 0) {
|
|
|
|
switch (af) {
|
|
|
|
case AF_INET:
|
|
|
|
len = 4;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
len = 16;
|
|
|
|
break;
|
2002-11-18 12:56:42 +05:30
|
|
|
default:;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
}
|
2007-06-05 02:33:51 +05:30
|
|
|
if (len > 0) {
|
|
|
|
h_ent = gethostbyaddr(addr, len, af);
|
|
|
|
if (h_ent != NULL) {
|
2015-10-14 17:26:42 +05:30
|
|
|
return auto_string(xstrdup(h_ent->h_name));
|
2007-06-05 02:33:51 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
}
|
2015-10-14 17:26:42 +05:30
|
|
|
return rt_addr_n2a(af, addr);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2009-03-05 14:51:57 +05:30
|
|
|
#endif
|