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 
39 static 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 
50 static 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  xb->expire = 0; // not used by tx bits
62 
63  GenericVarAppend(&txd->txbits, (GenericVar *)xb);
64  return 1;
65  }
66  return 0;
67 }
68 
69 int 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 
80 int 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 }
tx-bit.h
GenericVarAppend
void GenericVarAppend(GenericVar **list, GenericVar *gv)
Definition: util-var.c:98
XBit_::expire
uint32_t expire
Definition: util-var.h:63
XBit_::next
GenericVar * next
Definition: util-var.h:62
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
threads.h
rust.h
util-var.h
util-debug.h
GenericVar_::next
struct GenericVar_ * next
Definition: util-var.h:55
TxBitIsset
int TxBitIsset(AppLayerTxData *txd, uint32_t idx)
Definition: tx-bit.c:69
detect.h
DETECT_XBITS
@ DETECT_XBITS
Definition: detect-engine-register.h:64
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1466
XBit_::type
uint16_t type
Definition: util-var.h:59
suricata-common.h
GenericVar_
Definition: util-var.h:51
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
TxBitSet
int TxBitSet(AppLayerTxData *txd, uint32_t idx)
Definition: tx-bit.c:80
XBit_::idx
uint32_t idx
Definition: util-var.h:61
XBit_
Definition: util-var.h:58