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  (f)->livedev = NULL; \
44  (f)->vlan_idx = 0; \
45  SC_ATOMIC_INIT((f)->flow_state); \
46  SC_ATOMIC_INIT((f)->use_cnt); \
47  (f)->tenant_id = 0; \
48  (f)->parent_id = 0; \
49  (f)->probing_parser_toserver_alproto_masks = 0; \
50  (f)->probing_parser_toclient_alproto_masks = 0; \
51  (f)->flags = 0; \
52  (f)->file_flags = 0; \
53  (f)->protodetect_dp = 0; \
54  (f)->lastts.tv_sec = 0; \
55  (f)->lastts.tv_usec = 0; \
56  FLOWLOCK_INIT((f)); \
57  (f)->protoctx = NULL; \
58  (f)->flow_end_flags = 0; \
59  (f)->alproto = 0; \
60  (f)->alproto_ts = 0; \
61  (f)->alproto_tc = 0; \
62  (f)->alproto_orig = 0; \
63  (f)->alproto_expect = 0; \
64  (f)->de_ctx_version = 0; \
65  (f)->thread_id = 0; \
66  (f)->alparser = NULL; \
67  (f)->alstate = NULL; \
68  (f)->sgh_toserver = NULL; \
69  (f)->sgh_toclient = NULL; \
70  (f)->flowvar = NULL; \
71  (f)->hnext = NULL; \
72  (f)->hprev = NULL; \
73  (f)->lnext = NULL; \
74  (f)->lprev = NULL; \
75  RESET_COUNTERS((f)); \
76  } while (0)
77 
78 /** \brief macro to recycle a flow before it goes into the spare queue for reuse.
79  *
80  * Note that the lnext, lprev, hnext, hprev fields are untouched, those are
81  * managed by the queueing code. Same goes for fb (FlowBucket ptr) field.
82  */
83 #define FLOW_RECYCLE(f) do { \
84  FlowCleanupAppLayer((f)); \
85  (f)->sp = 0; \
86  (f)->dp = 0; \
87  (f)->proto = 0; \
88  (f)->livedev = NULL; \
89  (f)->vlan_idx = 0; \
90  SC_ATOMIC_RESET((f)->flow_state); \
91  SC_ATOMIC_RESET((f)->use_cnt); \
92  (f)->tenant_id = 0; \
93  (f)->parent_id = 0; \
94  (f)->probing_parser_toserver_alproto_masks = 0; \
95  (f)->probing_parser_toclient_alproto_masks = 0; \
96  (f)->flags = 0; \
97  (f)->file_flags = 0; \
98  (f)->protodetect_dp = 0; \
99  (f)->lastts.tv_sec = 0; \
100  (f)->lastts.tv_usec = 0; \
101  (f)->protoctx = NULL; \
102  (f)->flow_end_flags = 0; \
103  (f)->alparser = NULL; \
104  (f)->alstate = NULL; \
105  (f)->alproto = 0; \
106  (f)->alproto_ts = 0; \
107  (f)->alproto_tc = 0; \
108  (f)->alproto_orig = 0; \
109  (f)->alproto_expect = 0; \
110  (f)->de_ctx_version = 0; \
111  (f)->thread_id = 0; \
112  (f)->sgh_toserver = NULL; \
113  (f)->sgh_toclient = NULL; \
114  GenericVarFree((f)->flowvar); \
115  (f)->flowvar = NULL; \
116  RESET_COUNTERS((f)); \
117  } while(0)
118 
119 #define FLOW_DESTROY(f) do { \
120  FlowCleanupAppLayer((f)); \
121  SC_ATOMIC_DESTROY((f)->flow_state); \
122  SC_ATOMIC_DESTROY((f)->use_cnt); \
123  \
124  FLOWLOCK_DESTROY((f)); \
125  GenericVarFree((f)->flowvar); \
126  } while(0)
127 
128 /** \brief check if a memory alloc would fit in the memcap
129  *
130  * \param size memory allocation size to check
131  *
132  * \retval 1 it fits
133  * \retval 0 no fit
134  */
135 #define FLOW_CHECK_MEMCAP(size) \
136  ((((uint64_t)SC_ATOMIC_GET(flow_memuse) + (uint64_t)(size)) <= SC_ATOMIC_GET(flow_config.memcap)))
137 
138 Flow *FlowAlloc(void);
139 Flow *FlowAllocDirect(void);
140 void FlowFree(Flow *);
141 uint8_t FlowGetProtoMapping(uint8_t);
142 void FlowInit(Flow *, const Packet *);
143 uint8_t FlowGetReverseProtoMapping(uint8_t rproto);
144 
145 #endif /* __FLOW_UTIL_H__ */
146 
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:325