Coverage Report

Created: 2025-07-23 07:29

/src/suricata7/src/defrag-queue.c
Line
Count
Source (jump to first uncovered line)
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
32
DefragTrackerQueue *DefragTrackerQueueInit (DefragTrackerQueue *q)
33
33
{
34
33
    if (q != NULL) {
35
33
        memset(q, 0, sizeof(DefragTrackerQueue));
36
33
        DQLOCK_INIT(q);
37
33
    }
38
33
    return q;
39
33
}
40
41
DefragTrackerQueue *DefragTrackerQueueNew(void)
42
0
{
43
0
    DefragTrackerQueue *q = (DefragTrackerQueue *)SCMalloc(sizeof(DefragTrackerQueue));
44
0
    if (q == NULL) {
45
0
        SCLogError("Fatal error encountered in DefragTrackerQueueNew. Exiting...");
46
0
        exit(EXIT_SUCCESS);
47
0
    }
48
0
    q = DefragTrackerQueueInit(q);
49
0
    return q;
50
0
}
51
52
/**
53
 *  \brief Destroy a tracker queue
54
 *
55
 *  \param q the tracker queue to destroy
56
 */
57
void DefragTrackerQueueDestroy (DefragTrackerQueue *q)
58
0
{
59
0
    DQLOCK_DESTROY(q);
60
0
}
61
62
/**
63
 *  \brief add a tracker to a queue
64
 *
65
 *  \param q queue
66
 *  \param dt tracker
67
 */
68
void DefragTrackerEnqueue (DefragTrackerQueue *q, DefragTracker *dt)
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
 */
101
DefragTracker *DefragTrackerDequeue (DefragTrackerQueue *q)
102
22.9k
{
103
22.9k
    DQLOCK_LOCK(q);
104
105
22.9k
    DefragTracker *dt = q->bot;
106
22.9k
    if (dt == NULL) {
107
22.9k
        DQLOCK_UNLOCK(q);
108
22.9k
        return NULL;
109
22.9k
    }
110
111
    /* more packets in queue */
112
0
    if (q->bot->lprev != NULL) {
113
0
        q->bot = q->bot->lprev;
114
0
        q->bot->lnext = NULL;
115
    /* just the one we remove, so now empty */
116
0
    } else {
117
0
        q->top = NULL;
118
0
        q->bot = NULL;
119
0
    }
120
121
#ifdef DEBUG
122
    BUG_ON(q->len == 0);
123
#endif
124
0
    if (q->len > 0)
125
0
        q->len--;
126
127
0
    dt->lnext = NULL;
128
0
    dt->lprev = NULL;
129
130
0
    DQLOCK_UNLOCK(q);
131
0
    return dt;
132
22.9k
}
133
134
uint32_t DefragTrackerQueueLen(DefragTrackerQueue *q)
135
0
{
136
0
    uint32_t len;
137
0
    DQLOCK_LOCK(q);
138
0
    len = q->len;
139
0
    DQLOCK_UNLOCK(q);
140
0
    return len;
141
0
}
142