aboutsummaryrefslogtreecommitdiff
path: root/networking/libiproute/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'networking/libiproute/utils.c')
-rw-r--r--networking/libiproute/utils.c121
1 files changed, 33 insertions, 88 deletions
diff --git a/networking/libiproute/utils.c b/networking/libiproute/utils.c
index 6b9f6a7f5..c84d018eb 100644
--- a/networking/libiproute/utils.c
+++ b/networking/libiproute/utils.c
@@ -15,102 +15,46 @@
#include "utils.h"
#include "inet_common.h"
-int get_integer(int *val, char *arg, int base)
-{
- long res;
- char *ptr;
-
- if (!arg || !*arg)
- return -1;
- res = strtol(arg, &ptr, base);
- if (!ptr || ptr == arg || *ptr || res > INT_MAX || res < INT_MIN)
- return -1;
- *val = res;
- return 0;
-}
-//XXX: FIXME: use some libbb function instead
-int get_unsigned(unsigned *val, char *arg, int base)
-{
- unsigned long res;
- char *ptr;
-
- if (!arg || !*arg)
- return -1;
- res = strtoul(arg, &ptr, base);
- if (!ptr || ptr == arg || *ptr || res > UINT_MAX)
- return -1;
- *val = res;
- return 0;
-}
-
-int get_u32(uint32_t *val, char *arg, int base)
+unsigned get_unsigned(char *arg, const char *errmsg)
{
unsigned long res;
char *ptr;
- if (!arg || !*arg)
- return -1;
- res = strtoul(arg, &ptr, base);
- if (!ptr || ptr == arg || *ptr || res > 0xFFFFFFFFUL)
- return -1;
- *val = res;
- return 0;
+ if (*arg) {
+ res = strtoul(arg, &ptr, 0);
+ if (!*ptr && res <= UINT_MAX) {
+ return res;
+ }
+ }
+ invarg(arg, errmsg); /* does not return */
}
-int get_u16(uint16_t *val, char *arg, int base)
+uint32_t get_u32(char *arg, const char *errmsg)
{
unsigned long res;
char *ptr;
- if (!arg || !*arg)
- return -1;
- res = strtoul(arg, &ptr, base);
- if (!ptr || ptr == arg || *ptr || res > 0xFFFF)
- return -1;
- *val = res;
- return 0;
+ if (*arg) {
+ res = strtoul(arg, &ptr, 0);
+ if (!*ptr && res <= 0xFFFFFFFFUL) {
+ return res;
+ }
+ }
+ invarg(arg, errmsg); /* does not return */
}
-int get_u8(uint8_t *val, char *arg, int base)
+uint16_t get_u16(char *arg, const char *errmsg)
{
unsigned long res;
char *ptr;
- if (!arg || !*arg)
- return -1;
- res = strtoul(arg, &ptr, base);
- if (!ptr || ptr == arg || *ptr || res > 0xFF)
- return -1;
- *val = res;
- return 0;
-}
-
-int get_s16(int16_t *val, char *arg, int base)
-{
- long res;
- char *ptr;
-
- if (!arg || !*arg)
- return -1;
- res = strtol(arg, &ptr, base);
- if (!ptr || ptr == arg || *ptr || res > 0x7FFF || res < -0x8000)
- return -1;
- *val = res;
- return 0;
-}
-
-int get_s8(int8_t *val, char *arg, int base)
-{
- long res;
- char *ptr;
-
- if (!arg || !*arg)
- return -1;
- res = strtol(arg, &ptr, base);
- if (!ptr || ptr == arg || *ptr || res > 0x7F || res < -0x80)
- return -1;
- *val = res;
- return 0;
+ if (*arg) {
+ res = strtoul(arg, &ptr, 0);
+ if (!*ptr && res <= 0xFFFF) {
+ return res;
+ }
+ }
+ invarg(arg, errmsg); /* does not return */
}
int get_addr_1(inet_prefix *addr, char *name, int family)
@@ -161,8 +105,8 @@ static int get_prefix_1(inet_prefix *dst, char *arg, int family)
|| strcmp(arg, "any") == 0
) {
dst->family = family;
- dst->bytelen = 0;
- dst->bitlen = 0;
+ /*dst->bytelen = 0; - done by memset */
+ /*dst->bitlen = 0;*/
return 0;
}
@@ -176,7 +120,8 @@ static int get_prefix_1(inet_prefix *dst, char *arg, int family)
inet_prefix netmask_pfx;
netmask_pfx.family = AF_UNSPEC;
- if ((get_unsigned(&plen, slash + 1, 0) || plen > dst->bitlen)
+ plen = bb_strtou(slash + 1, NULL, 0);
+ if ((errno || plen > dst->bitlen)
&& (get_addr_1(&netmask_pfx, slash + 1, family)))
err = -1;
else if (netmask_pfx.family == AF_INET) {
@@ -262,7 +207,7 @@ int inet_addr_match(inet_prefix *a, inet_prefix *b, int bits)
{
uint32_t *a1 = a->data;
uint32_t *a2 = b->data;
- int words = bits >> 0x05;
+ int words = bits >> 5;
bits &= 0x1f;
@@ -286,7 +231,7 @@ int inet_addr_match(inet_prefix *a, inet_prefix *b, int bits)
return 0;
}
-const char *rt_addr_n2a(int af, int UNUSED_PARAM len,
+const char *rt_addr_n2a(int af,
void *addr, char *buf, int buflen)
{
switch (af) {
@@ -298,9 +243,9 @@ const char *rt_addr_n2a(int af, int UNUSED_PARAM len,
}
}
+#ifdef RESOLVE_HOSTNAMES
const char *format_host(int af, int len, void *addr, char *buf, int buflen)
{
-#ifdef RESOLVE_HOSTNAMES
if (resolve_hosts) {
struct hostent *h_ent;
@@ -323,6 +268,6 @@ const char *format_host(int af, int len, void *addr, char *buf, int buflen)
}
}
}
-#endif
- return rt_addr_n2a(af, len, addr, buf, buflen);
+ return rt_addr_n2a(af, addr, buf, buflen);
}
+#endif