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 
55 #include "util-print.h"
56 
58 
59 void *HttpHeaderThreadDataInit(void *data)
60 {
61  HttpHeaderThreadData *td = SCCalloc(1, sizeof(*td));
62  if (td != NULL) {
63  if (data == NULL) {
64  td->size_step = 512;
65  } else {
67  td->size_step = c->size_step;
68  }
69 
70  /* initialize minimal buffers */
71  (void)HttpHeaderExpandBuffer(td, &td->buffer, 1);
72  }
73  return td;
74 }
75 
76 void HttpHeaderThreadDataFree(void *data)
77 {
78  HttpHeaderThreadData *hdrnames = data;
79  SCFree(hdrnames->buffer.buffer);
80  SCFree(hdrnames);
81 }
82 
84  HttpHeaderBuffer *buf, uint32_t size)
85 {
86  size_t extra = td->size_step;
87  while ((buf->size + extra) < (size + buf->len)) {
88  extra += td->size_step;
89  }
90  SCLogDebug("adding %"PRIuMAX" to the buffer", (uintmax_t)extra);
91 
92  uint8_t *new_buffer = SCRealloc(buf->buffer, buf->size + extra);
93  if (unlikely(new_buffer == NULL)) {
94  buf->len = 0;
95  return -1;
96  }
97  buf->buffer = new_buffer;
98  buf->size += extra;
99  return 0;
100 }
101 
103  const int keyword_id, HttpHeaderThreadData **ret_hdr_td)
104 {
105  *ret_hdr_td = NULL;
106 
107  HttpHeaderThreadData *hdr_td =
108  DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, keyword_id);
109  if (hdr_td == NULL)
110  return NULL;
111  *ret_hdr_td = hdr_td;
112 
113  HttpHeaderBuffer *buf = &hdr_td->buffer;
114  buf->len = 0;
115  return buf;
116 }
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:298
HttpHeaderBuffer_::len
uint32_t len
Definition: detect-http-header-common.h:30
threads.h
Flow_
Flow data structure.
Definition: flow.h:353
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:1060
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:59
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:3552
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:102
HttpHeaderExpandBuffer
int HttpHeaderExpandBuffer(HttpHeaderThreadData *td, HttpHeaderBuffer *buf, uint32_t size)
Definition: detect-http-header-common.c:83
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:76
app-layer.h