1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
13 #include "inet_common.h"
15 unsigned FAST_FUNC get_hz(void)
17 static unsigned hz_internal;
23 fp = fopen_for_read("/proc/net/psched");
27 if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
33 hz_internal = bb_clk_tck();
37 unsigned FAST_FUNC get_unsigned(char *arg, const char *errmsg)
43 res = strtoul(arg, &ptr, 0);
44 //FIXME: "" will be accepted too, is it correct?!
45 if (!*ptr && res <= UINT_MAX) {
49 invarg_1_to_2(arg, errmsg); /* does not return */
52 uint32_t FAST_FUNC get_u32(char *arg, const char *errmsg)
58 res = strtoul(arg, &ptr, 0);
59 //FIXME: "" will be accepted too, is it correct?!
60 if (!*ptr && res <= 0xFFFFFFFFUL) {
64 invarg_1_to_2(arg, errmsg); /* does not return */
67 uint16_t FAST_FUNC get_u16(char *arg, const char *errmsg)
73 res = strtoul(arg, &ptr, 0);
74 //FIXME: "" will be accepted too, is it correct?!
75 if (!*ptr && res <= 0xFFFF) {
79 invarg_1_to_2(arg, errmsg); /* does not return */
82 int FAST_FUNC get_addr_1(inet_prefix *addr, char *name, int family)
84 memset(addr, 0, sizeof(*addr));
86 if (strcmp(name, "default") == 0
87 || strcmp(name, "all") == 0
88 || strcmp(name, "any") == 0
90 addr->family = family;
91 addr->bytelen = (family == AF_INET6 ? 16 : 4);
96 if (strchr(name, ':')) {
97 addr->family = AF_INET6;
98 if (family != AF_UNSPEC && family != AF_INET6)
100 if (inet_pton(AF_INET6, name, addr->data) <= 0)
107 if (family != AF_UNSPEC && family != AF_INET)
110 /* Try to parse it as IPv4 */
111 addr->family = AF_INET;
112 #if 0 /* Doesn't handle e.g. "10.10", for example, "ip r l root 10.10/16" */
113 if (inet_pton(AF_INET, name, addr->data) <= 0)
119 const char *cp = name - 1;
121 if ((unsigned char)(*cp - '0') <= 9) {
122 n = 10 * n + (unsigned char)(*cp - '0');
125 ((uint8_t*)addr->data)[i] = n;
128 if (*cp == '.' && ++i <= 3) {
142 static void get_prefix_1(inet_prefix *dst, char *arg, int family)
146 memset(dst, 0, sizeof(*dst));
148 if (strcmp(arg, "default") == 0
149 || strcmp(arg, "all") == 0
150 || strcmp(arg, "any") == 0
152 dst->family = family;
153 /*dst->bytelen = 0; - done by memset */
158 slash = strchr(arg, '/');
162 if (get_addr_1(dst, arg, family) == 0) {
163 dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
166 inet_prefix netmask_pfx;
168 netmask_pfx.family = AF_UNSPEC;
169 plen = bb_strtou(slash + 1, NULL, 0);
170 if ((errno || plen > dst->bitlen)
171 && get_addr_1(&netmask_pfx, slash + 1, family) != 0
175 if (netmask_pfx.family == AF_INET) {
176 /* fill in prefix length of dotted quad */
177 uint32_t mask = ntohl(netmask_pfx.data[0]);
178 uint32_t host = ~mask;
180 /* a valid netmask must be 2^n - 1 */
181 if (host & (host + 1))
184 for (plen = 0; mask; mask <<= 1)
186 if (plen > dst->bitlen)
188 /* dst->flags |= PREFIXLEN_SPECIFIED; */
198 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
201 int FAST_FUNC get_addr(inet_prefix *dst, char *arg, int family)
203 if (family == AF_PACKET) {
204 bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address");
206 if (get_addr_1(dst, arg, family)) {
207 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg);
212 void FAST_FUNC get_prefix(inet_prefix *dst, char *arg, int family)
214 if (family == AF_PACKET) {
215 bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix");
217 get_prefix_1(dst, arg, family);
220 uint32_t FAST_FUNC get_addr32(char *name)
224 if (get_addr_1(&addr, name, AF_INET)) {
225 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name);
230 char** FAST_FUNC next_arg(char **argv)
233 bb_error_msg_and_die("command line is not complete, try \"help\"");
237 void FAST_FUNC invarg_1_to_2(const char *arg, const char *opt)
239 bb_error_msg_and_die(bb_msg_invalid_arg_to, arg, opt);
242 void FAST_FUNC duparg(const char *key, const char *arg)
244 bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg);
247 void FAST_FUNC duparg2(const char *key, const char *arg)
249 bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
252 int FAST_FUNC inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
254 const uint32_t *a1 = a->data;
255 const uint32_t *a2 = b->data;
256 int words = bits >> 5;
261 if (memcmp(a1, a2, words << 2))
271 mask = htonl((0xffffffff) << (0x20 - bits));
273 if ((w1 ^ w2) & mask)
280 const char* FAST_FUNC rt_addr_n2a(int af, void *addr)
285 return inet_ntop(af, addr,
286 auto_string(xzalloc(INET6_ADDRSTRLEN)), INET6_ADDRSTRLEN
293 #ifdef RESOLVE_HOSTNAMES
294 const char* FAST_FUNC format_host(int af, int len, void *addr)
297 struct hostent *h_ent;
311 h_ent = gethostbyaddr(addr, len, af);
313 return auto_string(xstrdup(h_ent->h_name));
317 return rt_addr_n2a(af, addr);