Work on the radix code, added support to compile on OpenWRT,
[ipfw.git] / dummynet / radix.c
index 2a5fcc3..575c47c 100644 (file)
  * SUCH DAMAGE.
  *
  *     @(#)radix.c     8.5 (Berkeley) 5/19/95
- * $FreeBSD: head/sys/net/radix.c 186176 2008-12-16 11:01:36Z kmacy $
+ * $FreeBSD: head/sys/net/radix.c 200354 2009-12-10 10:34:30Z luigi $
  */
 
-#include "missing.h"
 /*
  * Routines to build and maintain radix trees for routing lookups.
  */
-#ifndef _RADIX_H_
 #include <sys/param.h>
 #ifdef _KERNEL
+#include <sys/cdefs.h>
+#include "missing.h"
 #include <sys/lock.h>
 #include <sys/mutex.h>
 #include <sys/rwlock.h>
 #include <sys/systm.h>
 #include <sys/malloc.h>
-// #include <sys/domain.h>
-#else
-#include <stdlib.h>
-#endif
 #include <sys/syslog.h>
 #include <net/radix.h>
-#endif
-
-// #include "opt_mpath.h"
-
+#include "opt_mpath.h"
 #ifdef RADIX_MPATH
 #include <net/radix_mpath.h>
 #endif
-
+#else /* !_KERNEL */
+#include <stdio.h>
+#include <strings.h>
+#include <stdlib.h>
+#define log(x, arg...) fprintf(stderr, ## arg)
+#define panic(x)       fprintf(stderr, "PANIC: %s", x), exit(1)
+#define min(a, b) ((a) < (b) ? (a) : (b) )
+#include "include/net/radix.h"
+#endif /* !_KERNEL */
 
 static int     rn_walktree_from(struct radix_node_head *h, void *a, void *m,
                    walktree_f_t *f, void *w);
@@ -73,6 +74,8 @@ static struct radix_node_head *mask_rnhead;
 /*
  * Work area -- the following point to 3 buffers of size max_keylen,
  * allocated in this order in a block of memory malloc'ed by rn_init.
+ * rn_zeros, rn_ones are set in rn_init and used in readonly afterwards.
+ * addmask_key is used in rn_addmask in rw mode and not thread-safe.
  */
 static char *rn_zeros, *rn_ones, *addmask_key;
 
@@ -136,8 +139,9 @@ static int  rn_satisfies_leaf(char *trial, struct radix_node *leaf,
  * To make the assumption more explicit, we use the LEN() macro to access
  * this field. It is safe to pass an expression with side effects
  * to LEN() as the argument is evaluated only once.
+ * We cast the result to int as this is the dominant usage.
  */
-#define LEN(x) (*(const u_char *)(x))
+#define LEN(x) ( (int) (*(const u_char *)(x)) )
 
 /*
  * XXX THIS NEEDS TO BE FIXED
@@ -198,7 +202,7 @@ rn_refines(m_arg, n_arg)
 {
        register caddr_t m = m_arg, n = n_arg;
        register caddr_t lim, lim2 = lim = n + LEN(n);
-       int longer = LEN(n++) - (int)LEN(m++);
+       int longer = LEN(n++) - LEN(m++);
        int masks_are_equal = 1;
 
        if (longer > 0)
@@ -251,10 +255,10 @@ rn_satisfies_leaf(trial, leaf, skip)
        char *cplim;
        int length = min(LEN(cp), LEN(cp2));
 
-       if (cp3 == 0)
+       if (cp3 == NULL)
                cp3 = rn_ones;
        else
-               length = min(length, (int)(*(u_char *)cp3));
+               length = min(length, LEN(cp3));
        cplim = cp + length; cp3 += skip; cp2 += skip;
        for (cp += skip; cp < cplim; cp++, cp2++, cp3++)
                if ((*cp ^ *cp2) & *cp3)
@@ -425,7 +429,7 @@ rn_insert(v_arg, head, dupentry, nodes)
 {
        caddr_t v = v_arg;
        struct radix_node *top = head->rnh_treetop;
-       int head_off = top->rn_offset, vlen = (int)LEN(v);
+       int head_off = top->rn_offset, vlen = LEN(v);
        register struct radix_node *t = rn_search(v_arg, top);
        register caddr_t cp = v + head_off;
        register int b;
@@ -1160,16 +1164,11 @@ rn_inithead(head, off)
 }
 
 void
-rn_init()
+rn_init(int maxk)
 {
        char *cp, *cplim;
-#ifdef _KERNEL
-       struct domain *dom;
 
-       for (dom = domains; dom; dom = dom->dom_next)
-               if (dom->dom_maxrtkey > max_keylen)
-                       max_keylen = dom->dom_maxrtkey;
-#endif
+       max_keylen = maxk;
        if (max_keylen == 0) {
                log(LOG_ERR,
                    "rn_init: radix functions require max_keylen be set\n");