suricata
host-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  * Host queue handler functions
24  */
25 
26 #include "suricata-common.h"
27 #include "threads.h"
28 #include "host-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(HostQueue));
37  HQLOCK_INIT(q);
38  }
39  return q;
40 }
41 
43 {
44  HostQueue *q = (HostQueue *)SCMalloc(sizeof(HostQueue));
45  if (q == NULL) {
46  SCLogError("Fatal error encountered in HostQueueNew. Exiting...");
47  exit(EXIT_SUCCESS);
48  }
49  q = HostQueueInit(q);
50  return q;
51 }
52 
53 /**
54  * \brief Destroy a host queue
55  *
56  * \param q the host queue to destroy
57  */
59 {
60  HQLOCK_DESTROY(q);
61 }
62 
63 /**
64  * \brief add a host to a queue
65  *
66  * \param q queue
67  * \param h host
68  */
69 void HostEnqueue (HostQueue *q, Host *h)
70 {
71 #ifdef DEBUG
72  BUG_ON(q == NULL || h == NULL);
73 #endif
74 
75  HQLOCK_LOCK(q);
76 
77  /* more hosts in queue */
78  if (q->top != NULL) {
79  h->lnext = q->top;
80  q->top->lprev = h;
81  q->top = h;
82  /* only host */
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 */
92  HQLOCK_UNLOCK(q);
93 }
94 
95 /**
96  * \brief remove a host from the queue
97  *
98  * \param q queue
99  *
100  * \retval h host or NULL if empty list.
101  */
103 {
104  HQLOCK_LOCK(q);
105 
106  Host *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 }
HQLOCK_LOCK
#define HQLOCK_LOCK(q)
Definition: host-queue.h:67
HostQueue_
Definition: host-queue.h:42
HQLOCK_DESTROY
#define HQLOCK_DESTROY(q)
Definition: host-queue.h:66
threads.h
HostQueueDestroy
void HostQueueDestroy(HostQueue *q)
Destroy a host queue.
Definition: host-queue.c:58
Host_::lnext
struct Host_ * lnext
Definition: host.h:76
util-debug.h
util-error.h
util-print.h
host-queue.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
Host_::lprev
struct Host_ * lprev
Definition: host.h:77
HostQueueNew
HostQueue * HostQueueNew(void)
Definition: host-queue.c:42
HQLOCK_UNLOCK
#define HQLOCK_UNLOCK(q)
Definition: host-queue.h:69
suricata-common.h
HostQueue_::len
uint32_t len
Definition: host-queue.h:45
HQLOCK_INIT
#define HQLOCK_INIT(q)
Definition: host-queue.h:65
HostQueue_::bot
Host * bot
Definition: host-queue.h:44
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
HostDequeue
Host * HostDequeue(HostQueue *q)
remove a host from the queue
Definition: host-queue.c:102
HostEnqueue
void HostEnqueue(HostQueue *q, Host *h)
add a host to a queue
Definition: host-queue.c:69
HostQueue_::top
Host * top
Definition: host-queue.h:43
Host_
Definition: host.h:58
HostQueueInit
HostQueue * HostQueueInit(HostQueue *q)
Definition: host-queue.c:33