suricata
defrag-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  * Defrag tracker queue handler functions
24  */
25 
26 #include "suricata-common.h"
27 #include "defrag-queue.h"
28 #include "util-error.h"
29 #include "util-debug.h"
30 #include "util-print.h"
31 
33 {
34  if (q != NULL) {
35  memset(q, 0, sizeof(DefragTrackerQueue));
36  DQLOCK_INIT(q);
37  }
38  return q;
39 }
40 
42 {
44  if (q == NULL) {
45  SCLogError("Fatal error encountered in DefragTrackerQueueNew. Exiting...");
46  exit(EXIT_SUCCESS);
47  }
49  return q;
50 }
51 
52 /**
53  * \brief Destroy a tracker queue
54  *
55  * \param q the tracker queue to destroy
56  */
58 {
59  DQLOCK_DESTROY(q);
60 }
61 
62 /**
63  * \brief add a tracker to a queue
64  *
65  * \param q queue
66  * \param dt tracker
67  */
69 {
70 #ifdef DEBUG
71  BUG_ON(q == NULL || dt == NULL);
72 #endif
73 
74  DQLOCK_LOCK(q);
75 
76  /* more trackers in queue */
77  if (q->top != NULL) {
78  dt->lnext = q->top;
79  q->top->lprev = dt;
80  q->top = dt;
81  /* only tracker */
82  } else {
83  q->top = dt;
84  q->bot = dt;
85  }
86  q->len++;
87 #ifdef DBG_PERF
88  if (q->len > q->dbg_maxlen)
89  q->dbg_maxlen = q->len;
90 #endif /* DBG_PERF */
91  DQLOCK_UNLOCK(q);
92 }
93 
94 /**
95  * \brief remove a tracker from the queue
96  *
97  * \param q queue
98  *
99  * \retval dt tracker or NULL if empty list.
100  */
102 {
103  DQLOCK_LOCK(q);
104 
105  DefragTracker *dt = q->bot;
106  if (dt == NULL) {
107  DQLOCK_UNLOCK(q);
108  return NULL;
109  }
110 
111  /* more packets in queue */
112  if (q->bot->lprev != NULL) {
113  q->bot = q->bot->lprev;
114  q->bot->lnext = NULL;
115  /* just the one we remove, so now empty */
116  } else {
117  q->top = NULL;
118  q->bot = NULL;
119  }
120 
121 #ifdef DEBUG
122  BUG_ON(q->len == 0);
123 #endif
124  if (q->len > 0)
125  q->len--;
126 
127  dt->lnext = NULL;
128  dt->lprev = NULL;
129 
130  DQLOCK_UNLOCK(q);
131  return dt;
132 }
133 
135 {
136  uint32_t len;
137  DQLOCK_LOCK(q);
138  len = q->len;
139  DQLOCK_UNLOCK(q);
140  return len;
141 }
142 
DefragTrackerQueueNew
DefragTrackerQueue * DefragTrackerQueueNew(void)
Definition: defrag-queue.c:41
len
uint8_t len
Definition: app-layer-dnp3.h:2
DQLOCK_INIT
#define DQLOCK_INIT(q)
Definition: defrag-queue.h:65
DefragTrackerQueueDestroy
void DefragTrackerQueueDestroy(DefragTrackerQueue *q)
Destroy a tracker queue.
Definition: defrag-queue.c:57
DQLOCK_LOCK
#define DQLOCK_LOCK(q)
Definition: defrag-queue.h:67
util-debug.h
util-error.h
DefragTrackerQueueLen
uint32_t DefragTrackerQueueLen(DefragTrackerQueue *q)
Definition: defrag-queue.c:134
DefragTrackerQueue_::bot
DefragTracker * bot
Definition: defrag-queue.h:44
DefragTracker_
Definition: defrag.h:86
util-print.h
defrag-queue.h
DefragTrackerDequeue
DefragTracker * DefragTrackerDequeue(DefragTrackerQueue *q)
remove a tracker from the queue
Definition: defrag-queue.c:101
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:289
DefragTrackerQueue_
Definition: defrag-queue.h:42
DefragTrackerQueue_::len
uint32_t len
Definition: defrag-queue.h:45
suricata-common.h
DQLOCK_DESTROY
#define DQLOCK_DESTROY(q)
Definition: defrag-queue.h:66
DefragTrackerQueueInit
DefragTrackerQueue * DefragTrackerQueueInit(DefragTrackerQueue *q)
Definition: defrag-queue.c:32
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
DefragTrackerEnqueue
void DefragTrackerEnqueue(DefragTrackerQueue *q, DefragTracker *dt)
add a tracker to a queue
Definition: defrag-queue.c:68
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
DefragTracker_::lnext
struct DefragTracker_ * lnext
Definition: defrag.h:122
DefragTracker_::lprev
struct DefragTracker_ * lprev
Definition: defrag.h:123
DefragTrackerQueue_::top
DefragTracker * top
Definition: defrag-queue.h:43
DQLOCK_UNLOCK
#define DQLOCK_UNLOCK(q)
Definition: defrag-queue.h:69