suricata
app-layer-htp-range.h
Go to the documentation of this file.
1 /* Copyright (C) 2024 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 #ifndef SURICATA_APP_LAYER_HTP_RANGE_H
19 #define SURICATA_APP_LAYER_HTP_RANGE_H
20 
21 #include "util-file.h"
22 
23 // forward declarations
26 typedef struct HTTPContentRange HTTPContentRange;
27 
28 /** A structure representing a single range request :
29  * either skipping, buffering, or appending
30  * As this belongs to a flow, appending data to it is ensured to be thread-safe
31  * Only one block per file has the pointer to the container
32  */
33 typedef struct HttpRangeContainerBlock {
34  /** state where we skip content */
35  uint64_t toskip;
36  /** current out of order range to write into */
38  /** pointer to the main file container, where to directly append data */
40  /** file container we are owning for now */
43 
45 
46 // HttpRangeContainerBlock but trouble with headers inclusion order
47 HttpRangeContainerBlock *SCHttpRangeContainerOpenFile(const unsigned char *key, uint32_t keylen,
48  const Flow *f, const HTTPContentRange *cr, const StreamingBufferConfig *sbcfg,
49  const unsigned char *name, uint16_t name_len, uint16_t flags, const unsigned char *data,
50  uint32_t data_len);
51 
53  const uint8_t *data, uint32_t len);
54 
55 bool SCHTPFileCloseHandleRange(const StreamingBufferConfig *sbcfg, FileContainer *, const uint16_t,
56  HttpRangeContainerBlock *, const uint8_t *, uint32_t);
57 
58 #ifndef SURICATA_BINDGEN_H
59 
60 #include "util-streaming-buffer.h"
61 #include "util-thash.h"
62 
63 void HttpRangeContainersInit(void);
66 
67 // linked list of ranges : buffer with offset
68 typedef struct HttpRangeContainerBuffer {
69  /** red and black tree */
71  /** allocated buffer */
72  uint8_t *buffer;
73  /** length of buffer */
74  uint64_t buflen;
75  /** the start of the range (offset relative to the absolute beginning of the file) */
76  uint64_t start;
77  /** offset of bytes written in buffer (relative to the start of the range) */
78  uint64_t offset;
79  /** number of gaped bytes */
80  uint64_t gap;
82 
84 
87 
88 /** Item in hash table for a file in multiple ranges
89  * Thread-safety is ensured with the thread-safe hash table cf THashData
90  * The number of use is increased for each flow opening a new HttpRangeContainerBlock
91  * until it closes this HttpRangeContainerBlock
92  * The design goal is to have concurrency only on opening and closing a range request
93  * and have a lock-free data structure belonging to one Flow
94  * (see HttpRangeContainerBlock below)
95  * for every append in between (we suppose we have many appends per range request)
96  */
97 typedef struct HttpRangeContainerFile {
98  /** key for hashtable */
99  uint8_t *key;
100  /** key length */
101  uint32_t len;
102  /** expire time in epoch */
104  /** pointer to hashtable data, for locking and use count */
106  /** total expected size of the file in ranges */
107  uint64_t totalsize;
108  /** size of the file after last sync */
109  uint64_t lastsize;
110  /** streaming buffer config for files below */
112  /** file container, with only one file */
114  /** red and black tree list of ranges which came out of order */
115  struct HTTP_RANGES fragment_tree;
116  /** file flags */
117  uint16_t flags;
118  /** error condition for this range. Its up to timeout handling to cleanup */
119  bool error;
121 
123  const StreamingBufferConfig *sbcfg, HttpRangeContainerBlock *c, uint16_t flags);
124 
125 uint64_t HTPByteRangeMemcapGlobalCounter(void);
126 uint64_t HTPByteRangeMemuseGlobalCounter(void);
127 int HTPByteRangeSetMemcap(uint64_t);
128 #endif // SURICATA_BINDGEN_H
129 
130 #endif /* SURICATA_APP_LAYER_HTP_RANGE_H */
HttpRangeContainerBuffer::gap
uint64_t gap
Definition: app-layer-htp-range.h:80
HttpRangeContainerBuffer::buffer
uint8_t * buffer
Definition: app-layer-htp-range.h:72
SCHttpRangeAppendData
int SCHttpRangeAppendData(const StreamingBufferConfig *sbcfg, HttpRangeContainerBlock *c, const uint8_t *data, uint32_t len)
Definition: app-layer-htp-range.c:378
FileContainer_
Definition: util-file.h:37
len
uint8_t len
Definition: app-layer-dnp3.h:2
ts
uint64_t ts
Definition: source-erf-file.c:55
HttpRangeContainersTimeoutHash
uint32_t HttpRangeContainersTimeoutHash(const SCTime_t ts)
Definition: app-layer-htp-range.c:210
HttpRangeContainerBuffer::buflen
uint64_t buflen
Definition: app-layer-htp-range.h:74
HttpRangeContainersDestroy
void HttpRangeContainersDestroy(void)
Definition: app-layer-htp-range.c:205
HttpRangeContainerFile::files
FileContainer * files
Definition: app-layer-htp-range.h:113
name
const char * name
Definition: detect-engine-proto.c:48
Flow_
Flow data structure.
Definition: flow.h:348
HttpRangeContainerBuffer::start
uint64_t start
Definition: app-layer-htp-range.h:76
HttpRangeContainerFile::error
bool error
Definition: app-layer-htp-range.h:119
HttpRangeContainerBlock::toskip
uint64_t toskip
Definition: app-layer-htp-range.h:35
HttpRangeContainerFile::flags
uint16_t flags
Definition: app-layer-htp-range.h:117
HttpRangeContainerBlock
Definition: app-layer-htp-range.h:33
HttpRangeContainerFile
Definition: app-layer-htp-range.h:97
HTTPContentRange
struct HTTPContentRange HTTPContentRange
Definition: app-layer-htp-range.h:26
HttpRangeClose
File * HttpRangeClose(const StreamingBufferConfig *sbcfg, HttpRangeContainerBlock *c, uint16_t flags)
Definition: app-layer-htp-range.c:451
HttpRangeContainerFile::lastsize
uint64_t lastsize
Definition: app-layer-htp-range.h:109
HttpRangeContainerBlock
struct HttpRangeContainerBlock HttpRangeContainerBlock
HttpRangeContainerFile
struct HttpRangeContainerFile HttpRangeContainerFile
Definition: app-layer-htp-range.h:25
HttpRangeContainerBuffer
struct HttpRangeContainerBuffer HttpRangeContainerBuffer
Definition: app-layer-htp-range.h:24
HttpRangeContainerBlock::current
HttpRangeContainerBuffer * current
Definition: app-layer-htp-range.h:37
SCHTPFileCloseHandleRange
bool SCHTPFileCloseHandleRange(const StreamingBufferConfig *sbcfg, FileContainer *, const uint16_t, HttpRangeContainerBlock *, const uint8_t *, uint32_t)
close range, add reassembled file if possible
Definition: app-layer-htp-range.c:635
HttpRangeContainerBuffer::RB_ENTRY
RB_ENTRY(HttpRangeContainerBuffer) rb
SCTime_t
Definition: util-time.h:40
HTPByteRangeMemuseGlobalCounter
uint64_t HTPByteRangeMemuseGlobalCounter(void)
Definition: app-layer-htp-range.c:62
HttpRangeContainerFile::key
uint8_t * key
Definition: app-layer-htp-range.h:99
SCHttpRangeContainerOpenFile
HttpRangeContainerBlock * SCHttpRangeContainerOpenFile(const unsigned char *key, uint32_t keylen, const Flow *f, const HTTPContentRange *cr, const StreamingBufferConfig *sbcfg, const unsigned char *name, uint16_t name_len, uint16_t flags, const unsigned char *data, uint32_t data_len)
HttpRangeContainerFile::hdata
THashData * hdata
Definition: app-layer-htp-range.h:105
HttpRangeContainerBlock::container
HttpRangeContainerFile * container
Definition: app-layer-htp-range.h:39
HttpRangeContainerBuffer::offset
uint64_t offset
Definition: app-layer-htp-range.h:78
util-file.h
File_
Definition: util-file.h:139
HttpRangeContainerBufferCompare
int HttpRangeContainerBufferCompare(HttpRangeContainerBuffer *a, HttpRangeContainerBuffer *b)
Definition: app-layer-htp-range.c:68
THashData_
Definition: util-thash.h:85
RB_HEAD
RB_HEAD(HTTP_RANGES, HttpRangeContainerBuffer)
flags
uint8_t flags
Definition: decode-gre.h:0
HttpRangeContainersInit
void HttpRangeContainersInit(void)
Definition: app-layer-htp-range.c:172
util-streaming-buffer.h
HttpRangeContainerFile::totalsize
uint64_t totalsize
Definition: app-layer-htp-range.h:107
HttpRangeContainerFile::len
uint32_t len
Definition: app-layer-htp-range.h:101
StreamingBufferConfig_
Definition: util-streaming-buffer.h:65
SCHttpRangeFreeBlock
void SCHttpRangeFreeBlock(HttpRangeContainerBlock *b)
Definition: app-layer-htp-range.c:608
HTPByteRangeSetMemcap
int HTPByteRangeSetMemcap(uint64_t)
Definition: app-layer-htp-range.c:46
HttpRangeContainerFile::fragment_tree
struct HTTP_RANGES fragment_tree
Definition: app-layer-htp-range.h:115
util-thash.h
HttpRangeContainerFile::expire
SCTime_t expire
Definition: app-layer-htp-range.h:103
HTPByteRangeMemcapGlobalCounter
uint64_t HTPByteRangeMemcapGlobalCounter(void)
Definition: app-layer-htp-range.c:56
HttpRangeContainerFile::sbcfg
const StreamingBufferConfig * sbcfg
Definition: app-layer-htp-range.h:111
RB_PROTOTYPE
RB_PROTOTYPE(HTTP_RANGES, HttpRangeContainerBuffer, rb, HttpRangeContainerBufferCompare)
HttpRangeContainerBlock::files
FileContainer * files
Definition: app-layer-htp-range.h:41
HttpRangeContainerBuffer
Definition: app-layer-htp-range.h:68