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