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