]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
interval-tree: add augmentation fns to the tree
authorShivani Bhardwaj <shivani@oisf.net>
Mon, 29 Jan 2024 06:08:51 +0000 (11:38 +0530)
committerVictor Julien <victor@inliniac.net>
Mon, 4 Mar 2024 10:50:30 +0000 (11:50 +0100)
An interval tree uses red-black tree as its base data structure and
follows all the properties of a usual red-black tree. The additional
params are:
1. An interval such as [low, high] per node.
2. A max attribute per node. This attribute stores the maximum high
   value of any subtree rooted at this node.

At any point in time, an inorder traversal of an interval tree should
give the port ranges sorted by the low key in ascending order.

This commit modifies the IRB_AUGMENT macro and it's call sites to make
sure that on every insertion, the max attribute of the tree is properly
updated.

Ticket 6792
Bug 6414

src/Makefile.am
src/interval-tree.h

index 24f1a9781e21f42139c7fb7609c350154ccb8339..3802ccf3a84beec9963dec094a513e36db7a0053 100755 (executable)
@@ -511,6 +511,7 @@ noinst_HEADERS = \
        tm-threads-common.h \
        tm-threads.h \
        tree.h \
+       interval-tree.h \
        unix-manager.h \
        util-action.h \
        util-affinity.h \
index eb588fd435d9b14002276768fbf5062c8de269d1..934083447982091ea576d6f93efb7f7ae6db3d4a 100644 (file)
@@ -2,7 +2,8 @@
 /*     $OpenBSD: interval-tree.h,v 1.7 2002/10/17 21:51:54 art Exp $   */
 /* $FreeBSD$ */
 
-/* This is a COPY of the in-tree tree.h */
+/* This is a COPY of the in-tree tree.h modified to accomodate interval
+ * tree operations */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 #endif
 
 /*
- * This file defines data structures for red-black trees.
+ * This file defines data structures for interval trees which are
+ * implemented using red-black trees.
  *
- * A red-black tree is a binary search interval-tree with the node color as an
+ * A red-black tree is a binary search tree with the node color as an
  * extra attribute.  It fulfills a set of conditions:
  *     - every search path from the root to a leaf consists of the
  *       same number of black nodes,
         IRB_COLOR(red, field) = IRB_RED;                                                           \
     } while (/*CONSTCOND*/ 0)
 
+/*
+ * The implementation of the following macro has been updated.
+ * In order to incorporte it properly, the call sites of this
+ * function have also been updated compared to the standard
+ * Red Black tree implementation in tree.h of BSD */
 #ifndef IRB_AUGMENT
-#define IRB_AUGMENT(x)                                                                             \
+#define IRB_AUGMENT(x, field)                                                                      \
     do {                                                                                           \
+        if (x != NULL) {                                                                           \
+            x->max = x->port2;                                                                     \
+            if (IRB_LEFT(x, field) != NULL) {                                                      \
+                x->max = MAX(x->max, IRB_LEFT(x, field)->max);                                     \
+            }                                                                                      \
+            if (IRB_RIGHT(x, field) != NULL) {                                                     \
+                x->max = MAX(x->max, IRB_RIGHT(x, field)->max);                                    \
+            }                                                                                      \
+        }                                                                                          \
     } while (0)
 #endif
 
         if ((IRB_RIGHT(elm, field) = IRB_LEFT(tmp, field)) != NULL) {                              \
             IRB_PARENT(IRB_LEFT(tmp, field), field) = (elm);                                       \
         }                                                                                          \
-        IRB_AUGMENT(elm);                                                                          \
         if ((IRB_PARENT(tmp, field) = IRB_PARENT(elm, field)) != NULL) {                           \
             if ((elm) == IRB_LEFT(IRB_PARENT(elm, field), field))                                  \
                 IRB_LEFT(IRB_PARENT(elm, field), field) = (tmp);                                   \
             (head)->rbh_root = (tmp);                                                              \
         IRB_LEFT(tmp, field) = (elm);                                                              \
         IRB_PARENT(elm, field) = (tmp);                                                            \
-        IRB_AUGMENT(tmp);                                                                          \
+        IRB_AUGMENT(elm, field);                                                                   \
+        IRB_AUGMENT(tmp, field);                                                                   \
         if ((IRB_PARENT(tmp, field)))                                                              \
-            IRB_AUGMENT(IRB_PARENT(tmp, field));                                                   \
+            IRB_AUGMENT(IRB_PARENT(tmp, field), field);                                            \
     } while (/*CONSTCOND*/ 0)
 
 #define IRB_ROTATE_RIGHT(head, elm, tmp, field)                                                    \
         if ((IRB_LEFT(elm, field) = IRB_RIGHT(tmp, field)) != NULL) {                              \
             IRB_PARENT(IRB_RIGHT(tmp, field), field) = (elm);                                      \
         }                                                                                          \
-        IRB_AUGMENT(elm);                                                                          \
         if ((IRB_PARENT(tmp, field) = IRB_PARENT(elm, field)) != NULL) {                           \
             if ((elm) == IRB_LEFT(IRB_PARENT(elm, field), field))                                  \
                 IRB_LEFT(IRB_PARENT(elm, field), field) = (tmp);                                   \
             (head)->rbh_root = (tmp);                                                              \
         IRB_RIGHT(tmp, field) = (elm);                                                             \
         IRB_PARENT(elm, field) = (tmp);                                                            \
-        IRB_AUGMENT(tmp);                                                                          \
+        IRB_AUGMENT(elm, field);                                                                   \
+        IRB_AUGMENT(tmp, field);                                                                   \
         if ((IRB_PARENT(tmp, field)))                                                              \
-            IRB_AUGMENT(IRB_PARENT(tmp, field));                                                   \
+            IRB_AUGMENT(IRB_PARENT(tmp, field), field);                                            \
     } while (/*CONSTCOND*/ 0)
 
 /* Generates prototypes and inline functions */
                     IRB_LEFT(parent, field) = child;                                               \
                 else                                                                               \
                     IRB_RIGHT(parent, field) = child;                                              \
-                IRB_AUGMENT(parent);                                                               \
+                IRB_AUGMENT(parent, field);                                                        \
             } else                                                                                 \
                 IRB_ROOT(head) = child;                                                            \
             if (IRB_PARENT(elm, field) == old)                                                     \
                     IRB_LEFT(IRB_PARENT(old, field), field) = elm;                                 \
                 else                                                                               \
                     IRB_RIGHT(IRB_PARENT(old, field), field) = elm;                                \
-                IRB_AUGMENT(IRB_PARENT(old, field));                                               \
+                IRB_AUGMENT(IRB_PARENT(old, field), field);                                        \
             } else                                                                                 \
                 IRB_ROOT(head) = elm;                                                              \
             _T_ASSERT(old);                                                                        \
             if (parent) {                                                                          \
                 left = parent;                                                                     \
                 do {                                                                               \
-                    IRB_AUGMENT(left);                                                             \
+                    IRB_AUGMENT(left, field);                                                      \
                 } while ((left = IRB_PARENT(left, field)) != NULL);                                \
             }                                                                                      \
             goto color;                                                                            \
                 IRB_LEFT(parent, field) = child;                                                   \
             else                                                                                   \
                 IRB_RIGHT(parent, field) = child;                                                  \
-            IRB_AUGMENT(parent);                                                                   \
+            IRB_AUGMENT(parent, field);                                                            \
         } else                                                                                     \
             IRB_ROOT(head) = child;                                                                \
     color:                                                                                         \
         while (tmp) {                                                                              \
             parent = tmp;                                                                          \
             comp = (cmp)(elm, parent);                                                             \
-            if (comp < 0)                                                                          \
+            if (comp < 0) {                                                                        \
                 tmp = IRB_LEFT(tmp, field);                                                        \
-            else if (comp > 0)                                                                     \
+            } else if (comp > 0) {                                                                 \
                 tmp = IRB_RIGHT(tmp, field);                                                       \
-            else                                                                                   \
+            } else                                                                                 \
                 return (tmp);                                                                      \
         }                                                                                          \
         IRB_SET(elm, parent, field);                                                               \
                 IRB_LEFT(parent, field) = elm;                                                     \
             else                                                                                   \
                 IRB_RIGHT(parent, field) = elm;                                                    \
-            IRB_AUGMENT(parent);                                                                   \
         } else                                                                                     \
             IRB_ROOT(head) = elm;                                                                  \
+        IRB_AUGMENT(elm, field);                                                                   \
         name##_IRB_INSERT_COLOR(head, elm);                                                        \
         return (NULL);                                                                             \
     }