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