]> jfr.im git - irc/quakenet/newserv.git/blobdiff - patricia/patricialib.c
Rename trusts_cidr2str to CIDRtostr() and move it to lib/irc_ipv6.c.
[irc/quakenet/newserv.git] / patricia / patricialib.c
index c9dcd4ef9395f1f29aac7dc1e793a85364b82b5d..009b1dbcb704cbf96c42e7fadb40a970ede129f9 100644 (file)
@@ -53,6 +53,7 @@ comp_with_mask (void *addr, void *dest, u_int mask)
     return (0);
 }
 
+
 prefix_t *
 patricia_new_prefix (struct irc_in_addr *dest, int bitlen)
 {
@@ -89,8 +90,8 @@ patricia_deref_prefix (prefix_t * prefix)
 
     prefix->ref_count--;
     if (prefix->ref_count <= 0) {
-       freeprefix(prefix);
-       return;
+      freeprefix(prefix);
+      return;
     }
 }
 
@@ -212,7 +213,7 @@ patricia_search_exact (patricia_tree_t *patricia, struct irc_in_addr *sin, unsig
     addr = (u_char *)sin;
 
     while (node->bit < bitlen) {
-       if (BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07)))
+        if (is_bit_set(addr,node->bit))
            node = node->r;
        else
            node = node->l;
@@ -225,7 +226,7 @@ patricia_search_exact (patricia_tree_t *patricia, struct irc_in_addr *sin, unsig
        return (NULL);
     assert (node->bit == bitlen);
     assert (node->bit == node->prefix->bitlen);
-    if (comp_with_mask (prefix_tochar (node->prefix), addr,    bitlen)) {
+    if (comp_with_mask (prefix_tochar (node->prefix), addr, bitlen)) {
        return (node);
     }
     return (NULL);
@@ -257,7 +258,7 @@ patricia_search_best2 (patricia_tree_t *patricia, struct irc_in_addr *sin, unsig
            stack[cnt++] = node;
        }
 
-       if (BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) {
+        if (is_bit_set(addr,node->bit)) {
            node = node->r;
        }
        else {
@@ -320,7 +321,7 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
     while (node->bit < bitlen || node->prefix == NULL) {
         /* check that we're not at the lowest leaf i.e. node->bit is less than max bits */
        if (node->bit < patricia->maxbits &&
-           (addr[node->bit >> 3]) & (0x80 >> (node->bit & 0x07))) {
+            (is_bit_set(addr,node->bit))) {
            if (node->r == NULL)
                break;
            node = node->r;
@@ -346,7 +347,7 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
            continue;
        }
        /* I know the better way, but for now */
-       for (j = 0; j < 8; j++) {
+        for (j = 0; j < 8; j++) {
            if ((r) & ((0x80 >> j)))
                break;
        }
@@ -373,11 +374,10 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
     }
 
     new_node = patricia_new_node(patricia, prefix->bitlen, patricia_ref_prefix (prefix));
-
     if (node->bit == differ_bit) {
        new_node->parent = node;
        if (node->bit < patricia->maxbits &&
-           (addr[node->bit >> 3]) & (0x80 >> (node->bit & 0x07))) {
+           (is_bit_set(addr, node->bit))) {
            assert (node->r == NULL);
            node->r = new_node;
        }
@@ -390,7 +390,7 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
 
     if (bitlen == differ_bit) {
        if (bitlen < patricia->maxbits &&
-           (test_addr[bitlen >> 3]) & (0x80 >> (bitlen & 0x07))) {
+           (is_bit_set(test_addr,bitlen))) {
            new_node->r = node;
        }
        else {
@@ -408,13 +408,13 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
            node->parent->l = new_node;
        }
        node->parent = new_node;
+        new_node->usercount = node->usercount;
     }
     else {
         glue = patricia_new_node(patricia, differ_bit, NULL);
         glue->parent = node->parent;
-        
        if (differ_bit < patricia->maxbits &&
-           (addr[differ_bit >> 3]) & (0x80 >> (differ_bit & 0x07))) {
+           (is_bit_set(addr, differ_bit))) {
            glue->r = new_node;
            glue->l = node;
        }
@@ -435,7 +435,9 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
            node->parent->l = glue;
        }
        node->parent = glue;
+        glue->usercount = node->usercount;
     }
+
     return (new_node);
 }
 
@@ -571,3 +573,27 @@ patricia_node_t *patricia_new_node(patricia_tree_t *patricia, unsigned char bit,
   return new_node;  
 }
 
+void node_increment_usercount( patricia_node_t *node) {
+  while(node) {
+    node->usercount++;
+    node=node->parent;
+  }
+}
+
+void node_decrement_usercount( patricia_node_t *node) {
+  while(node) {
+    node->usercount--;
+    node=node->parent;
+  }
+}
+
+int is_normalized_ipmask( struct irc_in_addr *sin, unsigned char bitlen ) {
+  u_char *addr = (u_char *)sin;
+
+  while (bitlen < PATRICIA_MAXBITS) {
+    if (is_bit_set(addr,bitlen))
+       return 0;
+    bitlen++;
+  }
+  return 1;
+}