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 #ifndef SURICATA_UTIL_STREAMING_BUFFER_H
59 #define SURICATA_UTIL_STREAMING_BUFFER_H
60 
61 #include "tree.h"
62 
63 #define STREAMING_BUFFER_REGION_GAP_DEFAULT 262144
64 
65 typedef struct StreamingBufferConfig_ {
66  uint32_t buf_size;
67  uint16_t max_regions; /**< max concurrent memory regions. 0 means no limit. */
68  uint32_t region_gap; /**< max gap size before a new region will be created. */
69  void *(*Calloc)(size_t n, size_t size);
70  void *(*Realloc)(void *ptr, size_t orig_size, size_t size);
71  void (*Free)(void *ptr, size_t size);
73 
74 #define STREAMING_BUFFER_CONFIG_INITIALIZER \
75  { \
76  2048, 8, STREAMING_BUFFER_REGION_GAP_DEFAULT, NULL, NULL, NULL, \
77  }
78 
79 #define STREAMING_BUFFER_REGION_INIT \
80  { \
81  NULL, 0, 0, 0ULL, NULL, \
82  }
83 
84 typedef struct StreamingBufferRegion_ {
85  uint8_t *buf; /**< memory block for reassembly */
86  uint32_t buf_size; /**< size of memory block */
87  uint32_t buf_offset; /**< how far we are in buf_size */
88  uint64_t stream_offset; /**< stream offset of this region */
91 
92 /**
93  * \brief block of continues data
94  */
95 typedef struct StreamingBufferBlock {
96  uint64_t offset;
98  uint32_t len;
99 } __attribute__((__packed__)) StreamingBufferBlock;
100 
101 int SBBCompare(struct StreamingBufferBlock *a, struct StreamingBufferBlock *b);
102 
103 /* red-black tree prototype for SACK records */
107 
108 typedef struct StreamingBuffer_ {
110  struct SBB sbb_tree; /**< red black tree of Stream Buffer Blocks */
111  StreamingBufferBlock *head; /**< head, should always be the same as RB_MIN */
112  uint32_t sbb_size; /**< data size covered by sbbs */
113  uint16_t regions;
114  uint16_t max_regions;
115 #ifdef DEBUG
116  uint32_t buf_size_max;
117 #endif
119 
120 static inline bool StreamingBufferHasData(const StreamingBuffer *sb)
121 {
122  return (sb->region.stream_offset || sb->region.buf_offset || sb->region.next != NULL ||
123  !RB_EMPTY(&sb->sbb_tree));
124 }
125 
126 static inline uint64_t StreamingBufferGetConsecutiveDataRightEdge(const StreamingBuffer *sb)
127 {
128  return sb->region.stream_offset + sb->region.buf_offset;
129 }
130 
131 static inline uint64_t StreamingBufferGetOffset(const StreamingBuffer *sb)
132 {
133  return sb->region.stream_offset;
134 }
135 
136 #ifndef DEBUG
137 #define STREAMING_BUFFER_INITIALIZER \
138  { \
139  STREAMING_BUFFER_REGION_INIT, \
140  { NULL }, \
141  NULL, \
142  0, \
143  1, \
144  1, \
145  };
146 #else
147 #define STREAMING_BUFFER_INITIALIZER { STREAMING_BUFFER_REGION_INIT, { NULL }, NULL, 0, 1, 1, 0 };
148 #endif
149 
150 typedef struct StreamingBufferSegment_ {
151  uint32_t segment_len;
152  uint64_t stream_offset;
153 } __attribute__((__packed__)) StreamingBufferSegment;
154 
158 
160  StreamingBuffer *sb, const StreamingBufferConfig *cfg, uint64_t offset);
161 
163  StreamingBufferSegment *seg, const uint8_t *data, uint32_t data_len) WARN_UNUSED;
165  const uint8_t *data, uint32_t data_len) WARN_UNUSED;
167  StreamingBufferSegment *seg, const uint8_t *data, uint32_t data_len,
168  uint64_t offset) WARN_UNUSED;
169 
171  const StreamingBufferSegment *seg,
172  const uint8_t **data, uint32_t *data_len);
173 
175  const StreamingBufferBlock *sbb,
176  const uint8_t **data, uint32_t *data_len);
177 
179  const StreamingBufferBlock *sbb,
180  const uint8_t **data, uint32_t *data_len,
181  uint64_t offset);
182 
184  const StreamingBufferSegment *seg,
185  const uint8_t *rawdata, uint32_t rawdata_len);
187  const uint8_t *rawdata, uint32_t rawdata_len);
188 
190  const uint8_t **data, uint32_t *data_len,
191  uint64_t *stream_offset);
192 
194  const uint8_t **data, uint32_t *data_len,
195  uint64_t offset);
196 
198  const StreamingBufferSegment *seg);
199 
201 
202 #endif /* SURICATA_UTIL_STREAMING_BUFFER_H */
StreamingBufferSegmentIsBeforeWindow
int StreamingBufferSegmentIsBeforeWindow(const StreamingBuffer *sb, const StreamingBufferSegment *seg)
Definition: util-streaming-buffer.c:1628
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:104
SBBCompare
int SBBCompare(struct StreamingBufferBlock *a, struct StreamingBufferBlock *b)
Definition: util-streaming-buffer.c:73
StreamingBufferAppendNoTrack
int StreamingBufferAppendNoTrack(StreamingBuffer *sb, const StreamingBufferConfig *cfg, const uint8_t *data, uint32_t data_len) WARN_UNUSED
add data w/o tracking a segment
Definition: util-streaming-buffer.c:1125
StreamingBufferConfig_::buf_size
uint32_t buf_size
Definition: util-streaming-buffer.h:66
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
StreamingBuffer_::head
StreamingBufferBlock * head
Definition: util-streaming-buffer.h:111
StreamingBufferRegion_::next
struct StreamingBufferRegion_ * next
Definition: util-streaming-buffer.h:89
StreamingBufferSegment_::stream_offset
uint64_t stream_offset
Definition: util-streaming-buffer.h:152
StreamingBuffer_::max_regions
uint16_t max_regions
Definition: util-streaming-buffer.h:114
StreamingBuffer_::regions
uint16_t regions
Definition: util-streaming-buffer.h:113
StreamingBufferSegment_
Definition: util-streaming-buffer.h:150
stream_offset
uint64_t stream_offset
Definition: util-streaming-buffer.h:1
StreamingBufferConfig_::max_regions
uint16_t max_regions
Definition: util-streaming-buffer.h:67
StreamingBufferGetDataAtOffset
int StreamingBufferGetDataAtOffset(const StreamingBuffer *sb, const uint8_t **data, uint32_t *data_len, uint64_t offset)
Definition: util-streaming-buffer.c:1803
__attribute__
struct StreamingBufferBlock __attribute__((__packed__)) StreamingBufferBlock
block of continues data
Definition: decode-vlan.c:103
StreamingBufferFree
void StreamingBufferFree(StreamingBuffer *sb, const StreamingBufferConfig *cfg)
Definition: util-streaming-buffer.c:297
StreamingBufferInit
StreamingBuffer * StreamingBufferInit(const StreamingBufferConfig *cfg)
Definition: util-streaming-buffer.c:251
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:1660
RB_EMPTY
#define RB_EMPTY(head)
Definition: tree.h:327
StreamingBufferRegion_::buf_size
uint32_t buf_size
Definition: util-streaming-buffer.h:86
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:1696
StreamingBufferRegisterTests
void StreamingBufferRegisterTests(void)
Definition: util-streaming-buffer.c:2412
StreamingBuffer_::sbb_size
uint32_t sbb_size
Definition: util-streaming-buffer.h:112
StreamingBufferSlideToOffset
void StreamingBufferSlideToOffset(StreamingBuffer *sb, const StreamingBufferConfig *cfg, uint64_t offset)
slide to absolute offset
Definition: util-streaming-buffer.c:1004
StreamingBufferRegion_::buf
uint8_t * buf
Definition: util-streaming-buffer.h:85
StreamingBufferRegion_::buf_offset
uint32_t buf_offset
Definition: util-streaming-buffer.h:87
StreamingBufferSegmentCompareRawData
int StreamingBufferSegmentCompareRawData(const StreamingBuffer *sb, const StreamingBufferSegment *seg, const uint8_t *rawdata, uint32_t rawdata_len)
Definition: util-streaming-buffer.c:1770
tree.h
StreamingBuffer_::sbb_tree
struct SBB sbb_tree
Definition: util-streaming-buffer.h:110
StreamingBuffer_
Definition: util-streaming-buffer.h:108
StreamingBufferBlock::RB_ENTRY
RB_ENTRY(StreamingBufferBlock) rb
StreamingBufferInsertAt
int StreamingBufferInsertAt(StreamingBuffer *sb, const StreamingBufferConfig *cfg, StreamingBufferSegment *seg, const uint8_t *data, uint32_t data_len, uint64_t offset) WARN_UNUSED
Definition: util-streaming-buffer.c:1509
StreamingBuffer
struct StreamingBuffer_ StreamingBuffer
StreamingBufferRegion
struct StreamingBufferRegion_ StreamingBufferRegion
StreamingBufferGetData
int StreamingBufferGetData(const StreamingBuffer *sb, const uint8_t **data, uint32_t *data_len, uint64_t *stream_offset)
Definition: util-streaming-buffer.c:1786
StreamingBufferAppend
int StreamingBufferAppend(StreamingBuffer *sb, const StreamingBufferConfig *cfg, StreamingBufferSegment *seg, const uint8_t *data, uint32_t data_len) WARN_UNUSED
Definition: util-streaming-buffer.c:1081
StreamingBufferConfig
struct StreamingBufferConfig_ StreamingBufferConfig
RB_HEAD
RB_HEAD(SBB, StreamingBufferBlock)
StreamingBufferConfig_
Definition: util-streaming-buffer.h:65
StreamingBufferCompareRawData
int StreamingBufferCompareRawData(const StreamingBuffer *sb, const uint8_t *rawdata, uint32_t rawdata_len)
Definition: util-streaming-buffer.c:1825
head
Flow * head
Definition: flow-hash.h:1
StreamingBufferConfig_::Free
void(* Free)(void *ptr, size_t size)
Definition: util-streaming-buffer.h:71
StreamingBufferRegion_
Definition: util-streaming-buffer.h:84
StreamingBufferSegmentGetData
void StreamingBufferSegmentGetData(const StreamingBuffer *sb, const StreamingBufferSegment *seg, const uint8_t **data, uint32_t *data_len)
Definition: util-streaming-buffer.c:1737
StreamingBufferClear
void StreamingBufferClear(StreamingBuffer *sb, const StreamingBufferConfig *cfg)
Definition: util-streaming-buffer.c:274
StreamingBufferSegment_::segment_len
uint32_t segment_len
Definition: util-streaming-buffer.h:151
StreamingBuffer_::region
StreamingBufferRegion region
Definition: util-streaming-buffer.h:109
StreamingBufferConfig_::region_gap
uint32_t region_gap
Definition: util-streaming-buffer.h:68
StreamingBufferBlock::len
uint32_t len
Definition: util-streaming-buffer.h:98
StreamingBufferBlock::offset
uint64_t offset
Definition: util-streaming-buffer.h:96
StreamingBufferBlock
block of continues data
Definition: util-streaming-buffer.h:95
WARN_UNUSED
#define WARN_UNUSED
Definition: suricata-common.h:403
StreamingBufferRegion_::stream_offset
uint64_t stream_offset
Definition: util-streaming-buffer.h:88