INET Framework for OMNeT++/OMNEST
TCPDump Class Reference

#include <TCPDump.h>

List of all members.

Public Member Functions

 TCPDump ()
 ~TCPDump ()
virtual void handleMessage (cMessage *msg)
virtual void initialize ()
virtual void finish ()

Protected Attributes

unsigned char * ringBuffer [RBUFFER_SIZE]
TCPDumper tcpdump
unsigned int snaplen
unsigned long first
unsigned long last
unsigned long space

Detailed Description

Dumps every packet using the TCPDumper class


Constructor & Destructor Documentation

TCPDump::TCPDump ( )
                 : cSimpleModule(), tcpdump(ev.getOStream())
{


}
TCPDump::~TCPDump ( )
{
}

Member Function Documentation

void TCPDump::finish ( ) [virtual]
{
     tcpdump.dump("", "tcpdump finished");
     if (strcmp(this->par("dumpFile"),"")!=0)
          fclose(tcpdump.dumpfile);
}
void TCPDump::handleMessage ( cMessage *  msg) [virtual]
{

    if (!ev.disable_tracing)
    {
        bool l2r;

        if (dynamic_cast<IPDatagram *>(msg))
        {
            if (((IPDatagram *)msg)->getTransportProtocol()==132)
            {
                tcpdump.ipDump("", (IPDatagram *)msg);
            }
            else
            {
                if (PK(msg)->hasBitError())
                {
                    delete msg;
                    return;
                }
                l2r = msg->arrivedOn("in1");
                if (((IPDatagram *)msg)->getTransportProtocol()==6)
                {
                    tcpdump.tcpDump(l2r, "", (IPDatagram *)msg, "");
                }
                else if (((IPDatagram *)msg)->getTransportProtocol()==17)
                    tcpdump.udpDump(l2r, "", (IPDatagram *)msg, "");
            }
        }
        else if (dynamic_cast<SCTPMessage *>(msg))
        {
            l2r = msg->arrivedOn("in1");
            tcpdump.sctpDump("", (SCTPMessage *)msg, std::string(l2r?"A":"B"),std::string(l2r?"B":"A"));
        }
        else if (dynamic_cast<TCPSegment *>(msg))
        {
            if (PK(msg)->hasBitError())
            {
                delete msg;
                return;
            }
            l2r = msg->arrivedOn("in1");
            tcpdump.tcpDump(l2r, "", (TCPSegment *)msg, std::string(l2r?"A":"B"),std::string(l2r?"B":"A"));
        }
        else if (dynamic_cast<ICMPMessage *>(msg))
        {
            if (PK(msg)->hasBitError())
            {
                delete msg;
                return;
            }
            std::cout<<"ICMPMessage\n";
        }
        else
        {
            // search for encapsulated IP[v6]Datagram in it
            cPacket *encapmsg = PK(msg);
            while (encapmsg && dynamic_cast<IPDatagram *>(encapmsg)==NULL && dynamic_cast<IPv6Datagram_Base *>(encapmsg)==NULL)
                encapmsg = encapmsg->getEncapsulatedPacket();
                l2r = msg->arrivedOn("in1");
            if (!encapmsg)
            {
                //We do not want this to end in an error if EtherAutoconf messages
                //are passed, so just print a warning. -WEI
                EV << "CANNOT DECODE: packet " << msg->getName() << " doesn't contain either IP or IPv6 Datagram\n";
            }
            else
            {
                if (dynamic_cast<IPDatagram *>(encapmsg))
                    tcpdump.tcpDump(l2r, "", (IPDatagram *)encapmsg);
                else if (dynamic_cast<IPv6Datagram_Base *>(encapmsg))
                    tcpdump.dumpIPv6(l2r, "", (IPv6Datagram_Base *)encapmsg);
                else
                ASSERT(0); // cannot get here
            }
        }
    }


    if (tcpdump.dumpfile!=NULL && dynamic_cast<IPDatagram *>(msg))
    {
        uint8 buf[MAXBUFLENGTH];
        memset((void*)&buf, 0, sizeof(buf));

        const simtime_t stime = simulation.getSimTime();
        // Write PCap header

        struct pcaprec_hdr ph;
        ph.ts_sec = (int32)stime.dbl();
        ph.ts_usec = (uint32)((stime.dbl() - ph.ts_sec)*1000000);
         // Write Ethernet header
        uint32 hdr = 2; //AF_INET
                     //We do not want this to end in an error if EtherAutoconf messages
        IPDatagram *ipPacket = check_and_cast<IPDatagram *>(msg);
        // IP header:
        //struct sockaddr_in *to = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in));
        //int32 tosize = sizeof(struct sockaddr_in);
        int32 serialized_ip = IPSerializer().serialize(ipPacket, buf, sizeof(buf));
        ph.incl_len = serialized_ip + sizeof(uint32);

        ph.orig_len = ph.incl_len;
        fwrite(&ph, sizeof(ph), 1, tcpdump.dumpfile);
        fwrite(&hdr, sizeof(uint32), 1, tcpdump.dumpfile);
        fwrite(buf, serialized_ip, 1, tcpdump.dumpfile);
    }


    // forward
    int32 index = msg->getArrivalGate()->getIndex();
    int32 id;
    if (msg->getArrivalGate()->isName("ifIn"))
        id = findGate("out2",index);
    else
        id = findGate("ifOut",index);

    send(msg, id);
}
void TCPDump::initialize ( ) [virtual]
{
    struct pcap_hdr fh;
    const char* file = this->par("dumpFile");
    snaplen = this->par("snaplen");
    tcpdump.setVerbosity(par("verbosity"));


    if (strcmp(file,"")!=0)
    {
        tcpdump.dumpfile = fopen(file, "wb");
        if (!tcpdump.dumpfile)
        {
            fprintf(stderr, "Cannot open file [%s] for writing: %s\n", file, strerror(errno));
            exit(-1);
        }

        fh.magic = PCAP_MAGIC;
        fh.version_major = 2;
        fh.version_minor = 4;
        fh.thiszone = 0;
        fh.sigfigs = 0;
        fh.snaplen = snaplen;
        fh.network = 0;
        fwrite(&fh, sizeof(fh), 1, tcpdump.dumpfile);
    }
    else
        tcpdump.dumpfile = NULL;
}

Member Data Documentation

unsigned long TCPDump::first [protected]
unsigned long TCPDump::last [protected]
unsigned char* TCPDump::ringBuffer[RBUFFER_SIZE] [protected]
unsigned int TCPDump::snaplen [protected]

Referenced by initialize().

unsigned long TCPDump::space [protected]

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