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