]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
2 | * utils.c | |
3 | * | |
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. | |
8 | * | |
9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
10 | * | |
aba5acdf SH |
11 | */ |
12 | ||
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
697ac639 | 15 | #include <math.h> |
aba5acdf SH |
16 | #include <unistd.h> |
17 | #include <syslog.h> | |
18 | #include <fcntl.h> | |
5bd9dd49 | 19 | #include <limits.h> |
aba5acdf SH |
20 | #include <sys/socket.h> |
21 | #include <netinet/in.h> | |
22 | #include <string.h> | |
23 | #include <netdb.h> | |
24 | #include <arpa/inet.h> | |
5dfe5561 | 25 | #include <asm/types.h> |
aba5acdf | 26 | #include <linux/pkt_sched.h> |
5bd9dd49 | 27 | #include <linux/param.h> |
0b218ab1 | 28 | #include <linux/if_arp.h> |
dacc5d41 | 29 | #include <linux/mpls.h> |
90f93024 SH |
30 | #include <time.h> |
31 | #include <sys/time.h> | |
30d10db5 | 32 | #include <errno.h> |
90f93024 | 33 | |
0b218ab1 | 34 | #include "rt_names.h" |
aba5acdf | 35 | #include "utils.h" |
e998e118 | 36 | #include "namespace.h" |
aba5acdf | 37 | |
79aa79d0 VK |
38 | int timestamp_short = 0; |
39 | ||
609640f5 SD |
40 | int get_hex(char c) |
41 | { | |
42 | if (c >= 'A' && c <= 'F') | |
43 | return c - 'A' + 10; | |
44 | if (c >= 'a' && c <= 'f') | |
45 | return c - 'a' + 10; | |
46 | if (c >= '0' && c <= '9') | |
47 | return c - '0'; | |
48 | ||
49 | return -1; | |
50 | } | |
51 | ||
4094db72 | 52 | int get_integer(int *val, const char *arg, int base) |
aba5acdf SH |
53 | { |
54 | long res; | |
55 | char *ptr; | |
56 | ||
57 | if (!arg || !*arg) | |
58 | return -1; | |
697ac639 | 59 | |
aba5acdf | 60 | res = strtol(arg, &ptr, base); |
697ac639 SH |
61 | |
62 | /* If there were no digits at all, strtol() stores | |
63 | * the original value of nptr in *endptr (and returns 0). | |
64 | * In particular, if *nptr is not '\0' but **endptr is '\0' on return, | |
65 | * the entire string is valid. | |
66 | */ | |
67 | if (!ptr || ptr == arg || *ptr) | |
68 | return -1; | |
69 | ||
70 | /* If an underflow occurs, strtol() returns LONG_MIN. | |
71 | * If an overflow occurs, strtol() returns LONG_MAX. | |
72 | * In both cases, errno is set to ERANGE. | |
73 | */ | |
74 | if ((res == LONG_MAX || res == LONG_MIN) && errno == ERANGE) | |
aba5acdf | 75 | return -1; |
697ac639 SH |
76 | |
77 | /* Outside range of int */ | |
78 | if (res < INT_MIN || res > INT_MAX) | |
79 | return -1; | |
80 | ||
aba5acdf SH |
81 | *val = res; |
82 | return 0; | |
83 | } | |
84 | ||
6b1ac654 | 85 | int mask2bits(__u32 netmask) |
4b270b17 SH |
86 | { |
87 | unsigned bits = 0; | |
6b1ac654 SH |
88 | __u32 mask = ntohl(netmask); |
89 | __u32 host = ~mask; | |
4b270b17 | 90 | |
6b1ac654 SH |
91 | /* a valid netmask must be 2^n - 1 */ |
92 | if ((host & (host + 1)) != 0) | |
93 | return -1; | |
4b270b17 | 94 | |
6b1ac654 SH |
95 | for (; mask; mask <<= 1) |
96 | ++bits; | |
4b270b17 SH |
97 | return bits; |
98 | } | |
99 | ||
5a0d1cbf AH |
100 | static int get_netmask(unsigned *val, const char *arg, int base) |
101 | { | |
102 | inet_prefix addr; | |
103 | ||
104 | if (!get_unsigned(val, arg, base)) | |
105 | return 0; | |
106 | ||
107 | /* try coverting dotted quad to CIDR */ | |
6b1ac654 SH |
108 | if (!get_addr_1(&addr, arg, AF_INET) && addr.family == AF_INET) { |
109 | int b = mask2bits(addr.data[0]); | |
e9e9365b | 110 | |
6b1ac654 SH |
111 | if (b >= 0) { |
112 | *val = b; | |
5a0d1cbf | 113 | return 0; |
6b1ac654 | 114 | } |
5a0d1cbf AH |
115 | } |
116 | ||
117 | return -1; | |
118 | } | |
119 | ||
4094db72 | 120 | int get_unsigned(unsigned *val, const char *arg, int base) |
aba5acdf SH |
121 | { |
122 | unsigned long res; | |
123 | char *ptr; | |
124 | ||
125 | if (!arg || !*arg) | |
126 | return -1; | |
697ac639 | 127 | |
aba5acdf | 128 | res = strtoul(arg, &ptr, base); |
697ac639 SH |
129 | |
130 | /* empty string or trailing non-digits */ | |
131 | if (!ptr || ptr == arg || *ptr) | |
132 | return -1; | |
133 | ||
134 | /* overflow */ | |
135 | if (res == ULONG_MAX && errno == ERANGE) | |
aba5acdf | 136 | return -1; |
697ac639 SH |
137 | |
138 | /* out side range of unsigned */ | |
139 | if (res > UINT_MAX) | |
140 | return -1; | |
141 | ||
aba5acdf SH |
142 | *val = res; |
143 | return 0; | |
144 | } | |
145 | ||
54e0b2e7 | 146 | /* |
db6b0cfa GR |
147 | * get_time_rtt is "translated" from a similar routine "get_time" in |
148 | * tc_util.c. We don't use the exact same routine because tc passes | |
149 | * microseconds to the kernel and the callers of get_time_rtt want to | |
150 | * pass milliseconds (standard unit for rtt values since 2.6.27), and | |
151 | * have a different assumption for the units of a "raw" number. | |
54e0b2e7 | 152 | */ |
db6b0cfa | 153 | int get_time_rtt(unsigned *val, const char *arg, int *raw) |
54e0b2e7 RJ |
154 | { |
155 | double t; | |
156 | unsigned long res; | |
157 | char *p; | |
158 | ||
697ac639 SH |
159 | if (strchr(arg, '.') != NULL) { |
160 | t = strtod(arg, &p); | |
54e0b2e7 RJ |
161 | if (t < 0.0) |
162 | return -1; | |
697ac639 | 163 | |
4d9a264f FW |
164 | /* no digits? */ |
165 | if (!p || p == arg) | |
697ac639 SH |
166 | return -1; |
167 | ||
168 | /* over/underflow */ | |
169 | if ((t == HUGE_VALF || t == HUGE_VALL) && errno == ERANGE) | |
170 | return -1; | |
171 | } else { | |
897fb84f | 172 | res = strtoul(arg, &p, 0); |
697ac639 | 173 | |
4d9a264f FW |
174 | /* empty string? */ |
175 | if (!p || p == arg) | |
697ac639 SH |
176 | return -1; |
177 | ||
178 | /* overflow */ | |
179 | if (res == ULONG_MAX && errno == ERANGE) | |
54e0b2e7 | 180 | return -1; |
697ac639 | 181 | |
54e0b2e7 RJ |
182 | t = (double)res; |
183 | } | |
697ac639 | 184 | |
54e0b2e7 RJ |
185 | if (p == arg) |
186 | return -1; | |
54e0b2e7 RJ |
187 | *raw = 1; |
188 | ||
189 | if (*p) { | |
190 | *raw = 0; | |
191 | if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 || | |
192 | strcasecmp(p, "secs")==0) | |
fca1dae8 | 193 | t *= 1000; |
54e0b2e7 RJ |
194 | else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 || |
195 | strcasecmp(p, "msecs") == 0) | |
54e0b2e7 RJ |
196 | t *= 1.0; /* allow suffix, do nothing */ |
197 | else | |
198 | return -1; | |
199 | } | |
200 | ||
201 | /* emulate ceil() without having to bring-in -lm and always be >= 1 */ | |
202 | ||
db6b0cfa GR |
203 | *val = t; |
204 | if (*val < t) | |
205 | *val += 1; | |
e9e9365b | 206 | |
54e0b2e7 RJ |
207 | return 0; |
208 | ||
209 | } | |
210 | ||
c7699875 | 211 | int get_u64(__u64 *val, const char *arg, int base) |
212 | { | |
213 | unsigned long long res; | |
214 | char *ptr; | |
215 | ||
216 | if (!arg || !*arg) | |
217 | return -1; | |
697ac639 | 218 | |
c7699875 | 219 | res = strtoull(arg, &ptr, base); |
697ac639 SH |
220 | |
221 | /* empty string or trailing non-digits */ | |
222 | if (!ptr || ptr == arg || *ptr) | |
223 | return -1; | |
224 | ||
225 | /* overflow */ | |
226 | if (res == ULLONG_MAX && errno == ERANGE) | |
227 | return -1; | |
228 | ||
229 | /* in case ULL is 128 bits */ | |
230 | if (res > 0xFFFFFFFFFFFFFFFFULL) | |
231 | return -1; | |
232 | ||
c7699875 | 233 | *val = res; |
234 | return 0; | |
235 | } | |
236 | ||
4094db72 | 237 | int get_u32(__u32 *val, const char *arg, int base) |
aba5acdf SH |
238 | { |
239 | unsigned long res; | |
240 | char *ptr; | |
241 | ||
242 | if (!arg || !*arg) | |
243 | return -1; | |
244 | res = strtoul(arg, &ptr, base); | |
697ac639 SH |
245 | |
246 | /* empty string or trailing non-digits */ | |
247 | if (!ptr || ptr == arg || *ptr) | |
248 | return -1; | |
249 | ||
250 | /* overflow */ | |
251 | if (res == ULONG_MAX && errno == ERANGE) | |
252 | return -1; | |
253 | ||
254 | /* in case UL > 32 bits */ | |
255 | if (res > 0xFFFFFFFFUL) | |
aba5acdf | 256 | return -1; |
697ac639 | 257 | |
aba5acdf SH |
258 | *val = res; |
259 | return 0; | |
260 | } | |
261 | ||
4094db72 | 262 | int get_u16(__u16 *val, const char *arg, int base) |
aba5acdf SH |
263 | { |
264 | unsigned long res; | |
265 | char *ptr; | |
266 | ||
267 | if (!arg || !*arg) | |
268 | return -1; | |
269 | res = strtoul(arg, &ptr, base); | |
697ac639 SH |
270 | |
271 | /* empty string or trailing non-digits */ | |
272 | if (!ptr || ptr == arg || *ptr) | |
273 | return -1; | |
274 | ||
275 | /* overflow */ | |
276 | if (res == ULONG_MAX && errno == ERANGE) | |
aba5acdf | 277 | return -1; |
697ac639 SH |
278 | |
279 | if (res > 0xFFFFUL) | |
280 | return -1; | |
281 | ||
aba5acdf SH |
282 | *val = res; |
283 | return 0; | |
284 | } | |
285 | ||
4094db72 | 286 | int get_u8(__u8 *val, const char *arg, int base) |
aba5acdf SH |
287 | { |
288 | unsigned long res; | |
289 | char *ptr; | |
290 | ||
291 | if (!arg || !*arg) | |
292 | return -1; | |
697ac639 | 293 | |
aba5acdf | 294 | res = strtoul(arg, &ptr, base); |
697ac639 SH |
295 | /* empty string or trailing non-digits */ |
296 | if (!ptr || ptr == arg || *ptr) | |
297 | return -1; | |
298 | ||
299 | /* overflow */ | |
300 | if (res == ULONG_MAX && errno == ERANGE) | |
aba5acdf | 301 | return -1; |
697ac639 SH |
302 | |
303 | if (res > 0xFFUL) | |
304 | return -1; | |
305 | ||
aba5acdf SH |
306 | *val = res; |
307 | return 0; | |
308 | } | |
309 | ||
30d10db5 HPP |
310 | int get_s32(__s32 *val, const char *arg, int base) |
311 | { | |
312 | long res; | |
313 | char *ptr; | |
314 | ||
315 | errno = 0; | |
316 | ||
317 | if (!arg || !*arg) | |
318 | return -1; | |
319 | res = strtol(arg, &ptr, base); | |
697ac639 SH |
320 | if (!ptr || ptr == arg || *ptr) |
321 | return -1; | |
322 | if ((res == LONG_MIN || res == LONG_MAX) && errno == ERANGE) | |
30d10db5 | 323 | return -1; |
697ac639 SH |
324 | if (res > INT32_MAX || res < INT32_MIN) |
325 | return -1; | |
326 | ||
30d10db5 HPP |
327 | *val = res; |
328 | return 0; | |
329 | } | |
330 | ||
4094db72 | 331 | int get_s16(__s16 *val, const char *arg, int base) |
aba5acdf SH |
332 | { |
333 | long res; | |
334 | char *ptr; | |
335 | ||
336 | if (!arg || !*arg) | |
337 | return -1; | |
338 | res = strtol(arg, &ptr, base); | |
697ac639 SH |
339 | if (!ptr || ptr == arg || *ptr) |
340 | return -1; | |
341 | if ((res == LONG_MIN || res == LONG_MAX) && errno == ERANGE) | |
342 | return -1; | |
343 | if (res > 0x7FFF || res < -0x8000) | |
aba5acdf | 344 | return -1; |
697ac639 | 345 | |
aba5acdf SH |
346 | *val = res; |
347 | return 0; | |
348 | } | |
349 | ||
4094db72 | 350 | int get_s8(__s8 *val, const char *arg, int base) |
aba5acdf SH |
351 | { |
352 | long res; | |
353 | char *ptr; | |
354 | ||
355 | if (!arg || !*arg) | |
356 | return -1; | |
357 | res = strtol(arg, &ptr, base); | |
697ac639 SH |
358 | if (!ptr || ptr == arg || *ptr) |
359 | return -1; | |
360 | if ((res == LONG_MIN || res == LONG_MAX) && errno == ERANGE) | |
361 | return -1; | |
362 | if (res > 0x7F || res < -0x80) | |
aba5acdf SH |
363 | return -1; |
364 | *val = res; | |
365 | return 0; | |
366 | } | |
367 | ||
9f7401fa SD |
368 | int get_be64(__be64 *val, const char *arg, int base) |
369 | { | |
370 | __u64 v; | |
371 | int ret = get_u64(&v, arg, base); | |
372 | ||
373 | if (!ret) | |
374 | *val = htonll(v); | |
375 | ||
376 | return ret; | |
377 | } | |
378 | ||
379 | int get_be32(__be32 *val, const char *arg, int base) | |
380 | { | |
381 | __u32 v; | |
382 | int ret = get_u32(&v, arg, base); | |
383 | ||
384 | if (!ret) | |
385 | *val = htonl(v); | |
386 | ||
387 | return ret; | |
388 | } | |
389 | ||
390 | int get_be16(__be16 *val, const char *arg, int base) | |
391 | { | |
392 | __u16 v; | |
393 | int ret = get_u16(&v, arg, base); | |
394 | ||
395 | if (!ret) | |
396 | *val = htons(v); | |
397 | ||
398 | return ret; | |
399 | } | |
400 | ||
cafa6c8e SH |
401 | /* This uses a non-standard parsing (ie not inet_aton, or inet_pton) |
402 | * because of legacy choice to parse 10.8 as 10.8.0.0 not 10.0.0.8 | |
403 | */ | |
404 | static int get_addr_ipv4(__u8 *ap, const char *cp) | |
aba5acdf | 405 | { |
cafa6c8e SH |
406 | int i; |
407 | ||
408 | for (i = 0; i < 4; i++) { | |
409 | unsigned long n; | |
410 | char *endp; | |
e9e9365b | 411 | |
cafa6c8e SH |
412 | n = strtoul(cp, &endp, 0); |
413 | if (n > 255) | |
414 | return -1; /* bogus network value */ | |
415 | ||
416 | if (endp == cp) /* no digits */ | |
417 | return -1; | |
94afda75 | 418 | |
cafa6c8e SH |
419 | ap[i] = n; |
420 | ||
421 | if (*endp == '\0') | |
422 | break; | |
423 | ||
424 | if (i == 3 || *endp != '.') | |
425 | return -1; /* extra characters */ | |
426 | cp = endp + 1; | |
427 | } | |
428 | ||
429 | return 1; | |
430 | } | |
431 | ||
5866bddd TH |
432 | int get_addr64(__u64 *ap, const char *cp) |
433 | { | |
434 | int i; | |
435 | ||
436 | union { | |
437 | __u16 v16[4]; | |
438 | __u64 v64; | |
439 | } val; | |
440 | ||
441 | for (i = 0; i < 4; i++) { | |
442 | unsigned long n; | |
443 | char *endp; | |
444 | ||
445 | n = strtoul(cp, &endp, 16); | |
446 | if (n > 0xffff) | |
447 | return -1; /* bogus network value */ | |
448 | ||
449 | if (endp == cp) /* no digits */ | |
450 | return -1; | |
451 | ||
452 | val.v16[i] = htons(n); | |
453 | ||
454 | if (*endp == '\0') | |
455 | break; | |
456 | ||
457 | if (i == 3 || *endp != ':') | |
458 | return -1; /* extra characters */ | |
459 | cp = endp + 1; | |
460 | } | |
461 | ||
462 | *ap = val.v64; | |
463 | ||
464 | return 1; | |
465 | } | |
466 | ||
cafa6c8e SH |
467 | int get_addr_1(inet_prefix *addr, const char *name, int family) |
468 | { | |
aba5acdf SH |
469 | memset(addr, 0, sizeof(*addr)); |
470 | ||
471 | if (strcmp(name, "default") == 0 || | |
472 | strcmp(name, "all") == 0 || | |
473 | strcmp(name, "any") == 0) { | |
dacc5d41 | 474 | if ((family == AF_DECnet) || (family == AF_MPLS)) |
aba5acdf SH |
475 | return -1; |
476 | addr->family = family; | |
477 | addr->bytelen = (family == AF_INET6 ? 16 : 4); | |
478 | addr->bitlen = -1; | |
479 | return 0; | |
480 | } | |
481 | ||
0b218ab1 EB |
482 | if (family == AF_PACKET) { |
483 | int len; | |
484 | len = ll_addr_a2n((char *)&addr->data, sizeof(addr->data), name); | |
485 | if (len < 0) | |
486 | return -1; | |
487 | ||
488 | addr->family = AF_PACKET; | |
489 | addr->bytelen = len; | |
490 | addr->bitlen = len * 8; | |
491 | return 0; | |
492 | } | |
493 | ||
aba5acdf SH |
494 | if (strchr(name, ':')) { |
495 | addr->family = AF_INET6; | |
496 | if (family != AF_UNSPEC && family != AF_INET6) | |
497 | return -1; | |
498 | if (inet_pton(AF_INET6, name, addr->data) <= 0) | |
499 | return -1; | |
500 | addr->bytelen = 16; | |
501 | addr->bitlen = -1; | |
502 | return 0; | |
503 | } | |
504 | ||
505 | if (family == AF_DECnet) { | |
506 | struct dn_naddr dna; | |
507 | addr->family = AF_DECnet; | |
508 | if (dnet_pton(AF_DECnet, name, &dna) <= 0) | |
509 | return -1; | |
510 | memcpy(addr->data, dna.a_addr, 2); | |
511 | addr->bytelen = 2; | |
512 | addr->bitlen = -1; | |
513 | return 0; | |
514 | } | |
515 | ||
dacc5d41 EB |
516 | if (family == AF_MPLS) { |
517 | int i; | |
518 | addr->family = AF_MPLS; | |
519 | if (mpls_pton(AF_MPLS, name, addr->data) <= 0) | |
520 | return -1; | |
521 | addr->bytelen = 4; | |
522 | addr->bitlen = 20; | |
523 | /* How many bytes do I need? */ | |
524 | for (i = 0; i < 8; i++) { | |
525 | if (ntohl(addr->data[i]) & MPLS_LS_S_MASK) { | |
526 | addr->bytelen = (i + 1)*4; | |
527 | break; | |
528 | } | |
529 | } | |
530 | return 0; | |
531 | } | |
532 | ||
aba5acdf SH |
533 | addr->family = AF_INET; |
534 | if (family != AF_UNSPEC && family != AF_INET) | |
535 | return -1; | |
94afda75 | 536 | |
cafa6c8e | 537 | if (get_addr_ipv4((__u8 *)addr->data, name) <= 0) |
516ffb6b | 538 | return -1; |
94afda75 | 539 | |
aba5acdf SH |
540 | addr->bytelen = 4; |
541 | addr->bitlen = -1; | |
aba5acdf SH |
542 | return 0; |
543 | } | |
544 | ||
f3a2ddc1 VK |
545 | int af_bit_len(int af) |
546 | { | |
547 | switch (af) { | |
548 | case AF_INET6: | |
549 | return 128; | |
550 | case AF_INET: | |
551 | return 32; | |
552 | case AF_DECnet: | |
553 | return 16; | |
554 | case AF_IPX: | |
555 | return 80; | |
dacc5d41 EB |
556 | case AF_MPLS: |
557 | return 20; | |
f3a2ddc1 VK |
558 | } |
559 | ||
560 | return 0; | |
561 | } | |
562 | ||
563 | int af_byte_len(int af) | |
564 | { | |
565 | return af_bit_len(af) / 8; | |
566 | } | |
567 | ||
fb9b1d0f | 568 | int get_prefix_1(inet_prefix *dst, char *arg, int family) |
aba5acdf SH |
569 | { |
570 | int err; | |
571 | unsigned plen; | |
572 | char *slash; | |
573 | ||
574 | memset(dst, 0, sizeof(*dst)); | |
575 | ||
576 | if (strcmp(arg, "default") == 0 || | |
577 | strcmp(arg, "any") == 0 || | |
578 | strcmp(arg, "all") == 0) { | |
f7dd7e5e | 579 | if ((family == AF_DECnet) || (family == AF_MPLS)) |
aba5acdf SH |
580 | return -1; |
581 | dst->family = family; | |
582 | dst->bytelen = 0; | |
583 | dst->bitlen = 0; | |
584 | return 0; | |
585 | } | |
586 | ||
587 | slash = strchr(arg, '/'); | |
588 | if (slash) | |
589 | *slash = 0; | |
fb9b1d0f | 590 | |
aba5acdf SH |
591 | err = get_addr_1(dst, arg, family); |
592 | if (err == 0) { | |
106ca277 | 593 | dst->bitlen = af_bit_len(dst->family); |
f3a2ddc1 | 594 | |
aba5acdf | 595 | if (slash) { |
5a0d1cbf | 596 | if (get_netmask(&plen, slash+1, 0) |
656111b2 | 597 | || plen > dst->bitlen) { |
aba5acdf SH |
598 | err = -1; |
599 | goto done; | |
600 | } | |
f082b64f | 601 | dst->flags |= PREFIXLEN_SPECIFIED; |
aba5acdf SH |
602 | dst->bitlen = plen; |
603 | } | |
604 | } | |
605 | done: | |
606 | if (slash) | |
607 | *slash = '/'; | |
608 | return err; | |
609 | } | |
610 | ||
c7699875 | 611 | int get_addr(inet_prefix *dst, const char *arg, int family) |
aba5acdf | 612 | { |
aba5acdf | 613 | if (get_addr_1(dst, arg, family)) { |
b5bb1820 | 614 | fprintf(stderr, "Error: %s address is expected rather than \"%s\".\n", |
334af761 | 615 | family_name(dst->family) ,arg); |
aba5acdf SH |
616 | exit(1); |
617 | } | |
618 | return 0; | |
619 | } | |
620 | ||
fb9b1d0f | 621 | int get_prefix(inet_prefix *dst, char *arg, int family) |
aba5acdf SH |
622 | { |
623 | if (family == AF_PACKET) { | |
624 | fprintf(stderr, "Error: \"%s\" may be inet prefix, but it is not allowed in this context.\n", arg); | |
625 | exit(1); | |
626 | } | |
334af761 | 627 | |
aba5acdf | 628 | if (get_prefix_1(dst, arg, family)) { |
b5bb1820 | 629 | fprintf(stderr, "Error: %s prefix is expected rather than \"%s\".\n", |
334af761 | 630 | family_name(dst->family) ,arg); |
aba5acdf SH |
631 | exit(1); |
632 | } | |
633 | return 0; | |
634 | } | |
635 | ||
c7699875 | 636 | __u32 get_addr32(const char *name) |
aba5acdf SH |
637 | { |
638 | inet_prefix addr; | |
639 | if (get_addr_1(&addr, name, AF_INET)) { | |
640 | fprintf(stderr, "Error: an IP address is expected rather than \"%s\"\n", name); | |
641 | exit(1); | |
642 | } | |
643 | return addr.data[0]; | |
644 | } | |
645 | ||
4094db72 | 646 | void incomplete_command(void) |
aba5acdf SH |
647 | { |
648 | fprintf(stderr, "Command line is not complete. Try option \"help\"\n"); | |
649 | exit(-1); | |
650 | } | |
651 | ||
c7699875 | 652 | void missarg(const char *key) |
653 | { | |
654 | fprintf(stderr, "Error: argument \"%s\" is required\n", key); | |
655 | exit(-1); | |
656 | } | |
657 | ||
4094db72 | 658 | void invarg(const char *msg, const char *arg) |
aba5acdf SH |
659 | { |
660 | fprintf(stderr, "Error: argument \"%s\" is wrong: %s\n", arg, msg); | |
661 | exit(-1); | |
662 | } | |
663 | ||
4094db72 | 664 | void duparg(const char *key, const char *arg) |
aba5acdf SH |
665 | { |
666 | fprintf(stderr, "Error: duplicate \"%s\": \"%s\" is the second value.\n", key, arg); | |
667 | exit(-1); | |
668 | } | |
669 | ||
4094db72 | 670 | void duparg2(const char *key, const char *arg) |
aba5acdf SH |
671 | { |
672 | fprintf(stderr, "Error: either \"%s\" is duplicate, or \"%s\" is a garbage.\n", key, arg); | |
673 | exit(-1); | |
674 | } | |
675 | ||
4094db72 | 676 | int matches(const char *cmd, const char *pattern) |
aba5acdf SH |
677 | { |
678 | int len = strlen(cmd); | |
679 | if (len > strlen(pattern)) | |
680 | return -1; | |
681 | return memcmp(pattern, cmd, len); | |
682 | } | |
683 | ||
4094db72 | 684 | int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits) |
aba5acdf | 685 | { |
9626dfd5 SH |
686 | const __u32 *a1 = a->data; |
687 | const __u32 *a2 = b->data; | |
aba5acdf SH |
688 | int words = bits >> 0x05; |
689 | ||
690 | bits &= 0x1f; | |
691 | ||
692 | if (words) | |
693 | if (memcmp(a1, a2, words << 2)) | |
694 | return -1; | |
695 | ||
696 | if (bits) { | |
697 | __u32 w1, w2; | |
698 | __u32 mask; | |
699 | ||
700 | w1 = a1[words]; | |
701 | w2 = a2[words]; | |
702 | ||
703 | mask = htonl((0xffffffff) << (0x20 - bits)); | |
704 | ||
705 | if ((w1 ^ w2) & mask) | |
706 | return 1; | |
707 | } | |
708 | ||
709 | return 0; | |
710 | } | |
711 | ||
712 | int __iproute2_hz_internal; | |
713 | ||
714 | int __get_hz(void) | |
715 | { | |
716 | char name[1024]; | |
717 | int hz = 0; | |
718 | FILE *fp; | |
719 | ||
720 | if (getenv("HZ")) | |
721 | return atoi(getenv("HZ")) ? : HZ; | |
722 | ||
723 | if (getenv("PROC_NET_PSCHED")) { | |
724 | snprintf(name, sizeof(name)-1, "%s", getenv("PROC_NET_PSCHED")); | |
c7699875 | 725 | } else if (getenv("PROC_ROOT")) { |
aba5acdf SH |
726 | snprintf(name, sizeof(name)-1, "%s/net/psched", getenv("PROC_ROOT")); |
727 | } else { | |
728 | strcpy(name, "/proc/net/psched"); | |
729 | } | |
730 | fp = fopen(name, "r"); | |
731 | ||
732 | if (fp) { | |
733 | unsigned nom, denom; | |
734 | if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2) | |
735 | if (nom == 1000000) | |
736 | hz = denom; | |
737 | fclose(fp); | |
738 | } | |
739 | if (hz) | |
740 | return hz; | |
741 | return HZ; | |
742 | } | |
743 | ||
5e8bc631 | 744 | int __iproute2_user_hz_internal; |
745 | ||
746 | int __get_user_hz(void) | |
747 | { | |
748 | return sysconf(_SC_CLK_TCK); | |
749 | } | |
750 | ||
2e96d2cc | 751 | const char *rt_addr_n2a_r(int af, int len, const void *addr, char *buf, int buflen) |
aba5acdf SH |
752 | { |
753 | switch (af) { | |
754 | case AF_INET: | |
755 | case AF_INET6: | |
756 | return inet_ntop(af, addr, buf, buflen); | |
dacc5d41 EB |
757 | case AF_MPLS: |
758 | return mpls_ntop(af, addr, buf, buflen); | |
aba5acdf SH |
759 | case AF_IPX: |
760 | return ipx_ntop(af, addr, buf, buflen); | |
761 | case AF_DECnet: | |
762 | { | |
763 | struct dn_naddr dna = { 2, { 0, 0, }}; | |
764 | memcpy(dna.a_addr, addr, 2); | |
765 | return dnet_ntop(af, &dna, buf, buflen); | |
766 | } | |
0b218ab1 EB |
767 | case AF_PACKET: |
768 | return ll_addr_n2a(addr, len, ARPHRD_VOID, buf, buflen); | |
aba5acdf SH |
769 | default: |
770 | return "???"; | |
771 | } | |
772 | } | |
773 | ||
2e96d2cc PS |
774 | const char *rt_addr_n2a(int af, int len, const void *addr) |
775 | { | |
776 | static char buf[256]; | |
777 | ||
778 | return rt_addr_n2a_r(af, len, addr, buf, 256); | |
779 | } | |
780 | ||
45c90d19 EB |
781 | int read_family(const char *name) |
782 | { | |
783 | int family = AF_UNSPEC; | |
784 | if (strcmp(name, "inet") == 0) | |
785 | family = AF_INET; | |
786 | else if (strcmp(name, "inet6") == 0) | |
787 | family = AF_INET6; | |
788 | else if (strcmp(name, "dnet") == 0) | |
789 | family = AF_DECnet; | |
790 | else if (strcmp(name, "link") == 0) | |
791 | family = AF_PACKET; | |
792 | else if (strcmp(name, "ipx") == 0) | |
793 | family = AF_IPX; | |
dacc5d41 EB |
794 | else if (strcmp(name, "mpls") == 0) |
795 | family = AF_MPLS; | |
45c90d19 EB |
796 | else if (strcmp(name, "bridge") == 0) |
797 | family = AF_BRIDGE; | |
798 | return family; | |
799 | } | |
800 | ||
801 | const char *family_name(int family) | |
802 | { | |
803 | if (family == AF_INET) | |
804 | return "inet"; | |
805 | if (family == AF_INET6) | |
806 | return "inet6"; | |
807 | if (family == AF_DECnet) | |
808 | return "dnet"; | |
809 | if (family == AF_PACKET) | |
810 | return "link"; | |
811 | if (family == AF_IPX) | |
812 | return "ipx"; | |
dacc5d41 EB |
813 | if (family == AF_MPLS) |
814 | return "mpls"; | |
45c90d19 EB |
815 | if (family == AF_BRIDGE) |
816 | return "bridge"; | |
817 | return "???"; | |
818 | } | |
819 | ||
aba5acdf SH |
820 | #ifdef RESOLVE_HOSTNAMES |
821 | struct namerec | |
822 | { | |
823 | struct namerec *next; | |
7dec1bf8 | 824 | const char *name; |
aba5acdf | 825 | inet_prefix addr; |
aba5acdf SH |
826 | }; |
827 | ||
7dec1bf8 SH |
828 | #define NHASH 257 |
829 | static struct namerec *nht[NHASH]; | |
aba5acdf | 830 | |
7dec1bf8 | 831 | static const char *resolve_address(const void *addr, int len, int af) |
aba5acdf SH |
832 | { |
833 | struct namerec *n; | |
834 | struct hostent *h_ent; | |
835 | unsigned hash; | |
836 | static int notfirst; | |
837 | ||
838 | ||
839 | if (af == AF_INET6 && ((__u32*)addr)[0] == 0 && | |
840 | ((__u32*)addr)[1] == 0 && ((__u32*)addr)[2] == htonl(0xffff)) { | |
841 | af = AF_INET; | |
842 | addr += 12; | |
843 | len = 4; | |
844 | } | |
845 | ||
7dec1bf8 | 846 | hash = *(__u32 *)(addr + len - 4) % NHASH; |
aba5acdf SH |
847 | |
848 | for (n = nht[hash]; n; n = n->next) { | |
849 | if (n->addr.family == af && | |
850 | n->addr.bytelen == len && | |
851 | memcmp(n->addr.data, addr, len) == 0) | |
852 | return n->name; | |
853 | } | |
854 | if ((n = malloc(sizeof(*n))) == NULL) | |
855 | return NULL; | |
856 | n->addr.family = af; | |
857 | n->addr.bytelen = len; | |
858 | n->name = NULL; | |
859 | memcpy(n->addr.data, addr, len); | |
860 | n->next = nht[hash]; | |
861 | nht[hash] = n; | |
862 | if (++notfirst == 1) | |
863 | sethostent(1); | |
864 | fflush(stdout); | |
865 | ||
c7699875 | 866 | if ((h_ent = gethostbyaddr(addr, len, af)) != NULL) |
aba5acdf SH |
867 | n->name = strdup(h_ent->h_name); |
868 | ||
869 | /* Even if we fail, "negative" entry is remembered. */ | |
870 | return n->name; | |
871 | } | |
872 | #endif | |
873 | ||
a418e451 | 874 | const char *format_host_r(int af, int len, const void *addr, |
4094db72 | 875 | char *buf, int buflen) |
aba5acdf SH |
876 | { |
877 | #ifdef RESOLVE_HOSTNAMES | |
878 | if (resolve_hosts) { | |
7dec1bf8 SH |
879 | const char *n; |
880 | ||
f3a2ddc1 VK |
881 | len = len <= 0 ? af_byte_len(af) : len; |
882 | ||
aba5acdf SH |
883 | if (len > 0 && |
884 | (n = resolve_address(addr, len, af)) != NULL) | |
885 | return n; | |
886 | } | |
887 | #endif | |
2e96d2cc | 888 | return rt_addr_n2a_r(af, len, addr, buf, buflen); |
aba5acdf SH |
889 | } |
890 | ||
a418e451 PS |
891 | const char *format_host(int af, int len, const void *addr) |
892 | { | |
893 | static char buf[256]; | |
894 | ||
895 | return format_host_r(af, len, addr, buf, 256); | |
896 | } | |
897 | ||
aba5acdf | 898 | |
f332d169 | 899 | char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen) |
aba5acdf | 900 | { |
f332d169 | 901 | char *ptr = buf; |
aba5acdf SH |
902 | int i; |
903 | ||
904 | for (i=0; i<len; i++) { | |
905 | if (blen < 3) | |
906 | break; | |
907 | sprintf(ptr, "%02x", str[i]); | |
908 | ptr += 2; | |
909 | blen -= 2; | |
aba5acdf SH |
910 | } |
911 | return buf; | |
912 | } | |
913 | ||
89ae5020 | 914 | __u8 *hexstring_a2n(const char *str, __u8 *buf, int blen, unsigned int *len) |
aba5acdf | 915 | { |
89ae5020 | 916 | unsigned int cnt = 0; |
316c2346 | 917 | char *endptr; |
aba5acdf | 918 | |
316c2346 JP |
919 | if (strlen(str) % 2) |
920 | return NULL; | |
921 | while (cnt < blen && strlen(str) > 1) { | |
922 | unsigned int tmp; | |
923 | char tmpstr[3]; | |
924 | ||
925 | strncpy(tmpstr, str, 2); | |
926 | tmpstr[2] = '\0'; | |
927 | tmp = strtoul(tmpstr, &endptr, 16); | |
928 | if (errno != 0 || tmp > 0xFF || *endptr != '\0') | |
aba5acdf | 929 | return NULL; |
316c2346 JP |
930 | buf[cnt++] = tmp; |
931 | str += 2; | |
aba5acdf | 932 | } |
89ae5020 SD |
933 | |
934 | if (len) | |
935 | *len = cnt; | |
936 | ||
aba5acdf SH |
937 | return buf; |
938 | } | |
90f93024 | 939 | |
5866bddd TH |
940 | int addr64_n2a(__u64 addr, char *buff, size_t len) |
941 | { | |
942 | __u16 *words = (__u16 *)&addr; | |
943 | __u16 v; | |
944 | int i, ret; | |
945 | size_t written = 0; | |
946 | char *sep = ":"; | |
947 | ||
948 | for (i = 0; i < 4; i++) { | |
949 | v = ntohs(words[i]); | |
950 | ||
951 | if (i == 3) | |
952 | sep = ""; | |
953 | ||
954 | ret = snprintf(&buff[written], len - written, "%x%s", v, sep); | |
955 | if (ret < 0) | |
956 | return ret; | |
957 | ||
958 | written += ret; | |
959 | } | |
960 | ||
961 | return written; | |
962 | } | |
963 | ||
90f93024 SH |
964 | int print_timestamp(FILE *fp) |
965 | { | |
966 | struct timeval tv; | |
79aa79d0 | 967 | struct tm *tm; |
90f93024 | 968 | |
90f93024 | 969 | gettimeofday(&tv, NULL); |
79aa79d0 VK |
970 | tm = localtime(&tv.tv_sec); |
971 | ||
972 | if (timestamp_short) { | |
973 | char tshort[40]; | |
974 | ||
975 | strftime(tshort, sizeof(tshort), "%Y-%m-%dT%H:%M:%S", tm); | |
976 | fprintf(fp, "[%s.%06ld] ", tshort, tv.tv_usec); | |
977 | } else { | |
978 | char *tstr = asctime(tm); | |
979 | ||
980 | tstr[strlen(tstr)-1] = 0; | |
981 | fprintf(fp, "Timestamp: %s %ld usec\n", | |
982 | tstr, tv.tv_usec); | |
983 | } | |
90f93024 | 984 | |
90f93024 SH |
985 | return 0; |
986 | } | |
351efcde SH |
987 | |
988 | int cmdlineno; | |
989 | ||
990 | /* Like glibc getline but handle continuation lines and comments */ | |
d21e8835 | 991 | ssize_t getcmdline(char **linep, size_t *lenp, FILE *in) |
351efcde | 992 | { |
d21e8835 | 993 | ssize_t cc; |
351efcde | 994 | char *cp; |
ae665a52 | 995 | |
351efcde SH |
996 | if ((cc = getline(linep, lenp, in)) < 0) |
997 | return cc; /* eof or error */ | |
998 | ++cmdlineno; | |
999 | ||
1000 | cp = strchr(*linep, '#'); | |
ae665a52 | 1001 | if (cp) |
351efcde | 1002 | *cp = '\0'; |
ae665a52 | 1003 | |
351efcde SH |
1004 | while ((cp = strstr(*linep, "\\\n")) != NULL) { |
1005 | char *line1 = NULL; | |
1006 | size_t len1 = 0; | |
21a85d3b | 1007 | ssize_t cc1; |
351efcde SH |
1008 | |
1009 | if ((cc1 = getline(&line1, &len1, in)) < 0) { | |
1010 | fprintf(stderr, "Missing continuation line\n"); | |
1011 | return cc1; | |
1012 | } | |
1013 | ||
1014 | ++cmdlineno; | |
1015 | *cp = 0; | |
1016 | ||
1017 | cp = strchr(line1, '#'); | |
ae665a52 | 1018 | if (cp) |
351efcde SH |
1019 | *cp = '\0'; |
1020 | ||
d21e8835 AH |
1021 | *lenp = strlen(*linep) + strlen(line1) + 1; |
1022 | *linep = realloc(*linep, *lenp); | |
351efcde SH |
1023 | if (!*linep) { |
1024 | fprintf(stderr, "Out of memory\n"); | |
d21e8835 | 1025 | *lenp = 0; |
351efcde SH |
1026 | return -1; |
1027 | } | |
1028 | cc += cc1 - 2; | |
1029 | strcat(*linep, line1); | |
1030 | free(line1); | |
1031 | } | |
1032 | return cc; | |
1033 | } | |
1034 | ||
1035 | /* split command line into argument vector */ | |
1036 | int makeargs(char *line, char *argv[], int maxargs) | |
1037 | { | |
1038 | static const char ws[] = " \t\r\n"; | |
1039 | char *cp; | |
1040 | int argc = 0; | |
1041 | ||
39e3d383 | 1042 | for (cp = line + strspn(line, ws); *cp; cp += strspn(cp, ws)) { |
351efcde SH |
1043 | if (argc >= (maxargs - 1)) { |
1044 | fprintf(stderr, "Too many arguments to command\n"); | |
1045 | exit(1); | |
1046 | } | |
39e3d383 CG |
1047 | |
1048 | /* word begins with quote */ | |
1049 | if (*cp == '\'' || *cp == '"') { | |
1050 | char quote = *cp++; | |
1051 | ||
1052 | argv[argc++] = cp; | |
1053 | /* find ending quote */ | |
1054 | cp = strchr(cp, quote); | |
1055 | if (cp == NULL) { | |
1056 | fprintf(stderr, "Unterminated quoted string\n"); | |
1057 | exit(1); | |
1058 | } | |
1059 | *cp++ = 0; | |
1060 | continue; | |
1061 | } | |
1062 | ||
351efcde | 1063 | argv[argc++] = cp; |
39e3d383 CG |
1064 | /* find end of word */ |
1065 | cp += strcspn(cp, ws); | |
1066 | *cp++ = 0; | |
351efcde SH |
1067 | } |
1068 | argv[argc] = NULL; | |
1069 | ||
1070 | return argc; | |
1071 | } | |
aa574cd6 WC |
1072 | |
1073 | int inet_get_addr(const char *src, __u32 *dst, struct in6_addr *dst6) | |
1074 | { | |
1075 | if (strchr(src, ':')) | |
1076 | return inet_pton(AF_INET6, src, dst6); | |
1077 | else | |
1078 | return inet_pton(AF_INET, src, dst); | |
1079 | } | |
ddb1129b VK |
1080 | |
1081 | void print_nlmsg_timestamp(FILE *fp, const struct nlmsghdr *n) | |
1082 | { | |
1083 | char *tstr; | |
1084 | time_t secs = ((__u32*)NLMSG_DATA(n))[0]; | |
1085 | long usecs = ((__u32*)NLMSG_DATA(n))[1]; | |
1086 | tstr = asctime(localtime(&secs)); | |
1087 | tstr[strlen(tstr)-1] = 0; | |
1088 | fprintf(fp, "Timestamp: %s %lu us\n", tstr, usecs); | |
1089 | } | |
e998e118 VK |
1090 | |
1091 | static int on_netns(char *nsname, void *arg) | |
1092 | { | |
1093 | struct netns_func *f = arg; | |
1094 | ||
1095 | if (netns_switch(nsname)) | |
1096 | return -1; | |
1097 | ||
1098 | return f->func(nsname, f->arg); | |
1099 | } | |
1100 | ||
1101 | static int on_netns_label(char *nsname, void *arg) | |
1102 | { | |
1103 | printf("\nnetns: %s\n", nsname); | |
1104 | return on_netns(nsname, arg); | |
1105 | } | |
1106 | ||
1107 | int do_each_netns(int (*func)(char *nsname, void *arg), void *arg, | |
1108 | bool show_label) | |
1109 | { | |
1110 | struct netns_func nsf = { .func = func, .arg = arg }; | |
1111 | ||
1112 | if (show_label) | |
1113 | return netns_foreach(on_netns_label, &nsf); | |
1114 | ||
1115 | return netns_foreach(on_netns, &nsf); | |
1116 | } | |
b217df10 VK |
1117 | |
1118 | char *int_to_str(int val, char *buf) | |
1119 | { | |
1120 | sprintf(buf, "%d", val); | |
1121 | return buf; | |
1122 | } |