suricata
flow-callbacks.c
Go to the documentation of this file.
1 /* Copyright (C) 2024 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 #include "flow-callbacks.h"
19 
20 typedef struct FlowInitCallback_ {
22  void *user;
25 
26 static FlowInitCallback *init_callbacks = NULL;
27 
28 typedef struct FlowUpdateCallback_ {
30  void *user;
33 
34 static FlowUpdateCallback *update_callbacks = NULL;
35 
36 typedef struct FlowFinishCallback_ {
38  void *user;
41 
42 static FlowFinishCallback *finish_callbacks = NULL;
43 
45 {
46  FlowInitCallback *cb = SCCalloc(1, sizeof(*cb));
47  if (cb == NULL) {
48  return false;
49  }
50  cb->Callback = fn;
51  cb->user = user;
52  if (init_callbacks == NULL) {
53  init_callbacks = cb;
54  } else {
55  FlowInitCallback *current = init_callbacks;
56  while (current->next != NULL) {
57  current = current->next;
58  }
59  current->next = cb;
60  }
61  return true;
62 }
63 
65 {
66  FlowInitCallback *cb = init_callbacks;
67  while (cb != NULL) {
68  cb->Callback(tv, f, p, cb->user);
69  cb = cb->next;
70  }
71 }
72 
74 {
75  FlowUpdateCallback *cb = SCCalloc(1, sizeof(*cb));
76  if (cb == NULL) {
77  return false;
78  }
79  cb->Callback = fn;
80  cb->user = user;
81  if (update_callbacks == NULL) {
82  update_callbacks = cb;
83  } else {
84  FlowUpdateCallback *current = update_callbacks;
85  while (current->next != NULL) {
86  current = current->next;
87  }
88  current->next = cb;
89  }
90  return true;
91 }
92 
94 {
95  FlowUpdateCallback *cb = update_callbacks;
96  while (cb != NULL) {
97  cb->Callback(tv, f, p, cb->user);
98  cb = cb->next;
99  }
100 }
101 
103 {
104  FlowFinishCallback *cb = SCCalloc(1, sizeof(*cb));
105  if (cb == NULL) {
106  return false;
107  }
108  cb->Callback = fn;
109  cb->user = user;
110  if (finish_callbacks == NULL) {
111  finish_callbacks = cb;
112  } else {
113  FlowFinishCallback *current = finish_callbacks;
114  while (current->next != NULL) {
115  current = current->next;
116  }
117  current->next = cb;
118  }
119  return true;
120 }
121 
123 {
124  FlowFinishCallback *cb = finish_callbacks;
125  while (cb != NULL) {
126  cb->Callback(tv, f, cb->user);
127  cb = cb->next;
128  }
129 }
FlowInitCallback_::user
void * user
Definition: flow-callbacks.c:22
FlowInitCallback_::Callback
SCFlowInitCallbackFn Callback
Definition: flow-callbacks.c:21
FlowInitCallback
struct FlowInitCallback_ FlowInitCallback
Flow_
Flow data structure.
Definition: flow.h:356
SCFlowRegisterInitCallback
bool SCFlowRegisterInitCallback(SCFlowInitCallbackFn fn, void *user)
Register a flow init callback.
Definition: flow-callbacks.c:44
SCFlowRunUpdateCallbacks
void SCFlowRunUpdateCallbacks(ThreadVars *tv, Flow *f, Packet *p)
Definition: flow-callbacks.c:93
SCFlowRegisterUpdateCallback
bool SCFlowRegisterUpdateCallback(SCFlowUpdateCallbackFn fn, void *user)
Register a flow update callback.
Definition: flow-callbacks.c:73
FlowUpdateCallback
struct FlowUpdateCallback_ FlowUpdateCallback
SCFlowInitCallbackFn
void(* SCFlowInitCallbackFn)(ThreadVars *tv, Flow *f, const Packet *p, void *user)
Function type for flow initialization callbacks.
Definition: flow-callbacks.h:36
FlowFinishCallback_
Definition: flow-callbacks.c:36
FlowFinishCallback_::user
void * user
Definition: flow-callbacks.c:38
SCFlowRunFinishCallbacks
void SCFlowRunFinishCallbacks(ThreadVars *tv, Flow *f)
Definition: flow-callbacks.c:122
SCFlowFinishCallbackFn
void(* SCFlowFinishCallbackFn)(ThreadVars *tv, Flow *f, void *user)
Function type for flow finish callbacks.
Definition: flow-callbacks.h:100
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
FlowInitCallback_::next
struct FlowInitCallback_ * next
Definition: flow-callbacks.c:23
Packet_
Definition: decode.h:473
FlowFinishCallback
struct FlowFinishCallback_ FlowFinishCallback
FlowUpdateCallback_::Callback
SCFlowUpdateCallbackFn Callback
Definition: flow-callbacks.c:29
FlowUpdateCallback_::next
struct FlowUpdateCallback_ * next
Definition: flow-callbacks.c:31
SCFlowRunInitCallbacks
void SCFlowRunInitCallbacks(ThreadVars *tv, Flow *f, const Packet *p)
Definition: flow-callbacks.c:64
SCFlowUpdateCallbackFn
void(* SCFlowUpdateCallbackFn)(ThreadVars *tv, Flow *f, Packet *p, void *user)
Function type for flow update callbacks.
Definition: flow-callbacks.h:69
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
FlowFinishCallback_::Callback
SCFlowFinishCallbackFn Callback
Definition: flow-callbacks.c:37
FlowUpdateCallback_::user
void * user
Definition: flow-callbacks.c:30
flow-callbacks.h
FlowInitCallback_
Definition: flow-callbacks.c:20
FlowUpdateCallback_
Definition: flow-callbacks.c:28
SCFlowRegisterFinishCallback
bool SCFlowRegisterFinishCallback(SCFlowFinishCallbackFn fn, void *user)
Register a flow init callback.
Definition: flow-callbacks.c:102
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
FlowFinishCallback_::next
struct FlowFinishCallback_ * next
Definition: flow-callbacks.c:39