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