1 /* vi: set sw=4 ts=4: */
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
20 #include "inet_common.h"
22 int get_integer(int *val, char *arg, int base)
29 res = strtol(arg, &ptr, base);
30 if (!ptr || ptr == arg || *ptr || res > INT_MAX || res < INT_MIN)
36 int get_unsigned(unsigned *val, char *arg, int base)
43 res = strtoul(arg, &ptr, base);
44 if (!ptr || ptr == arg || *ptr || res > UINT_MAX)
50 int get_u32(__u32 * val, char *arg, int base)
57 res = strtoul(arg, &ptr, base);
58 if (!ptr || ptr == arg || *ptr || res > 0xFFFFFFFFUL)
64 int get_u16(__u16 * val, char *arg, int base)
71 res = strtoul(arg, &ptr, base);
72 if (!ptr || ptr == arg || *ptr || res > 0xFFFF)
78 int get_u8(__u8 * val, char *arg, int base)
85 res = strtoul(arg, &ptr, base);
86 if (!ptr || ptr == arg || *ptr || res > 0xFF)
92 int get_s16(__s16 * val, char *arg, int base)
99 res = strtol(arg, &ptr, base);
100 if (!ptr || ptr == arg || *ptr || res > 0x7FFF || res < -0x8000)
106 int get_s8(__s8 * val, char *arg, int base)
113 res = strtol(arg, &ptr, base);
114 if (!ptr || ptr == arg || *ptr || res > 0x7F || res < -0x80)
120 int get_addr_1(inet_prefix * addr, char *name, int family)
123 unsigned char *ap = (unsigned char *) addr->data;
126 memset(addr, 0, sizeof(*addr));
128 if (strcmp(name, bb_INET_default) == 0 ||
129 strcmp(name, "all") == 0 || strcmp(name, "any") == 0) {
130 addr->family = family;
131 addr->bytelen = (family == AF_INET6 ? 16 : 4);
136 if (strchr(name, ':')) {
137 addr->family = AF_INET6;
138 if (family != AF_UNSPEC && family != AF_INET6)
140 if (inet_pton(AF_INET6, name, addr->data) <= 0)
147 addr->family = AF_INET;
148 if (family != AF_UNSPEC && family != AF_INET)
152 for (cp = name, i = 0; *cp; cp++) {
153 if (*cp <= '9' && *cp >= '0') {
154 ap[i] = 10 * ap[i] + (*cp - '0');
157 if (*cp == '.' && ++i <= 3)
164 int get_prefix_1(inet_prefix * dst, char *arg, int family)
170 memset(dst, 0, sizeof(*dst));
172 if (strcmp(arg, bb_INET_default) == 0 || strcmp(arg, "any") == 0) {
173 dst->family = family;
179 slash = strchr(arg, '/');
182 err = get_addr_1(dst, arg, family);
184 switch (dst->family) {
193 if (get_integer(&plen, slash + 1, 0) || plen > dst->bitlen) {
206 int get_addr(inet_prefix * dst, char *arg, int family)
208 if (family == AF_PACKET) {
209 bb_error_msg_and_die("\"%s\" may be inet address, but it is not allowed in this context.", arg);
211 if (get_addr_1(dst, arg, family)) {
212 bb_error_msg_and_die("an inet address is expected rather than \"%s\".", arg);
217 int get_prefix(inet_prefix * dst, char *arg, int family)
219 if (family == AF_PACKET) {
220 bb_error_msg_and_die("\"%s\" may be inet address, but it is not allowed in this context.", arg);
222 if (get_prefix_1(dst, arg, family)) {
223 bb_error_msg_and_die("an inet address is expected rather than \"%s\".", arg);
228 __u32 get_addr32(char *name)
232 if (get_addr_1(&addr, name, AF_INET)) {
233 bb_error_msg_and_die("an IP address is expected rather than \"%s\"", name);
238 void incomplete_command(void)
240 bb_error_msg("Command line is not complete. Try option \"help\"");
244 void invarg(const char * const arg, const char * const opt)
246 bb_error_msg(bb_msg_invalid_arg, arg, opt);
250 void duparg(char *key, char *arg)
252 bb_error_msg("duplicate \"%s\": \"%s\" is the second value.", key, arg);
256 void duparg2(char *key, char *arg)
258 bb_error_msg("either \"%s\" is duplicate, or \"%s\" is a garbage.", key, arg);
262 int matches(char *cmd, char *pattern)
264 int len = strlen(cmd);
266 if (len > strlen(pattern)) {
269 return memcmp(pattern, cmd, len);
272 int inet_addr_match(inet_prefix * a, inet_prefix * b, int bits)
276 int words = bits >> 0x05;
281 if (memcmp(a1, a2, words << 2))
291 mask = htonl((0xffffffff) << (0x20 - bits));
293 if ((w1 ^ w2) & mask)
300 int __iproute2_hz_internal;
305 FILE *fp = fopen("/proc/net/psched", "r");
310 if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
317 return sysconf(_SC_CLK_TCK);
320 const char *rt_addr_n2a(int af, int ATTRIBUTE_UNUSED len,
321 void *addr, char *buf, int buflen)
326 return inet_ntop(af, addr, buf, buflen);
333 const char *format_host(int af, int len, void *addr, char *buf, int buflen)
335 #ifdef RESOLVE_HOSTNAMES
337 struct hostent *h_ent;
350 if (len > 0 && (h_ent = gethostbyaddr(addr, len, af)) != NULL) {
351 snprintf(buf, buflen - 1, "%s", h_ent->h_name);
356 return rt_addr_n2a(af, len, addr, buf, buflen);