]>
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 | 16 | #include <unistd.h> |
aba5acdf | 17 | #include <fcntl.h> |
5bd9dd49 | 18 | #include <limits.h> |
aba5acdf SH |
19 | #include <sys/socket.h> |
20 | #include <netinet/in.h> | |
21 | #include <string.h> | |
625df645 | 22 | #include <ctype.h> |
aba5acdf SH |
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> |
ba2fc55b LB |
33 | #ifdef HAVE_LIBCAP |
34 | #include <sys/capability.h> | |
35 | #endif | |
90f93024 | 36 | |
0b218ab1 | 37 | #include "rt_names.h" |
aba5acdf | 38 | #include "utils.h" |
f5b50a18 | 39 | #include "ll_map.h" |
e998e118 | 40 | #include "namespace.h" |
aba5acdf | 41 | |
66488539 | 42 | int resolve_hosts; |
e1933b92 | 43 | int timestamp_short; |
54336567 | 44 | int pretty; |
b85076cd | 45 | const char *_SL_ = "\n"; |
79aa79d0 | 46 | |
927e3cfb ND |
47 | int read_prop(const char *dev, char *prop, long *value) |
48 | { | |
49 | char fname[128], buf[80], *endp, *nl; | |
50 | FILE *fp; | |
51 | long result; | |
52 | int ret; | |
53 | ||
54 | ret = snprintf(fname, sizeof(fname), "/sys/class/net/%s/%s", | |
55 | dev, prop); | |
56 | ||
57 | if (ret <= 0 || ret >= sizeof(fname)) { | |
58 | fprintf(stderr, "could not build pathname for property\n"); | |
59 | return -1; | |
60 | } | |
61 | ||
62 | fp = fopen(fname, "r"); | |
63 | if (fp == NULL) { | |
64 | fprintf(stderr, "fopen %s: %s\n", fname, strerror(errno)); | |
65 | return -1; | |
66 | } | |
67 | ||
68 | if (!fgets(buf, sizeof(buf), fp)) { | |
69 | fprintf(stderr, "property \"%s\" in file %s is currently unknown\n", prop, fname); | |
70 | fclose(fp); | |
71 | goto out; | |
72 | } | |
73 | ||
74 | nl = strchr(buf, '\n'); | |
75 | if (nl) | |
76 | *nl = '\0'; | |
77 | ||
78 | fclose(fp); | |
79 | result = strtol(buf, &endp, 0); | |
80 | ||
81 | if (*endp || buf == endp) { | |
82 | fprintf(stderr, "value \"%s\" in file %s is not a number\n", | |
83 | buf, fname); | |
84 | goto out; | |
85 | } | |
86 | ||
87 | if ((result == LONG_MAX || result == LONG_MIN) && errno == ERANGE) { | |
88 | fprintf(stderr, "strtol %s: %s", fname, strerror(errno)); | |
89 | goto out; | |
90 | } | |
91 | ||
92 | *value = result; | |
93 | return 0; | |
94 | out: | |
95 | fprintf(stderr, "Failed to parse %s\n", fname); | |
96 | return -1; | |
97 | } | |
98 | ||
99 | /* Parse a percent e.g: '30%' | |
100 | * return: 0 = ok, -1 = error, 1 = out of range | |
101 | */ | |
102 | int parse_percent(double *val, const char *str) | |
103 | { | |
104 | char *p; | |
105 | ||
106 | *val = strtod(str, &p) / 100.; | |
107 | if (*val == HUGE_VALF || *val == HUGE_VALL) | |
108 | return 1; | |
405e0c4f | 109 | if (*p && strcmp(p, "%")) |
927e3cfb ND |
110 | return -1; |
111 | ||
112 | return 0; | |
113 | } | |
114 | ||
609640f5 SD |
115 | int get_hex(char c) |
116 | { | |
117 | if (c >= 'A' && c <= 'F') | |
118 | return c - 'A' + 10; | |
119 | if (c >= 'a' && c <= 'f') | |
120 | return c - 'a' + 10; | |
121 | if (c >= '0' && c <= '9') | |
122 | return c - '0'; | |
123 | ||
124 | return -1; | |
125 | } | |
126 | ||
4094db72 | 127 | int get_integer(int *val, const char *arg, int base) |
aba5acdf SH |
128 | { |
129 | long res; | |
130 | char *ptr; | |
131 | ||
132 | if (!arg || !*arg) | |
133 | return -1; | |
697ac639 | 134 | |
aba5acdf | 135 | res = strtol(arg, &ptr, base); |
697ac639 SH |
136 | |
137 | /* If there were no digits at all, strtol() stores | |
e1933b92 | 138 | * the original value of nptr in *endptr (and returns 0). |
697ac639 SH |
139 | * In particular, if *nptr is not '\0' but **endptr is '\0' on return, |
140 | * the entire string is valid. | |
141 | */ | |
142 | if (!ptr || ptr == arg || *ptr) | |
143 | return -1; | |
144 | ||
145 | /* If an underflow occurs, strtol() returns LONG_MIN. | |
146 | * If an overflow occurs, strtol() returns LONG_MAX. | |
147 | * In both cases, errno is set to ERANGE. | |
148 | */ | |
149 | if ((res == LONG_MAX || res == LONG_MIN) && errno == ERANGE) | |
aba5acdf | 150 | return -1; |
697ac639 SH |
151 | |
152 | /* Outside range of int */ | |
153 | if (res < INT_MIN || res > INT_MAX) | |
154 | return -1; | |
155 | ||
aba5acdf SH |
156 | *val = res; |
157 | return 0; | |
158 | } | |
159 | ||
6b1ac654 | 160 | int mask2bits(__u32 netmask) |
4b270b17 | 161 | { |
e1933b92 | 162 | unsigned int bits = 0; |
6b1ac654 SH |
163 | __u32 mask = ntohl(netmask); |
164 | __u32 host = ~mask; | |
4b270b17 | 165 | |
6b1ac654 SH |
166 | /* a valid netmask must be 2^n - 1 */ |
167 | if ((host & (host + 1)) != 0) | |
168 | return -1; | |
4b270b17 | 169 | |
6b1ac654 SH |
170 | for (; mask; mask <<= 1) |
171 | ++bits; | |
4b270b17 SH |
172 | return bits; |
173 | } | |
174 | ||
e1933b92 | 175 | static int get_netmask(unsigned int *val, const char *arg, int base) |
5a0d1cbf AH |
176 | { |
177 | inet_prefix addr; | |
178 | ||
179 | if (!get_unsigned(val, arg, base)) | |
180 | return 0; | |
181 | ||
182 | /* try coverting dotted quad to CIDR */ | |
6b1ac654 SH |
183 | if (!get_addr_1(&addr, arg, AF_INET) && addr.family == AF_INET) { |
184 | int b = mask2bits(addr.data[0]); | |
e9e9365b | 185 | |
6b1ac654 SH |
186 | if (b >= 0) { |
187 | *val = b; | |
5a0d1cbf | 188 | return 0; |
6b1ac654 | 189 | } |
5a0d1cbf AH |
190 | } |
191 | ||
192 | return -1; | |
193 | } | |
194 | ||
e1933b92 | 195 | int get_unsigned(unsigned int *val, const char *arg, int base) |
aba5acdf SH |
196 | { |
197 | unsigned long res; | |
198 | char *ptr; | |
199 | ||
200 | if (!arg || !*arg) | |
201 | return -1; | |
697ac639 | 202 | |
aba5acdf | 203 | res = strtoul(arg, &ptr, base); |
697ac639 SH |
204 | |
205 | /* empty string or trailing non-digits */ | |
206 | if (!ptr || ptr == arg || *ptr) | |
207 | return -1; | |
208 | ||
209 | /* overflow */ | |
210 | if (res == ULONG_MAX && errno == ERANGE) | |
aba5acdf | 211 | return -1; |
697ac639 SH |
212 | |
213 | /* out side range of unsigned */ | |
214 | if (res > UINT_MAX) | |
215 | return -1; | |
216 | ||
aba5acdf SH |
217 | *val = res; |
218 | return 0; | |
219 | } | |
220 | ||
54e0b2e7 | 221 | /* |
db6b0cfa GR |
222 | * get_time_rtt is "translated" from a similar routine "get_time" in |
223 | * tc_util.c. We don't use the exact same routine because tc passes | |
224 | * microseconds to the kernel and the callers of get_time_rtt want to | |
225 | * pass milliseconds (standard unit for rtt values since 2.6.27), and | |
226 | * have a different assumption for the units of a "raw" number. | |
54e0b2e7 | 227 | */ |
e1933b92 | 228 | int get_time_rtt(unsigned int *val, const char *arg, int *raw) |
54e0b2e7 RJ |
229 | { |
230 | double t; | |
231 | unsigned long res; | |
232 | char *p; | |
233 | ||
697ac639 SH |
234 | if (strchr(arg, '.') != NULL) { |
235 | t = strtod(arg, &p); | |
54e0b2e7 RJ |
236 | if (t < 0.0) |
237 | return -1; | |
697ac639 | 238 | |
4d9a264f FW |
239 | /* no digits? */ |
240 | if (!p || p == arg) | |
697ac639 SH |
241 | return -1; |
242 | ||
243 | /* over/underflow */ | |
244 | if ((t == HUGE_VALF || t == HUGE_VALL) && errno == ERANGE) | |
245 | return -1; | |
246 | } else { | |
897fb84f | 247 | res = strtoul(arg, &p, 0); |
697ac639 | 248 | |
4d9a264f FW |
249 | /* empty string? */ |
250 | if (!p || p == arg) | |
697ac639 SH |
251 | return -1; |
252 | ||
253 | /* overflow */ | |
254 | if (res == ULONG_MAX && errno == ERANGE) | |
54e0b2e7 | 255 | return -1; |
697ac639 | 256 | |
54e0b2e7 RJ |
257 | t = (double)res; |
258 | } | |
697ac639 | 259 | |
54e0b2e7 RJ |
260 | if (p == arg) |
261 | return -1; | |
54e0b2e7 RJ |
262 | *raw = 1; |
263 | ||
264 | if (*p) { | |
265 | *raw = 0; | |
e1933b92 SH |
266 | if (strcasecmp(p, "s") == 0 || |
267 | strcasecmp(p, "sec") == 0 || | |
268 | strcasecmp(p, "secs") == 0) | |
269 | t *= 1000; | |
270 | else if (strcasecmp(p, "ms") == 0 || | |
271 | strcasecmp(p, "msec") == 0 || | |
272 | strcasecmp(p, "msecs") == 0) | |
54e0b2e7 | 273 | t *= 1.0; /* allow suffix, do nothing */ |
e1933b92 SH |
274 | else |
275 | return -1; | |
276 | } | |
54e0b2e7 RJ |
277 | |
278 | /* emulate ceil() without having to bring-in -lm and always be >= 1 */ | |
db6b0cfa GR |
279 | *val = t; |
280 | if (*val < t) | |
281 | *val += 1; | |
e9e9365b | 282 | |
e1933b92 | 283 | return 0; |
54e0b2e7 RJ |
284 | |
285 | } | |
286 | ||
c7699875 | 287 | int get_u64(__u64 *val, const char *arg, int base) |
288 | { | |
289 | unsigned long long res; | |
290 | char *ptr; | |
291 | ||
292 | if (!arg || !*arg) | |
293 | return -1; | |
697ac639 | 294 | |
c7699875 | 295 | res = strtoull(arg, &ptr, base); |
697ac639 SH |
296 | |
297 | /* empty string or trailing non-digits */ | |
298 | if (!ptr || ptr == arg || *ptr) | |
299 | return -1; | |
300 | ||
301 | /* overflow */ | |
302 | if (res == ULLONG_MAX && errno == ERANGE) | |
303 | return -1; | |
304 | ||
305 | /* in case ULL is 128 bits */ | |
306 | if (res > 0xFFFFFFFFFFFFFFFFULL) | |
307 | return -1; | |
308 | ||
e1933b92 SH |
309 | *val = res; |
310 | return 0; | |
c7699875 | 311 | } |
312 | ||
4094db72 | 313 | int get_u32(__u32 *val, const char *arg, int base) |
aba5acdf SH |
314 | { |
315 | unsigned long res; | |
316 | char *ptr; | |
317 | ||
318 | if (!arg || !*arg) | |
319 | return -1; | |
320 | res = strtoul(arg, &ptr, base); | |
697ac639 SH |
321 | |
322 | /* empty string or trailing non-digits */ | |
323 | if (!ptr || ptr == arg || *ptr) | |
324 | return -1; | |
325 | ||
326 | /* overflow */ | |
327 | if (res == ULONG_MAX && errno == ERANGE) | |
328 | return -1; | |
329 | ||
330 | /* in case UL > 32 bits */ | |
331 | if (res > 0xFFFFFFFFUL) | |
aba5acdf | 332 | return -1; |
697ac639 | 333 | |
aba5acdf SH |
334 | *val = res; |
335 | return 0; | |
336 | } | |
337 | ||
4094db72 | 338 | int get_u16(__u16 *val, const char *arg, int base) |
aba5acdf SH |
339 | { |
340 | unsigned long res; | |
341 | char *ptr; | |
342 | ||
343 | if (!arg || !*arg) | |
344 | return -1; | |
345 | res = strtoul(arg, &ptr, base); | |
697ac639 SH |
346 | |
347 | /* empty string or trailing non-digits */ | |
348 | if (!ptr || ptr == arg || *ptr) | |
349 | return -1; | |
350 | ||
351 | /* overflow */ | |
352 | if (res == ULONG_MAX && errno == ERANGE) | |
aba5acdf | 353 | return -1; |
697ac639 SH |
354 | |
355 | if (res > 0xFFFFUL) | |
356 | return -1; | |
357 | ||
aba5acdf SH |
358 | *val = res; |
359 | return 0; | |
360 | } | |
361 | ||
4094db72 | 362 | int get_u8(__u8 *val, const char *arg, int base) |
aba5acdf SH |
363 | { |
364 | unsigned long res; | |
365 | char *ptr; | |
366 | ||
367 | if (!arg || !*arg) | |
368 | return -1; | |
697ac639 | 369 | |
aba5acdf | 370 | res = strtoul(arg, &ptr, base); |
697ac639 SH |
371 | /* empty string or trailing non-digits */ |
372 | if (!ptr || ptr == arg || *ptr) | |
373 | return -1; | |
374 | ||
375 | /* overflow */ | |
376 | if (res == ULONG_MAX && errno == ERANGE) | |
aba5acdf | 377 | return -1; |
697ac639 SH |
378 | |
379 | if (res > 0xFFUL) | |
380 | return -1; | |
381 | ||
aba5acdf SH |
382 | *val = res; |
383 | return 0; | |
384 | } | |
385 | ||
30d10db5 HPP |
386 | int get_s32(__s32 *val, const char *arg, int base) |
387 | { | |
388 | long res; | |
389 | char *ptr; | |
390 | ||
391 | errno = 0; | |
392 | ||
393 | if (!arg || !*arg) | |
394 | return -1; | |
395 | res = strtol(arg, &ptr, base); | |
697ac639 SH |
396 | if (!ptr || ptr == arg || *ptr) |
397 | return -1; | |
398 | if ((res == LONG_MIN || res == LONG_MAX) && errno == ERANGE) | |
30d10db5 | 399 | return -1; |
697ac639 SH |
400 | if (res > INT32_MAX || res < INT32_MIN) |
401 | return -1; | |
402 | ||
30d10db5 HPP |
403 | *val = res; |
404 | return 0; | |
405 | } | |
406 | ||
4094db72 | 407 | int get_s16(__s16 *val, const char *arg, int base) |
aba5acdf SH |
408 | { |
409 | long res; | |
410 | char *ptr; | |
411 | ||
412 | if (!arg || !*arg) | |
413 | return -1; | |
414 | res = strtol(arg, &ptr, base); | |
697ac639 SH |
415 | if (!ptr || ptr == arg || *ptr) |
416 | return -1; | |
417 | if ((res == LONG_MIN || res == LONG_MAX) && errno == ERANGE) | |
418 | return -1; | |
419 | if (res > 0x7FFF || res < -0x8000) | |
aba5acdf | 420 | return -1; |
697ac639 | 421 | |
aba5acdf SH |
422 | *val = res; |
423 | return 0; | |
424 | } | |
425 | ||
4094db72 | 426 | int get_s8(__s8 *val, const char *arg, int base) |
aba5acdf SH |
427 | { |
428 | long res; | |
429 | char *ptr; | |
430 | ||
431 | if (!arg || !*arg) | |
432 | return -1; | |
433 | res = strtol(arg, &ptr, base); | |
697ac639 SH |
434 | if (!ptr || ptr == arg || *ptr) |
435 | return -1; | |
436 | if ((res == LONG_MIN || res == LONG_MAX) && errno == ERANGE) | |
437 | return -1; | |
438 | if (res > 0x7F || res < -0x80) | |
aba5acdf SH |
439 | return -1; |
440 | *val = res; | |
441 | return 0; | |
442 | } | |
443 | ||
9f7401fa SD |
444 | int get_be64(__be64 *val, const char *arg, int base) |
445 | { | |
446 | __u64 v; | |
447 | int ret = get_u64(&v, arg, base); | |
448 | ||
449 | if (!ret) | |
450 | *val = htonll(v); | |
451 | ||
452 | return ret; | |
453 | } | |
454 | ||
455 | int get_be32(__be32 *val, const char *arg, int base) | |
456 | { | |
457 | __u32 v; | |
458 | int ret = get_u32(&v, arg, base); | |
459 | ||
460 | if (!ret) | |
461 | *val = htonl(v); | |
462 | ||
463 | return ret; | |
464 | } | |
465 | ||
466 | int get_be16(__be16 *val, const char *arg, int base) | |
467 | { | |
468 | __u16 v; | |
469 | int ret = get_u16(&v, arg, base); | |
470 | ||
471 | if (!ret) | |
472 | *val = htons(v); | |
473 | ||
474 | return ret; | |
475 | } | |
476 | ||
cafa6c8e SH |
477 | /* This uses a non-standard parsing (ie not inet_aton, or inet_pton) |
478 | * because of legacy choice to parse 10.8 as 10.8.0.0 not 10.0.0.8 | |
479 | */ | |
480 | static int get_addr_ipv4(__u8 *ap, const char *cp) | |
aba5acdf | 481 | { |
cafa6c8e SH |
482 | int i; |
483 | ||
484 | for (i = 0; i < 4; i++) { | |
485 | unsigned long n; | |
486 | char *endp; | |
e9e9365b | 487 | |
cafa6c8e SH |
488 | n = strtoul(cp, &endp, 0); |
489 | if (n > 255) | |
490 | return -1; /* bogus network value */ | |
491 | ||
492 | if (endp == cp) /* no digits */ | |
493 | return -1; | |
94afda75 | 494 | |
cafa6c8e SH |
495 | ap[i] = n; |
496 | ||
497 | if (*endp == '\0') | |
498 | break; | |
499 | ||
500 | if (i == 3 || *endp != '.') | |
e1933b92 | 501 | return -1; /* extra characters */ |
cafa6c8e SH |
502 | cp = endp + 1; |
503 | } | |
504 | ||
505 | return 1; | |
506 | } | |
507 | ||
5866bddd TH |
508 | int get_addr64(__u64 *ap, const char *cp) |
509 | { | |
510 | int i; | |
511 | ||
512 | union { | |
513 | __u16 v16[4]; | |
514 | __u64 v64; | |
515 | } val; | |
516 | ||
517 | for (i = 0; i < 4; i++) { | |
518 | unsigned long n; | |
519 | char *endp; | |
520 | ||
521 | n = strtoul(cp, &endp, 16); | |
522 | if (n > 0xffff) | |
523 | return -1; /* bogus network value */ | |
524 | ||
525 | if (endp == cp) /* no digits */ | |
526 | return -1; | |
527 | ||
528 | val.v16[i] = htons(n); | |
529 | ||
530 | if (*endp == '\0') | |
531 | break; | |
532 | ||
533 | if (i == 3 || *endp != ':') | |
534 | return -1; /* extra characters */ | |
535 | cp = endp + 1; | |
536 | } | |
537 | ||
538 | *ap = val.v64; | |
539 | ||
540 | return 1; | |
541 | } | |
542 | ||
27c523e2 SP |
543 | static void set_address_type(inet_prefix *addr) |
544 | { | |
545 | switch (addr->family) { | |
546 | case AF_INET: | |
547 | if (!addr->data[0]) | |
548 | addr->flags |= ADDRTYPE_INET_UNSPEC; | |
549 | else if (IN_MULTICAST(ntohl(addr->data[0]))) | |
550 | addr->flags |= ADDRTYPE_INET_MULTI; | |
551 | else | |
552 | addr->flags |= ADDRTYPE_INET; | |
553 | break; | |
554 | case AF_INET6: | |
555 | if (IN6_IS_ADDR_UNSPECIFIED(addr->data)) | |
556 | addr->flags |= ADDRTYPE_INET_UNSPEC; | |
557 | else if (IN6_IS_ADDR_MULTICAST(addr->data)) | |
558 | addr->flags |= ADDRTYPE_INET_MULTI; | |
559 | else | |
560 | addr->flags |= ADDRTYPE_INET; | |
561 | break; | |
562 | } | |
563 | } | |
564 | ||
7bf5e876 | 565 | static int __get_addr_1(inet_prefix *addr, const char *name, int family) |
cafa6c8e | 566 | { |
aba5acdf SH |
567 | memset(addr, 0, sizeof(*addr)); |
568 | ||
7696f109 | 569 | if (strcmp(name, "default") == 0) { |
dacc5d41 | 570 | if ((family == AF_DECnet) || (family == AF_MPLS)) |
aba5acdf | 571 | return -1; |
d42c7891 | 572 | addr->family = family; |
f2522007 | 573 | addr->bytelen = af_byte_len(addr->family); |
93fa1241 | 574 | addr->bitlen = -2; |
7696f109 AZ |
575 | addr->flags |= PREFIXLEN_SPECIFIED; |
576 | return 0; | |
577 | } | |
578 | ||
579 | if (strcmp(name, "all") == 0 || | |
580 | strcmp(name, "any") == 0) { | |
581 | if ((family == AF_DECnet) || (family == AF_MPLS)) | |
582 | return -1; | |
d42c7891 | 583 | addr->family = family; |
7696f109 AZ |
584 | addr->bytelen = 0; |
585 | addr->bitlen = -2; | |
aba5acdf SH |
586 | return 0; |
587 | } | |
588 | ||
0b218ab1 EB |
589 | if (family == AF_PACKET) { |
590 | int len; | |
e1933b92 | 591 | |
8b59612f DA |
592 | len = ll_addr_a2n((char *) &addr->data, sizeof(addr->data), |
593 | name); | |
0b218ab1 EB |
594 | if (len < 0) |
595 | return -1; | |
596 | ||
597 | addr->family = AF_PACKET; | |
598 | addr->bytelen = len; | |
599 | addr->bitlen = len * 8; | |
600 | return 0; | |
601 | } | |
602 | ||
aba5acdf SH |
603 | if (strchr(name, ':')) { |
604 | addr->family = AF_INET6; | |
605 | if (family != AF_UNSPEC && family != AF_INET6) | |
606 | return -1; | |
607 | if (inet_pton(AF_INET6, name, addr->data) <= 0) | |
608 | return -1; | |
609 | addr->bytelen = 16; | |
610 | addr->bitlen = -1; | |
611 | return 0; | |
612 | } | |
613 | ||
614 | if (family == AF_DECnet) { | |
615 | struct dn_naddr dna; | |
e1933b92 | 616 | |
aba5acdf SH |
617 | addr->family = AF_DECnet; |
618 | if (dnet_pton(AF_DECnet, name, &dna) <= 0) | |
619 | return -1; | |
620 | memcpy(addr->data, dna.a_addr, 2); | |
621 | addr->bytelen = 2; | |
622 | addr->bitlen = -1; | |
623 | return 0; | |
624 | } | |
625 | ||
dacc5d41 | 626 | if (family == AF_MPLS) { |
4af44716 | 627 | unsigned int maxlabels; |
dacc5d41 | 628 | int i; |
e1933b92 | 629 | |
dacc5d41 | 630 | addr->family = AF_MPLS; |
4af44716 DA |
631 | if (mpls_pton(AF_MPLS, name, addr->data, |
632 | sizeof(addr->data)) <= 0) | |
dacc5d41 EB |
633 | return -1; |
634 | addr->bytelen = 4; | |
635 | addr->bitlen = 20; | |
636 | /* How many bytes do I need? */ | |
4af44716 DA |
637 | maxlabels = sizeof(addr->data) / sizeof(struct mpls_label); |
638 | for (i = 0; i < maxlabels; i++) { | |
dacc5d41 EB |
639 | if (ntohl(addr->data[i]) & MPLS_LS_S_MASK) { |
640 | addr->bytelen = (i + 1)*4; | |
641 | break; | |
642 | } | |
643 | } | |
644 | return 0; | |
645 | } | |
646 | ||
aba5acdf SH |
647 | addr->family = AF_INET; |
648 | if (family != AF_UNSPEC && family != AF_INET) | |
649 | return -1; | |
94afda75 | 650 | |
cafa6c8e | 651 | if (get_addr_ipv4((__u8 *)addr->data, name) <= 0) |
516ffb6b | 652 | return -1; |
94afda75 | 653 | |
aba5acdf SH |
654 | addr->bytelen = 4; |
655 | addr->bitlen = -1; | |
aba5acdf SH |
656 | return 0; |
657 | } | |
658 | ||
7bf5e876 SP |
659 | int get_addr_1(inet_prefix *addr, const char *name, int family) |
660 | { | |
661 | int ret; | |
662 | ||
663 | ret = __get_addr_1(addr, name, family); | |
664 | if (ret) | |
665 | return ret; | |
666 | ||
27c523e2 | 667 | set_address_type(addr); |
7bf5e876 SP |
668 | return 0; |
669 | } | |
670 | ||
f3a2ddc1 VK |
671 | int af_bit_len(int af) |
672 | { | |
673 | switch (af) { | |
674 | case AF_INET6: | |
675 | return 128; | |
676 | case AF_INET: | |
677 | return 32; | |
678 | case AF_DECnet: | |
679 | return 16; | |
680 | case AF_IPX: | |
681 | return 80; | |
dacc5d41 EB |
682 | case AF_MPLS: |
683 | return 20; | |
f3a2ddc1 VK |
684 | } |
685 | ||
686 | return 0; | |
687 | } | |
688 | ||
689 | int af_byte_len(int af) | |
690 | { | |
691 | return af_bit_len(af) / 8; | |
692 | } | |
693 | ||
fb9b1d0f | 694 | int get_prefix_1(inet_prefix *dst, char *arg, int family) |
aba5acdf | 695 | { |
aba5acdf | 696 | char *slash; |
93fa1241 | 697 | int err, bitlen, flags; |
aba5acdf SH |
698 | |
699 | slash = strchr(arg, '/'); | |
700 | if (slash) | |
701 | *slash = 0; | |
fb9b1d0f | 702 | |
aba5acdf | 703 | err = get_addr_1(dst, arg, family); |
93fa1241 | 704 | |
aba5acdf SH |
705 | if (slash) |
706 | *slash = '/'; | |
93fa1241 SP |
707 | |
708 | if (err) | |
709 | return err; | |
710 | ||
711 | bitlen = af_bit_len(dst->family); | |
712 | ||
7696f109 | 713 | flags = 0; |
93fa1241 SP |
714 | if (slash) { |
715 | unsigned int plen; | |
716 | ||
717 | if (dst->bitlen == -2) | |
718 | return -1; | |
719 | if (get_netmask(&plen, slash + 1, 0)) | |
720 | return -1; | |
721 | if (plen > bitlen) | |
722 | return -1; | |
723 | ||
7696f109 | 724 | flags |= PREFIXLEN_SPECIFIED; |
93fa1241 SP |
725 | bitlen = plen; |
726 | } else { | |
727 | if (dst->bitlen == -2) | |
728 | bitlen = 0; | |
93fa1241 SP |
729 | } |
730 | ||
731 | dst->flags |= flags; | |
732 | dst->bitlen = bitlen; | |
733 | ||
734 | return 0; | |
aba5acdf SH |
735 | } |
736 | ||
34705c80 PS |
737 | static const char *family_name_verbose(int family) |
738 | { | |
739 | if (family == AF_UNSPEC) | |
740 | return "any valid"; | |
741 | return family_name(family); | |
742 | } | |
743 | ||
c7699875 | 744 | int get_addr(inet_prefix *dst, const char *arg, int family) |
aba5acdf | 745 | { |
aba5acdf | 746 | if (get_addr_1(dst, arg, family)) { |
e1933b92 SH |
747 | fprintf(stderr, |
748 | "Error: %s address is expected rather than \"%s\".\n", | |
34705c80 | 749 | family_name_verbose(family), arg); |
aba5acdf SH |
750 | exit(1); |
751 | } | |
752 | return 0; | |
753 | } | |
754 | ||
27c523e2 SP |
755 | int get_addr_rta(inet_prefix *dst, const struct rtattr *rta, int family) |
756 | { | |
757 | const int len = RTA_PAYLOAD(rta); | |
758 | const void *data = RTA_DATA(rta); | |
759 | ||
760 | switch (len) { | |
761 | case 4: | |
762 | dst->family = AF_INET; | |
763 | dst->bytelen = 4; | |
764 | memcpy(dst->data, data, 4); | |
765 | break; | |
766 | case 16: | |
767 | dst->family = AF_INET6; | |
768 | dst->bytelen = 16; | |
769 | memcpy(dst->data, data, 16); | |
770 | break; | |
771 | case 2: | |
772 | dst->family = AF_DECnet; | |
773 | dst->bytelen = 2; | |
774 | memcpy(dst->data, data, 2); | |
775 | break; | |
776 | case 10: | |
777 | dst->family = AF_IPX; | |
778 | dst->bytelen = 10; | |
779 | memcpy(dst->data, data, 10); | |
780 | break; | |
781 | default: | |
782 | return -1; | |
783 | } | |
784 | ||
785 | if (family != AF_UNSPEC && family != dst->family) | |
786 | return -2; | |
787 | ||
788 | dst->bitlen = -1; | |
789 | dst->flags = 0; | |
790 | ||
791 | set_address_type(dst); | |
792 | return 0; | |
793 | } | |
794 | ||
fb9b1d0f | 795 | int get_prefix(inet_prefix *dst, char *arg, int family) |
aba5acdf SH |
796 | { |
797 | if (family == AF_PACKET) { | |
e1933b92 SH |
798 | fprintf(stderr, |
799 | "Error: \"%s\" may be inet prefix, but it is not allowed in this context.\n", | |
800 | arg); | |
aba5acdf SH |
801 | exit(1); |
802 | } | |
334af761 | 803 | |
aba5acdf | 804 | if (get_prefix_1(dst, arg, family)) { |
e1933b92 SH |
805 | fprintf(stderr, |
806 | "Error: %s prefix is expected rather than \"%s\".\n", | |
34705c80 | 807 | family_name_verbose(family), arg); |
aba5acdf SH |
808 | exit(1); |
809 | } | |
810 | return 0; | |
811 | } | |
812 | ||
c7699875 | 813 | __u32 get_addr32(const char *name) |
aba5acdf SH |
814 | { |
815 | inet_prefix addr; | |
e1933b92 | 816 | |
aba5acdf | 817 | if (get_addr_1(&addr, name, AF_INET)) { |
e1933b92 SH |
818 | fprintf(stderr, |
819 | "Error: an IP address is expected rather than \"%s\"\n", | |
820 | name); | |
aba5acdf SH |
821 | exit(1); |
822 | } | |
823 | return addr.data[0]; | |
824 | } | |
825 | ||
4094db72 | 826 | void incomplete_command(void) |
aba5acdf SH |
827 | { |
828 | fprintf(stderr, "Command line is not complete. Try option \"help\"\n"); | |
829 | exit(-1); | |
830 | } | |
831 | ||
c7699875 | 832 | void missarg(const char *key) |
833 | { | |
834 | fprintf(stderr, "Error: argument \"%s\" is required\n", key); | |
835 | exit(-1); | |
836 | } | |
837 | ||
4094db72 | 838 | void invarg(const char *msg, const char *arg) |
aba5acdf SH |
839 | { |
840 | fprintf(stderr, "Error: argument \"%s\" is wrong: %s\n", arg, msg); | |
841 | exit(-1); | |
842 | } | |
843 | ||
4094db72 | 844 | void duparg(const char *key, const char *arg) |
aba5acdf | 845 | { |
e1933b92 SH |
846 | fprintf(stderr, |
847 | "Error: duplicate \"%s\": \"%s\" is the second value.\n", | |
848 | key, arg); | |
aba5acdf SH |
849 | exit(-1); |
850 | } | |
851 | ||
4094db72 | 852 | void duparg2(const char *key, const char *arg) |
aba5acdf | 853 | { |
e1933b92 SH |
854 | fprintf(stderr, |
855 | "Error: either \"%s\" is duplicate, or \"%s\" is a garbage.\n", | |
856 | key, arg); | |
aba5acdf SH |
857 | exit(-1); |
858 | } | |
859 | ||
fe99adbc SP |
860 | int nodev(const char *dev) |
861 | { | |
862 | fprintf(stderr, "Cannot find device \"%s\"\n", dev); | |
863 | return -1; | |
864 | } | |
865 | ||
625df645 PS |
866 | int check_ifname(const char *name) |
867 | { | |
868 | /* These checks mimic kernel checks in dev_valid_name */ | |
869 | if (*name == '\0') | |
870 | return -1; | |
871 | if (strlen(name) >= IFNAMSIZ) | |
872 | return -1; | |
873 | ||
874 | while (*name) { | |
875 | if (*name == '/' || isspace(*name)) | |
876 | return -1; | |
877 | ++name; | |
878 | } | |
879 | return 0; | |
880 | } | |
881 | ||
882 | /* buf is assumed to be IFNAMSIZ */ | |
883 | int get_ifname(char *buf, const char *name) | |
884 | { | |
885 | int ret; | |
886 | ||
887 | ret = check_ifname(name); | |
888 | if (ret == 0) | |
889 | strncpy(buf, name, IFNAMSIZ); | |
890 | ||
891 | return ret; | |
892 | } | |
893 | ||
fcac9665 SP |
894 | const char *get_ifname_rta(int ifindex, const struct rtattr *rta) |
895 | { | |
896 | const char *name; | |
897 | ||
898 | if (rta) { | |
899 | name = rta_getattr_str(rta); | |
900 | } else { | |
901 | fprintf(stderr, | |
902 | "BUG: device with ifindex %d has nil ifname\n", | |
903 | ifindex); | |
904 | name = ll_idx_n2a(ifindex); | |
905 | } | |
906 | ||
907 | if (check_ifname(name)) | |
908 | return NULL; | |
909 | ||
910 | return name; | |
911 | } | |
912 | ||
4094db72 | 913 | int matches(const char *cmd, const char *pattern) |
aba5acdf SH |
914 | { |
915 | int len = strlen(cmd); | |
e1933b92 | 916 | |
aba5acdf SH |
917 | if (len > strlen(pattern)) |
918 | return -1; | |
919 | return memcmp(pattern, cmd, len); | |
920 | } | |
921 | ||
4094db72 | 922 | int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits) |
aba5acdf | 923 | { |
9626dfd5 SH |
924 | const __u32 *a1 = a->data; |
925 | const __u32 *a2 = b->data; | |
aba5acdf SH |
926 | int words = bits >> 0x05; |
927 | ||
928 | bits &= 0x1f; | |
929 | ||
930 | if (words) | |
931 | if (memcmp(a1, a2, words << 2)) | |
932 | return -1; | |
933 | ||
934 | if (bits) { | |
935 | __u32 w1, w2; | |
936 | __u32 mask; | |
937 | ||
938 | w1 = a1[words]; | |
939 | w2 = a2[words]; | |
940 | ||
941 | mask = htonl((0xffffffff) << (0x20 - bits)); | |
942 | ||
943 | if ((w1 ^ w2) & mask) | |
944 | return 1; | |
945 | } | |
946 | ||
947 | return 0; | |
948 | } | |
949 | ||
27c523e2 SP |
950 | int inet_addr_match_rta(const inet_prefix *m, const struct rtattr *rta) |
951 | { | |
952 | inet_prefix dst; | |
953 | ||
954 | if (!rta || m->family == AF_UNSPEC || m->bitlen <= 0) | |
955 | return 0; | |
956 | ||
957 | if (get_addr_rta(&dst, rta, m->family)) | |
958 | return -1; | |
959 | ||
960 | return inet_addr_match(&dst, m, m->bitlen); | |
961 | } | |
962 | ||
aba5acdf SH |
963 | int __iproute2_hz_internal; |
964 | ||
965 | int __get_hz(void) | |
966 | { | |
967 | char name[1024]; | |
968 | int hz = 0; | |
969 | FILE *fp; | |
970 | ||
971 | if (getenv("HZ")) | |
972 | return atoi(getenv("HZ")) ? : HZ; | |
973 | ||
e1933b92 SH |
974 | if (getenv("PROC_NET_PSCHED")) |
975 | snprintf(name, sizeof(name)-1, | |
976 | "%s", getenv("PROC_NET_PSCHED")); | |
977 | else if (getenv("PROC_ROOT")) | |
978 | snprintf(name, sizeof(name)-1, | |
979 | "%s/net/psched", getenv("PROC_ROOT")); | |
980 | else | |
aba5acdf | 981 | strcpy(name, "/proc/net/psched"); |
e1933b92 | 982 | |
aba5acdf SH |
983 | fp = fopen(name, "r"); |
984 | ||
985 | if (fp) { | |
e1933b92 SH |
986 | unsigned int nom, denom; |
987 | ||
aba5acdf SH |
988 | if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2) |
989 | if (nom == 1000000) | |
990 | hz = denom; | |
991 | fclose(fp); | |
992 | } | |
993 | if (hz) | |
994 | return hz; | |
995 | return HZ; | |
996 | } | |
997 | ||
5e8bc631 | 998 | int __iproute2_user_hz_internal; |
999 | ||
1000 | int __get_user_hz(void) | |
1001 | { | |
1002 | return sysconf(_SC_CLK_TCK); | |
1003 | } | |
1004 | ||
e1933b92 SH |
1005 | const char *rt_addr_n2a_r(int af, int len, |
1006 | const void *addr, char *buf, int buflen) | |
aba5acdf SH |
1007 | { |
1008 | switch (af) { | |
1009 | case AF_INET: | |
1010 | case AF_INET6: | |
1011 | return inet_ntop(af, addr, buf, buflen); | |
dacc5d41 EB |
1012 | case AF_MPLS: |
1013 | return mpls_ntop(af, addr, buf, buflen); | |
aba5acdf SH |
1014 | case AF_IPX: |
1015 | return ipx_ntop(af, addr, buf, buflen); | |
1016 | case AF_DECnet: | |
1017 | { | |
e1933b92 SH |
1018 | struct dn_naddr dna = { 2, { 0, 0, } }; |
1019 | ||
aba5acdf SH |
1020 | memcpy(dna.a_addr, addr, 2); |
1021 | return dnet_ntop(af, &dna, buf, buflen); | |
1022 | } | |
0b218ab1 EB |
1023 | case AF_PACKET: |
1024 | return ll_addr_n2a(addr, len, ARPHRD_VOID, buf, buflen); | |
728eb8d0 DS |
1025 | case AF_BRIDGE: |
1026 | { | |
1027 | const union { | |
1028 | struct sockaddr sa; | |
1029 | struct sockaddr_in sin; | |
1030 | struct sockaddr_in6 sin6; | |
1031 | } *sa = addr; | |
1032 | ||
1033 | switch (sa->sa.sa_family) { | |
1034 | case AF_INET: | |
1035 | return inet_ntop(AF_INET, &sa->sin.sin_addr, | |
1036 | buf, buflen); | |
1037 | case AF_INET6: | |
1038 | return inet_ntop(AF_INET6, &sa->sin6.sin6_addr, | |
1039 | buf, buflen); | |
1040 | } | |
1041 | ||
1042 | /* fallthrough */ | |
1043 | } | |
aba5acdf SH |
1044 | default: |
1045 | return "???"; | |
1046 | } | |
1047 | } | |
1048 | ||
2e96d2cc PS |
1049 | const char *rt_addr_n2a(int af, int len, const void *addr) |
1050 | { | |
1051 | static char buf[256]; | |
1052 | ||
1053 | return rt_addr_n2a_r(af, len, addr, buf, 256); | |
1054 | } | |
1055 | ||
45c90d19 EB |
1056 | int read_family(const char *name) |
1057 | { | |
1058 | int family = AF_UNSPEC; | |
e1933b92 | 1059 | |
45c90d19 EB |
1060 | if (strcmp(name, "inet") == 0) |
1061 | family = AF_INET; | |
1062 | else if (strcmp(name, "inet6") == 0) | |
1063 | family = AF_INET6; | |
1064 | else if (strcmp(name, "dnet") == 0) | |
1065 | family = AF_DECnet; | |
1066 | else if (strcmp(name, "link") == 0) | |
1067 | family = AF_PACKET; | |
1068 | else if (strcmp(name, "ipx") == 0) | |
1069 | family = AF_IPX; | |
dacc5d41 EB |
1070 | else if (strcmp(name, "mpls") == 0) |
1071 | family = AF_MPLS; | |
45c90d19 EB |
1072 | else if (strcmp(name, "bridge") == 0) |
1073 | family = AF_BRIDGE; | |
1074 | return family; | |
1075 | } | |
1076 | ||
1077 | const char *family_name(int family) | |
1078 | { | |
1079 | if (family == AF_INET) | |
1080 | return "inet"; | |
1081 | if (family == AF_INET6) | |
1082 | return "inet6"; | |
1083 | if (family == AF_DECnet) | |
1084 | return "dnet"; | |
1085 | if (family == AF_PACKET) | |
1086 | return "link"; | |
1087 | if (family == AF_IPX) | |
1088 | return "ipx"; | |
dacc5d41 EB |
1089 | if (family == AF_MPLS) |
1090 | return "mpls"; | |
45c90d19 EB |
1091 | if (family == AF_BRIDGE) |
1092 | return "bridge"; | |
1093 | return "???"; | |
1094 | } | |
1095 | ||
aba5acdf | 1096 | #ifdef RESOLVE_HOSTNAMES |
e1933b92 | 1097 | struct namerec { |
aba5acdf | 1098 | struct namerec *next; |
7dec1bf8 | 1099 | const char *name; |
aba5acdf | 1100 | inet_prefix addr; |
aba5acdf SH |
1101 | }; |
1102 | ||
7dec1bf8 SH |
1103 | #define NHASH 257 |
1104 | static struct namerec *nht[NHASH]; | |
aba5acdf | 1105 | |
7dec1bf8 | 1106 | static const char *resolve_address(const void *addr, int len, int af) |
aba5acdf SH |
1107 | { |
1108 | struct namerec *n; | |
1109 | struct hostent *h_ent; | |
e1933b92 | 1110 | unsigned int hash; |
aba5acdf SH |
1111 | static int notfirst; |
1112 | ||
1113 | ||
e1933b92 SH |
1114 | if (af == AF_INET6 && ((__u32 *)addr)[0] == 0 && |
1115 | ((__u32 *)addr)[1] == 0 && ((__u32 *)addr)[2] == htonl(0xffff)) { | |
aba5acdf SH |
1116 | af = AF_INET; |
1117 | addr += 12; | |
1118 | len = 4; | |
1119 | } | |
1120 | ||
7dec1bf8 | 1121 | hash = *(__u32 *)(addr + len - 4) % NHASH; |
aba5acdf SH |
1122 | |
1123 | for (n = nht[hash]; n; n = n->next) { | |
1124 | if (n->addr.family == af && | |
1125 | n->addr.bytelen == len && | |
1126 | memcmp(n->addr.data, addr, len) == 0) | |
1127 | return n->name; | |
1128 | } | |
e1933b92 SH |
1129 | n = malloc(sizeof(*n)); |
1130 | if (n == NULL) | |
aba5acdf SH |
1131 | return NULL; |
1132 | n->addr.family = af; | |
1133 | n->addr.bytelen = len; | |
1134 | n->name = NULL; | |
1135 | memcpy(n->addr.data, addr, len); | |
1136 | n->next = nht[hash]; | |
1137 | nht[hash] = n; | |
1138 | if (++notfirst == 1) | |
1139 | sethostent(1); | |
1140 | fflush(stdout); | |
1141 | ||
e1933b92 SH |
1142 | h_ent = gethostbyaddr(addr, len, af); |
1143 | if (h_ent != NULL) | |
aba5acdf SH |
1144 | n->name = strdup(h_ent->h_name); |
1145 | ||
1146 | /* Even if we fail, "negative" entry is remembered. */ | |
1147 | return n->name; | |
1148 | } | |
1149 | #endif | |
1150 | ||
a418e451 | 1151 | const char *format_host_r(int af, int len, const void *addr, |
4094db72 | 1152 | char *buf, int buflen) |
aba5acdf SH |
1153 | { |
1154 | #ifdef RESOLVE_HOSTNAMES | |
1155 | if (resolve_hosts) { | |
7dec1bf8 SH |
1156 | const char *n; |
1157 | ||
f3a2ddc1 VK |
1158 | len = len <= 0 ? af_byte_len(af) : len; |
1159 | ||
aba5acdf SH |
1160 | if (len > 0 && |
1161 | (n = resolve_address(addr, len, af)) != NULL) | |
1162 | return n; | |
1163 | } | |
1164 | #endif | |
2e96d2cc | 1165 | return rt_addr_n2a_r(af, len, addr, buf, buflen); |
aba5acdf SH |
1166 | } |
1167 | ||
a418e451 PS |
1168 | const char *format_host(int af, int len, const void *addr) |
1169 | { | |
1170 | static char buf[256]; | |
1171 | ||
1172 | return format_host_r(af, len, addr, buf, 256); | |
1173 | } | |
1174 | ||
aba5acdf | 1175 | |
f332d169 | 1176 | char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen) |
aba5acdf | 1177 | { |
f332d169 | 1178 | char *ptr = buf; |
aba5acdf SH |
1179 | int i; |
1180 | ||
e1933b92 | 1181 | for (i = 0; i < len; i++) { |
aba5acdf SH |
1182 | if (blen < 3) |
1183 | break; | |
1184 | sprintf(ptr, "%02x", str[i]); | |
1185 | ptr += 2; | |
1186 | blen -= 2; | |
aba5acdf SH |
1187 | } |
1188 | return buf; | |
1189 | } | |
1190 | ||
89ae5020 | 1191 | __u8 *hexstring_a2n(const char *str, __u8 *buf, int blen, unsigned int *len) |
aba5acdf | 1192 | { |
89ae5020 | 1193 | unsigned int cnt = 0; |
316c2346 | 1194 | char *endptr; |
aba5acdf | 1195 | |
316c2346 JP |
1196 | if (strlen(str) % 2) |
1197 | return NULL; | |
1198 | while (cnt < blen && strlen(str) > 1) { | |
1199 | unsigned int tmp; | |
1200 | char tmpstr[3]; | |
1201 | ||
1202 | strncpy(tmpstr, str, 2); | |
1203 | tmpstr[2] = '\0'; | |
9ba4126d | 1204 | errno = 0; |
316c2346 JP |
1205 | tmp = strtoul(tmpstr, &endptr, 16); |
1206 | if (errno != 0 || tmp > 0xFF || *endptr != '\0') | |
aba5acdf | 1207 | return NULL; |
316c2346 JP |
1208 | buf[cnt++] = tmp; |
1209 | str += 2; | |
aba5acdf | 1210 | } |
89ae5020 SD |
1211 | |
1212 | if (len) | |
1213 | *len = cnt; | |
1214 | ||
aba5acdf SH |
1215 | return buf; |
1216 | } | |
90f93024 | 1217 | |
1c570c50 JHS |
1218 | int hex2mem(const char *buf, uint8_t *mem, int count) |
1219 | { | |
1220 | int i, j; | |
1221 | int c; | |
1222 | ||
1223 | for (i = 0, j = 0; i < count; i++, j += 2) { | |
1224 | c = get_hex(buf[j]); | |
1225 | if (c < 0) | |
2bf1a81a | 1226 | return -1; |
1c570c50 JHS |
1227 | |
1228 | mem[i] = c << 4; | |
1229 | ||
1230 | c = get_hex(buf[j + 1]); | |
1231 | if (c < 0) | |
2bf1a81a | 1232 | return -1; |
1c570c50 JHS |
1233 | |
1234 | mem[i] |= c; | |
1235 | } | |
1236 | ||
1237 | return 0; | |
1c570c50 JHS |
1238 | } |
1239 | ||
5866bddd TH |
1240 | int addr64_n2a(__u64 addr, char *buff, size_t len) |
1241 | { | |
1242 | __u16 *words = (__u16 *)&addr; | |
1243 | __u16 v; | |
1244 | int i, ret; | |
1245 | size_t written = 0; | |
1246 | char *sep = ":"; | |
1247 | ||
1248 | for (i = 0; i < 4; i++) { | |
1249 | v = ntohs(words[i]); | |
1250 | ||
1251 | if (i == 3) | |
1252 | sep = ""; | |
1253 | ||
1254 | ret = snprintf(&buff[written], len - written, "%x%s", v, sep); | |
1255 | if (ret < 0) | |
1256 | return ret; | |
1257 | ||
1258 | written += ret; | |
1259 | } | |
1260 | ||
1261 | return written; | |
1262 | } | |
1263 | ||
7c72df5a ID |
1264 | /* Print buffer and escape bytes that are !isprint or among 'escape' */ |
1265 | void print_escape_buf(const __u8 *buf, size_t len, const char *escape) | |
1266 | { | |
1267 | size_t i; | |
1268 | ||
1269 | for (i = 0; i < len; ++i) { | |
1270 | if (isprint(buf[i]) && buf[i] != '\\' && | |
1271 | !strchr(escape, buf[i])) | |
1272 | printf("%c", buf[i]); | |
1273 | else | |
1274 | printf("\\%03o", buf[i]); | |
1275 | } | |
1276 | } | |
1277 | ||
90f93024 SH |
1278 | int print_timestamp(FILE *fp) |
1279 | { | |
1280 | struct timeval tv; | |
79aa79d0 | 1281 | struct tm *tm; |
90f93024 | 1282 | |
90f93024 | 1283 | gettimeofday(&tv, NULL); |
79aa79d0 VK |
1284 | tm = localtime(&tv.tv_sec); |
1285 | ||
1286 | if (timestamp_short) { | |
1287 | char tshort[40]; | |
1288 | ||
1289 | strftime(tshort, sizeof(tshort), "%Y-%m-%dT%H:%M:%S", tm); | |
1290 | fprintf(fp, "[%s.%06ld] ", tshort, tv.tv_usec); | |
1291 | } else { | |
1292 | char *tstr = asctime(tm); | |
1293 | ||
1294 | tstr[strlen(tstr)-1] = 0; | |
1295 | fprintf(fp, "Timestamp: %s %ld usec\n", | |
1296 | tstr, tv.tv_usec); | |
1297 | } | |
90f93024 | 1298 | |
90f93024 SH |
1299 | return 0; |
1300 | } | |
351efcde | 1301 | |
4328b687 | 1302 | unsigned int print_name_and_link(const char *fmt, |
f5b50a18 SP |
1303 | const char *name, struct rtattr *tb[]) |
1304 | { | |
1305 | const char *link = NULL; | |
1306 | unsigned int m_flag = 0; | |
1307 | SPRINT_BUF(b1); | |
1308 | ||
1309 | if (tb[IFLA_LINK]) { | |
1310 | int iflink = rta_getattr_u32(tb[IFLA_LINK]); | |
1311 | ||
1312 | if (iflink) { | |
1313 | if (tb[IFLA_LINK_NETNSID]) { | |
1314 | if (is_json_context()) { | |
1315 | print_int(PRINT_JSON, | |
1316 | "link_index", NULL, iflink); | |
1317 | } else { | |
1318 | link = ll_idx_n2a(iflink); | |
1319 | } | |
1320 | } else { | |
1321 | link = ll_index_to_name(iflink); | |
1322 | ||
1323 | if (is_json_context()) { | |
1324 | print_string(PRINT_JSON, | |
1325 | "link", NULL, link); | |
1326 | link = NULL; | |
1327 | } | |
1328 | ||
1329 | m_flag = ll_index_to_flags(iflink); | |
1330 | m_flag = !(m_flag & IFF_UP); | |
1331 | } | |
1332 | } else { | |
1333 | if (is_json_context()) | |
1334 | print_null(PRINT_JSON, "link", NULL, NULL); | |
1335 | else | |
1336 | link = "NONE"; | |
1337 | } | |
1338 | ||
1339 | if (link) { | |
1340 | snprintf(b1, sizeof(b1), "%s@%s", name, link); | |
1341 | name = b1; | |
1342 | } | |
1343 | } | |
1344 | ||
4328b687 | 1345 | print_color_string(PRINT_ANY, COLOR_IFNAME, "ifname", fmt, name); |
f5b50a18 SP |
1346 | |
1347 | return m_flag; | |
1348 | } | |
1349 | ||
351efcde SH |
1350 | int cmdlineno; |
1351 | ||
1352 | /* Like glibc getline but handle continuation lines and comments */ | |
d21e8835 | 1353 | ssize_t getcmdline(char **linep, size_t *lenp, FILE *in) |
351efcde | 1354 | { |
d21e8835 | 1355 | ssize_t cc; |
351efcde | 1356 | char *cp; |
ae665a52 | 1357 | |
e1933b92 SH |
1358 | cc = getline(linep, lenp, in); |
1359 | if (cc < 0) | |
351efcde SH |
1360 | return cc; /* eof or error */ |
1361 | ++cmdlineno; | |
1362 | ||
1363 | cp = strchr(*linep, '#'); | |
ae665a52 | 1364 | if (cp) |
351efcde | 1365 | *cp = '\0'; |
ae665a52 | 1366 | |
351efcde SH |
1367 | while ((cp = strstr(*linep, "\\\n")) != NULL) { |
1368 | char *line1 = NULL; | |
1369 | size_t len1 = 0; | |
21a85d3b | 1370 | ssize_t cc1; |
351efcde | 1371 | |
e1933b92 SH |
1372 | cc1 = getline(&line1, &len1, in); |
1373 | if (cc1 < 0) { | |
351efcde SH |
1374 | fprintf(stderr, "Missing continuation line\n"); |
1375 | return cc1; | |
1376 | } | |
1377 | ||
1378 | ++cmdlineno; | |
1379 | *cp = 0; | |
1380 | ||
1381 | cp = strchr(line1, '#'); | |
ae665a52 | 1382 | if (cp) |
351efcde SH |
1383 | *cp = '\0'; |
1384 | ||
d21e8835 AH |
1385 | *lenp = strlen(*linep) + strlen(line1) + 1; |
1386 | *linep = realloc(*linep, *lenp); | |
351efcde SH |
1387 | if (!*linep) { |
1388 | fprintf(stderr, "Out of memory\n"); | |
d21e8835 | 1389 | *lenp = 0; |
351efcde SH |
1390 | return -1; |
1391 | } | |
1392 | cc += cc1 - 2; | |
1393 | strcat(*linep, line1); | |
1394 | free(line1); | |
1395 | } | |
1396 | return cc; | |
1397 | } | |
1398 | ||
1399 | /* split command line into argument vector */ | |
1400 | int makeargs(char *line, char *argv[], int maxargs) | |
1401 | { | |
1402 | static const char ws[] = " \t\r\n"; | |
bd9cea5d | 1403 | char *cp = line; |
351efcde SH |
1404 | int argc = 0; |
1405 | ||
bd9cea5d SH |
1406 | while (*cp) { |
1407 | /* skip leading whitespace */ | |
1408 | cp += strspn(cp, ws); | |
1409 | ||
1410 | if (*cp == '\0') | |
1411 | break; | |
1412 | ||
351efcde SH |
1413 | if (argc >= (maxargs - 1)) { |
1414 | fprintf(stderr, "Too many arguments to command\n"); | |
1415 | exit(1); | |
1416 | } | |
39e3d383 CG |
1417 | |
1418 | /* word begins with quote */ | |
1419 | if (*cp == '\'' || *cp == '"') { | |
1420 | char quote = *cp++; | |
1421 | ||
1422 | argv[argc++] = cp; | |
1423 | /* find ending quote */ | |
1424 | cp = strchr(cp, quote); | |
1425 | if (cp == NULL) { | |
1426 | fprintf(stderr, "Unterminated quoted string\n"); | |
1427 | exit(1); | |
1428 | } | |
bd9cea5d SH |
1429 | } else { |
1430 | argv[argc++] = cp; | |
1431 | ||
1432 | /* find end of word */ | |
1433 | cp += strcspn(cp, ws); | |
1434 | if (*cp == '\0') | |
1435 | break; | |
39e3d383 CG |
1436 | } |
1437 | ||
bd9cea5d | 1438 | /* seperate words */ |
39e3d383 | 1439 | *cp++ = 0; |
351efcde SH |
1440 | } |
1441 | argv[argc] = NULL; | |
1442 | ||
1443 | return argc; | |
1444 | } | |
aa574cd6 | 1445 | |
ddb1129b VK |
1446 | void print_nlmsg_timestamp(FILE *fp, const struct nlmsghdr *n) |
1447 | { | |
1448 | char *tstr; | |
e1933b92 SH |
1449 | time_t secs = ((__u32 *)NLMSG_DATA(n))[0]; |
1450 | long usecs = ((__u32 *)NLMSG_DATA(n))[1]; | |
1451 | ||
ddb1129b VK |
1452 | tstr = asctime(localtime(&secs)); |
1453 | tstr[strlen(tstr)-1] = 0; | |
1454 | fprintf(fp, "Timestamp: %s %lu us\n", tstr, usecs); | |
1455 | } | |
e998e118 VK |
1456 | |
1457 | static int on_netns(char *nsname, void *arg) | |
1458 | { | |
1459 | struct netns_func *f = arg; | |
1460 | ||
1461 | if (netns_switch(nsname)) | |
1462 | return -1; | |
1463 | ||
1464 | return f->func(nsname, f->arg); | |
1465 | } | |
1466 | ||
1467 | static int on_netns_label(char *nsname, void *arg) | |
1468 | { | |
1469 | printf("\nnetns: %s\n", nsname); | |
1470 | return on_netns(nsname, arg); | |
1471 | } | |
1472 | ||
1473 | int do_each_netns(int (*func)(char *nsname, void *arg), void *arg, | |
1474 | bool show_label) | |
1475 | { | |
1476 | struct netns_func nsf = { .func = func, .arg = arg }; | |
1477 | ||
1478 | if (show_label) | |
1479 | return netns_foreach(on_netns_label, &nsf); | |
1480 | ||
1481 | return netns_foreach(on_netns, &nsf); | |
1482 | } | |
b217df10 VK |
1483 | |
1484 | char *int_to_str(int val, char *buf) | |
1485 | { | |
1486 | sprintf(buf, "%d", val); | |
1487 | return buf; | |
1488 | } | |
d91fb3f4 EC |
1489 | |
1490 | int get_guid(__u64 *guid, const char *arg) | |
1491 | { | |
1492 | unsigned long int tmp; | |
1493 | char *endptr; | |
1494 | int i; | |
1495 | ||
1496 | #define GUID_STR_LEN 23 | |
1497 | /* Verify strict format: format string must be | |
1498 | * xx:xx:xx:xx:xx:xx:xx:xx where xx can be an arbitrary | |
1499 | * hex digit | |
1500 | */ | |
1501 | ||
1502 | if (strlen(arg) != GUID_STR_LEN) | |
1503 | return -1; | |
1504 | ||
1505 | /* make sure columns are in place */ | |
1506 | for (i = 0; i < 7; i++) | |
1507 | if (arg[2 + i * 3] != ':') | |
1508 | return -1; | |
1509 | ||
1510 | *guid = 0; | |
1511 | for (i = 0; i < 8; i++) { | |
1512 | tmp = strtoul(arg + i * 3, &endptr, 16); | |
1513 | if (endptr != arg + i * 3 + 2) | |
1514 | return -1; | |
1515 | ||
1516 | if (tmp > 255) | |
1517 | return -1; | |
1518 | ||
1519 | *guid |= tmp << (56 - 8 * i); | |
1520 | } | |
1521 | ||
1522 | return 0; | |
1523 | } | |
56e3eb4c NA |
1524 | |
1525 | /* This is a necessary workaround for multicast route dumps */ | |
1526 | int get_real_family(int rtm_type, int rtm_family) | |
1527 | { | |
1528 | if (rtm_type != RTN_MULTICAST) | |
1529 | return rtm_family; | |
1530 | ||
5ce897a0 HL |
1531 | if (rtm_family == RTNL_FAMILY_IPMR) |
1532 | return AF_INET; | |
1533 | ||
1534 | if (rtm_family == RTNL_FAMILY_IP6MR) | |
1535 | return AF_INET6; | |
1536 | ||
1537 | return rtm_family; | |
56e3eb4c | 1538 | } |
8d15e012 | 1539 | |
9a7bd544 SP |
1540 | /* Based on copy_rtnl_link_stats() from kernel at net/core/rtnetlink.c */ |
1541 | static void copy_rtnl_link_stats64(struct rtnl_link_stats64 *stats64, | |
1542 | const struct rtnl_link_stats *stats) | |
1543 | { | |
1544 | __u64 *a = (__u64 *)stats64; | |
1545 | const __u32 *b = (const __u32 *)stats; | |
1546 | const __u32 *e = b + sizeof(*stats) / sizeof(*b); | |
1547 | ||
1548 | while (b < e) | |
1549 | *a++ = *b++; | |
1550 | } | |
1551 | ||
1552 | int get_rtnl_link_stats_rta(struct rtnl_link_stats64 *stats64, | |
1553 | struct rtattr *tb[]) | |
1554 | { | |
1555 | struct rtnl_link_stats stats; | |
1556 | void *s; | |
1557 | struct rtattr *rta; | |
1558 | int size, len; | |
1559 | ||
1560 | if (tb[IFLA_STATS64]) { | |
1561 | rta = tb[IFLA_STATS64]; | |
1562 | size = sizeof(struct rtnl_link_stats64); | |
1563 | s = stats64; | |
1564 | } else if (tb[IFLA_STATS]) { | |
1565 | rta = tb[IFLA_STATS]; | |
1566 | size = sizeof(struct rtnl_link_stats); | |
1567 | s = &stats; | |
1568 | } else { | |
1569 | return -1; | |
1570 | } | |
1571 | ||
1572 | len = RTA_PAYLOAD(rta); | |
1573 | if (len < size) | |
1574 | memset(s + len, 0, size - len); | |
1575 | else | |
1576 | len = size; | |
1577 | ||
1578 | memcpy(s, RTA_DATA(rta), len); | |
1579 | ||
1580 | if (s != stats64) | |
1581 | copy_rtnl_link_stats64(stats64, s); | |
1582 | return size; | |
1583 | } | |
1584 | ||
4f6b7338 | 1585 | #ifdef NEED_STRLCPY |
8d15e012 PS |
1586 | size_t strlcpy(char *dst, const char *src, size_t size) |
1587 | { | |
50ea3c64 PS |
1588 | size_t srclen = strlen(src); |
1589 | ||
8d15e012 | 1590 | if (size) { |
50ea3c64 PS |
1591 | size_t minlen = min(srclen, size - 1); |
1592 | ||
1593 | memcpy(dst, src, minlen); | |
1594 | dst[minlen] = '\0'; | |
8d15e012 | 1595 | } |
50ea3c64 | 1596 | return srclen; |
8d15e012 PS |
1597 | } |
1598 | ||
1599 | size_t strlcat(char *dst, const char *src, size_t size) | |
1600 | { | |
1601 | size_t dlen = strlen(dst); | |
1602 | ||
50ea3c64 | 1603 | if (dlen >= size) |
8d15e012 PS |
1604 | return dlen + strlen(src); |
1605 | ||
1606 | return dlen + strlcpy(dst + dlen, src, size - dlen); | |
1607 | } | |
4f6b7338 | 1608 | #endif |
ba2fc55b LB |
1609 | |
1610 | void drop_cap(void) | |
1611 | { | |
1612 | #ifdef HAVE_LIBCAP | |
1613 | /* don't harmstring root/sudo */ | |
1614 | if (getuid() != 0 && geteuid() != 0) { | |
1615 | cap_t capabilities; | |
9b13cc98 LB |
1616 | cap_value_t net_admin = CAP_NET_ADMIN; |
1617 | cap_flag_t inheritable = CAP_INHERITABLE; | |
1618 | cap_flag_value_t is_set; | |
ba2fc55b LB |
1619 | |
1620 | capabilities = cap_get_proc(); | |
1621 | if (!capabilities) | |
1622 | exit(EXIT_FAILURE); | |
9b13cc98 LB |
1623 | if (cap_get_flag(capabilities, net_admin, inheritable, |
1624 | &is_set) != 0) | |
ba2fc55b | 1625 | exit(EXIT_FAILURE); |
9b13cc98 LB |
1626 | /* apps with ambient caps can fork and call ip */ |
1627 | if (is_set == CAP_CLEAR) { | |
1628 | if (cap_clear(capabilities) != 0) | |
1629 | exit(EXIT_FAILURE); | |
1630 | if (cap_set_proc(capabilities) != 0) | |
1631 | exit(EXIT_FAILURE); | |
1632 | } | |
ba2fc55b LB |
1633 | cap_free(capabilities); |
1634 | } | |
1635 | #endif | |
1636 | } | |
abf70ef4 DT |
1637 | |
1638 | int get_time(unsigned int *time, const char *str) | |
1639 | { | |
1640 | double t; | |
1641 | char *p; | |
1642 | ||
1643 | t = strtod(str, &p); | |
1644 | if (p == str) | |
1645 | return -1; | |
1646 | ||
1647 | if (*p) { | |
1648 | if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec") == 0 || | |
1649 | strcasecmp(p, "secs") == 0) | |
1650 | t *= TIME_UNITS_PER_SEC; | |
1651 | else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec") == 0 || | |
1652 | strcasecmp(p, "msecs") == 0) | |
1653 | t *= TIME_UNITS_PER_SEC/1000; | |
1654 | else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec") == 0 || | |
1655 | strcasecmp(p, "usecs") == 0) | |
1656 | t *= TIME_UNITS_PER_SEC/1000000; | |
1657 | else | |
1658 | return -1; | |
1659 | } | |
1660 | ||
1661 | *time = t; | |
1662 | return 0; | |
1663 | } | |
1664 | ||
1665 | ||
1666 | void print_time(char *buf, int len, __u32 time) | |
1667 | { | |
1668 | double tmp = time; | |
1669 | ||
1670 | if (tmp >= TIME_UNITS_PER_SEC) | |
1671 | snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC); | |
1672 | else if (tmp >= TIME_UNITS_PER_SEC/1000) | |
1673 | snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000)); | |
1674 | else | |
1675 | snprintf(buf, len, "%uus", time); | |
1676 | } | |
1677 | ||
1678 | char *sprint_time(__u32 time, char *buf) | |
1679 | { | |
1680 | print_time(buf, SPRINT_BSIZE-1, time); | |
1681 | return buf; | |
1682 | } | |
1683 | ||
1684 | /* 64 bit times are represented internally in nanoseconds */ | |
1685 | int get_time64(__s64 *time, const char *str) | |
1686 | { | |
1687 | double nsec; | |
1688 | char *p; | |
1689 | ||
1690 | nsec = strtod(str, &p); | |
1691 | if (p == str) | |
1692 | return -1; | |
1693 | ||
1694 | if (*p) { | |
1695 | if (strcasecmp(p, "s") == 0 || | |
1696 | strcasecmp(p, "sec") == 0 || | |
1697 | strcasecmp(p, "secs") == 0) | |
1698 | nsec *= NSEC_PER_SEC; | |
1699 | else if (strcasecmp(p, "ms") == 0 || | |
1700 | strcasecmp(p, "msec") == 0 || | |
1701 | strcasecmp(p, "msecs") == 0) | |
1702 | nsec *= NSEC_PER_MSEC; | |
1703 | else if (strcasecmp(p, "us") == 0 || | |
1704 | strcasecmp(p, "usec") == 0 || | |
1705 | strcasecmp(p, "usecs") == 0) | |
1706 | nsec *= NSEC_PER_USEC; | |
1707 | else if (strcasecmp(p, "ns") == 0 || | |
1708 | strcasecmp(p, "nsec") == 0 || | |
1709 | strcasecmp(p, "nsecs") == 0) | |
1710 | nsec *= 1; | |
1711 | else | |
1712 | return -1; | |
1713 | } | |
1714 | ||
1715 | *time = nsec; | |
1716 | return 0; | |
1717 | } | |
1718 | ||
1719 | void print_time64(char *buf, int len, __s64 time) | |
1720 | { | |
1721 | double nsec = time; | |
1722 | ||
1723 | if (time >= NSEC_PER_SEC) | |
1724 | snprintf(buf, len, "%.3fs", nsec/NSEC_PER_SEC); | |
1725 | else if (time >= NSEC_PER_MSEC) | |
1726 | snprintf(buf, len, "%.3fms", nsec/NSEC_PER_MSEC); | |
1727 | else if (time >= NSEC_PER_USEC) | |
1728 | snprintf(buf, len, "%.3fus", nsec/NSEC_PER_USEC); | |
1729 | else | |
1730 | snprintf(buf, len, "%lldns", time); | |
1731 | } | |
1732 | ||
1733 | char *sprint_time64(__s64 time, char *buf) | |
1734 | { | |
1735 | print_time64(buf, SPRINT_BSIZE-1, time); | |
1736 | return buf; | |
1737 | } |