suricata
capture-hooks.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2026 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  * Lightweight indirection layer for capture-related callbacks.
21  *
22  * This module lets the capture implementation register small hooks that the
23  * generic engine can invoke without hard dependencies. Two hooks are used:
24  * - on-alerts: invoked when a packet produced alerts so capture can update
25  * per-input stats (e.g., deciding if a pcap should be deleted or kept).
26  * - on-pseudo-created: invoked when the engine creates pseudo packets (e.g.,
27  * flow timeout or shutdown flush). This allows capture to retain references
28  * or track alert outcomes tied to those pseudo packets.
29  */
30 
31 #include "suricata-common.h"
32 #include "capture-hooks.h"
33 
34 static CaptureOnPacketWithAlertsHook g_on_alerts_hook = NULL;
35 static CaptureOnPseudoPacketCreatedHook g_on_pseudo_created_hook = NULL;
36 
39 {
40  /* Allow re-setting the same hooks (e.g. during pcap file reload), but BUG_ON
41  * if overwriting with different ones without clearing. */
42  if (g_on_alerts_hook != NULL) {
43  BUG_ON(g_on_alerts_hook != OnAlerts);
44  }
45  if (g_on_pseudo_created_hook != NULL) {
46  BUG_ON(g_on_pseudo_created_hook != OnPseudoCreated);
47  }
48 
49  g_on_alerts_hook = OnAlerts;
50  g_on_pseudo_created_hook = OnPseudoCreated;
51 }
52 
54 {
55  if (g_on_alerts_hook != NULL) {
56  g_on_alerts_hook(p);
57  }
58 }
59 
61 {
62  if (g_on_pseudo_created_hook != NULL) {
63  g_on_pseudo_created_hook(p);
64  }
65 }
CaptureHooksOnPacketWithAlerts
void CaptureHooksOnPacketWithAlerts(const Packet *p)
Definition: capture-hooks.c:53
capture-hooks.h
CaptureOnPacketWithAlertsHook
void(* CaptureOnPacketWithAlertsHook)(const Packet *p)
Definition: capture-hooks.h:32
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:317
Packet_
Definition: decode.h:501
CaptureHooksOnPseudoPacketCreated
void CaptureHooksOnPseudoPacketCreated(Packet *p)
Definition: capture-hooks.c:60
suricata-common.h
CaptureOnPseudoPacketCreatedHook
void(* CaptureOnPseudoPacketCreatedHook)(Packet *p)
Definition: capture-hooks.h:33
CaptureHooksSet
void CaptureHooksSet(CaptureOnPacketWithAlertsHook OnAlerts, CaptureOnPseudoPacketCreatedHook OnPseudoCreated)
Definition: capture-hooks.c:37