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 #include "app-layer-parser.h"
39 
40 static XBit *TxBitGet(AppLayerTxData *txd, uint32_t idx)
41 {
42  for (GenericVar *gv = txd->txbits; gv != NULL; gv = gv->next) {
43  if (gv->type == DETECT_XBITS && gv->idx == idx) {
44  return (XBit *)gv;
45  }
46  }
47 
48  return NULL;
49 }
50 
51 static int TxBitAdd(AppLayerTxData *txd, uint32_t idx)
52 {
53  XBit *xb = TxBitGet(txd, idx);
54  if (xb == NULL) {
55  xb = SCMalloc(sizeof(XBit));
56  if (unlikely(xb == NULL))
57  return -1;
58 
59  xb->type = DETECT_XBITS;
60  xb->idx = idx;
61  xb->next = NULL;
62  SCTIME_INIT(xb->expire); // not used by tx bits
63 
64  GenericVarAppend(&txd->txbits, (GenericVar *)xb);
65  return 1;
66  }
67  return 0;
68 }
69 
70 int TxBitIsset(AppLayerTxData *txd, uint32_t idx)
71 {
72  XBit *xb = TxBitGet(txd, idx);
73  if (xb != NULL) {
74  SCLogDebug("isset %u return 1", idx);
75  return 1;
76  }
77  SCLogDebug("isset %u r 0", idx);
78  return 0;
79 }
80 
81 int TxBitSet(AppLayerTxData *txd, uint32_t idx)
82 {
83  int r = TxBitAdd(txd, idx);
84  SCLogDebug("set %u r %d", idx, r);
85  return (r == 1);
86 }
tx-bit.h
GenericVarAppend
void GenericVarAppend(GenericVar **list, GenericVar *gv)
Definition: util-var.c:98
XBit_::next
GenericVar * next
Definition: util-var.h:65
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
threads.h
XBit_::expire
SCTime_t expire
Definition: util-var.h:66
rust.h
util-var.h
util-debug.h
GenericVar_::next
struct GenericVar_ * next
Definition: util-var.h:57
AppLayerTxData
Definition: app-layer-parser.h:177
TxBitIsset
int TxBitIsset(AppLayerTxData *txd, uint32_t idx)
Definition: tx-bit.c:70
SCTIME_INIT
#define SCTIME_INIT(t)
Definition: util-time.h:45
detect.h
app-layer-parser.h
DETECT_XBITS
@ DETECT_XBITS
Definition: detect-engine-register.h:64
AppLayerTxData::txbits
GenericVar * txbits
Definition: app-layer-parser.h:220
XBit_::type
uint16_t type
Definition: util-var.h:62
suricata-common.h
GenericVar_
Definition: util-var.h:53
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
TxBitSet
int TxBitSet(AppLayerTxData *txd, uint32_t idx)
Definition: tx-bit.c:81
XBit_::idx
uint32_t idx
Definition: util-var.h:64
XBit_
Definition: util-var.h:61