suricata
flow-util.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2012 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  *
21  * \author Victor Julien <victor@inliniac.net>
22  */
23 
24 #ifndef __FLOW_UTIL_H__
25 #define __FLOW_UTIL_H__
26 
27 #include "detect-engine-state.h"
28 #include "tmqh-flow.h"
29 
30 #define COPY_TIMESTAMP(src,dst) ((dst)->tv_sec = (src)->tv_sec, (dst)->tv_usec = (src)->tv_usec)
31 
32 #define RESET_COUNTERS(f) do { \
33  (f)->todstpktcnt = 0; \
34  (f)->tosrcpktcnt = 0; \
35  (f)->todstbytecnt = 0; \
36  (f)->tosrcbytecnt = 0; \
37  } while (0)
38 
39 #define FLOW_INITIALIZE(f) do { \
40  (f)->sp = 0; \
41  (f)->dp = 0; \
42  (f)->proto = 0; \
43  SC_ATOMIC_INIT((f)->flow_state); \
44  SC_ATOMIC_INIT((f)->use_cnt); \
45  (f)->tenant_id = 0; \
46  (f)->parent_id = 0; \
47  (f)->probing_parser_toserver_alproto_masks = 0; \
48  (f)->probing_parser_toclient_alproto_masks = 0; \
49  (f)->flags = 0; \
50  (f)->file_flags = 0; \
51  (f)->protodetect_dp = 0; \
52  (f)->lastts.tv_sec = 0; \
53  (f)->lastts.tv_usec = 0; \
54  FLOWLOCK_INIT((f)); \
55  (f)->protoctx = NULL; \
56  (f)->flow_end_flags = 0; \
57  (f)->alproto = 0; \
58  (f)->alproto_ts = 0; \
59  (f)->alproto_tc = 0; \
60  (f)->alproto_orig = 0; \
61  (f)->alproto_expect = 0; \
62  (f)->de_ctx_version = 0; \
63  (f)->thread_id = 0; \
64  (f)->alparser = NULL; \
65  (f)->alstate = NULL; \
66  (f)->sgh_toserver = NULL; \
67  (f)->sgh_toclient = NULL; \
68  (f)->flowvar = NULL; \
69  (f)->hnext = NULL; \
70  (f)->hprev = NULL; \
71  (f)->lnext = NULL; \
72  (f)->lprev = NULL; \
73  RESET_COUNTERS((f)); \
74  } while (0)
75 
76 /** \brief macro to recycle a flow before it goes into the spare queue for reuse.
77  *
78  * Note that the lnext, lprev, hnext, hprev fields are untouched, those are
79  * managed by the queueing code. Same goes for fb (FlowBucket ptr) field.
80  */
81 #define FLOW_RECYCLE(f) do { \
82  FlowCleanupAppLayer((f)); \
83  (f)->sp = 0; \
84  (f)->dp = 0; \
85  (f)->proto = 0; \
86  SC_ATOMIC_RESET((f)->flow_state); \
87  SC_ATOMIC_RESET((f)->use_cnt); \
88  (f)->tenant_id = 0; \
89  (f)->parent_id = 0; \
90  (f)->probing_parser_toserver_alproto_masks = 0; \
91  (f)->probing_parser_toclient_alproto_masks = 0; \
92  (f)->flags = 0; \
93  (f)->file_flags = 0; \
94  (f)->protodetect_dp = 0; \
95  (f)->lastts.tv_sec = 0; \
96  (f)->lastts.tv_usec = 0; \
97  (f)->protoctx = NULL; \
98  (f)->flow_end_flags = 0; \
99  (f)->alparser = NULL; \
100  (f)->alstate = NULL; \
101  (f)->alproto = 0; \
102  (f)->alproto_ts = 0; \
103  (f)->alproto_tc = 0; \
104  (f)->alproto_orig = 0; \
105  (f)->alproto_expect = 0; \
106  (f)->de_ctx_version = 0; \
107  (f)->thread_id = 0; \
108  (f)->sgh_toserver = NULL; \
109  (f)->sgh_toclient = NULL; \
110  GenericVarFree((f)->flowvar); \
111  (f)->flowvar = NULL; \
112  RESET_COUNTERS((f)); \
113  } while(0)
114 
115 #define FLOW_DESTROY(f) do { \
116  FlowCleanupAppLayer((f)); \
117  SC_ATOMIC_DESTROY((f)->flow_state); \
118  SC_ATOMIC_DESTROY((f)->use_cnt); \
119  \
120  FLOWLOCK_DESTROY((f)); \
121  GenericVarFree((f)->flowvar); \
122  } while(0)
123 
124 /** \brief check if a memory alloc would fit in the memcap
125  *
126  * \param size memory allocation size to check
127  *
128  * \retval 1 it fits
129  * \retval 0 no fit
130  */
131 #define FLOW_CHECK_MEMCAP(size) \
132  ((((uint64_t)SC_ATOMIC_GET(flow_memuse) + (uint64_t)(size)) <= SC_ATOMIC_GET(flow_config.memcap)))
133 
134 Flow *FlowAlloc(void);
135 Flow *FlowAllocDirect(void);
136 void FlowFree(Flow *);
137 uint8_t FlowGetProtoMapping(uint8_t);
138 void FlowInit(Flow *, const Packet *);
139 uint8_t FlowGetReverseProtoMapping(uint8_t rproto);
140 
141 #endif /* __FLOW_UTIL_H__ */
142 
uint8_t FlowGetProtoMapping(uint8_t)
Function to map the protocol to the defined FLOW_PROTO_* enumeration.
Definition: flow-util.c:95
Data structures and function prototypes for keeping state for the detection engine.
Flow * FlowAlloc(void)
allocate a flow
Definition: flow-util.c:51
void FlowFree(Flow *)
cleanup & free the memory of a flow
Definition: flow-util.c:80
uint8_t FlowGetReverseProtoMapping(uint8_t rproto)
Definition: flow-util.c:111
Flow * FlowAllocDirect(void)
void FlowInit(Flow *, const Packet *)
Definition: flow-util.c:147
Flow data structure.
Definition: flow.h:327