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-buffer.h"
32 #include "detect-engine-mpm.h"
33 #include "detect-engine-state.h"
36 #include "detect-content.h"
37 #include "detect-pcre.h"
38 
39 #include "flow.h"
40 #include "flow-var.h"
41 #include "flow-util.h"
42 
43 #include "util-debug.h"
44 #include "util-unittest.h"
45 #include "util-unittest-helper.h"
46 #include "util-spm.h"
47 #include "util-print.h"
48 
49 #include "app-layer.h"
50 #include "app-layer-parser.h"
51 
52 #include "app-layer-htp.h"
53 #include "detect-http-header.h"
54 #include "stream-tcp.h"
55 
57 
58 void *HttpHeaderThreadDataInit(void *data)
59 {
60  HttpHeaderThreadData *td = SCCalloc(1, sizeof(*td));
61  if (td != NULL) {
62  if (data == NULL) {
63  td->size_step = 512;
64  } else {
66  td->size_step = c->size_step;
67  }
68 
69  /* initialize minimal buffers */
70  (void)HttpHeaderExpandBuffer(td, &td->buffer, 1);
71  }
72  return td;
73 }
74 
75 void HttpHeaderThreadDataFree(void *data)
76 {
77  HttpHeaderThreadData *hdrnames = data;
78  SCFree(hdrnames->buffer.buffer);
79  SCFree(hdrnames);
80 }
81 
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 
106  if (hdr_td == NULL)
107  return NULL;
108  *ret_hdr_td = hdr_td;
109 
110  HttpHeaderBuffer *buf = &hdr_td->buffer;
111  buf->len = 0;
112  return buf;
113 }
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:282
SCDetectThreadCtxGetGlobalKeywordThreadCtx
void * SCDetectThreadCtxGetGlobalKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
Definition: detect-engine.c:4057
HttpHeaderBuffer_::len
uint32_t len
Definition: detect-http-header-common.h:30
threads.h
HttpHeaderGetBufferSpace
HttpHeaderBuffer * HttpHeaderGetBufferSpace(DetectEngineThreadCtx *det_ctx, uint8_t flags, const int keyword_id, HttpHeaderThreadData **ret_hdr_td)
Definition: detect-http-header-common.c:100
detect-http-header.h
HttpHeaderExpandBuffer
int HttpHeaderExpandBuffer(HttpHeaderThreadData *td, HttpHeaderBuffer *buf, size_t size)
Definition: detect-http-header-common.c:82
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:1300
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:58
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
util-spm.h
detect-engine-buffer.h
HttpHeaderBuffer_::size
uint32_t size
Definition: detect-http-header-common.h:29
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
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:75
app-layer.h