1 /* vi: set sw=4 ts=4: */
2 /*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4 *
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 *
7 * Changes:
8 *
9 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
10 */
11 #include "libbb.h"
12 #include "utils.h"
13 #include "inet_common.h"
14
get_hz(void)15 unsigned FAST_FUNC get_hz(void)
16 {
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
get_unsigned(char * arg,const char * errmsg)37 unsigned FAST_FUNC get_unsigned(char *arg, const char *errmsg)
38 {
39 unsigned long res;
40 char *ptr;
41
42 if (*arg) {
43 res = strtoul(arg, &ptr, 0);
44 //FIXME: "" will be accepted too, is it correct?!
45 if (!*ptr && res <= UINT_MAX) {
46 return res;
47 }
48 }
49 invarg_1_to_2(arg, errmsg); /* does not return */
50 }
51
get_u32(char * arg,const char * errmsg)52 uint32_t FAST_FUNC get_u32(char *arg, const char *errmsg)
53 {
54 unsigned long res;
55 char *ptr;
56
57 if (*arg) {
58 res = strtoul(arg, &ptr, 0);
59 //FIXME: "" will be accepted too, is it correct?!
60 if (!*ptr && res <= 0xFFFFFFFFUL) {
61 return res;
62 }
63 }
64 invarg_1_to_2(arg, errmsg); /* does not return */
65 }
66
get_u16(char * arg,const char * errmsg)67 uint16_t FAST_FUNC get_u16(char *arg, const char *errmsg)
68 {
69 unsigned long res;
70 char *ptr;
71
72 if (*arg) {
73 res = strtoul(arg, &ptr, 0);
74 //FIXME: "" will be accepted too, is it correct?!
75 if (!*ptr && res <= 0xFFFF) {
76 return res;
77 }
78 }
79 invarg_1_to_2(arg, errmsg); /* does not return */
80 }
81
get_addr_1(inet_prefix * addr,char * name,int family)82 int FAST_FUNC get_addr_1(inet_prefix *addr, char *name, int family)
83 {
84 memset(addr, 0, sizeof(*addr));
85
86 if (strcmp(name, "default") == 0
87 || strcmp(name, "all") == 0
88 || strcmp(name, "any") == 0
89 ) {
90 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
107 if (family != AF_UNSPEC && family != AF_INET)
108 return -1;
109
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" */
113 if (inet_pton(AF_INET, name, addr->data) <= 0)
114 return -1;
115 #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
136 addr->bytelen = 4;
137 addr->bitlen = -1;
138
139 return 0;
140 }
141
get_prefix_1(inet_prefix * dst,char * arg,int family)142 static void get_prefix_1(inet_prefix *dst, char *arg, int family)
143 {
144 char *slash;
145
146 memset(dst, 0, sizeof(*dst));
147
148 if (strcmp(arg, "default") == 0
149 || strcmp(arg, "all") == 0
150 || strcmp(arg, "any") == 0
151 ) {
152 dst->family = family;
153 /*dst->bytelen = 0; - done by memset */
154 /*dst->bitlen = 0;*/
155 return;
156 }
157
158 slash = strchr(arg, '/');
159 if (slash)
160 *slash = '\0';
161
162 if (get_addr_1(dst, arg, family) == 0) {
163 dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
164 if (slash) {
165 unsigned plen;
166 inet_prefix netmask_pfx;
167
168 netmask_pfx.family = AF_UNSPEC;
169 plen = bb_strtou(slash + 1, NULL, 0);
170 if ((errno || plen > dst->bitlen)
171 && get_addr_1(&netmask_pfx, slash + 1, family) != 0
172 ) {
173 goto bad;
174 }
175 if (netmask_pfx.family == AF_INET) {
176 /* 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 */
181 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; */
189 }
190 dst->bitlen = plen;
191 }
192 }
193
194 if (slash)
195 *slash = '/';
196 return;
197 bad:
198 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
199 }
200
get_addr(inet_prefix * dst,char * arg,int family)201 int FAST_FUNC get_addr(inet_prefix *dst, char *arg, int family)
202 {
203 if (family == AF_PACKET) {
204 bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address");
205 }
206 if (get_addr_1(dst, arg, family)) {
207 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg);
208 }
209 return 0;
210 }
211
get_prefix(inet_prefix * dst,char * arg,int family)212 void FAST_FUNC get_prefix(inet_prefix *dst, char *arg, int family)
213 {
214 if (family == AF_PACKET) {
215 bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix");
216 }
217 get_prefix_1(dst, arg, family);
218 }
219
get_addr32(char * name)220 uint32_t FAST_FUNC get_addr32(char *name)
221 {
222 inet_prefix addr;
223
224 if (get_addr_1(&addr, name, AF_INET)) {
225 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name);
226 }
227 return addr.data[0];
228 }
229
next_arg(char ** argv)230 char** FAST_FUNC next_arg(char **argv)
231 {
232 if (!*++argv)
233 bb_simple_error_msg_and_die("command line is not complete, try \"help\"");
234 return argv;
235 }
236
invarg_1_to_2(const char * arg,const char * opt)237 void FAST_FUNC invarg_1_to_2(const char *arg, const char *opt)
238 {
239 bb_error_msg_and_die(bb_msg_invalid_arg_to, arg, opt);
240 }
241
duparg(const char * key,const char * arg)242 void FAST_FUNC duparg(const char *key, const char *arg)
243 {
244 bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg);
245 }
246
duparg2(const char * key,const char * arg)247 void FAST_FUNC duparg2(const char *key, const char *arg)
248 {
249 bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
250 }
251
inet_addr_match(const inet_prefix * a,const inet_prefix * b,int bits)252 int FAST_FUNC inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
253 {
254 const uint32_t *a1 = a->data;
255 const uint32_t *a2 = b->data;
256 int words = bits >> 5;
257
258 bits &= 0x1f;
259
260 if (words)
261 if (memcmp(a1, a2, words << 2))
262 return -1;
263
264 if (bits) {
265 uint32_t w1, w2;
266 uint32_t mask;
267
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
rt_addr_n2a(int af,void * addr)280 const char* FAST_FUNC rt_addr_n2a(int af, void *addr)
281 {
282 switch (af) {
283 case AF_INET:
284 case AF_INET6:
285 return inet_ntop(af, addr,
286 auto_string(xzalloc(INET6_ADDRSTRLEN)), INET6_ADDRSTRLEN
287 );
288 default:
289 return "???";
290 }
291 }
292
293 #ifdef RESOLVE_HOSTNAMES
format_host(int af,int len,void * addr)294 const char* FAST_FUNC format_host(int af, int len, void *addr)
295 {
296 if (resolve_hosts) {
297 struct hostent *h_ent;
298
299 if (len <= 0) {
300 switch (af) {
301 case AF_INET:
302 len = 4;
303 break;
304 case AF_INET6:
305 len = 16;
306 break;
307 default:;
308 }
309 }
310 if (len > 0) {
311 h_ent = gethostbyaddr(addr, len, af);
312 if (h_ent != NULL) {
313 return auto_string(xstrdup(h_ent->h_name));
314 }
315 }
316 }
317 return rt_addr_n2a(af, addr);
318 }
319 #endif
320