blob: 42025bc669015f0ac56ab08a3f02df73f9e32ae7 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00004 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00006 *
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00007 * Changes:
8 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02009 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000010 */
11
Rob Landleyecae66a2006-06-02 20:53:38 +000012#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000013#include "utils.h"
"Vladimir N. Oleynik"007a0112005-09-22 11:11:11 +000014#include "inet_common.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015
Curt Brune69934702015-10-14 12:53:47 +020016unsigned get_hz(void)
17{
18 static unsigned hz_internal;
19 FILE *fp;
20
21 if (hz_internal)
22 return hz_internal;
23
24 fp = fopen_for_read("/proc/net/psched");
25 if (fp) {
26 unsigned nom, denom;
27
28 if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
29 if (nom == 1000000)
30 hz_internal = denom;
31 fclose(fp);
32 }
33 if (!hz_internal)
34 hz_internal = bb_clk_tck();
35 return hz_internal;
36}
37
Denis Vlasenko76140a72009-03-05 09:21:57 +000038unsigned get_unsigned(char *arg, const char *errmsg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000039{
40 unsigned long res;
41 char *ptr;
42
Denis Vlasenko76140a72009-03-05 09:21:57 +000043 if (*arg) {
44 res = strtoul(arg, &ptr, 0);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020045//FIXME: "" will be accepted too, is it correct?!
Denis Vlasenko76140a72009-03-05 09:21:57 +000046 if (!*ptr && res <= UINT_MAX) {
47 return res;
48 }
49 }
Denys Vlasenko0f296a32015-10-14 13:21:01 +020050 invarg_1_to_2(arg, errmsg); /* does not return */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000051}
52
Denis Vlasenko76140a72009-03-05 09:21:57 +000053uint32_t get_u32(char *arg, const char *errmsg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000054{
55 unsigned long res;
56 char *ptr;
57
Denis Vlasenko76140a72009-03-05 09:21:57 +000058 if (*arg) {
59 res = strtoul(arg, &ptr, 0);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020060//FIXME: "" will be accepted too, is it correct?!
Denis Vlasenko76140a72009-03-05 09:21:57 +000061 if (!*ptr && res <= 0xFFFFFFFFUL) {
62 return res;
63 }
64 }
Denys Vlasenko0f296a32015-10-14 13:21:01 +020065 invarg_1_to_2(arg, errmsg); /* does not return */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000066}
67
Denis Vlasenko76140a72009-03-05 09:21:57 +000068uint16_t get_u16(char *arg, const char *errmsg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000069{
70 unsigned long res;
71 char *ptr;
72
Denis Vlasenko76140a72009-03-05 09:21:57 +000073 if (*arg) {
74 res = strtoul(arg, &ptr, 0);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020075//FIXME: "" will be accepted too, is it correct?!
Denis Vlasenko76140a72009-03-05 09:21:57 +000076 if (!*ptr && res <= 0xFFFF) {
77 return res;
78 }
79 }
Denys Vlasenko0f296a32015-10-14 13:21:01 +020080 invarg_1_to_2(arg, errmsg); /* does not return */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000081}
82
Denis Vlasenko787a4922009-03-03 14:55:29 +000083int get_addr_1(inet_prefix *addr, char *name, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000084{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000085 memset(addr, 0, sizeof(*addr));
86
Denys Vlasenko0004e992010-09-01 12:01:17 +020087 if (strcmp(name, "default") == 0
Denis Vlasenko787a4922009-03-03 14:55:29 +000088 || strcmp(name, "all") == 0
89 || strcmp(name, "any") == 0
90 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000091 addr->family = family;
92 addr->bytelen = (family == AF_INET6 ? 16 : 4);
93 addr->bitlen = -1;
94 return 0;
95 }
96
97 if (strchr(name, ':')) {
98 addr->family = AF_INET6;
99 if (family != AF_UNSPEC && family != AF_INET6)
100 return -1;
101 if (inet_pton(AF_INET6, name, addr->data) <= 0)
102 return -1;
103 addr->bytelen = 16;
104 addr->bitlen = -1;
105 return 0;
106 }
107
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000108 if (family != AF_UNSPEC && family != AF_INET)
109 return -1;
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100110
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" */
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000114 if (inet_pton(AF_INET, name, addr->data) <= 0)
115 return -1;
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100116#else
117 {
118 unsigned i = 0;
119 unsigned n = 0;
120 const char *cp = name - 1;
121 while (*++cp) {
122 if ((unsigned char)(*cp - '0') <= 9) {
123 n = 10 * n + (unsigned char)(*cp - '0');
124 if (n >= 256)
125 return -1;
126 ((uint8_t*)addr->data)[i] = n;
127 continue;
128 }
129 if (*cp == '.' && ++i <= 3) {
130 n = 0;
131 continue;
132 }
133 return -1;
134 }
135 }
136#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000137 addr->bytelen = 4;
138 addr->bitlen = -1;
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100139
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000140 return 0;
141}
142
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100143static void get_prefix_1(inet_prefix *dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000144{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000145 char *slash;
146
147 memset(dst, 0, sizeof(*dst));
148
Denys Vlasenko0004e992010-09-01 12:01:17 +0200149 if (strcmp(arg, "default") == 0
Denis Vlasenko787a4922009-03-03 14:55:29 +0000150 || strcmp(arg, "all") == 0
151 || strcmp(arg, "any") == 0
152 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000153 dst->family = family;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000154 /*dst->bytelen = 0; - done by memset */
155 /*dst->bitlen = 0;*/
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100156 return;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000157 }
158
159 slash = strchr(arg, '/');
160 if (slash)
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000161 *slash = '\0';
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100162
163 if (get_addr_1(dst, arg, family) == 0) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000164 dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000165 if (slash) {
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100166 unsigned plen;
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000167 inet_prefix netmask_pfx;
168
169 netmask_pfx.family = AF_UNSPEC;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000170 plen = bb_strtou(slash + 1, NULL, 0);
171 if ((errno || plen > dst->bitlen)
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100172 && get_addr_1(&netmask_pfx, slash + 1, family) != 0
173 ) {
174 goto bad;
175 }
176 if (netmask_pfx.family == AF_INET) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000177 /* fill in prefix length of dotted quad */
178 uint32_t mask = ntohl(netmask_pfx.data[0]);
179 uint32_t host = ~mask;
180
181 /* a valid netmask must be 2^n - 1 */
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100182 if (host & (host + 1))
183 goto bad;
184
185 for (plen = 0; mask; mask <<= 1)
186 ++plen;
187 if (plen > dst->bitlen)
188 goto bad;
189 /* dst->flags |= PREFIXLEN_SPECIFIED; */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000190 }
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100191 dst->bitlen = plen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000192 }
193 }
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100194
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000195 if (slash)
196 *slash = '/';
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100197 return;
198 bad:
199 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000200}
201
Denis Vlasenko787a4922009-03-03 14:55:29 +0000202int get_addr(inet_prefix *dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000203{
204 if (family == AF_PACKET) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000205 bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000206 }
207 if (get_addr_1(dst, arg, family)) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000208 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000209 }
210 return 0;
211}
212
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100213void get_prefix(inet_prefix *dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000214{
215 if (family == AF_PACKET) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000216 bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000217 }
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100218 get_prefix_1(dst, arg, family);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000219}
220
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000221uint32_t get_addr32(char *name)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000222{
223 inet_prefix addr;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000224
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000225 if (get_addr_1(&addr, name, AF_INET)) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000226 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000227 }
228 return addr.data[0];
229}
230
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000231void incomplete_command(void)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000232{
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200233 bb_error_msg_and_die("command line is not complete, try \"help\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000234}
235
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200236void invarg_1_to_2(const char *arg, const char *opt)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000237{
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200238 bb_error_msg_and_die(bb_msg_invalid_arg_to, arg, opt);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000239}
240
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000241void duparg(const char *key, const char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000242{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000243 bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000244}
245
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000246void duparg2(const char *key, const char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000247{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000248 bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000249}
250
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100251int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000252{
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100253 const uint32_t *a1 = a->data;
254 const uint32_t *a2 = b->data;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000255 int words = bits >> 5;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000256
257 bits &= 0x1f;
258
259 if (words)
260 if (memcmp(a1, a2, words << 2))
261 return -1;
262
263 if (bits) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000264 uint32_t w1, w2;
265 uint32_t mask;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000266
267 w1 = a1[words];
268 w2 = a2[words];
269
270 mask = htonl((0xffffffff) << (0x20 - bits));
271
272 if ((w1 ^ w2) & mask)
273 return 1;
274 }
275
276 return 0;
277}
278
Denys Vlasenko926d8012015-10-14 13:56:42 +0200279const char *rt_addr_n2a(int af, void *addr)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000280{
281 switch (af) {
282 case AF_INET:
283 case AF_INET6:
Denys Vlasenko926d8012015-10-14 13:56:42 +0200284 return inet_ntop(af, addr,
285 auto_string(xzalloc(INET6_ADDRSTRLEN)), INET6_ADDRSTRLEN
286 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000287 default:
288 return "???";
289 }
290}
291
Denis Vlasenko76140a72009-03-05 09:21:57 +0000292#ifdef RESOLVE_HOSTNAMES
Denys Vlasenko926d8012015-10-14 13:56:42 +0200293const char *format_host(int af, int len, void *addr)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000294{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000295 if (resolve_hosts) {
296 struct hostent *h_ent;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000297
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000298 if (len <= 0) {
299 switch (af) {
300 case AF_INET:
301 len = 4;
302 break;
303 case AF_INET6:
304 len = 16;
305 break;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000306 default:;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000307 }
308 }
Denis Vlasenko8c1aaf32007-06-04 21:03:51 +0000309 if (len > 0) {
310 h_ent = gethostbyaddr(addr, len, af);
311 if (h_ent != NULL) {
Denys Vlasenko926d8012015-10-14 13:56:42 +0200312 return auto_string(xstrdup(h_ent->h_name));
Denis Vlasenko8c1aaf32007-06-04 21:03:51 +0000313 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000314 }
315 }
Denys Vlasenko926d8012015-10-14 13:56:42 +0200316 return rt_addr_n2a(af, addr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000317}
Denis Vlasenko76140a72009-03-05 09:21:57 +0000318#endif