suricata
tx-bit.c
Go to the documentation of this file.
1/* Copyright (C) 2014-2021 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 * Implements per ippair bits. Actually, not a bit,
24 * but called that way because of Snort's flowbits.
25 * It's a binary storage.
26 *
27 * \todo move away from a linked list implementation
28 * \todo use different datatypes, such as string, int, etc.
29 */
30
31#include "suricata-common.h"
32#include "threads.h"
33#include "tx-bit.h"
34#include "detect.h"
35#include "util-var.h"
36#include "util-debug.h"
37#include "rust.h"
38
39static XBit *TxBitGet(AppLayerTxData *txd, uint32_t idx)
40{
41 for (GenericVar *gv = txd->txbits; gv != NULL; gv = gv->next) {
42 if (gv->type == DETECT_XBITS && gv->idx == idx) {
43 return (XBit *)gv;
44 }
45 }
46
47 return NULL;
48}
49
50static int TxBitAdd(AppLayerTxData *txd, uint32_t idx)
51{
52 XBit *xb = TxBitGet(txd, idx);
53 if (xb == NULL) {
54 xb = SCMalloc(sizeof(XBit));
55 if (unlikely(xb == NULL))
56 return -1;
57
58 xb->type = DETECT_XBITS;
59 xb->idx = idx;
60 xb->next = NULL;
61 SCTIME_INIT(xb->expire); // not used by tx bits
62
63 GenericVarAppend(&txd->txbits, (GenericVar *)xb);
64 return 1;
65 }
66 return 0;
67}
68
69int TxBitIsset(AppLayerTxData *txd, uint32_t idx)
70{
71 XBit *xb = TxBitGet(txd, idx);
72 if (xb != NULL) {
73 SCLogDebug("isset %u return 1", idx);
74 return 1;
75 }
76 SCLogDebug("isset %u r 0", idx);
77 return 0;
78}
79
80int TxBitSet(AppLayerTxData *txd, uint32_t idx)
81{
82 int r = TxBitAdd(txd, idx);
83 SCLogDebug("set %u r %d", idx, r);
84 return (r == 1);
85}
struct AppLayerTxData AppLayerTxData
uint16_t type
Definition util-var.h:61
GenericVar * next
Definition util-var.h:64
SCTime_t expire
Definition util-var.h:65
uint32_t idx
Definition util-var.h:63
int TxBitIsset(AppLayerTxData *txd, uint32_t idx)
Definition tx-bit.c:69
int TxBitSet(AppLayerTxData *txd, uint32_t idx)
Definition tx-bit.c:80
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCMalloc(sz)
Definition util-mem.h:47
#define unlikely(expr)
#define SCTIME_INIT(t)
Definition util-time.h:45
void GenericVarAppend(GenericVar **list, GenericVar *gv)
Definition util-var.c:98