Public Member Functions | Protected Attributes

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

Definition at line 99 of file TCPDump.h.


Constructor & Destructor Documentation

TCPDump::TCPDump (  ) 

Definition at line 359 of file TCPDump.cc.

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


}

TCPDump::~TCPDump (  ) 

Definition at line 318 of file TCPDump.cc.

{
}


Member Function Documentation

void TCPDump::finish (  )  [virtual]

Definition at line 668 of file TCPDump.cc.

{
     tcpdump.dump("", "tcpdump finished");
     if (strcmp(this->par("dumpFile"),"")!=0)
          fclose(tcpdump.dumpfile);
}

void TCPDump::handleMessage ( cMessage *  msg  )  [virtual]

Definition at line 550 of file TCPDump.cc.

{

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

Definition at line 520 of file TCPDump.cc.

{
    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]

Definition at line 105 of file TCPDump.h.

unsigned long TCPDump::last [protected]

Definition at line 105 of file TCPDump.h.

unsigned char* TCPDump::ringBuffer[RBUFFER_SIZE] [protected]

Definition at line 102 of file TCPDump.h.

unsigned int TCPDump::snaplen [protected]

Definition at line 104 of file TCPDump.h.

Referenced by initialize().

unsigned long TCPDump::space [protected]

Definition at line 105 of file TCPDump.h.

Definition at line 103 of file TCPDump.h.

Referenced by finish(), handleMessage(), and initialize().


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