Classes | Public Member Functions | Protected Types | Protected Attributes

IPFragBuf Class Reference

#include <IPFragBuf.h>

List of all members.

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
ICMPicmpModule

Detailed Description

Reassembly buffer for fragmented IP datagrams.

Definition at line 34 of file IPFragBuf.h.


Member Typedef Documentation

typedef std::map<Key,DatagramBuffer> IPFragBuf::Buffers [protected]

Definition at line 62 of file IPFragBuf.h.


Constructor & Destructor Documentation

IPFragBuf::IPFragBuf (  ) 

Ctor.

Definition at line 27 of file IPFragBuf.cc.

{
    icmpModule = NULL;
}

IPFragBuf::~IPFragBuf (  ) 

Dtor.

Definition at line 32 of file IPFragBuf.cc.

{
}


Member Function Documentation

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.

Definition at line 41 of file IPFragBuf.cc.

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 (getEncapsulatedMsg()), other (empty) ones are only
    // preserved so that we can send them in ICMP if reassembly times out.
    if (datagram->getEncapsulatedMsg())
    {
        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().

Definition at line 36 of file IPFragBuf.cc.

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.

Definition at line 102 of file IPFragBuf.cc.

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 getEncapsulatedMsg().
            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;
        }
    }
}


Member Data Documentation

Buffers IPFragBuf::bufs [protected]

Definition at line 65 of file IPFragBuf.h.

Referenced by addFragment(), and purgeStaleFragments().

Definition at line 68 of file IPFragBuf.h.

Referenced by init(), IPFragBuf(), and purgeStaleFragments().


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