]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
flow: add function to get flow using flow_id
authorEric Leblond <el@stamus-networks.com>
Fri, 26 Mar 2021 00:00:31 +0000 (01:00 +0100)
committerVictor Julien <vjulien@oisf.net>
Mon, 3 Oct 2022 09:03:09 +0000 (11:03 +0200)
src/flow-hash.c
src/flow-hash.h

index c858b294a9629e38af675a90e03db02cfeaed376..49c0a02acd5b79f040c375a53d9911d76938981f 100644 (file)
@@ -856,6 +856,61 @@ static inline int FlowCompareKey(Flow *f, FlowKey *key)
     return CmpFlowKey(f, key);
 }
 
+/** \brief Look for existing Flow using a flow id value
+ *
+ * Hash retrieval function for flows. Looks up the hash bucket containing the
+ * flow pointer. Then compares the packet with the found flow to see if it is
+ * the flow we need. If it isn't, walk the list until the right flow is found.
+ *
+ *
+ *  \param flow_id Flow ID of the flow to look for
+ *  \retval f *LOCKED* flow or NULL
+ */
+
+Flow *FlowGetExistingFlowFromFlowId(int64_t flow_id)
+{
+    uint32_t hash = flow_id & 0x0000FFFF;
+    /* get our hash bucket and lock it */
+    FlowBucket *fb = &flow_hash[hash % flow_config.hash_size];
+    FBLOCK_LOCK(fb);
+
+    SCLogDebug("fb %p fb->head %p", fb, fb->head);
+
+    /* return if the bucket don't have a flow */
+    if (fb->head == NULL) {
+        FBLOCK_UNLOCK(fb);
+        return NULL;
+    }
+
+    /* ok, we have a flow in the bucket. Let's find out if it is our flow */
+    Flow *f = fb->head;
+
+    /* see if this is the flow we are looking for */
+    if (FlowGetId(f) != flow_id) {
+        while (f) {
+            f = f->next;
+
+            if (f == NULL) {
+                FBLOCK_UNLOCK(fb);
+                return NULL;
+            }
+            if (FlowGetId(f) != flow_id) {
+                /* found our flow, lock & return */
+                FLOWLOCK_WRLOCK(f);
+
+                FBLOCK_UNLOCK(fb);
+                return f;
+            }
+        }
+    }
+
+    /* lock & return */
+    FLOWLOCK_WRLOCK(f);
+
+    FBLOCK_UNLOCK(fb);
+    return f;
+}
+
 /** \brief Get or create a Flow using a FlowKey
  *
  * Hash retrieval function for flows. Looks up the hash bucket containing the
index 20bd2d9013ebbca94f3d009c3a984a378663d9be..e109e5531670723b16572328447114f13dda61d0 100644 (file)
@@ -83,6 +83,7 @@ Flow *FlowGetFlowFromHash(ThreadVars *tv, FlowLookupStruct *tctx, Packet *, Flow
 
 Flow *FlowGetFromFlowKey(FlowKey *key, struct timespec *ttime, const uint32_t hash);
 Flow *FlowGetExistingFlowFromHash(FlowKey * key, uint32_t hash);
+Flow *FlowGetExistingFlowFromFlowId(int64_t flow_id);
 uint32_t FlowKeyGetHash(FlowKey *flow_key);
 
 /** \note f->fb must be locked */