15 |
#include "utils.h" |
#include "utils.h" |
16 |
#include "inet_common.h" |
#include "inet_common.h" |
17 |
|
|
18 |
int get_integer(int *val, char *arg, int base) |
unsigned get_unsigned(char *arg, const char *errmsg) |
|
{ |
|
|
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) |
|
19 |
{ |
{ |
20 |
unsigned long res; |
unsigned long res; |
21 |
char *ptr; |
char *ptr; |
22 |
|
|
23 |
if (!arg || !*arg) |
if (*arg) { |
24 |
return -1; |
res = strtoul(arg, &ptr, 0); |
25 |
res = strtoul(arg, &ptr, base); |
//FIXME: "" will be accepted too, is it correct?! |
26 |
if (!ptr || ptr == arg || *ptr || res > 0xFFFFFFFFUL) |
if (!*ptr && res <= UINT_MAX) { |
27 |
return -1; |
return res; |
28 |
*val = res; |
} |
29 |
return 0; |
} |
30 |
|
invarg(arg, errmsg); /* does not return */ |
31 |
} |
} |
32 |
|
|
33 |
int get_u16(uint16_t * val, char *arg, int base) |
uint32_t get_u32(char *arg, const char *errmsg) |
34 |
{ |
{ |
35 |
unsigned long res; |
unsigned long res; |
36 |
char *ptr; |
char *ptr; |
37 |
|
|
38 |
if (!arg || !*arg) |
if (*arg) { |
39 |
return -1; |
res = strtoul(arg, &ptr, 0); |
40 |
res = strtoul(arg, &ptr, base); |
//FIXME: "" will be accepted too, is it correct?! |
41 |
if (!ptr || ptr == arg || *ptr || res > 0xFFFF) |
if (!*ptr && res <= 0xFFFFFFFFUL) { |
42 |
return -1; |
return res; |
43 |
*val = res; |
} |
44 |
return 0; |
} |
45 |
|
invarg(arg, errmsg); /* does not return */ |
46 |
} |
} |
47 |
|
|
48 |
int get_u8(uint8_t * val, char *arg, int base) |
uint16_t get_u16(char *arg, const char *errmsg) |
49 |
{ |
{ |
50 |
unsigned long res; |
unsigned long res; |
51 |
char *ptr; |
char *ptr; |
52 |
|
|
53 |
if (!arg || !*arg) |
if (*arg) { |
54 |
return -1; |
res = strtoul(arg, &ptr, 0); |
55 |
res = strtoul(arg, &ptr, base); |
//FIXME: "" will be accepted too, is it correct?! |
56 |
if (!ptr || ptr == arg || *ptr || res > 0xFF) |
if (!*ptr && res <= 0xFFFF) { |
57 |
return -1; |
return res; |
58 |
*val = res; |
} |
59 |
return 0; |
} |
60 |
} |
invarg(arg, errmsg); /* does not return */ |
|
|
|
|
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; |
|
61 |
} |
} |
62 |
|
|
63 |
int get_addr_1(inet_prefix * addr, char *name, int family) |
int get_addr_1(inet_prefix *addr, char *name, int family) |
64 |
{ |
{ |
65 |
memset(addr, 0, sizeof(*addr)); |
memset(addr, 0, sizeof(*addr)); |
66 |
|
|
67 |
if (strcmp(name, bb_str_default) == 0 || |
if (strcmp(name, bb_str_default) == 0 |
68 |
strcmp(name, "all") == 0 || strcmp(name, "any") == 0) { |
|| strcmp(name, "all") == 0 |
69 |
|
|| strcmp(name, "any") == 0 |
70 |
|
) { |
71 |
addr->family = family; |
addr->family = family; |
72 |
addr->bytelen = (family == AF_INET6 ? 16 : 4); |
addr->bytelen = (family == AF_INET6 ? 16 : 4); |
73 |
addr->bitlen = -1; |
addr->bitlen = -1; |
95 |
return 0; |
return 0; |
96 |
} |
} |
97 |
|
|
98 |
int get_prefix_1(inet_prefix * dst, char *arg, int family) |
static int get_prefix_1(inet_prefix *dst, char *arg, int family) |
99 |
{ |
{ |
100 |
int err; |
int err; |
101 |
unsigned plen; |
unsigned plen; |
103 |
|
|
104 |
memset(dst, 0, sizeof(*dst)); |
memset(dst, 0, sizeof(*dst)); |
105 |
|
|
106 |
if (strcmp(arg, bb_str_default) == 0 || strcmp(arg, "any") == 0) { |
if (strcmp(arg, bb_str_default) == 0 |
107 |
|
|| strcmp(arg, "all") == 0 |
108 |
|
|| strcmp(arg, "any") == 0 |
109 |
|
) { |
110 |
dst->family = family; |
dst->family = family; |
111 |
dst->bytelen = 0; |
/*dst->bytelen = 0; - done by memset */ |
112 |
dst->bitlen = 0; |
/*dst->bitlen = 0;*/ |
113 |
return 0; |
return 0; |
114 |
} |
} |
115 |
|
|
123 |
inet_prefix netmask_pfx; |
inet_prefix netmask_pfx; |
124 |
|
|
125 |
netmask_pfx.family = AF_UNSPEC; |
netmask_pfx.family = AF_UNSPEC; |
126 |
if ((get_unsigned(&plen, slash + 1, 0) || plen > dst->bitlen) |
plen = bb_strtou(slash + 1, NULL, 0); |
127 |
&& (get_addr_1(&netmask_pfx, slash + 1, family))) |
if ((errno || plen > dst->bitlen) |
128 |
|
&& (get_addr_1(&netmask_pfx, slash + 1, family))) |
129 |
err = -1; |
err = -1; |
130 |
else if (netmask_pfx.family == AF_INET) { |
else if (netmask_pfx.family == AF_INET) { |
131 |
/* fill in prefix length of dotted quad */ |
/* fill in prefix length of dotted quad */ |
136 |
if (!(host & (host + 1))) { |
if (!(host & (host + 1))) { |
137 |
for (plen = 0; mask; mask <<= 1) |
for (plen = 0; mask; mask <<= 1) |
138 |
++plen; |
++plen; |
139 |
if (plen >= 0 && plen <= dst->bitlen) { |
if (plen <= dst->bitlen) { |
140 |
dst->bitlen = plen; |
dst->bitlen = plen; |
141 |
/* dst->flags |= PREFIXLEN_SPECIFIED; */ |
/* dst->flags |= PREFIXLEN_SPECIFIED; */ |
142 |
} else |
} else |
143 |
err = -1; |
err = -1; |
144 |
} else |
} else |
154 |
return err; |
return err; |
155 |
} |
} |
156 |
|
|
157 |
int get_addr(inet_prefix * dst, char *arg, int family) |
int get_addr(inet_prefix *dst, char *arg, int family) |
158 |
{ |
{ |
159 |
if (family == AF_PACKET) { |
if (family == AF_PACKET) { |
160 |
bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address"); |
bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address"); |
165 |
return 0; |
return 0; |
166 |
} |
} |
167 |
|
|
168 |
int get_prefix(inet_prefix * dst, char *arg, int family) |
int get_prefix(inet_prefix *dst, char *arg, int family) |
169 |
{ |
{ |
170 |
if (family == AF_PACKET) { |
if (family == AF_PACKET) { |
171 |
bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix"); |
bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix"); |
206 |
bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg); |
bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg); |
207 |
} |
} |
208 |
|
|
209 |
int inet_addr_match(inet_prefix * a, inet_prefix * b, int bits) |
int inet_addr_match(inet_prefix *a, inet_prefix *b, int bits) |
210 |
{ |
{ |
211 |
uint32_t *a1 = a->data; |
uint32_t *a1 = a->data; |
212 |
uint32_t *a2 = b->data; |
uint32_t *a2 = b->data; |
213 |
int words = bits >> 0x05; |
int words = bits >> 5; |
214 |
|
|
215 |
bits &= 0x1f; |
bits &= 0x1f; |
216 |
|
|
234 |
return 0; |
return 0; |
235 |
} |
} |
236 |
|
|
237 |
const char *rt_addr_n2a(int af, int UNUSED_PARAM len, |
const char *rt_addr_n2a(int af, |
238 |
void *addr, char *buf, int buflen) |
void *addr, char *buf, int buflen) |
239 |
{ |
{ |
240 |
switch (af) { |
switch (af) { |
246 |
} |
} |
247 |
} |
} |
248 |
|
|
249 |
|
#ifdef RESOLVE_HOSTNAMES |
250 |
const char *format_host(int af, int len, void *addr, char *buf, int buflen) |
const char *format_host(int af, int len, void *addr, char *buf, int buflen) |
251 |
{ |
{ |
|
#ifdef RESOLVE_HOSTNAMES |
|
252 |
if (resolve_hosts) { |
if (resolve_hosts) { |
253 |
struct hostent *h_ent; |
struct hostent *h_ent; |
254 |
|
|
271 |
} |
} |
272 |
} |
} |
273 |
} |
} |
274 |
#endif |
return rt_addr_n2a(af, addr, buf, buflen); |
|
return rt_addr_n2a(af, len, addr, buf, buflen); |
|
275 |
} |
} |
276 |
|
#endif |