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