suricata
ippair-queue.c
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 * IPPair queue handler functions
24 */
25
26#include "suricata-common.h"
27#include "threads.h"
28#include "ippair-queue.h"
29#include "util-error.h"
30#include "util-debug.h"
31#include "util-print.h"
32
34{
35 if (q != NULL) {
36 memset(q, 0, sizeof(IPPairQueue));
37 HQLOCK_INIT(q);
38 }
39 return q;
40}
41
43{
45 if (q == NULL) {
46 SCLogError("Fatal error encountered in IPPairQueueNew. Exiting...");
47 exit(EXIT_SUCCESS);
48 }
49 q = IPPairQueueInit(q);
50 return q;
51}
52
53/**
54 * \brief Destroy a ippair queue
55 *
56 * \param q the ippair queue to destroy
57 */
62
63/**
64 * \brief add a ippair to a queue
65 *
66 * \param q queue
67 * \param h ippair
68 */
70{
71#ifdef DEBUG
72 BUG_ON(q == NULL || h == NULL);
73#endif
74
75 HQLOCK_LOCK(q);
76
77 /* more ippairs in queue */
78 if (q->top != NULL) {
79 h->lnext = q->top;
80 q->top->lprev = h;
81 q->top = h;
82 /* only ippair */
83 } else {
84 q->top = h;
85 q->bot = h;
86 }
87 q->len++;
88#ifdef DBG_PERF
89 if (q->len > q->dbg_maxlen)
90 q->dbg_maxlen = q->len;
91#endif /* DBG_PERF */
93}
94
95/**
96 * \brief remove a ippair from the queue
97 *
98 * \param q queue
99 *
100 * \retval h ippair or NULL if empty list.
101 */
103{
104 HQLOCK_LOCK(q);
105
106 IPPair *h = q->bot;
107 if (h == NULL) {
108 HQLOCK_UNLOCK(q);
109 return NULL;
110 }
111
112 /* more packets in queue */
113 if (q->bot->lprev != NULL) {
114 q->bot = q->bot->lprev;
115 q->bot->lnext = NULL;
116 /* just the one we remove, so now empty */
117 } else {
118 q->top = NULL;
119 q->bot = NULL;
120 }
121
122#ifdef DEBUG
123 BUG_ON(q->len == 0);
124#endif
125 if (q->len > 0)
126 q->len--;
127
128 h->lnext = NULL;
129 h->lprev = NULL;
130
131 HQLOCK_UNLOCK(q);
132 return h;
133}
#define HQLOCK_INIT(q)
Definition host-queue.h:65
#define HQLOCK_DESTROY(q)
Definition host-queue.h:66
#define HQLOCK_LOCK(q)
Definition host-queue.h:67
#define HQLOCK_UNLOCK(q)
Definition host-queue.h:69
void IPPairEnqueue(IPPairQueue *q, IPPair *h)
add a ippair to a queue
void IPPairQueueDestroy(IPPairQueue *q)
Destroy a ippair queue.
IPPairQueue * IPPairQueueNew(void)
IPPairQueue * IPPairQueueInit(IPPairQueue *q)
IPPair * IPPairDequeue(IPPairQueue *q)
remove a ippair from the queue
IPPair * bot
uint32_t len
IPPair * top
struct IPPair_ * lnext
Definition ippair.h:73
struct IPPair_ * lprev
Definition ippair.h:74
#define BUG_ON(x)
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267
#define SCMalloc(sz)
Definition util-mem.h:47