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