-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
flow: add callbacks for flow init and flow updates
Adds user registerable callbacks for flow initialization and flow update. Some plugins, such as other DPI libraries like nDPI need a way to hook into these flow lifecycle events. Ticket: #7319 Ticket: #7320
- Loading branch information
Showing
5 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* Copyright (C) 2024 Open Information Security Foundation | ||
* | ||
* You can copy, redistribute or modify this Program under the terms of | ||
* the GNU General Public License version 2 as published by the Free | ||
* Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* version 2 along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
* 02110-1301, USA. | ||
*/ | ||
|
||
#include "flow-callbacks.h" | ||
|
||
typedef struct FlowInitCallback_ { | ||
SCFlowInitCallbackFn fn; | ||
struct FlowInitCallback_ *next; | ||
} FlowInitCallback; | ||
|
||
static FlowInitCallback *init_callbacks = NULL; | ||
|
||
typedef struct FlowUpdateCallback_ { | ||
SCFlowUpdateCallbackFn fn; | ||
struct FlowUpdateCallback_ *next; | ||
} FlowUpdateCallback; | ||
|
||
static FlowUpdateCallback *update_callbacks = NULL; | ||
|
||
bool SCFlowRegisterInitCallback(SCFlowInitCallbackFn fn) | ||
{ | ||
FlowInitCallback *cb = SCCalloc(1, sizeof(*cb)); | ||
if (cb == NULL) { | ||
return false; | ||
} | ||
cb->fn = fn; | ||
if (init_callbacks == NULL) { | ||
init_callbacks = cb; | ||
} else { | ||
FlowInitCallback *current = init_callbacks; | ||
while (current->next != NULL) { | ||
current = current->next; | ||
} | ||
current->next = cb; | ||
} | ||
return true; | ||
} | ||
|
||
void SCFlowRunInitCallbacks(Flow *f, const Packet *p) | ||
{ | ||
FlowInitCallback *cb = init_callbacks; | ||
while (cb != NULL) { | ||
cb->fn(f, p); | ||
cb = cb->next; | ||
} | ||
} | ||
|
||
bool SCFlowRegisterUpdateCallback(SCFlowUpdateCallbackFn fn) | ||
{ | ||
FlowUpdateCallback *cb = SCCalloc(1, sizeof(*cb)); | ||
if (cb == NULL) { | ||
return false; | ||
} | ||
cb->fn = fn; | ||
if (update_callbacks == NULL) { | ||
update_callbacks = cb; | ||
} else { | ||
FlowUpdateCallback *current = update_callbacks; | ||
while (current->next != NULL) { | ||
current = current->next; | ||
} | ||
current->next = cb; | ||
} | ||
return true; | ||
} | ||
|
||
void SCFlowRunUpdateCallbacks(Flow *f, Packet *p, ThreadVars *tv) | ||
{ | ||
FlowUpdateCallback *cb = update_callbacks; | ||
while (cb != NULL) { | ||
cb->fn(f, p, tv); | ||
cb = cb->next; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* Copyright (C) 2024 Open Information Security Foundation | ||
* | ||
* You can copy, redistribute or modify this Program under the terms of | ||
* the GNU General Public License version 2 as published by the Free | ||
* Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* version 2 along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
* 02110-1301, USA. | ||
*/ | ||
|
||
#ifndef SURICATA_FLOW_CALLBACKS_H | ||
#define SURICATA_FLOW_CALLBACKS_H | ||
|
||
#include "suricata-common.h" | ||
#include "flow.h" | ||
|
||
typedef void (*SCFlowInitCallbackFn)(Flow *f, const Packet *p); | ||
|
||
/** \brief Register a flow init callback. | ||
* | ||
* Register a user provided function to be called every time a flow is | ||
* initialized for use. | ||
* | ||
* \returns true if callback was registered, otherwise false if the | ||
* callback could not be registered due to memory allocation error. | ||
*/ | ||
bool SCFlowRegisterInitCallback(SCFlowInitCallbackFn fn); | ||
|
||
/** \internal | ||
* | ||
* Run all registered flow init callbacks. | ||
*/ | ||
void SCFlowRunInitCallbacks(Flow *f, const Packet *p); | ||
|
||
typedef void (*SCFlowUpdateCallbackFn)(Flow *f, Packet *p, ThreadVars *tv); | ||
|
||
/** \brief Register a flow update callback. | ||
* | ||
* Register a user provided function to be called everytime a flow is | ||
* updated. | ||
* | ||
* \returns true if callback was registered, otherwise false if the | ||
* callback could not be registered due to memory allocation error. | ||
*/ | ||
bool SCFlowRegisterUpdateCallback(SCFlowUpdateCallbackFn fn); | ||
|
||
/** \internal | ||
* | ||
* Run all registered flow update callbacks. | ||
*/ | ||
void SCFlowRunUpdateCallbacks(Flow *f, Packet *p, ThreadVars *tv); | ||
|
||
#endif /* SURICATA_FLOW_CALLBACKS_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters