INET Framework for OMNeT++/OMNEST
|
#include <IPFragBuf.h>
Classes | |
struct | DatagramBuffer |
struct | Key |
Public Member Functions | |
IPFragBuf () | |
~IPFragBuf () | |
void | init (ICMP *icmp) |
IPDatagram * | addFragment (IPDatagram *datagram, simtime_t now) |
void | purgeStaleFragments (simtime_t lastupdate) |
Protected Types | |
typedef std::map< Key, DatagramBuffer > | Buffers |
Protected Attributes | |
Buffers | bufs |
ICMP * | icmpModule |
Reassembly buffer for fragmented IP datagrams.
typedef std::map<Key,DatagramBuffer> IPFragBuf::Buffers [protected] |
IPFragBuf::IPFragBuf | ( | ) |
Ctor.
{ icmpModule = NULL; }
IPFragBuf::~IPFragBuf | ( | ) |
Dtor.
{ }
IPDatagram * IPFragBuf::addFragment | ( | IPDatagram * | datagram, |
simtime_t | now | ||
) |
Takes a fragment and inserts it into the reassembly buffer. If this fragment completes a datagram, the full reassembled datagram is returned, otherwise NULL.
Referenced by IP::reassembleAndDeliver().
{ // find datagram buffer Key key; key.id = datagram->getIdentification(); key.src = datagram->getSrcAddress(); key.dest = datagram->getDestAddress(); Buffers::iterator i = bufs.find(key); DatagramBuffer *buf = NULL; if (i==bufs.end()) { // this is the first fragment of that datagram, create reassembly buffer for it buf = &bufs[key]; buf->datagram = NULL; } else { // use existing buffer buf = &(i->second); } // add fragment into reassembly buffer int bytes = datagram->getByteLength() - datagram->getHeaderLength(); bool isComplete = buf->buf.addFragment(datagram->getFragmentOffset(), datagram->getFragmentOffset() + bytes, !datagram->getMoreFragments()); // store datagram. Only one fragment carries the actual modelled // content (getEncapsulatedPacket()), other (empty) ones are only // preserved so that we can send them in ICMP if reassembly times out. if (datagram->getEncapsulatedPacket()) { delete buf->datagram; buf->datagram = datagram; } else { delete datagram; } // do we have the complete datagram? if (isComplete) { // datagram complete: deallocate buffer and return complete datagram IPDatagram *ret = buf->datagram; ret->setByteLength(ret->getHeaderLength()+buf->buf.getTotalLength()); ret->setFragmentOffset(0); ret->setMoreFragments(false); bufs.erase(i); return ret; } else { // there are still missing fragments buf->lastupdate = now; return NULL; } }
void IPFragBuf::init | ( | ICMP * | icmp | ) |
Initialize fragmentation buffer. ICMP module is needed for sending TIME_EXCEEDED ICMP message in purgeStaleFragments().
Referenced by IP::initialize().
{ icmpModule = icmp; }
void IPFragBuf::purgeStaleFragments | ( | simtime_t | lastupdate | ) |
Throws out all fragments which are incomplete and their last update (last fragment arrival) was before "lastupdate", and sends ICMP TIME EXCEEDED message about them.
Timeout should be between 60 seconds and 120 seconds (RFC1122). This method should be called more frequently, maybe every 10..30 seconds or so.
Referenced by IP::reassembleAndDeliver().
{ // this method shouldn't be called too often because iteration on // an std::map is *very* slow... ASSERT(icmpModule); for (Buffers::iterator i=bufs.begin(); i!=bufs.end(); ) { // if too old, remove it DatagramBuffer& buf = i->second; if (buf.lastupdate < lastupdate) { // send ICMP error. // Note: receiver MUST NOT call decapsulate() on the datagram fragment, // because its length (being a fragment) is smaller than the encapsulated // packet, resulting in "length became negative" error. Use getEncapsulatedPacket(). EV << "datagram fragment timed out in reassembly buffer, sending ICMP_TIME_EXCEEDED\n"; icmpModule->sendErrorMessage(buf.datagram, ICMP_TIME_EXCEEDED, 0); // delete Buffers::iterator oldi = i++; bufs.erase(oldi); } else { ++i; } } }
Buffers IPFragBuf::bufs [protected] |
Referenced by addFragment(), and purgeStaleFragments().
ICMP* IPFragBuf::icmpModule [protected] |
Referenced by init(), IPFragBuf(), and purgeStaleFragments().