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