/src/suricata8/src/host-queue.c
Line | Count | Source |
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 | | |
33 | | HostQueue *HostQueueInit (HostQueue *q) |
34 | 78 | { |
35 | 78 | if (q != NULL) { |
36 | 78 | memset(q, 0, sizeof(HostQueue)); |
37 | 78 | HQLOCK_INIT(q); |
38 | 78 | } |
39 | 78 | return q; |
40 | 78 | } |
41 | | |
42 | | HostQueue *HostQueueNew(void) |
43 | 0 | { |
44 | 0 | HostQueue *q = (HostQueue *)SCMalloc(sizeof(HostQueue)); |
45 | 0 | if (q == NULL) { |
46 | 0 | SCLogError("Fatal error encountered in HostQueueNew. Exiting..."); |
47 | 0 | exit(EXIT_SUCCESS); |
48 | 0 | } |
49 | 0 | q = HostQueueInit(q); |
50 | 0 | return q; |
51 | 0 | } |
52 | | |
53 | | /** |
54 | | * \brief Destroy a host queue |
55 | | * |
56 | | * \param q the host queue to destroy |
57 | | */ |
58 | | void HostQueueDestroy (HostQueue *q) |
59 | 0 | { |
60 | 0 | HQLOCK_DESTROY(q); |
61 | 0 | } |
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 | 78.0k | { |
71 | | #ifdef DEBUG |
72 | | BUG_ON(q == NULL || h == NULL); |
73 | | #endif |
74 | | |
75 | 78.0k | HQLOCK_LOCK(q); |
76 | | |
77 | | /* more hosts in queue */ |
78 | 78.0k | if (q->top != NULL) { |
79 | 77.9k | h->lnext = q->top; |
80 | 77.9k | q->top->lprev = h; |
81 | 77.9k | q->top = h; |
82 | | /* only host */ |
83 | 77.9k | } else { |
84 | 78 | q->top = h; |
85 | 78 | q->bot = h; |
86 | 78 | } |
87 | 78.0k | q->len++; |
88 | | #ifdef DBG_PERF |
89 | | if (q->len > q->dbg_maxlen) |
90 | | q->dbg_maxlen = q->len; |
91 | | #endif /* DBG_PERF */ |
92 | 78.0k | HQLOCK_UNLOCK(q); |
93 | 78.0k | } |
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 | | */ |
102 | | Host *HostDequeue (HostQueue *q) |
103 | 462 | { |
104 | 462 | HQLOCK_LOCK(q); |
105 | | |
106 | 462 | Host *h = q->bot; |
107 | 462 | if (h == NULL) { |
108 | 0 | HQLOCK_UNLOCK(q); |
109 | 0 | return NULL; |
110 | 0 | } |
111 | | |
112 | | /* more packets in queue */ |
113 | 462 | if (q->bot->lprev != NULL) { |
114 | 462 | q->bot = q->bot->lprev; |
115 | 462 | q->bot->lnext = NULL; |
116 | | /* just the one we remove, so now empty */ |
117 | 462 | } else { |
118 | 0 | q->top = NULL; |
119 | 0 | q->bot = NULL; |
120 | 0 | } |
121 | | |
122 | | #ifdef DEBUG |
123 | | BUG_ON(q->len == 0); |
124 | | #endif |
125 | 462 | if (q->len > 0) |
126 | 462 | q->len--; |
127 | | |
128 | 462 | h->lnext = NULL; |
129 | 462 | h->lprev = NULL; |
130 | | |
131 | 462 | HQLOCK_UNLOCK(q); |
132 | 462 | return h; |
133 | 462 | } |