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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <asm/types.h>
26 #include <linux/pkt_sched.h>
27 #include <linux/param.h>
35 int get_integer(int *val
, const char *arg
, int base
)
43 res
= strtol(arg
, &ptr
, base
);
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.
50 if (!ptr
|| ptr
== arg
|| *ptr
)
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.
57 if ((res
== LONG_MAX
|| res
== LONG_MIN
) && errno
== ERANGE
)
60 /* Outside range of int */
61 if (res
< INT_MIN
|| res
> INT_MAX
)
68 int mask2bits(__u32 netmask
)
71 __u32 mask
= ntohl(netmask
);
74 /* a valid netmask must be 2^n - 1 */
75 if ((host
& (host
+ 1)) != 0)
78 for (; mask
; mask
<<= 1)
83 static int get_netmask(unsigned *val
, const char *arg
, int base
)
87 if (!get_unsigned(val
, arg
, base
))
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]);
103 int get_unsigned(unsigned *val
, const char *arg
, int base
)
111 res
= strtoul(arg
, &ptr
, base
);
113 /* empty string or trailing non-digits */
114 if (!ptr
|| ptr
== arg
|| *ptr
)
118 if (res
== ULONG_MAX
&& errno
== ERANGE
)
121 /* out side range of unsigned */
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.
136 int get_time_rtt(unsigned *val
, const char *arg
, int *raw
)
142 if (strchr(arg
, '.') != NULL
) {
147 /* extra non-digits */
148 if (!p
|| p
== arg
|| *p
)
152 if ((t
== HUGE_VALF
|| t
== HUGE_VALL
) && errno
== ERANGE
)
155 res
= strtoul(arg
, &p
, 0);
157 /* empty string or trailing non-digits */
158 if (!p
|| p
== arg
|| *p
)
162 if (res
== ULONG_MAX
&& errno
== ERANGE
)
174 if (strcasecmp(p
, "s") == 0 || strcasecmp(p
, "sec")==0 ||
175 strcasecmp(p
, "secs")==0)
177 else if (strcasecmp(p
, "ms") == 0 || strcasecmp(p
, "msec")==0 ||
178 strcasecmp(p
, "msecs") == 0)
179 t
*= 1.0; /* allow suffix, do nothing */
184 /* emulate ceil() without having to bring-in -lm and always be >= 1 */
194 int get_u64(__u64
*val
, const char *arg
, int base
)
196 unsigned long long res
;
202 res
= strtoull(arg
, &ptr
, base
);
204 /* empty string or trailing non-digits */
205 if (!ptr
|| ptr
== arg
|| *ptr
)
209 if (res
== ULLONG_MAX
&& errno
== ERANGE
)
212 /* in case ULL is 128 bits */
213 if (res
> 0xFFFFFFFFFFFFFFFFULL
)
220 int get_u32(__u32
*val
, const char *arg
, int base
)
227 res
= strtoul(arg
, &ptr
, base
);
229 /* empty string or trailing non-digits */
230 if (!ptr
|| ptr
== arg
|| *ptr
)
234 if (res
== ULONG_MAX
&& errno
== ERANGE
)
237 /* in case UL > 32 bits */
238 if (res
> 0xFFFFFFFFUL
)
245 int get_u16(__u16
*val
, const char *arg
, int base
)
252 res
= strtoul(arg
, &ptr
, base
);
254 /* empty string or trailing non-digits */
255 if (!ptr
|| ptr
== arg
|| *ptr
)
259 if (res
== ULONG_MAX
&& errno
== ERANGE
)
269 int get_u8(__u8
*val
, const char *arg
, int base
)
277 res
= strtoul(arg
, &ptr
, base
);
278 /* empty string or trailing non-digits */
279 if (!ptr
|| ptr
== arg
|| *ptr
)
283 if (res
== ULONG_MAX
&& errno
== ERANGE
)
293 int get_s32(__s32
*val
, const char *arg
, int base
)
302 res
= strtol(arg
, &ptr
, base
);
303 if (!ptr
|| ptr
== arg
|| *ptr
)
305 if ((res
== LONG_MIN
|| res
== LONG_MAX
) && errno
== ERANGE
)
307 if (res
> INT32_MAX
|| res
< INT32_MIN
)
314 int get_s16(__s16
*val
, const char *arg
, int base
)
321 res
= strtol(arg
, &ptr
, base
);
322 if (!ptr
|| ptr
== arg
|| *ptr
)
324 if ((res
== LONG_MIN
|| res
== LONG_MAX
) && errno
== ERANGE
)
326 if (res
> 0x7FFF || res
< -0x8000)
333 int get_s8(__s8
*val
, const char *arg
, int base
)
340 res
= strtol(arg
, &ptr
, base
);
341 if (!ptr
|| ptr
== arg
|| *ptr
)
343 if ((res
== LONG_MIN
|| res
== LONG_MAX
) && errno
== ERANGE
)
345 if (res
> 0x7F || res
< -0x80)
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
354 static int get_addr_ipv4(__u8
*ap
, const char *cp
)
358 for (i
= 0; i
< 4; i
++) {
362 n
= strtoul(cp
, &endp
, 0);
364 return -1; /* bogus network value */
366 if (endp
== cp
) /* no digits */
374 if (i
== 3 || *endp
!= '.')
375 return -1; /* extra characters */
382 int get_addr_1(inet_prefix
*addr
, const char *name
, int family
)
384 memset(addr
, 0, sizeof(*addr
));
386 if (strcmp(name
, "default") == 0 ||
387 strcmp(name
, "all") == 0 ||
388 strcmp(name
, "any") == 0) {
389 if (family
== AF_DECnet
)
391 addr
->family
= family
;
392 addr
->bytelen
= (family
== AF_INET6
? 16 : 4);
397 if (strchr(name
, ':')) {
398 addr
->family
= AF_INET6
;
399 if (family
!= AF_UNSPEC
&& family
!= AF_INET6
)
401 if (inet_pton(AF_INET6
, name
, addr
->data
) <= 0)
408 if (family
== AF_DECnet
) {
410 addr
->family
= AF_DECnet
;
411 if (dnet_pton(AF_DECnet
, name
, &dna
) <= 0)
413 memcpy(addr
->data
, dna
.a_addr
, 2);
419 addr
->family
= AF_INET
;
420 if (family
!= AF_UNSPEC
&& family
!= AF_INET
)
423 if (get_addr_ipv4((__u8
*)addr
->data
, name
) <= 0)
431 int get_prefix_1(inet_prefix
*dst
, char *arg
, int family
)
437 memset(dst
, 0, sizeof(*dst
));
439 if (strcmp(arg
, "default") == 0 ||
440 strcmp(arg
, "any") == 0 ||
441 strcmp(arg
, "all") == 0) {
442 if (family
== AF_DECnet
)
444 dst
->family
= family
;
450 slash
= strchr(arg
, '/');
454 err
= get_addr_1(dst
, arg
, family
);
456 switch(dst
->family
) {
468 if (get_netmask(&plen
, slash
+1, 0)
469 || plen
> dst
->bitlen
) {
473 dst
->flags
|= PREFIXLEN_SPECIFIED
;
483 int get_addr(inet_prefix
*dst
, const char *arg
, int family
)
485 if (family
== AF_PACKET
) {
486 fprintf(stderr
, "Error: \"%s\" may be inet address, but it is not allowed in this context.\n", arg
);
489 if (get_addr_1(dst
, arg
, family
)) {
490 fprintf(stderr
, "Error: an inet address is expected rather than \"%s\".\n", arg
);
496 int get_prefix(inet_prefix
*dst
, char *arg
, int family
)
498 if (family
== AF_PACKET
) {
499 fprintf(stderr
, "Error: \"%s\" may be inet prefix, but it is not allowed in this context.\n", arg
);
502 if (get_prefix_1(dst
, arg
, family
)) {
503 fprintf(stderr
, "Error: an inet prefix is expected rather than \"%s\".\n", arg
);
509 __u32
get_addr32(const char *name
)
512 if (get_addr_1(&addr
, name
, AF_INET
)) {
513 fprintf(stderr
, "Error: an IP address is expected rather than \"%s\"\n", name
);
519 void incomplete_command(void)
521 fprintf(stderr
, "Command line is not complete. Try option \"help\"\n");
525 void missarg(const char *key
)
527 fprintf(stderr
, "Error: argument \"%s\" is required\n", key
);
531 void invarg(const char *msg
, const char *arg
)
533 fprintf(stderr
, "Error: argument \"%s\" is wrong: %s\n", arg
, msg
);
537 void duparg(const char *key
, const char *arg
)
539 fprintf(stderr
, "Error: duplicate \"%s\": \"%s\" is the second value.\n", key
, arg
);
543 void duparg2(const char *key
, const char *arg
)
545 fprintf(stderr
, "Error: either \"%s\" is duplicate, or \"%s\" is a garbage.\n", key
, arg
);
549 int matches(const char *cmd
, const char *pattern
)
551 int len
= strlen(cmd
);
552 if (len
> strlen(pattern
))
554 return memcmp(pattern
, cmd
, len
);
557 int inet_addr_match(const inet_prefix
*a
, const inet_prefix
*b
, int bits
)
559 const __u32
*a1
= a
->data
;
560 const __u32
*a2
= b
->data
;
561 int words
= bits
>> 0x05;
566 if (memcmp(a1
, a2
, words
<< 2))
576 mask
= htonl((0xffffffff) << (0x20 - bits
));
578 if ((w1
^ w2
) & mask
)
585 int __iproute2_hz_internal
;
594 return atoi(getenv("HZ")) ? : HZ
;
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"));
601 strcpy(name
, "/proc/net/psched");
603 fp
= fopen(name
, "r");
607 if (fscanf(fp
, "%*08x%*08x%08x%08x", &nom
, &denom
) == 2)
617 int __iproute2_user_hz_internal
;
619 int __get_user_hz(void)
621 return sysconf(_SC_CLK_TCK
);
624 const char *rt_addr_n2a(int af
, int len
, const void *addr
, char *buf
, int buflen
)
629 return inet_ntop(af
, addr
, buf
, buflen
);
631 return ipx_ntop(af
, addr
, buf
, buflen
);
634 struct dn_naddr dna
= { 2, { 0, 0, }};
635 memcpy(dna
.a_addr
, addr
, 2);
636 return dnet_ntop(af
, &dna
, buf
, buflen
);
643 #ifdef RESOLVE_HOSTNAMES
646 struct namerec
*next
;
652 static struct namerec
*nht
[NHASH
];
654 static const char *resolve_address(const void *addr
, int len
, int af
)
657 struct hostent
*h_ent
;
662 if (af
== AF_INET6
&& ((__u32
*)addr
)[0] == 0 &&
663 ((__u32
*)addr
)[1] == 0 && ((__u32
*)addr
)[2] == htonl(0xffff)) {
669 hash
= *(__u32
*)(addr
+ len
- 4) % NHASH
;
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)
677 if ((n
= malloc(sizeof(*n
))) == NULL
)
680 n
->addr
.bytelen
= len
;
682 memcpy(n
->addr
.data
, addr
, len
);
689 if ((h_ent
= gethostbyaddr(addr
, len
, af
)) != NULL
)
690 n
->name
= strdup(h_ent
->h_name
);
692 /* Even if we fail, "negative" entry is remembered. */
698 const char *format_host(int af
, int len
, const void *addr
,
699 char *buf
, int buflen
)
701 #ifdef RESOLVE_HOSTNAMES
717 /* I see no reasons why gethostbyname
718 may not work for DECnet */
727 (n
= resolve_address(addr
, len
, af
)) != NULL
)
731 return rt_addr_n2a(af
, len
, addr
, buf
, buflen
);
735 char *hexstring_n2a(const __u8
*str
, int len
, char *buf
, int blen
)
740 for (i
=0; i
<len
; i
++) {
743 sprintf(ptr
, "%02x", str
[i
]);
750 __u8
* hexstring_a2n(const char *str
, __u8
*buf
, int blen
)
757 while (cnt
< blen
&& strlen(str
) > 1) {
761 strncpy(tmpstr
, str
, 2);
763 tmp
= strtoul(tmpstr
, &endptr
, 16);
764 if (errno
!= 0 || tmp
> 0xFF || *endptr
!= '\0')
772 int print_timestamp(FILE *fp
)
777 memset(&tv
, 0, sizeof(tv
));
778 gettimeofday(&tv
, NULL
);
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
);
788 /* Like glibc getline but handle continuation lines and comments */
789 ssize_t
getcmdline(char **linep
, size_t *lenp
, FILE *in
)
794 if ((cc
= getline(linep
, lenp
, in
)) < 0)
795 return cc
; /* eof or error */
798 cp
= strchr(*linep
, '#');
802 while ((cp
= strstr(*linep
, "\\\n")) != NULL
) {
807 if ((cc1
= getline(&line1
, &len1
, in
)) < 0) {
808 fprintf(stderr
, "Missing continuation line\n");
815 cp
= strchr(line1
, '#');
819 *lenp
= strlen(*linep
) + strlen(line1
) + 1;
820 *linep
= realloc(*linep
, *lenp
);
822 fprintf(stderr
, "Out of memory\n");
827 strcat(*linep
, line1
);
833 /* split command line into argument vector */
834 int makeargs(char *line
, char *argv
[], int maxargs
)
836 static const char ws
[] = " \t\r\n";
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");
852 int inet_get_addr(const char *src
, __u32
*dst
, struct in6_addr
*dst6
)
854 if (strchr(src
, ':'))
855 return inet_pton(AF_INET6
, src
, dst6
);
857 return inet_pton(AF_INET
, src
, dst
);