blob: 4ce2303569bdc9c72f1fada68f781810c4bd05ac [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 */
Rob Landleyecae66a2006-06-02 20:53:38 +000011#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000012#include "utils.h"
"Vladimir N. Oleynik"007a0112005-09-22 11:11:11 +000013#include "inet_common.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000014
Denys Vlasenko826bdcd2017-04-07 17:15:40 +020015unsigned FAST_FUNC get_hz(void)
Curt Brune69934702015-10-14 12:53:47 +020016{
17 static unsigned hz_internal;
18 FILE *fp;
19
20 if (hz_internal)
21 return hz_internal;
22
23 fp = fopen_for_read("/proc/net/psched");
24 if (fp) {
25 unsigned nom, denom;
26
27 if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
28 if (nom == 1000000)
29 hz_internal = denom;
30 fclose(fp);
31 }
32 if (!hz_internal)
33 hz_internal = bb_clk_tck();
34 return hz_internal;
35}
36
Denys Vlasenko826bdcd2017-04-07 17:15:40 +020037unsigned FAST_FUNC get_unsigned(char *arg, const char *errmsg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000038{
39 unsigned long res;
40 char *ptr;
41
Denis Vlasenko76140a72009-03-05 09:21:57 +000042 if (*arg) {
43 res = strtoul(arg, &ptr, 0);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020044//FIXME: "" will be accepted too, is it correct?!
Denis Vlasenko76140a72009-03-05 09:21:57 +000045 if (!*ptr && res <= UINT_MAX) {
46 return res;
47 }
48 }
Denys Vlasenko0f296a32015-10-14 13:21:01 +020049 invarg_1_to_2(arg, errmsg); /* does not return */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000050}
51
Denys Vlasenko826bdcd2017-04-07 17:15:40 +020052uint32_t FAST_FUNC get_u32(char *arg, const char *errmsg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000053{
54 unsigned long res;
55 char *ptr;
56
Denis Vlasenko76140a72009-03-05 09:21:57 +000057 if (*arg) {
58 res = strtoul(arg, &ptr, 0);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020059//FIXME: "" will be accepted too, is it correct?!
Denis Vlasenko76140a72009-03-05 09:21:57 +000060 if (!*ptr && res <= 0xFFFFFFFFUL) {
61 return res;
62 }
63 }
Denys Vlasenko0f296a32015-10-14 13:21:01 +020064 invarg_1_to_2(arg, errmsg); /* does not return */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000065}
66
Denys Vlasenko826bdcd2017-04-07 17:15:40 +020067uint16_t FAST_FUNC get_u16(char *arg, const char *errmsg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000068{
69 unsigned long res;
70 char *ptr;
71
Denis Vlasenko76140a72009-03-05 09:21:57 +000072 if (*arg) {
73 res = strtoul(arg, &ptr, 0);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020074//FIXME: "" will be accepted too, is it correct?!
Denis Vlasenko76140a72009-03-05 09:21:57 +000075 if (!*ptr && res <= 0xFFFF) {
76 return res;
77 }
78 }
Denys Vlasenko0f296a32015-10-14 13:21:01 +020079 invarg_1_to_2(arg, errmsg); /* does not return */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000080}
81
Denys Vlasenko826bdcd2017-04-07 17:15:40 +020082int FAST_FUNC get_addr_1(inet_prefix *addr, char *name, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000083{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000084 memset(addr, 0, sizeof(*addr));
85
Denys Vlasenko0004e992010-09-01 12:01:17 +020086 if (strcmp(name, "default") == 0
Denis Vlasenko787a4922009-03-03 14:55:29 +000087 || strcmp(name, "all") == 0
88 || strcmp(name, "any") == 0
89 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000090 addr->family = family;
91 addr->bytelen = (family == AF_INET6 ? 16 : 4);
92 addr->bitlen = -1;
93 return 0;
94 }
95
96 if (strchr(name, ':')) {
97 addr->family = AF_INET6;
98 if (family != AF_UNSPEC && family != AF_INET6)
99 return -1;
100 if (inet_pton(AF_INET6, name, addr->data) <= 0)
101 return -1;
102 addr->bytelen = 16;
103 addr->bitlen = -1;
104 return 0;
105 }
106
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000107 if (family != AF_UNSPEC && family != AF_INET)
108 return -1;
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100109
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" */
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000113 if (inet_pton(AF_INET, name, addr->data) <= 0)
114 return -1;
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100115#else
116 {
117 unsigned i = 0;
118 unsigned n = 0;
119 const char *cp = name - 1;
120 while (*++cp) {
121 if ((unsigned char)(*cp - '0') <= 9) {
122 n = 10 * n + (unsigned char)(*cp - '0');
123 if (n >= 256)
124 return -1;
125 ((uint8_t*)addr->data)[i] = n;
126 continue;
127 }
128 if (*cp == '.' && ++i <= 3) {
129 n = 0;
130 continue;
131 }
132 return -1;
133 }
134 }
135#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000136 addr->bytelen = 4;
137 addr->bitlen = -1;
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100138
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000139 return 0;
140}
141
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100142static void get_prefix_1(inet_prefix *dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000143{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000144 char *slash;
145
146 memset(dst, 0, sizeof(*dst));
147
Denys Vlasenko0004e992010-09-01 12:01:17 +0200148 if (strcmp(arg, "default") == 0
Denis Vlasenko787a4922009-03-03 14:55:29 +0000149 || strcmp(arg, "all") == 0
150 || strcmp(arg, "any") == 0
151 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000152 dst->family = family;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000153 /*dst->bytelen = 0; - done by memset */
154 /*dst->bitlen = 0;*/
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100155 return;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000156 }
157
158 slash = strchr(arg, '/');
159 if (slash)
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000160 *slash = '\0';
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100161
162 if (get_addr_1(dst, arg, family) == 0) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000163 dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000164 if (slash) {
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100165 unsigned plen;
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000166 inet_prefix netmask_pfx;
167
168 netmask_pfx.family = AF_UNSPEC;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000169 plen = bb_strtou(slash + 1, NULL, 0);
170 if ((errno || plen > dst->bitlen)
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100171 && get_addr_1(&netmask_pfx, slash + 1, family) != 0
172 ) {
173 goto bad;
174 }
175 if (netmask_pfx.family == AF_INET) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000176 /* fill in prefix length of dotted quad */
177 uint32_t mask = ntohl(netmask_pfx.data[0]);
178 uint32_t host = ~mask;
179
180 /* a valid netmask must be 2^n - 1 */
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100181 if (host & (host + 1))
182 goto bad;
183
184 for (plen = 0; mask; mask <<= 1)
185 ++plen;
186 if (plen > dst->bitlen)
187 goto bad;
188 /* dst->flags |= PREFIXLEN_SPECIFIED; */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000189 }
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100190 dst->bitlen = plen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000191 }
192 }
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100193
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000194 if (slash)
195 *slash = '/';
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100196 return;
197 bad:
198 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000199}
200
Denys Vlasenko826bdcd2017-04-07 17:15:40 +0200201int FAST_FUNC get_addr(inet_prefix *dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000202{
203 if (family == AF_PACKET) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000204 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 +0000205 }
206 if (get_addr_1(dst, arg, family)) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000207 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000208 }
209 return 0;
210}
211
Denys Vlasenko826bdcd2017-04-07 17:15:40 +0200212void FAST_FUNC get_prefix(inet_prefix *dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000213{
214 if (family == AF_PACKET) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000215 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 +0000216 }
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100217 get_prefix_1(dst, arg, family);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000218}
219
Denys Vlasenko826bdcd2017-04-07 17:15:40 +0200220uint32_t FAST_FUNC get_addr32(char *name)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000221{
222 inet_prefix addr;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000223
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000224 if (get_addr_1(&addr, name, AF_INET)) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000225 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000226 }
227 return addr.data[0];
228}
229
Denys Vlasenko826bdcd2017-04-07 17:15:40 +0200230char** FAST_FUNC next_arg(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000231{
Denys Vlasenko826bdcd2017-04-07 17:15:40 +0200232 if (!*++argv)
James Byrne69374872019-07-02 11:35:03 +0200233 bb_simple_error_msg_and_die("command line is not complete, try \"help\"");
Denys Vlasenko826bdcd2017-04-07 17:15:40 +0200234 return argv;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000235}
236
Denys Vlasenko826bdcd2017-04-07 17:15:40 +0200237void FAST_FUNC invarg_1_to_2(const char *arg, const char *opt)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000238{
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200239 bb_error_msg_and_die(bb_msg_invalid_arg_to, arg, opt);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000240}
241
Denys Vlasenko826bdcd2017-04-07 17:15:40 +0200242void FAST_FUNC duparg(const char *key, const char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000243{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000244 bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000245}
246
Denys Vlasenko826bdcd2017-04-07 17:15:40 +0200247void FAST_FUNC duparg2(const char *key, const char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000248{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000249 bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000250}
251
Denys Vlasenko826bdcd2017-04-07 17:15:40 +0200252int FAST_FUNC inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000253{
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100254 const uint32_t *a1 = a->data;
255 const uint32_t *a2 = b->data;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000256 int words = bits >> 5;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000257
258 bits &= 0x1f;
259
260 if (words)
261 if (memcmp(a1, a2, words << 2))
262 return -1;
263
264 if (bits) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000265 uint32_t w1, w2;
266 uint32_t mask;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000267
268 w1 = a1[words];
269 w2 = a2[words];
270
271 mask = htonl((0xffffffff) << (0x20 - bits));
272
273 if ((w1 ^ w2) & mask)
274 return 1;
275 }
276
277 return 0;
278}
279
Denys Vlasenko826bdcd2017-04-07 17:15:40 +0200280const char* FAST_FUNC rt_addr_n2a(int af, void *addr)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000281{
282 switch (af) {
283 case AF_INET:
284 case AF_INET6:
Denys Vlasenko926d8012015-10-14 13:56:42 +0200285 return inet_ntop(af, addr,
286 auto_string(xzalloc(INET6_ADDRSTRLEN)), INET6_ADDRSTRLEN
287 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000288 default:
289 return "???";
290 }
291}
292
Denis Vlasenko76140a72009-03-05 09:21:57 +0000293#ifdef RESOLVE_HOSTNAMES
Denys Vlasenko826bdcd2017-04-07 17:15:40 +0200294const char* FAST_FUNC format_host(int af, int len, void *addr)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000295{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000296 if (resolve_hosts) {
297 struct hostent *h_ent;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000298
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000299 if (len <= 0) {
300 switch (af) {
301 case AF_INET:
302 len = 4;
303 break;
304 case AF_INET6:
305 len = 16;
306 break;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000307 default:;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000308 }
309 }
Denis Vlasenko8c1aaf32007-06-04 21:03:51 +0000310 if (len > 0) {
311 h_ent = gethostbyaddr(addr, len, af);
312 if (h_ent != NULL) {
Denys Vlasenko926d8012015-10-14 13:56:42 +0200313 return auto_string(xstrdup(h_ent->h_name));
Denis Vlasenko8c1aaf32007-06-04 21:03:51 +0000314 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000315 }
316 }
Denys Vlasenko926d8012015-10-14 13:56:42 +0200317 return rt_addr_n2a(af, addr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000318}
Denis Vlasenko76140a72009-03-05 09:21:57 +0000319#endif