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) \
69 do { \
70 if ((p) != NULL) { \
71 if ((p)->flow != NULL) { \
72 DEBUG_VALIDATE_FLOW((p)->flow); \
73 } \
74 if (!((p)->flags & (PKT_IS_FRAGMENT | PKT_IS_INVALID))) { \
75 if ((p)->proto == IPPROTO_TCP) { \
76 BUG_ON(PacketGetTCP((p)) == NULL); \
77 } else if ((p)->proto == IPPROTO_UDP) { \
78 BUG_ON(PacketGetUDP((p)) == NULL); \
79 } else if ((p)->proto == IPPROTO_ICMP) { \
80 BUG_ON(PacketGetICMPv4((p)) == NULL); \
81 } else if ((p)->proto == IPPROTO_SCTP) { \
82 BUG_ON(PacketGetSCTP((p)) == NULL); \
83 } else if ((p)->proto == IPPROTO_ICMPV6) { \
84 BUG_ON(PacketGetICMPv6((p)) == NULL); \
85 } \
86 } \
87 if ((p)->payload_len > 0) { \
88 BUG_ON((p)->payload == NULL); \
89 } \
90 BUG_ON((p)->flowflags != 0 && (p)->flow == NULL); \
91 BUG_ON((p)->flowflags &FLOW_PKT_TOSERVER && (p)->flowflags & FLOW_PKT_TOCLIENT); \
92 } \
93 } while (0)
94
95#define DEBUG_VALIDATE_BUG_ON(exp) BUG_ON((exp))
96
97#else /* DEBUG_VALIDATE */
98
99#define DEBUG_ASSERT_FLOW_LOCKED(f)
100#define DEBUG_VALIDATE_FLOW(f)
101#define DEBUG_VALIDATE_PACKET(p)
102#define DEBUG_VALIDATE_BUG_ON(exp)
103
104#endif /* DEBUG_VALIDATE */
105
106#endif /* SURICATA_UTIL_VALIDATE_H */