INET Framework for OMNeT++/OMNEST
inet::ReassemblyBuffer Class Reference

Generic reassembly buffer for a fragmented datagram (or a fragmented anything). More...

#include <ReassemblyBuffer.h>

Classes

struct  Region
 

Public Member Functions

 ReassemblyBuffer ()
 Ctor. More...
 
 ~ReassemblyBuffer ()
 Dtor. More...
 
bool addFragment (ushort beg, ushort end, bool islast)
 Add a fragment, and returns true if reassembly has completed (i.e. More...
 
ushort getTotalLength () const
 Returns the total (assembled) length of the datagram. More...
 

Protected Types

typedef std::vector< RegionRegionVector
 

Protected Member Functions

void merge (ushort beg, ushort end, bool islast)
 
void mergeFragments ()
 

Protected Attributes

Region main
 
RegionVectorfragments
 

Detailed Description

Generic reassembly buffer for a fragmented datagram (or a fragmented anything).

Currently used in IPv4FragBuf and IPv6FragBuf.

Member Typedef Documentation

typedef std::vector<Region> inet::ReassemblyBuffer::RegionVector
protected

Constructor & Destructor Documentation

inet::ReassemblyBuffer::ReassemblyBuffer ( )

Ctor.

28 {
29  main.beg = main.end = 0;
30  main.islast = false;
31  fragments = nullptr;
32 }
bool islast
Definition: ReassemblyBuffer.h:40
ushort end
Definition: ReassemblyBuffer.h:39
Region main
Definition: ReassemblyBuffer.h:54
RegionVector * fragments
Definition: ReassemblyBuffer.h:55
ushort beg
Definition: ReassemblyBuffer.h:38
inet::ReassemblyBuffer::~ReassemblyBuffer ( )

Dtor.

35 {
36  delete fragments;
37 }
RegionVector * fragments
Definition: ReassemblyBuffer.h:55

Member Function Documentation

bool inet::ReassemblyBuffer::addFragment ( ushort  beg,
ushort  end,
bool  islast 
)

Add a fragment, and returns true if reassembly has completed (i.e.

we have everything from offset 0 to the last fragment).

Referenced by inet::IPv4FragBuf::addFragment(), and inet::IPv6FragBuf::addFragment().

40 {
41  merge(beg, end, islast);
42 
43  // do we have the complete datagram?
44  return main.beg == 0 && main.islast;
45 }
bool islast
Definition: ReassemblyBuffer.h:40
Region main
Definition: ReassemblyBuffer.h:54
void merge(ushort beg, ushort end, bool islast)
Definition: ReassemblyBuffer.cc:47
ushort beg
Definition: ReassemblyBuffer.h:38
ushort inet::ReassemblyBuffer::getTotalLength ( ) const
inline

Returns the total (assembled) length of the datagram.

Can only be called after addFragment() returned true.

Referenced by inet::IPv4FragBuf::addFragment(), and inet::IPv6FragBuf::addFragment().

82 { return main.end; }
ushort end
Definition: ReassemblyBuffer.h:39
Region main
Definition: ReassemblyBuffer.h:54
void inet::ReassemblyBuffer::merge ( ushort  beg,
ushort  end,
bool  islast 
)
protected

Referenced by addFragment().

48 {
49  if (main.end == beg) {
50  // most typical case (<95%): new fragment follows last one
51  main.end = end;
52  if (islast)
53  main.islast = true;
54  if (fragments)
56  }
57  else if (main.beg == end) {
58  // new fragment precedes what we already have
59  main.beg = beg;
60  if (fragments)
62  }
63  else if (main.end < beg || main.beg > end) {
64  // disjoint fragment, store it until another fragment fills in the gap
65  if (!fragments)
66  fragments = new RegionVector();
67  Region r;
68  r.beg = beg;
69  r.end = end;
70  r.islast = islast;
71  fragments->push_back(r);
72  }
73  else {
74  // overlapping is not possible;
75  // fragment's range already contained in buffer (probably duplicate fragment)
76  }
77 }
bool islast
Definition: ReassemblyBuffer.h:40
ushort end
Definition: ReassemblyBuffer.h:39
Region main
Definition: ReassemblyBuffer.h:54
std::vector< Region > RegionVector
Definition: ReassemblyBuffer.h:43
void mergeFragments()
Definition: ReassemblyBuffer.cc:79
RegionVector * fragments
Definition: ReassemblyBuffer.h:55
ushort beg
Definition: ReassemblyBuffer.h:38
void inet::ReassemblyBuffer::mergeFragments ( )
protected

Referenced by merge().

80 {
81  RegionVector& frags = *fragments;
82 
83  bool oncemore;
84  do {
85  oncemore = false;
86  for (auto i = frags.begin(); i != frags.end(); ) {
87  bool deleteit = false;
88  Region& frag = *i;
89  if (main.end == frag.beg) {
90  main.end = frag.end;
91  if (frag.islast)
92  main.islast = true;
93  deleteit = true;
94  }
95  else if (main.beg == frag.end) {
96  main.beg = frag.beg;
97  deleteit = true;
98  }
99  else if (main.beg <= frag.beg && main.end >= frag.end) {
100  // we already have this region (duplicate fragment), delete it
101  deleteit = true;
102  }
103 
104  if (deleteit) {
105  // deletion is tricky because erase() invalidates iterator
106  int pos = i - frags.begin();
107  frags.erase(i);
108  i = frags.begin() + pos;
109  oncemore = true;
110  }
111  else {
112  ++i;
113  }
114  }
115  } while (oncemore);
116 }
bool islast
Definition: ReassemblyBuffer.h:40
ushort end
Definition: ReassemblyBuffer.h:39
Region main
Definition: ReassemblyBuffer.h:54
std::vector< Region > RegionVector
Definition: ReassemblyBuffer.h:43
RegionVector * fragments
Definition: ReassemblyBuffer.h:55
ushort beg
Definition: ReassemblyBuffer.h:38

Member Data Documentation

RegionVector* inet::ReassemblyBuffer::fragments
protected
Region inet::ReassemblyBuffer::main
protected

The documentation for this class was generated from the following files: