9#ifndef MIDDLEBOX_FLOWBUFFER_HH
10#define MIDDLEBOX_FLOWBUFFER_HH
12#include <clicknet/tcp.h>
13#include "memorypool.hh"
23 struct flowBufferEntry *prev;
24 struct flowBufferEntry *next;
28class FlowBufferChunkIter;
141 FlowBufferChunkIter enqueueAllChunkIter(
PacketBatch* batch);
164 inline bool isInitialized() {
248 inline unsigned char*
get_ptr();
260 inline operator bool()
const {
269 inline Packet* current() {
273 inline bool lastChunk() {
274 return entry->
next() == 0;
277 inline void moveToNextChunk() {
278 entry = entry->
next();
282 inline int leftInChunk() {
285 return entry->
length() - (entry->getContentOffset() + offsetInPacket);
291 char buf[n.leftInChunk() + 1];
292 memcpy(buf, n.
get_ptr(), n.leftInChunk());
293 buf[n.leftInChunk()] =
'\0';
294 click_chatter(
"'%s'",buf);
309 uint32_t offsetInPacket;
313 const unsigned char* bytes;
314 const unsigned length;
320class FlowBufferChunkIter
335 inline bool operator==(
const FlowBufferChunkIter& other)
const;
341 inline bool operator!=(
const FlowBufferChunkIter& other)
const;
351 inline FlowBufferChunkIter& operator++();
353 inline operator bool()
const {
362 inline Packet* current() {
375 Packet* _entry) : flowBuffer(_flowBuffer)
382 assert(entry != NULL);
389 assert(entry != NULL);
399 if(this->flowBuffer != other.flowBuffer)
402 if(this->entry == NULL && other.entry == NULL)
405 if(this->entry != other.entry)
413 return !(*
this == other);
417inline FlowBufferContentIter::FlowBufferContentIter(
FlowBuffer *_flowBuffer,
418 Packet* _entry,
int posInFirstPacket) : flowBuffer(_flowBuffer), entry(_entry), offsetInPacket(posInFirstPacket)
421 while (entry && entry->getContentOffset() + offsetInPacket >=
425 entry = entry->
next();
431 if(this->flowBuffer != other.flowBuffer)
434 if(this->entry == NULL && other.entry == NULL)
437 if(this->entry != other.entry)
440 if(this->offsetInPacket != other.offsetInPacket)
448 return !(*
this == other);
453 unsigned char* content =
static_cast<WritablePacket*
>(entry)->getPacketContent();
455 return *(content + offsetInPacket);
461 unsigned char* content =
static_cast<WritablePacket*
>(entry)->getPacketContent();
463 return content + offsetInPacket;
469 assert(entry != NULL);
475 while (entry && entry->getContentOffset() + offsetInPacket >=
479 entry = entry->
next();
487 assert(entry != NULL);
489 while (entry->getContentOffset() + offsetInPacket + p >= entry->
length()) {
490 p -= entry->
length() - entry->getContentOffset() + offsetInPacket;
492 entry = entry->
next();
514inline FlowBufferChunkIter::FlowBufferChunkIter(
FlowBuffer *_flowBuffer,
515 Packet* _entry) : flowBuffer(_flowBuffer), entry(_entry)
518 while (entry && entry->getContentOffset() == entry->
length())
520 entry = entry->
next();
524inline bool FlowBufferChunkIter::operator==(
const FlowBufferChunkIter& other)
const
526 if(this->flowBuffer != other.flowBuffer)
529 if(this->entry == NULL && other.entry == NULL)
532 if(this->entry != other.entry)
538inline bool FlowBufferChunkIter::operator!=(
const FlowBufferChunkIter& other)
const
540 return !(*
this == other);
543inline Chunk FlowBufferChunkIter::operator*()
545 assert(entry != NULL);
548 int off = wp->getContentOffset();
549 unsigned char* content = wp->
data() + off;
551 return Chunk{content, wp->
length() - off};
555inline FlowBufferChunkIter& FlowBufferChunkIter::operator++()
557 assert(entry != NULL);
559 entry = entry->next();
561 while (entry && entry->getContentOffset() == entry->length())
563 entry = entry->next();
572 return this->flowBuffer->dequeueAll();
575 return this->flowBuffer->dequeueUpTo(entry);
This iterator allows to iterate the content in the buffer seamlessly accross the packets.
Definition flowbuffer.hh:216
FlowBufferContentIter & operator++()
Move the iterator to the next byte in the buffer.
Definition flowbuffer.hh:467
PacketBatch * flush()
Definition flowbuffer.hh:503
FlowBufferContentIter & operator+=(int p)
Move the iterator to the next N bytes in the buffer.
Definition flowbuffer.hh:485
unsigned char * get_ptr()
Return the byte to which this iterator points.
Definition flowbuffer.hh:459
bool operator!=(const FlowBufferContentIter &other) const
Compare two FlowBufferContentIter.
Definition flowbuffer.hh:446
bool operator==(const FlowBufferContentIter &other) const
Compare two FlowBufferContentIter.
Definition flowbuffer.hh:429
unsigned char & operator*()
Return the byte to which this iterator points.
Definition flowbuffer.hh:451
This buffer allows to search, replace or remove data in the packets buffered as in a contiguous flow....
Definition flowbuffer.hh:40
FlowBufferContentIter search(FlowBufferContentIter start, const char *pattern, int *feedback)
Search a pattern in the buffer.
Definition flowbuffer.cc:301
int getSize()
Return the size of the buffer.
Definition flowbuffer.cc:127
FlowBuffer()
Construct a FlowBuffer The FlowBuffer must be initialized.
Definition flowbuffer.cc:19
int removeInFlow(const char *pattern, const int pattern_length, CTXElement *remove)
Remove a pattern in the buffer.
Definition flowbuffer.cc:348
FlowBufferContentIter contentBegin(int posInFirstPacket=0)
Return a content iterator pointing to the first byte of content in the buffer.
Definition flowbuffer.cc:78
FlowBufferContentIter contentEnd()
Return a content iterator pointing after the end of the content in the buffer.
Definition flowbuffer.cc:83
void remove(const FlowBufferContentIter start, uint32_t length, CTXElement *owner)
Remove data in the flow (across the packets)
Definition flowbuffer.cc:361
FlowBufferIter end()
Return an iterator pointing to the end of the buffer.
Definition flowbuffer.cc:169
Packet * dequeue()
Dequeue a packet from the beginning of the buffer @returnt The dequeued packet.
Definition flowbuffer.cc:100
void enqueue(Packet *packet)
Enqueue a packet at the end of the buffer.
Definition flowbuffer.cc:67
~FlowBuffer()
Destruct a FlowBuffer.
Definition flowbuffer.cc:27
PacketBatch * dequeueUpTo(Packet *)
Dequeue packet up to the given one, not included @returnt The dequeued packet.
Definition flowbuffer.cc:133
PacketBatch * dequeueAll()
Dequeue all packets @returnt The dequeued packet.
Definition flowbuffer.cc:116
MemoryPool< struct flowBufferEntry > * getMemoryPool()
Return a pointer to the MemoryPool used by this FlowBuffer.
int replaceInFlow(FlowBufferContentIter pos, const int pattern_length, const char *replacement, const int replacement_length, const bool repeat, CTXElement *owner)
Replace a pattern in the buffer.
Definition flowbuffer.cc:396
int searchInFlow(const char *pattern)
Search a pattern in the buffer.
Definition flowbuffer.cc:89
FlowBufferIter begin()
Return an iterator pointing to the first packet of the buffer.
Definition flowbuffer.cc:164
void enqueueAll(PacketBatch *batch)
Ensure a flow buffer is initialized and enqueue all packets.
Definition flowbuffer.cc:38
This iterator allows to iterate over the packets in a FlowBuffer.
Definition flowbuffer.hh:177
FlowBufferIter & operator++()
Move the iterator to the next packet in the buffer.
Definition flowbuffer.hh:387
bool operator!=(const FlowBufferIter &other) const
Compare two FlowBufferIter.
Definition flowbuffer.hh:411
FlowBufferIter(FlowBuffer *_flowBuffer, Packet *_entry)
Construct a FlowBufferIter.
Definition flowbuffer.hh:374
WritablePacket *& operator*()
Return the packet to which this iterator points.
Definition flowbuffer.hh:380
bool operator==(const FlowBufferIter &other) const
Compare two FlowBufferIter.
Definition flowbuffer.hh:397
Template for memory pools.
Definition memorypool.hh:20
Definition packetbatch.hh:415
A network packet.
Definition packet.hh:68
uint32_t length() const
Return the packet's length.
Definition packet.hh:1276
Packet * next() const
Return the next packet annotation.
Definition packet.hh:1327
A network packet believed not to be shared.
Definition packet.hh:1004
unsigned char * data() const
Definition packet.hh:2941
The Packet class models packets in Click.
Timestamp operator*(const Timestamp &a, double b)
Scale a by a factor of b and return the result.
Definition timestamp.hh:1447