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
14 #include "inet_common.h"
18 static unsigned hz_internal;
24 fp = fopen_for_read("/proc/net/psched");
28 if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
34 hz_internal = bb_clk_tck();
38 unsigned get_unsigned(char *arg, const char *errmsg)
44 res = strtoul(arg, &ptr, 0);
45 //FIXME: "" will be accepted too, is it correct?!
46 if (!*ptr && res <= UINT_MAX) {
50 invarg(arg, errmsg); /* does not return */
53 uint32_t get_u32(char *arg, const char *errmsg)
59 res = strtoul(arg, &ptr, 0);
60 //FIXME: "" will be accepted too, is it correct?!
61 if (!*ptr && res <= 0xFFFFFFFFUL) {
65 invarg(arg, errmsg); /* does not return */
68 uint16_t get_u16(char *arg, const char *errmsg)
74 res = strtoul(arg, &ptr, 0);
75 //FIXME: "" will be accepted too, is it correct?!
76 if (!*ptr && res <= 0xFFFF) {
80 invarg(arg, errmsg); /* does not return */
83 int get_addr_1(inet_prefix *addr, char *name, int family)
85 memset(addr, 0, sizeof(*addr));
87 if (strcmp(name, "default") == 0
88 || strcmp(name, "all") == 0
89 || strcmp(name, "any") == 0
91 addr->family = family;
92 addr->bytelen = (family == AF_INET6 ? 16 : 4);
97 if (strchr(name, ':')) {
98 addr->family = AF_INET6;
99 if (family != AF_UNSPEC && family != AF_INET6)
101 if (inet_pton(AF_INET6, name, addr->data) <= 0)
108 if (family != AF_UNSPEC && family != AF_INET)
111 /* Try to parse it as IPv4 */
112 addr->family = AF_INET;
113 #if 0 /* Doesn't handle e.g. "10.10", for example, "ip r l root 10.10/16" */
114 if (inet_pton(AF_INET, name, addr->data) <= 0)
120 const char *cp = name - 1;
122 if ((unsigned char)(*cp - '0') <= 9) {
123 n = 10 * n + (unsigned char)(*cp - '0');
126 ((uint8_t*)addr->data)[i] = n;
129 if (*cp == '.' && ++i <= 3) {
143 static void get_prefix_1(inet_prefix *dst, char *arg, int family)
147 memset(dst, 0, sizeof(*dst));
149 if (strcmp(arg, "default") == 0
150 || strcmp(arg, "all") == 0
151 || strcmp(arg, "any") == 0
153 dst->family = family;
154 /*dst->bytelen = 0; - done by memset */
159 slash = strchr(arg, '/');
163 if (get_addr_1(dst, arg, family) == 0) {
164 dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
167 inet_prefix netmask_pfx;
169 netmask_pfx.family = AF_UNSPEC;
170 plen = bb_strtou(slash + 1, NULL, 0);
171 if ((errno || plen > dst->bitlen)
172 && get_addr_1(&netmask_pfx, slash + 1, family) != 0
176 if (netmask_pfx.family == AF_INET) {
177 /* fill in prefix length of dotted quad */
178 uint32_t mask = ntohl(netmask_pfx.data[0]);
179 uint32_t host = ~mask;
181 /* a valid netmask must be 2^n - 1 */
182 if (host & (host + 1))
185 for (plen = 0; mask; mask <<= 1)
187 if (plen > dst->bitlen)
189 /* dst->flags |= PREFIXLEN_SPECIFIED; */
199 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
202 int get_addr(inet_prefix *dst, char *arg, int family)
204 if (family == AF_PACKET) {
205 bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address");
207 if (get_addr_1(dst, arg, family)) {
208 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg);
213 void get_prefix(inet_prefix *dst, char *arg, int family)
215 if (family == AF_PACKET) {
216 bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix");
218 get_prefix_1(dst, arg, family);
221 uint32_t get_addr32(char *name)
225 if (get_addr_1(&addr, name, AF_INET)) {
226 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name);
231 void incomplete_command(void)
233 bb_error_msg_and_die("command line is not complete, try option \"help\"");
236 void invarg(const char *arg, const char *opt)
238 bb_error_msg_and_die(bb_msg_invalid_arg, arg, opt);
241 void duparg(const char *key, const char *arg)
243 bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg);
246 void duparg2(const char *key, const char *arg)
248 bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
251 int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
253 const uint32_t *a1 = a->data;
254 const uint32_t *a2 = b->data;
255 int words = bits >> 5;
260 if (memcmp(a1, a2, words << 2))
270 mask = htonl((0xffffffff) << (0x20 - bits));
272 if ((w1 ^ w2) & mask)
279 const char *rt_addr_n2a(int af,
280 void *addr, char *buf, int buflen)
285 return inet_ntop(af, addr, buf, buflen);
291 #ifdef RESOLVE_HOSTNAMES
292 const char *format_host(int af, int len, void *addr, char *buf, int buflen)
295 struct hostent *h_ent;
309 h_ent = gethostbyaddr(addr, len, af);
311 safe_strncpy(buf, h_ent->h_name, buflen);
316 return rt_addr_n2a(af, addr, buf, buflen);