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