suricata
util-validate.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 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  * Functions & Macro's for validation of data structures. This is used for
24  * code correctness.
25  *
26  * These will abort() the program if they fail, so they should _only_ be
27  * used for testing.
28  */
29 
30 #ifndef SURICATA_UTIL_VALIDATE_H
31 #define SURICATA_UTIL_VALIDATE_H
32 
33 #ifdef DEBUG_VALIDATION
34 
35 /** \brief test if a flow is locked.
36  *
37  * If trylock returns 0 it got a lock. Which means
38  * the flow was previously unlocked.
39  */
40 #define DEBUG_ASSERT_FLOW_LOCKED(f) do { \
41  if ((f) != NULL) { \
42  int r = SCMutexTrylock(&(f)->m); \
43  if (r == 0) { \
44  BUG_ON(1); \
45  } \
46  } \
47 } while(0)
48 
49 /** \brief validate the integrity of the flow
50  *
51  * BUG_ON's on problems
52  */
53 #define DEBUG_VALIDATE_FLOW(f) do { \
54  if ((f) != NULL) { \
55  BUG_ON((f)->flags & FLOW_IPV4 && \
56  (f)->flags & FLOW_IPV6); \
57  if ((f)->proto == IPPROTO_TCP) { \
58  BUG_ON((f)->alstate != NULL && \
59  (f)->alparser == NULL); \
60  } \
61  } \
62 } while(0)
63 
64 /** \brief validate the integrity of the packet
65  *
66  * BUG_ON's on problems
67  */
68 #define DEBUG_VALIDATE_PACKET(p) do { \
69  if ((p) != NULL) { \
70  if ((p)->flow != NULL) { \
71  DEBUG_VALIDATE_FLOW((p)->flow); \
72  } \
73  if (!((p)->flags & (PKT_IS_FRAGMENT|PKT_IS_INVALID))) { \
74  if ((p)->proto == IPPROTO_TCP) { \
75  BUG_ON((p)->tcph == NULL); \
76  } else if ((p)->proto == IPPROTO_UDP) { \
77  BUG_ON((p)->udph == NULL); \
78  } else if ((p)->proto == IPPROTO_ICMP) { \
79  BUG_ON((p)->icmpv4h == NULL); \
80  } else if ((p)->proto == IPPROTO_SCTP) { \
81  BUG_ON((p)->sctph == NULL); \
82  } else if ((p)->proto == IPPROTO_ICMPV6) { \
83  BUG_ON((p)->icmpv6h == NULL); \
84  } \
85  } \
86  if ((p)->payload_len > 0) { \
87  BUG_ON((p)->payload == NULL); \
88  } \
89  BUG_ON((p)->ip4h != NULL && (p)->ip6h != NULL); \
90  BUG_ON((p)->flowflags != 0 && (p)->flow == NULL); \
91  BUG_ON((p)->flowflags & FLOW_PKT_TOSERVER &&\
92  (p)->flowflags & FLOW_PKT_TOCLIENT); \
93  } \
94 } while(0)
95 
96 #define DEBUG_VALIDATE_BUG_ON(exp) BUG_ON((exp))
97 
98 #else /* DEBUG_VALIDATE */
99 
100 #define DEBUG_ASSERT_FLOW_LOCKED(f)
101 #define DEBUG_VALIDATE_FLOW(f)
102 #define DEBUG_VALIDATE_PACKET(p)
103 #define DEBUG_VALIDATE_BUG_ON(exp)
104 
105 #endif /* DEBUG_VALIDATE */
106 
107 #endif /* SURICATA_UTIL_VALIDATE_H */