suricata
util-streaming-buffer.h
Go to the documentation of this file.
1 /* Copyright (C) 2015-2016 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  * This API is meant to be used with streaming data. A single memory
20  * block is used to store the data. StreamingBufferSegment points to
21  * chunk of data in the single StreamingBuffer. It points by offset
22  * and length, so no pointers. The buffer is resized on demand and
23  * slides forward, either automatically or manually.
24  *
25  * When a segment needs it's data it uses StreamingBufferSegmentGetData
26  * which takes care of checking if the segment still has a valid offset
27  * and length.
28  *
29  * The StreamingBuffer::stream_offset is an absolute offset since the
30  * start of the data streaming.
31  *
32  * Similarly, StreamingBufferSegment::stream_offset is also an absolute
33  * offset.
34  *
35  * Using the segments is optional.
36  *
37  *
38  * stream_offset buf_offset stream_offset + buf_size
39  * ^ ^ ^
40  * | | |
41  * | | |
42  * +--------------------------------------------+
43  * | data | empty |
44  * | xxxxxxxxxx | |
45  * +------^--------^--------+-------------------+
46  * | |
47  * | |
48  * | |
49  * | |
50  * | |
51  * +------+--------+-------+
52  * | StreamingBufferSegment|
53  * +-----------+-----------+
54  * | offset | len |
55  * +-----------+-----------+
56  */
57 
58 
59 #ifndef __UTIL_STREAMING_BUFFER_H__
60 #define __UTIL_STREAMING_BUFFER_H__
61 
62 #include "tree.h"
63 
64 typedef struct StreamingBufferConfig_ {
65  uint32_t buf_slide;
66  uint32_t buf_size;
67  void *(*Calloc)(size_t n, size_t size);
68  void *(*Realloc)(void *ptr, size_t orig_size, size_t size);
69  void (*Free)(void *ptr, size_t size);
71 
72 #define STREAMING_BUFFER_CONFIG_INITIALIZER \
73  { \
74  0, 0, NULL, NULL, NULL, \
75  }
76 
77 /**
78  * \brief block of continues data
79  */
80 typedef struct StreamingBufferBlock {
81  uint64_t offset;
83  uint32_t len;
84 } __attribute__((__packed__)) StreamingBufferBlock;
85 
86 int SBBCompare(struct StreamingBufferBlock *a, struct StreamingBufferBlock *b);
87 
88 /* red-black tree prototype for SACK records */
92 
93 typedef struct StreamingBuffer_ {
95  uint64_t stream_offset; /**< offset of the start of the memory block */
96 
97  uint8_t *buf; /**< memory block for reassembly */
98  uint32_t buf_size; /**< size of memory block */
99  uint32_t buf_offset; /**< how far we are in buf_size */
100 
101  struct SBB sbb_tree; /**< red black tree of Stream Buffer Blocks */
102  StreamingBufferBlock *head; /**< head, should always be the same as RB_MIN */
103  uint32_t sbb_size; /**< data size covered by sbbs */
104 #ifdef DEBUG
105  uint32_t buf_size_max;
106 #endif
108 
109 #ifndef DEBUG
110 #define STREAMING_BUFFER_INITIALIZER(cfg) \
111  { \
112  (cfg), \
113  0, \
114  NULL, \
115  0, \
116  0, \
117  { NULL }, \
118  NULL, \
119  0, \
120  };
121 #else
122 #define STREAMING_BUFFER_INITIALIZER(cfg) { (cfg), 0, NULL, 0, 0, { NULL }, NULL, 0, 0 };
123 #endif
124 
125 typedef struct StreamingBufferSegment_ {
126  uint32_t segment_len;
127  uint64_t stream_offset;
128 } __attribute__((__packed__)) StreamingBufferSegment;
129 
133 
134 void StreamingBufferSlide(StreamingBuffer *sb, uint32_t slide);
136 
137 StreamingBufferSegment *StreamingBufferAppendRaw(StreamingBuffer *sb,
138  const uint8_t *data, uint32_t data_len) WARN_UNUSED;
139 int StreamingBufferAppend(StreamingBuffer *sb, StreamingBufferSegment *seg,
140  const uint8_t *data, uint32_t data_len) WARN_UNUSED;
142  const uint8_t *data, uint32_t data_len) WARN_UNUSED;
143 int StreamingBufferInsertAt(StreamingBuffer *sb, StreamingBufferSegment *seg,
144  const uint8_t *data, uint32_t data_len,
145  uint64_t offset) WARN_UNUSED;
146 
148  const StreamingBufferSegment *seg,
149  const uint8_t **data, uint32_t *data_len);
150 
152  const StreamingBufferBlock *sbb,
153  const uint8_t **data, uint32_t *data_len);
154 
156  const StreamingBufferBlock *sbb,
157  const uint8_t **data, uint32_t *data_len,
158  uint64_t offset);
159 
161  const StreamingBufferSegment *seg,
162  const uint8_t *rawdata, uint32_t rawdata_len);
164  const uint8_t *rawdata, uint32_t rawdata_len);
165 
167  const uint8_t **data, uint32_t *data_len,
168  uint64_t *stream_offset);
169 
171  const uint8_t **data, uint32_t *data_len,
172  uint64_t offset);
173 
175  const StreamingBufferSegment *seg);
176 
178 
179 #endif /* __UTIL_STREAMING_BUFFER_H__ */
StreamingBufferSegmentIsBeforeWindow
int StreamingBufferSegmentIsBeforeWindow(const StreamingBuffer *sb, const StreamingBufferSegment *seg)
Definition: util-streaming-buffer.c:712
RB_PROTOTYPE
RB_PROTOTYPE(SBB, StreamingBufferBlock, rb, SBBCompare)
SBB_RB_FIND_INCLUSIVE
StreamingBufferBlock * SBB_RB_FIND_INCLUSIVE(struct SBB *head, StreamingBufferBlock *elm)
Definition: util-streaming-buffer.c:77
SBBCompare
int SBBCompare(struct StreamingBufferBlock *a, struct StreamingBufferBlock *b)
Definition: util-streaming-buffer.c:45
StreamingBuffer_::stream_offset
uint64_t stream_offset
Definition: util-streaming-buffer.h:95
StreamingBuffer_::buf_offset
uint32_t buf_offset
Definition: util-streaming-buffer.h:99
StreamingBufferConfig_::buf_size
uint32_t buf_size
Definition: util-streaming-buffer.h:66
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
StreamingBufferAppend
int StreamingBufferAppend(StreamingBuffer *sb, StreamingBufferSegment *seg, const uint8_t *data, uint32_t data_len) WARN_UNUSED
Definition: util-streaming-buffer.c:569
StreamingBufferFree
void StreamingBufferFree(StreamingBuffer *sb)
Definition: util-streaming-buffer.c:143
StreamingBuffer_::head
StreamingBufferBlock * head
Definition: util-streaming-buffer.h:102
StreamingBufferSegment_::stream_offset
uint64_t stream_offset
Definition: util-streaming-buffer.h:127
StreamingBufferInsertAt
int StreamingBufferInsertAt(StreamingBuffer *sb, StreamingBufferSegment *seg, const uint8_t *data, uint32_t data_len, uint64_t offset) WARN_UNUSED
Definition: util-streaming-buffer.c:650
StreamingBuffer_::buf
uint8_t * buf
Definition: util-streaming-buffer.h:97
StreamingBuffer_::buf_size
uint32_t buf_size
Definition: util-streaming-buffer.h:98
StreamingBufferSegment_
Definition: util-streaming-buffer.h:125
stream_offset
uint64_t stream_offset
Definition: util-streaming-buffer.h:1
StreamingBufferGetDataAtOffset
int StreamingBufferGetDataAtOffset(const StreamingBuffer *sb, const uint8_t **data, uint32_t *data_len, uint64_t offset)
Definition: util-streaming-buffer.c:847
__attribute__
struct StreamingBufferBlock __attribute__((__packed__)) StreamingBufferBlock
block of continues data
Definition: decode-vlan.c:117
StreamingBufferInit
StreamingBuffer * StreamingBufferInit(const StreamingBufferConfig *cfg)
Definition: util-streaming-buffer.c:110
StreamingBufferSBBGetData
void StreamingBufferSBBGetData(const StreamingBuffer *sb, const StreamingBufferBlock *sbb, const uint8_t **data, uint32_t *data_len)
get the data for one SBB
Definition: util-streaming-buffer.c:724
StreamingBufferSBBGetDataAtOffset
void StreamingBufferSBBGetDataAtOffset(const StreamingBuffer *sb, const StreamingBufferBlock *sbb, const uint8_t **data, uint32_t *data_len, uint64_t offset)
get the data for one SBB
Definition: util-streaming-buffer.c:750
StreamingBufferRegisterTests
void StreamingBufferRegisterTests(void)
Definition: util-streaming-buffer.c:1402
StreamingBuffer_::sbb_size
uint32_t sbb_size
Definition: util-streaming-buffer.h:103
StreamingBufferSegmentCompareRawData
int StreamingBufferSegmentCompareRawData(const StreamingBuffer *sb, const StreamingBufferSegment *seg, const uint8_t *rawdata, uint32_t rawdata_len)
Definition: util-streaming-buffer.c:814
tree.h
StreamingBuffer_::sbb_tree
struct SBB sbb_tree
Definition: util-streaming-buffer.h:101
StreamingBufferSlideToOffset
void StreamingBufferSlideToOffset(StreamingBuffer *sb, uint64_t offset)
slide to absolute offset
Definition: util-streaming-buffer.c:504
StreamingBuffer_
Definition: util-streaming-buffer.h:93
StreamingBufferBlock::RB_ENTRY
RB_ENTRY(StreamingBufferBlock) rb
StreamingBuffer
struct StreamingBuffer_ StreamingBuffer
StreamingBuffer_::cfg
const StreamingBufferConfig * cfg
Definition: util-streaming-buffer.h:94
StreamingBufferGetData
int StreamingBufferGetData(const StreamingBuffer *sb, const uint8_t **data, uint32_t *data_len, uint64_t *stream_offset)
Definition: util-streaming-buffer.c:830
StreamingBufferConfig
struct StreamingBufferConfig_ StreamingBufferConfig
RB_HEAD
RB_HEAD(SBB, StreamingBufferBlock)
StreamingBufferConfig_
Definition: util-streaming-buffer.h:64
StreamingBufferCompareRawData
int StreamingBufferCompareRawData(const StreamingBuffer *sb, const uint8_t *rawdata, uint32_t rawdata_len)
Definition: util-streaming-buffer.c:870
head
Flow * head
Definition: flow-hash.h:1
StreamingBufferAppendRaw
StreamingBufferSegment * StreamingBufferAppendRaw(StreamingBuffer *sb, const uint8_t *data, uint32_t data_len) WARN_UNUSED
Definition: util-streaming-buffer.c:532
StreamingBufferConfig_::Free
void(* Free)(void *ptr, size_t size)
Definition: util-streaming-buffer.h:69
StreamingBufferSlide
void StreamingBufferSlide(StreamingBuffer *sb, uint32_t slide)
Definition: util-streaming-buffer.c:519
StreamingBufferAppendNoTrack
int StreamingBufferAppendNoTrack(StreamingBuffer *sb, const uint8_t *data, uint32_t data_len) WARN_UNUSED
add data w/o tracking a segment
Definition: util-streaming-buffer.c:608
StreamingBufferSegmentGetData
void StreamingBufferSegmentGetData(const StreamingBuffer *sb, const StreamingBufferSegment *seg, const uint8_t **data, uint32_t *data_len)
Definition: util-streaming-buffer.c:783
StreamingBufferSegment_::segment_len
uint32_t segment_len
Definition: util-streaming-buffer.h:126
StreamingBufferBlock::len
uint32_t len
Definition: util-streaming-buffer.h:83
StreamingBufferBlock::offset
uint64_t offset
Definition: util-streaming-buffer.h:81
StreamingBufferClear
void StreamingBufferClear(StreamingBuffer *sb)
Definition: util-streaming-buffer.c:130
StreamingBufferBlock
block of continues data
Definition: util-streaming-buffer.h:80
StreamingBufferConfig_::buf_slide
uint32_t buf_slide
Definition: util-streaming-buffer.h:65
WARN_UNUSED
#define WARN_UNUSED
Definition: suricata-common.h:384