4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
14 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
20 #include <arpa/inet.h>
24 #include "inet_common.h"
26 int get_integer(int *val, char *arg, int base)
33 res = strtol(arg, &ptr, base);
34 if (!ptr || ptr == arg || *ptr || res > INT_MAX || res < INT_MIN)
40 int get_unsigned(unsigned *val, char *arg, int base)
47 res = strtoul(arg, &ptr, base);
48 if (!ptr || ptr == arg || *ptr || res > UINT_MAX)
54 int get_u32(__u32 * val, char *arg, int base)
61 res = strtoul(arg, &ptr, base);
62 if (!ptr || ptr == arg || *ptr || res > 0xFFFFFFFFUL)
68 int get_u16(__u16 * val, char *arg, int base)
75 res = strtoul(arg, &ptr, base);
76 if (!ptr || ptr == arg || *ptr || res > 0xFFFF)
82 int get_u8(__u8 * val, char *arg, int base)
89 res = strtoul(arg, &ptr, base);
90 if (!ptr || ptr == arg || *ptr || res > 0xFF)
96 int get_s16(__s16 * val, char *arg, int base)
103 res = strtol(arg, &ptr, base);
104 if (!ptr || ptr == arg || *ptr || res > 0x7FFF || res < -0x8000)
110 int get_s8(__s8 * val, char *arg, int base)
117 res = strtol(arg, &ptr, base);
118 if (!ptr || ptr == arg || *ptr || res > 0x7F || res < -0x80)
124 int get_addr_1(inet_prefix * addr, char *name, int family)
127 unsigned char *ap = (unsigned char *) addr->data;
130 memset(addr, 0, sizeof(*addr));
132 if (strcmp(name, bb_INET_default) == 0 ||
133 strcmp(name, "all") == 0 || strcmp(name, "any") == 0) {
134 addr->family = family;
135 addr->bytelen = (family == AF_INET6 ? 16 : 4);
140 if (strchr(name, ':')) {
141 addr->family = AF_INET6;
142 if (family != AF_UNSPEC && family != AF_INET6)
144 if (inet_pton(AF_INET6, name, addr->data) <= 0)
151 addr->family = AF_INET;
152 if (family != AF_UNSPEC && family != AF_INET)
156 for (cp = name, i = 0; *cp; cp++) {
157 if (*cp <= '9' && *cp >= '0') {
158 ap[i] = 10 * ap[i] + (*cp - '0');
161 if (*cp == '.' && ++i <= 3)
168 int get_prefix_1(inet_prefix * dst, char *arg, int family)
174 memset(dst, 0, sizeof(*dst));
176 if (strcmp(arg, bb_INET_default) == 0 || strcmp(arg, "any") == 0) {
177 dst->family = family;
183 slash = strchr(arg, '/');
186 err = get_addr_1(dst, arg, family);
188 switch (dst->family) {
197 if (get_integer(&plen, slash + 1, 0) || plen > dst->bitlen) {
210 int get_addr(inet_prefix * dst, char *arg, int family)
212 if (family == AF_PACKET) {
213 bb_error_msg_and_die("\"%s\" may be inet address, but it is not allowed in this context.", arg);
215 if (get_addr_1(dst, arg, family)) {
216 bb_error_msg_and_die("an inet address is expected rather than \"%s\".", arg);
221 int get_prefix(inet_prefix * dst, char *arg, int family)
223 if (family == AF_PACKET) {
224 bb_error_msg_and_die("\"%s\" may be inet address, but it is not allowed in this context.", arg);
226 if (get_prefix_1(dst, arg, family)) {
227 bb_error_msg_and_die("an inet address is expected rather than \"%s\".", arg);
232 __u32 get_addr32(char *name)
236 if (get_addr_1(&addr, name, AF_INET)) {
237 bb_error_msg_and_die("an IP address is expected rather than \"%s\"", name);
242 void incomplete_command(void)
244 bb_error_msg("Command line is not complete. Try option \"help\"");
248 void invarg(char *msg, char *arg)
250 bb_error_msg("argument \"%s\" is wrong: %s", arg, msg);
254 void duparg(char *key, char *arg)
256 bb_error_msg("duplicate \"%s\": \"%s\" is the second value.", key, arg);
260 void duparg2(char *key, char *arg)
262 bb_error_msg("either \"%s\" is duplicate, or \"%s\" is a garbage.", key, arg);
266 int matches(char *cmd, char *pattern)
268 int len = strlen(cmd);
270 if (len > strlen(pattern)) {
273 return memcmp(pattern, cmd, len);
276 int inet_addr_match(inet_prefix * a, inet_prefix * b, int bits)
280 int words = bits >> 0x05;
285 if (memcmp(a1, a2, words << 2))
295 mask = htonl((0xffffffff) << (0x20 - bits));
297 if ((w1 ^ w2) & mask)
304 int __iproute2_hz_internal;
309 FILE *fp = fopen("/proc/net/psched", "r");
314 if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
321 return sysconf(_SC_CLK_TCK);
324 const char *rt_addr_n2a(int af, int len, void *addr, char *buf, int buflen)
329 return inet_ntop(af, addr, buf, buflen);
336 const char *format_host(int af, int len, void *addr, char *buf, int buflen)
338 #ifdef RESOLVE_HOSTNAMES
340 struct hostent *h_ent;
353 if (len > 0 && (h_ent = gethostbyaddr(addr, len, af)) != NULL) {
354 snprintf(buf, buflen - 1, "%s", h_ent->h_name);
359 return rt_addr_n2a(af, len, addr, buf, buflen);