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"
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
57void *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
75{
76 HttpHeaderThreadData *hdrnames = data;
77 SCFree(hdrnames->buffer.buffer);
78 SCFree(hdrnames);
79}
80
82{
83 size_t extra = td->size_step;
84 while ((buf->size + extra) < (size + buf->len)) {
85 extra += td->size_step;
86 }
87 SCLogDebug("adding %"PRIuMAX" to the buffer", (uintmax_t)extra);
88
89 uint8_t *new_buffer = SCRealloc(buf->buffer, buf->size + extra);
90 if (unlikely(new_buffer == NULL)) {
91 buf->len = 0;
92 return -1;
93 }
94 buf->buffer = new_buffer;
95 buf->size += extra;
96 return 0;
97}
98
100 const int keyword_id, HttpHeaderThreadData **ret_hdr_td)
101{
102 *ret_hdr_td = NULL;
103
104 HttpHeaderThreadData *hdr_td =
105 DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, keyword_id);
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}
uint8_t flags
Definition decode-gre.h:0
Data structures and function prototypes for keeping state for the detection engine.
void * DetectThreadCtxGetGlobalKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
int HttpHeaderExpandBuffer(HttpHeaderThreadData *td, HttpHeaderBuffer *buf, size_t size)
HttpHeaderBuffer * HttpHeaderGetBufferSpace(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, const int keyword_id, HttpHeaderThreadData **ret_hdr_td)
void * HttpHeaderThreadDataInit(void *data)
void HttpHeaderThreadDataFree(void *data)
Flow data structure.
Definition flow.h:356
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCFree(p)
Definition util-mem.h:61
#define SCRealloc(ptr, sz)
Definition util-mem.h:50
#define SCCalloc(nm, sz)
Definition util-mem.h:53
#define unlikely(expr)