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 #ifndef SURICATA_BINDGEN_H
56 
57 #include "util-streaming-buffer.h"
58 #include "util-thash.h"
59 
60 void HttpRangeContainersInit(void);
63 
64 // linked list of ranges : buffer with offset
65 typedef struct HttpRangeContainerBuffer {
66  /** red and black tree */
68  /** allocated buffer */
69  uint8_t *buffer;
70  /** length of buffer */
71  uint64_t buflen;
72  /** the start of the range (offset relative to the absolute beginning of the file) */
73  uint64_t start;
74  /** offset of bytes written in buffer (relative to the start of the range) */
75  uint64_t offset;
76  /** number of gaped bytes */
77  uint64_t gap;
79 
81 
84 
85 /** Item in hash table for a file in multiple ranges
86  * Thread-safety is ensured with the thread-safe hash table cf THashData
87  * The number of use is increased for each flow opening a new HttpRangeContainerBlock
88  * until it closes this HttpRangeContainerBlock
89  * The design goal is to have concurrency only on opening and closing a range request
90  * and have a lock-free data structure belonging to one Flow
91  * (see HttpRangeContainerBlock below)
92  * for every append in between (we suppose we have many appends per range request)
93  */
94 typedef struct HttpRangeContainerFile {
95  /** key for hashtable */
96  uint8_t *key;
97  /** key length */
98  uint32_t len;
99  /** expire time in epoch */
101  /** pointer to hashtable data, for locking and use count */
103  /** total expected size of the file in ranges */
104  uint64_t totalsize;
105  /** size of the file after last sync */
106  uint64_t lastsize;
107  /** streaming buffer config for files below */
109  /** file container, with only one file */
111  /** red and black tree list of ranges which came out of order */
112  struct HTTP_RANGES fragment_tree;
113  /** file flags */
114  uint16_t flags;
115  /** error condition for this range. Its up to timeout handling to cleanup */
116  bool error;
118 
120  const StreamingBufferConfig *sbcfg, HttpRangeContainerBlock *c, uint16_t flags);
121 
122 uint64_t HTPByteRangeMemcapGlobalCounter(void);
123 uint64_t HTPByteRangeMemuseGlobalCounter(void);
124 int HTPByteRangeSetMemcap(uint64_t);
125 #endif // SURICATA_BINDGEN_H
126 
127 #endif /* SURICATA_APP_LAYER_HTP_RANGE_H */
HttpRangeContainerBuffer::gap
uint64_t gap
Definition: app-layer-htp-range.h:77
HttpRangeContainerBuffer::buffer
uint8_t * buffer
Definition: app-layer-htp-range.h:69
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:71
HttpRangeContainersDestroy
void HttpRangeContainersDestroy(void)
Definition: app-layer-htp-range.c:205
HttpRangeContainerFile::files
FileContainer * files
Definition: app-layer-htp-range.h:110
Flow_
Flow data structure.
Definition: flow.h:348
HttpRangeContainerBuffer::start
uint64_t start
Definition: app-layer-htp-range.h:73
HttpRangeContainerFile::error
bool error
Definition: app-layer-htp-range.h:116
HttpRangeContainerBlock::toskip
uint64_t toskip
Definition: app-layer-htp-range.h:35
HttpRangeContainerFile::flags
uint16_t flags
Definition: app-layer-htp-range.h:114
HttpRangeContainerBlock
Definition: app-layer-htp-range.h:33
HttpRangeContainerFile
Definition: app-layer-htp-range.h:94
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:106
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
HttpRangeContainerBuffer::RB_ENTRY
RB_ENTRY(HttpRangeContainerBuffer) rb
SCTime_t
Definition: util-time.h:40
name
const char * name
Definition: tm-threads.c:2163
HTPByteRangeMemuseGlobalCounter
uint64_t HTPByteRangeMemuseGlobalCounter(void)
Definition: app-layer-htp-range.c:62
HttpRangeContainerFile::key
uint8_t * key
Definition: app-layer-htp-range.h:96
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:102
HttpRangeContainerBlock::container
HttpRangeContainerFile * container
Definition: app-layer-htp-range.h:39
HttpRangeContainerBuffer::offset
uint64_t offset
Definition: app-layer-htp-range.h:75
util-file.h
File_
Definition: util-file.h:107
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:104
HttpRangeContainerFile::len
uint32_t len
Definition: app-layer-htp-range.h:98
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:112
util-thash.h
HttpRangeContainerFile::expire
SCTime_t expire
Definition: app-layer-htp-range.h:100
HTPByteRangeMemcapGlobalCounter
uint64_t HTPByteRangeMemcapGlobalCounter(void)
Definition: app-layer-htp-range.c:56
HttpRangeContainerFile::sbcfg
const StreamingBufferConfig * sbcfg
Definition: app-layer-htp-range.h:108
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:65