blob: 094a3b0ac4b4c85343a4bb307db4db07c826de16 [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/*
3 * utils.c
4 *
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +00005 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00006 *
7 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
8 *
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00009 * Changes:
10 *
11 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
12 */
13
Rob Landleyecae66a2006-06-02 20:53:38 +000014#include "libbb.h"
15
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000016#include <string.h>
Eric Andersen496411b2003-02-12 10:56:53 +000017#include <unistd.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000018
19#include "utils.h"
"Vladimir N. Oleynik"007a0112005-09-22 11:11:11 +000020#include "inet_common.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000021
22int get_integer(int *val, char *arg, int base)
23{
24 long res;
25 char *ptr;
26
27 if (!arg || !*arg)
28 return -1;
29 res = strtol(arg, &ptr, base);
30 if (!ptr || ptr == arg || *ptr || res > INT_MAX || res < INT_MIN)
31 return -1;
32 *val = res;
33 return 0;
34}
35
36int get_unsigned(unsigned *val, char *arg, int base)
37{
38 unsigned long res;
39 char *ptr;
40
41 if (!arg || !*arg)
42 return -1;
43 res = strtoul(arg, &ptr, base);
44 if (!ptr || ptr == arg || *ptr || res > UINT_MAX)
45 return -1;
46 *val = res;
47 return 0;
48}
49
Glenn L McGrath50c00f42002-11-18 07:26:42 +000050int get_u32(__u32 * val, char *arg, int base)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000051{
52 unsigned long res;
53 char *ptr;
54
55 if (!arg || !*arg)
56 return -1;
57 res = strtoul(arg, &ptr, base);
58 if (!ptr || ptr == arg || *ptr || res > 0xFFFFFFFFUL)
59 return -1;
60 *val = res;
61 return 0;
62}
63
Glenn L McGrath50c00f42002-11-18 07:26:42 +000064int get_u16(__u16 * val, char *arg, int base)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000065{
66 unsigned long res;
67 char *ptr;
68
69 if (!arg || !*arg)
70 return -1;
71 res = strtoul(arg, &ptr, base);
72 if (!ptr || ptr == arg || *ptr || res > 0xFFFF)
73 return -1;
74 *val = res;
75 return 0;
76}
77
Glenn L McGrath50c00f42002-11-18 07:26:42 +000078int get_u8(__u8 * val, char *arg, int base)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000079{
80 unsigned long res;
81 char *ptr;
82
83 if (!arg || !*arg)
84 return -1;
85 res = strtoul(arg, &ptr, base);
86 if (!ptr || ptr == arg || *ptr || res > 0xFF)
87 return -1;
88 *val = res;
89 return 0;
90}
91
Glenn L McGrath50c00f42002-11-18 07:26:42 +000092int get_s16(__s16 * val, char *arg, int base)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000093{
94 long res;
95 char *ptr;
96
97 if (!arg || !*arg)
98 return -1;
99 res = strtol(arg, &ptr, base);
100 if (!ptr || ptr == arg || *ptr || res > 0x7FFF || res < -0x8000)
101 return -1;
102 *val = res;
103 return 0;
104}
105
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000106int get_s8(__s8 * val, char *arg, int base)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000107{
108 long res;
109 char *ptr;
110
111 if (!arg || !*arg)
112 return -1;
113 res = strtol(arg, &ptr, base);
114 if (!ptr || ptr == arg || *ptr || res > 0x7F || res < -0x80)
115 return -1;
116 *val = res;
117 return 0;
118}
119
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000120int get_addr_1(inet_prefix * addr, char *name, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000121{
122 char *cp;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000123 unsigned char *ap = (unsigned char *) addr->data;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000124 int i;
125
126 memset(addr, 0, sizeof(*addr));
127
"Vladimir N. Oleynik"007a0112005-09-22 11:11:11 +0000128 if (strcmp(name, bb_INET_default) == 0 ||
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000129 strcmp(name, "all") == 0 || strcmp(name, "any") == 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000130 addr->family = family;
131 addr->bytelen = (family == AF_INET6 ? 16 : 4);
132 addr->bitlen = -1;
133 return 0;
134 }
135
136 if (strchr(name, ':')) {
137 addr->family = AF_INET6;
138 if (family != AF_UNSPEC && family != AF_INET6)
139 return -1;
140 if (inet_pton(AF_INET6, name, addr->data) <= 0)
141 return -1;
142 addr->bytelen = 16;
143 addr->bitlen = -1;
144 return 0;
145 }
146
147 addr->family = AF_INET;
148 if (family != AF_UNSPEC && family != AF_INET)
149 return -1;
150 addr->bytelen = 4;
151 addr->bitlen = -1;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000152 for (cp = name, i = 0; *cp; cp++) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000153 if (*cp <= '9' && *cp >= '0') {
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000154 ap[i] = 10 * ap[i] + (*cp - '0');
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000155 continue;
156 }
157 if (*cp == '.' && ++i <= 3)
158 continue;
159 return -1;
160 }
161 return 0;
162}
163
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000164int get_prefix_1(inet_prefix * dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000165{
166 int err;
Eric Andersend78aea82006-01-30 18:00:02 +0000167 int plen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000168 char *slash;
169
170 memset(dst, 0, sizeof(*dst));
171
"Vladimir N. Oleynik"007a0112005-09-22 11:11:11 +0000172 if (strcmp(arg, bb_INET_default) == 0 || strcmp(arg, "any") == 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000173 dst->family = family;
174 dst->bytelen = 0;
175 dst->bitlen = 0;
176 return 0;
177 }
178
179 slash = strchr(arg, '/');
180 if (slash)
181 *slash = 0;
182 err = get_addr_1(dst, arg, family);
183 if (err == 0) {
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000184 switch (dst->family) {
185 case AF_INET6:
186 dst->bitlen = 128;
187 break;
188 default:
189 case AF_INET:
190 dst->bitlen = 32;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000191 }
192 if (slash) {
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000193 if (get_integer(&plen, slash + 1, 0) || plen > dst->bitlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000194 err = -1;
195 goto done;
196 }
197 dst->bitlen = plen;
198 }
199 }
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000200 done:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000201 if (slash)
202 *slash = '/';
203 return err;
204}
205
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000206int get_addr(inet_prefix * dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000207{
208 if (family == AF_PACKET) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000209 bb_error_msg_and_die("\"%s\" may be inet address, but it is not allowed in this context.", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000210 }
211 if (get_addr_1(dst, arg, family)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000212 bb_error_msg_and_die("an inet address is expected rather than \"%s\".", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000213 }
214 return 0;
215}
216
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000217int get_prefix(inet_prefix * dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000218{
219 if (family == AF_PACKET) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000220 bb_error_msg_and_die("\"%s\" may be inet address, but it is not allowed in this context.", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000221 }
222 if (get_prefix_1(dst, arg, family)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000223 bb_error_msg_and_die("an inet address is expected rather than \"%s\".", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000224 }
225 return 0;
226}
227
228__u32 get_addr32(char *name)
229{
230 inet_prefix addr;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000231
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000232 if (get_addr_1(&addr, name, AF_INET)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000233 bb_error_msg_and_die("an IP address is expected rather than \"%s\"", name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000234 }
235 return addr.data[0];
236}
237
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000238void incomplete_command(void)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000239{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000240 bb_error_msg("Command line is not complete. Try option \"help\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000241 exit(-1);
242}
243
Bernhard Reutner-Fischerf1bd42a2006-06-14 16:51:50 +0000244void invarg(const char * const arg, const char * const opt)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000245{
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000246 bb_error_msg(bb_msg_invalid_arg, arg, opt);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000247 exit(-1);
248}
249
250void duparg(char *key, char *arg)
251{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000252 bb_error_msg("duplicate \"%s\": \"%s\" is the second value.", key, arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000253 exit(-1);
254}
255
256void duparg2(char *key, char *arg)
257{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000258 bb_error_msg("either \"%s\" is duplicate, or \"%s\" is a garbage.", key, arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000259 exit(-1);
260}
261
262int matches(char *cmd, char *pattern)
263{
264 int len = strlen(cmd);
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000265
Glenn L McGrath82d2cd42002-11-28 12:45:48 +0000266 if (len > strlen(pattern)) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000267 return -1;
Glenn L McGrath82d2cd42002-11-28 12:45:48 +0000268 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000269 return memcmp(pattern, cmd, len);
270}
271
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000272int inet_addr_match(inet_prefix * a, inet_prefix * b, int bits)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000273{
274 __u32 *a1 = a->data;
275 __u32 *a2 = b->data;
276 int words = bits >> 0x05;
277
278 bits &= 0x1f;
279
280 if (words)
281 if (memcmp(a1, a2, words << 2))
282 return -1;
283
284 if (bits) {
285 __u32 w1, w2;
286 __u32 mask;
287
288 w1 = a1[words];
289 w2 = a2[words];
290
291 mask = htonl((0xffffffff) << (0x20 - bits));
292
293 if ((w1 ^ w2) & mask)
294 return 1;
295 }
296
297 return 0;
298}
299
300int __iproute2_hz_internal;
301
302int __get_hz(void)
303{
304 int hz = 0;
305 FILE *fp = fopen("/proc/net/psched", "r");
306
307 if (fp) {
308 unsigned nom, denom;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000309
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000310 if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
311 if (nom == 1000000)
312 hz = denom;
313 fclose(fp);
314 }
315 if (hz)
316 return hz;
Eric Andersen496411b2003-02-12 10:56:53 +0000317 return sysconf(_SC_CLK_TCK);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000318}
319
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +0000320const char *rt_addr_n2a(int af, int ATTRIBUTE_UNUSED len,
321 void *addr, char *buf, int buflen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000322{
323 switch (af) {
324 case AF_INET:
325 case AF_INET6:
326 return inet_ntop(af, addr, buf, buflen);
327 default:
328 return "???";
329 }
330}
331
332
333const char *format_host(int af, int len, void *addr, char *buf, int buflen)
334{
335#ifdef RESOLVE_HOSTNAMES
336 if (resolve_hosts) {
337 struct hostent *h_ent;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000338
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000339 if (len <= 0) {
340 switch (af) {
341 case AF_INET:
342 len = 4;
343 break;
344 case AF_INET6:
345 len = 16;
346 break;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000347 default:;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000348 }
349 }
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000350 if (len > 0 && (h_ent = gethostbyaddr(addr, len, af)) != NULL) {
351 snprintf(buf, buflen - 1, "%s", h_ent->h_name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000352 return buf;
353 }
354 }
355#endif
356 return rt_addr_n2a(af, len, addr, buf, buflen);
357}