*
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
*
- *
- * Changes:
- *
- * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
*/
#include <stdio.h>
#include <linux/pkt_sched.h>
#include <time.h>
#include <sys/time.h>
+#include <errno.h>
#include "utils.h"
return 0;
}
+int mask2bits(__u32 netmask)
+{
+ unsigned bits = 0;
+ __u32 mask = ntohl(netmask);
+ __u32 host = ~mask;
+
+ /* a valid netmask must be 2^n - 1 */
+ if ((host & (host + 1)) != 0)
+ return -1;
+
+ for (; mask; mask <<= 1)
+ ++bits;
+ return bits;
+}
+
+static int get_netmask(unsigned *val, const char *arg, int base)
+{
+ inet_prefix addr;
+
+ if (!get_unsigned(val, arg, base))
+ return 0;
+
+ /* try coverting dotted quad to CIDR */
+ if (!get_addr_1(&addr, arg, AF_INET) && addr.family == AF_INET) {
+ int b = mask2bits(addr.data[0]);
+
+ if (b >= 0) {
+ *val = b;
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
int get_unsigned(unsigned *val, const char *arg, int base)
{
unsigned long res;
return 0;
}
+/*
+ * get_time_rtt is "translated" from a similar routine "get_time" in
+ * tc_util.c. We don't use the exact same routine because tc passes
+ * microseconds to the kernel and the callers of get_time_rtt want to
+ * pass milliseconds (standard unit for rtt values since 2.6.27), and
+ * have a different assumption for the units of a "raw" number.
+ */
+int get_time_rtt(unsigned *val, const char *arg, int *raw)
+{
+ double t;
+ unsigned long res;
+ char *p;
+
+ if (strchr(arg,'.') != NULL) {
+ t = strtod(arg,&p);
+ if (t < 0.0)
+ return -1;
+ }
+ else {
+ res = strtoul(arg, &p, 0);
+ if (res > UINT_MAX)
+ return -1;
+ t = (double)res;
+ }
+ if (p == arg)
+ return -1;
+ *raw = 1;
+
+ if (*p) {
+ *raw = 0;
+ if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
+ strcasecmp(p, "secs")==0)
+ t *= 1000;
+ else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
+ strcasecmp(p, "msecs") == 0)
+ t *= 1.0; /* allow suffix, do nothing */
+ else
+ return -1;
+ }
+
+ /* emulate ceil() without having to bring-in -lm and always be >= 1 */
+
+ *val = t;
+ if (*val < t)
+ *val += 1;
+
+ return 0;
+
+}
+
int get_u64(__u64 *val, const char *arg, int base)
{
unsigned long long res;
return 0;
}
+int get_s32(__s32 *val, const char *arg, int base)
+{
+ long res;
+ char *ptr;
+
+ errno = 0;
+
+ if (!arg || !*arg)
+ return -1;
+ res = strtol(arg, &ptr, base);
+ if (ptr == arg || *ptr ||
+ ((res == LONG_MIN || res == LONG_MAX) && errno == ERANGE) ||
+ res > INT32_MAX || res < INT32_MIN)
+ return -1;
+ *val = res;
+ return 0;
+}
+
int get_s16(__s16 *val, const char *arg, int base)
{
long res;
return 0;
}
-int get_addr_1(inet_prefix *addr, const char *name, int family)
+/* This uses a non-standard parsing (ie not inet_aton, or inet_pton)
+ * because of legacy choice to parse 10.8 as 10.8.0.0 not 10.0.0.8
+ */
+static int get_addr_ipv4(__u8 *ap, const char *cp)
{
- const char *cp;
- unsigned char *ap = (unsigned char*)addr->data;
int i;
+ for (i = 0; i < 4; i++) {
+ unsigned long n;
+ char *endp;
+
+ n = strtoul(cp, &endp, 0);
+ if (n > 255)
+ return -1; /* bogus network value */
+
+ if (endp == cp) /* no digits */
+ return -1;
+
+ ap[i] = n;
+
+ if (*endp == '\0')
+ break;
+
+ if (i == 3 || *endp != '.')
+ return -1; /* extra characters */
+ cp = endp + 1;
+ }
+
+ return 1;
+}
+
+int get_addr_1(inet_prefix *addr, const char *name, int family)
+{
memset(addr, 0, sizeof(*addr));
if (strcmp(name, "default") == 0 ||
addr->family = AF_INET;
if (family != AF_UNSPEC && family != AF_INET)
return -1;
+
+ if (get_addr_ipv4((__u8 *)addr->data, name) <= 0)
+ return -1;
+
addr->bytelen = 4;
addr->bitlen = -1;
- for (cp=name, i=0; *cp; cp++) {
- if (*cp <= '9' && *cp >= '0') {
- ap[i] = 10*ap[i] + (*cp-'0');
- continue;
- }
- if (*cp == '.' && ++i <= 3)
- continue;
- return -1;
- }
return 0;
}
dst->bitlen = 32;
}
if (slash) {
- if (get_unsigned(&plen, slash+1, 0) || plen > dst->bitlen) {
+ if (get_netmask(&plen, slash+1, 0)
+ || plen > dst->bitlen) {
err = -1;
goto done;
}
struct namerec
{
struct namerec *next;
+ const char *name;
inet_prefix addr;
- char *name;
};
-static struct namerec *nht[256];
+#define NHASH 257
+static struct namerec *nht[NHASH];
-char *resolve_address(const char *addr, int len, int af)
+static const char *resolve_address(const void *addr, int len, int af)
{
struct namerec *n;
struct hostent *h_ent;
len = 4;
}
- hash = addr[len-1] ^ addr[len-2] ^ addr[len-3] ^ addr[len-4];
+ hash = *(__u32 *)(addr + len - 4) % NHASH;
for (n = nht[hash]; n; n = n->next) {
if (n->addr.family == af &&
{
#ifdef RESOLVE_HOSTNAMES
if (resolve_hosts) {
- char *n;
+ const char *n;
+
if (len <= 0) {
switch (af) {
case AF_INET:
int cmdlineno;
/* Like glibc getline but handle continuation lines and comments */
-size_t getcmdline(char **linep, size_t *lenp, FILE *in)
+ssize_t getcmdline(char **linep, size_t *lenp, FILE *in)
{
- size_t cc;
+ ssize_t cc;
char *cp;
if ((cc = getline(linep, lenp, in)) < 0)
while ((cp = strstr(*linep, "\\\n")) != NULL) {
char *line1 = NULL;
size_t len1 = 0;
- size_t cc1;
+ ssize_t cc1;
if ((cc1 = getline(&line1, &len1, in)) < 0) {
fprintf(stderr, "Missing continuation line\n");
if (cp)
*cp = '\0';
- *linep = realloc(*linep, strlen(*linep) + strlen(line1) + 1);
+ *lenp = strlen(*linep) + strlen(line1) + 1;
+ *linep = realloc(*linep, *lenp);
if (!*linep) {
fprintf(stderr, "Out of memory\n");
+ *lenp = 0;
return -1;
}
cc += cc1 - 2;