2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-10 01:33:55 +00:00
|
|
|
/*
|
|
|
|
* utils.c
|
|
|
|
*
|
2006-01-30 17:17:14 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2002-11-10 01:33:55 +00:00
|
|
|
*
|
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
|
|
|
*
|
|
|
|
* Changes:
|
|
|
|
*
|
|
|
|
* Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
|
|
|
|
*/
|
|
|
|
|
2006-06-02 20:53:38 +00:00
|
|
|
#include "libbb.h"
|
2002-11-10 01:33:55 +00:00
|
|
|
#include "utils.h"
|
2005-09-22 11:11:11 +00:00
|
|
|
#include "inet_common.h"
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2009-03-05 09:21:57 +00:00
|
|
|
unsigned get_unsigned(char *arg, const char *errmsg)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
|
|
|
unsigned long res;
|
|
|
|
char *ptr;
|
|
|
|
|
2009-03-05 09:21:57 +00:00
|
|
|
if (*arg) {
|
|
|
|
res = strtoul(arg, &ptr, 0);
|
2009-09-23 15:17:53 +00:00
|
|
|
//FIXME: "" will be accepted too, is it correct?!
|
2009-03-05 09:21:57 +00:00
|
|
|
if (!*ptr && res <= UINT_MAX) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
invarg(arg, errmsg); /* does not return */
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 09:21:57 +00:00
|
|
|
uint32_t get_u32(char *arg, const char *errmsg)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
|
|
|
unsigned long res;
|
|
|
|
char *ptr;
|
|
|
|
|
2009-03-05 09:21:57 +00:00
|
|
|
if (*arg) {
|
|
|
|
res = strtoul(arg, &ptr, 0);
|
2009-09-23 15:17:53 +00:00
|
|
|
//FIXME: "" will be accepted too, is it correct?!
|
2009-03-05 09:21:57 +00:00
|
|
|
if (!*ptr && res <= 0xFFFFFFFFUL) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
invarg(arg, errmsg); /* does not return */
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 09:21:57 +00:00
|
|
|
uint16_t get_u16(char *arg, const char *errmsg)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
|
|
|
unsigned long res;
|
|
|
|
char *ptr;
|
|
|
|
|
2009-03-05 09:21:57 +00:00
|
|
|
if (*arg) {
|
|
|
|
res = strtoul(arg, &ptr, 0);
|
2009-09-23 15:17:53 +00:00
|
|
|
//FIXME: "" will be accepted too, is it correct?!
|
2009-03-05 09:21:57 +00:00
|
|
|
if (!*ptr && res <= 0xFFFF) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
invarg(arg, errmsg); /* does not return */
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 14:55:29 +00:00
|
|
|
int get_addr_1(inet_prefix *addr, char *name, int family)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
|
|
|
memset(addr, 0, sizeof(*addr));
|
|
|
|
|
2009-03-03 14:55:29 +00:00
|
|
|
if (strcmp(name, bb_str_default) == 0
|
|
|
|
|| strcmp(name, "all") == 0
|
|
|
|
|| strcmp(name, "any") == 0
|
|
|
|
) {
|
2002-11-10 01:33:55 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr->family = AF_INET;
|
|
|
|
if (family != AF_UNSPEC && family != AF_INET)
|
|
|
|
return -1;
|
2008-10-21 12:42:45 +00:00
|
|
|
if (inet_pton(AF_INET, name, addr->data) <= 0)
|
|
|
|
return -1;
|
2002-11-10 01:33:55 +00:00
|
|
|
addr->bytelen = 4;
|
|
|
|
addr->bitlen = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-03 14:55:29 +00:00
|
|
|
static int get_prefix_1(inet_prefix *dst, char *arg, int family)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
|
|
|
int err;
|
2008-10-21 12:42:45 +00:00
|
|
|
unsigned plen;
|
2002-11-10 01:33:55 +00:00
|
|
|
char *slash;
|
|
|
|
|
|
|
|
memset(dst, 0, sizeof(*dst));
|
|
|
|
|
2009-03-03 14:55:29 +00:00
|
|
|
if (strcmp(arg, bb_str_default) == 0
|
|
|
|
|| strcmp(arg, "all") == 0
|
|
|
|
|| strcmp(arg, "any") == 0
|
|
|
|
) {
|
2002-11-10 01:33:55 +00:00
|
|
|
dst->family = family;
|
2009-03-05 09:21:57 +00:00
|
|
|
/*dst->bytelen = 0; - done by memset */
|
|
|
|
/*dst->bitlen = 0;*/
|
2002-11-10 01:33:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
slash = strchr(arg, '/');
|
|
|
|
if (slash)
|
2007-01-29 22:51:58 +00:00
|
|
|
*slash = '\0';
|
2002-11-10 01:33:55 +00:00
|
|
|
err = get_addr_1(dst, arg, family);
|
|
|
|
if (err == 0) {
|
2008-10-21 12:42:45 +00:00
|
|
|
dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
|
2002-11-10 01:33:55 +00:00
|
|
|
if (slash) {
|
2008-10-21 12:42:45 +00:00
|
|
|
inet_prefix netmask_pfx;
|
|
|
|
|
|
|
|
netmask_pfx.family = AF_UNSPEC;
|
2009-03-05 09:21:57 +00:00
|
|
|
plen = bb_strtou(slash + 1, NULL, 0);
|
|
|
|
if ((errno || plen > dst->bitlen)
|
2009-03-03 14:55:29 +00:00
|
|
|
&& (get_addr_1(&netmask_pfx, slash + 1, family)))
|
2002-11-10 01:33:55 +00:00
|
|
|
err = -1;
|
2008-10-21 12:42:45 +00:00
|
|
|
else if (netmask_pfx.family == AF_INET) {
|
|
|
|
/* 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 */
|
|
|
|
if (!(host & (host + 1))) {
|
|
|
|
for (plen = 0; mask; mask <<= 1)
|
|
|
|
++plen;
|
2009-10-03 23:13:35 +00:00
|
|
|
if (plen <= dst->bitlen) {
|
2009-03-03 14:55:29 +00:00
|
|
|
dst->bitlen = plen;
|
|
|
|
/* dst->flags |= PREFIXLEN_SPECIFIED; */
|
2008-10-21 12:42:45 +00:00
|
|
|
} else
|
|
|
|
err = -1;
|
|
|
|
} else
|
|
|
|
err = -1;
|
|
|
|
} else {
|
|
|
|
/* plain prefix */
|
|
|
|
dst->bitlen = plen;
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (slash)
|
|
|
|
*slash = '/';
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2009-03-03 14:55:29 +00:00
|
|
|
int get_addr(inet_prefix *dst, char *arg, int family)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
|
|
|
if (family == AF_PACKET) {
|
2008-10-21 12:42:45 +00:00
|
|
|
bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address");
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
if (get_addr_1(dst, arg, family)) {
|
2008-10-21 12:42:45 +00:00
|
|
|
bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg);
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-03 14:55:29 +00:00
|
|
|
int get_prefix(inet_prefix *dst, char *arg, int family)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
|
|
|
if (family == AF_PACKET) {
|
2008-10-21 12:42:45 +00:00
|
|
|
bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix");
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
if (get_prefix_1(dst, arg, family)) {
|
2008-10-21 12:42:45 +00:00
|
|
|
bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-31 18:57:37 +00:00
|
|
|
uint32_t get_addr32(char *name)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
|
|
|
inet_prefix addr;
|
2002-11-18 07:26:42 +00:00
|
|
|
|
2002-11-10 01:33:55 +00:00
|
|
|
if (get_addr_1(&addr, name, AF_INET)) {
|
2008-10-21 12:42:45 +00:00
|
|
|
bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name);
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
return addr.data[0];
|
|
|
|
}
|
|
|
|
|
2005-04-16 04:30:38 +00:00
|
|
|
void incomplete_command(void)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
2007-01-29 22:51:58 +00:00
|
|
|
bb_error_msg_and_die("command line is not complete, try option \"help\"");
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
|
2007-01-29 22:51:58 +00:00
|
|
|
void invarg(const char *arg, const char *opt)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
2007-01-29 22:51:58 +00:00
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg, arg, opt);
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
|
2007-01-29 22:51:58 +00:00
|
|
|
void duparg(const char *key, const char *arg)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
2007-01-29 22:51:58 +00:00
|
|
|
bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg);
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
|
2007-01-29 22:51:58 +00:00
|
|
|
void duparg2(const char *key, const char *arg)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
2007-01-29 22:51:58 +00:00
|
|
|
bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 14:55:29 +00:00
|
|
|
int inet_addr_match(inet_prefix *a, inet_prefix *b, int bits)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
2006-12-31 18:57:37 +00:00
|
|
|
uint32_t *a1 = a->data;
|
|
|
|
uint32_t *a2 = b->data;
|
2009-03-05 09:21:57 +00:00
|
|
|
int words = bits >> 5;
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
bits &= 0x1f;
|
|
|
|
|
|
|
|
if (words)
|
|
|
|
if (memcmp(a1, a2, words << 2))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (bits) {
|
2006-12-31 18:57:37 +00:00
|
|
|
uint32_t w1, w2;
|
|
|
|
uint32_t mask;
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
w1 = a1[words];
|
|
|
|
w2 = a2[words];
|
|
|
|
|
|
|
|
mask = htonl((0xffffffff) << (0x20 - bits));
|
|
|
|
|
|
|
|
if ((w1 ^ w2) & mask)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-05 09:21:57 +00:00
|
|
|
const char *rt_addr_n2a(int af,
|
2006-01-30 17:17:14 +00:00
|
|
|
void *addr, char *buf, int buflen)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
|
|
|
switch (af) {
|
|
|
|
case AF_INET:
|
|
|
|
case AF_INET6:
|
|
|
|
return inet_ntop(af, addr, buf, buflen);
|
|
|
|
default:
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-05 09:21:57 +00:00
|
|
|
#ifdef RESOLVE_HOSTNAMES
|
2002-11-10 01:33:55 +00:00
|
|
|
const char *format_host(int af, int len, void *addr, char *buf, int buflen)
|
|
|
|
{
|
|
|
|
if (resolve_hosts) {
|
|
|
|
struct hostent *h_ent;
|
2002-11-18 07:26:42 +00:00
|
|
|
|
2002-11-10 01:33:55 +00:00
|
|
|
if (len <= 0) {
|
|
|
|
switch (af) {
|
|
|
|
case AF_INET:
|
|
|
|
len = 4;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
len = 16;
|
|
|
|
break;
|
2002-11-18 07:26:42 +00:00
|
|
|
default:;
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-04 21:03:51 +00:00
|
|
|
if (len > 0) {
|
|
|
|
h_ent = gethostbyaddr(addr, len, af);
|
|
|
|
if (h_ent != NULL) {
|
|
|
|
safe_strncpy(buf, h_ent->h_name, buflen);
|
|
|
|
return buf;
|
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-05 09:21:57 +00:00
|
|
|
return rt_addr_n2a(af, addr, buf, buflen);
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
2009-03-05 09:21:57 +00:00
|
|
|
#endif
|