suricata
detect-http-header-common.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-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  * \ingroup httplayer
20  *
21  * @{
22  */
23 
24 #include "suricata-common.h"
25 #include "threads.h"
26 #include "decode.h"
27 
28 #include "detect.h"
29 #include "detect-parse.h"
30 #include "detect-engine.h"
31 #include "detect-engine-mpm.h"
32 #include "detect-engine-state.h"
35 #include "detect-content.h"
36 #include "detect-pcre.h"
37 
38 #include "flow.h"
39 #include "flow-var.h"
40 #include "flow-util.h"
41 
42 #include "util-debug.h"
43 #include "util-unittest.h"
44 #include "util-unittest-helper.h"
45 #include "util-spm.h"
46 #include "util-print.h"
47 
48 #include "app-layer.h"
49 #include "app-layer-parser.h"
50 
51 #include "app-layer-htp.h"
52 #include "detect-http-header.h"
53 #include "stream-tcp.h"
54 
56 
57 void *HttpHeaderThreadDataInit(void *data)
58 {
59  HttpHeaderThreadData *td = SCCalloc(1, sizeof(*td));
60  if (td != NULL) {
61  if (data == NULL) {
62  td->size_step = 512;
63  } else {
65  td->size_step = c->size_step;
66  }
67 
68  /* initialize minimal buffers */
69  (void)HttpHeaderExpandBuffer(td, &td->buffer, 1);
70  }
71  return td;
72 }
73 
74 void HttpHeaderThreadDataFree(void *data)
75 {
76  HttpHeaderThreadData *hdrnames = data;
77  SCFree(hdrnames->buffer.buffer);
78  SCFree(hdrnames);
79 }
80 
82  HttpHeaderBuffer *buf, uint32_t size)
83 {
84  size_t extra = td->size_step;
85  while ((buf->size + extra) < (size + buf->len)) {
86  extra += td->size_step;
87  }
88  SCLogDebug("adding %"PRIuMAX" to the buffer", (uintmax_t)extra);
89 
90  uint8_t *new_buffer = SCRealloc(buf->buffer, buf->size + extra);
91  if (unlikely(new_buffer == NULL)) {
92  buf->len = 0;
93  return -1;
94  }
95  buf->buffer = new_buffer;
96  buf->size += extra;
97  return 0;
98 }
99 
101  const int keyword_id, HttpHeaderThreadData **ret_hdr_td)
102 {
103  *ret_hdr_td = NULL;
104 
105  HttpHeaderThreadData *hdr_td =
106  DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, keyword_id);
107  if (hdr_td == NULL)
108  return NULL;
109  *ret_hdr_td = hdr_td;
110 
111  HttpHeaderBuffer *buf = &hdr_td->buffer;
112  buf->len = 0;
113  return buf;
114 }
detect-content.h
detect-engine.h
HttpHeaderThreadData_::buffer
HttpHeaderBuffer buffer
Definition: detect-http-header-common.h:38
HttpHeaderThreadData_::size_step
uint16_t size_step
Definition: detect-http-header-common.h:39
flow-util.h
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
HttpHeaderBuffer_::len
uint32_t len
Definition: detect-http-header-common.h:30
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
detect-http-header.h
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
util-unittest-helper.h
HttpHeaderThreadConfig_::size_step
uint16_t size_step
Definition: detect-http-header-common.h:34
app-layer-htp.h
decode.h
util-debug.h
DetectEngineThreadCtx_
Definition: detect.h:1098
util-print.h
detect-engine-mpm.h
detect.h
app-layer-parser.h
detect-http-header-common.h
HttpHeaderThreadConfig_
Definition: detect-http-header-common.h:33
HttpHeaderBuffer_::buffer
uint8_t * buffer
Definition: detect-http-header-common.h:28
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
detect-engine-content-inspection.h
HttpHeaderThreadDataInit
void * HttpHeaderThreadDataInit(void *data)
Definition: detect-http-header-common.c:57
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
util-spm.h
HttpHeaderBuffer_::size
uint32_t size
Definition: detect-http-header-common.h:29
DetectThreadCtxGetGlobalKeywordThreadCtx
void * DetectThreadCtxGetGlobalKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
Definition: detect-engine.c:3783
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
HttpHeaderGetBufferSpace
HttpHeaderBuffer * HttpHeaderGetBufferSpace(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, const int keyword_id, HttpHeaderThreadData **ret_hdr_td)
Definition: detect-http-header-common.c:100
HttpHeaderExpandBuffer
int HttpHeaderExpandBuffer(HttpHeaderThreadData *td, HttpHeaderBuffer *buf, uint32_t size)
Definition: detect-http-header-common.c:81
HttpHeaderBuffer_
Definition: detect-http-header-common.h:27
flow.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
flow-var.h
HttpHeaderThreadData_
Definition: detect-http-header-common.h:37
HttpHeaderThreadDataFree
void HttpHeaderThreadDataFree(void *data)
Definition: detect-http-header-common.c:74
app-layer.h