TCPDump Class Reference

#include <TCPDump.h>

List of all members.


Detailed Description

Dumps every packet using the TCPDumper class

Public Member Functions

 TCPDump ()
 ~TCPDump ()
 TCPDump (const char *name, cModule *parent)
virtual void handleMessage (cMessage *msg)
virtual void initialize ()
virtual void finish ()

Protected Attributes

TCPDumper tcpdump

Constructor & Destructor Documentation

TCPDump::TCPDump (  ) 

00364                  : cSimpleModule(), tcpdump(ev.getOStream())
00365 {
00366 
00367 
00368 }

TCPDump::~TCPDump (  ) 

00316 {
00317 }

TCPDump::TCPDump ( const char *  name,
cModule *  parent 
)

00359                                                   :
00360   cSimpleModule(name, parent, 0), tcpdump(ev.getOStream())
00361 {
00362 }


Member Function Documentation

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

00532 {
00533 struct pcaprec_hdr ph;
00534 simtime_t stime;
00535 int32 i;
00536 uint8 buf[MAXBUFLENGTH];
00537 bool l2r;
00538 uint32 hdr;
00539 
00540 for (i=0; i<MAXBUFLENGTH; i++)
00541     buf[i]=0;
00542     // dump
00543 
00544 
00545     if (!ev.disable_tracing)
00546     {
00547         if (dynamic_cast<IPDatagram *>(msg))
00548         {
00549             if (((IPDatagram *)msg)->getTransportProtocol()==132)
00550             {
00551                 tcpdump.ipDump("", (IPDatagram *)msg);
00552             }
00553             else
00554             {
00555                 if (PK(msg)->hasBitError())
00556                 {
00557                     delete msg;
00558                     return;
00559                 }
00560                 l2r = msg->arrivedOn("in1");
00561                 if (((IPDatagram *)msg)->getTransportProtocol()==6)
00562                 {
00563                     tcpdump.tcpDump(l2r, "", (IPDatagram *)msg, "");
00564                 }
00565                 else if (((IPDatagram *)msg)->getTransportProtocol()==17)
00566                     tcpdump.udpDump(l2r, "", (IPDatagram *)msg, "");
00567             }
00568         }
00569         else if (dynamic_cast<SCTPMessage *>(msg))
00570         {
00571             l2r = msg->arrivedOn("in1");
00572             tcpdump.sctpDump("", (SCTPMessage *)msg, std::string(l2r?"A":"B"),std::string(l2r?"B":"A"));
00573         }
00574         else if (dynamic_cast<TCPSegment *>(msg))
00575         {
00576             if (PK(msg)->hasBitError())
00577             {
00578                 delete msg;
00579                 return;
00580             }
00581             l2r = msg->arrivedOn("in1");
00582             tcpdump.tcpDump(l2r, "", (TCPSegment *)msg, std::string(l2r?"A":"B"),std::string(l2r?"B":"A"));
00583         }
00584         else if (dynamic_cast<ICMPMessage *>(msg))
00585         {
00586             if (PK(msg)->hasBitError())
00587             {
00588                 delete msg;
00589                 return;
00590             }
00591             std::cout<<"ICMPMessage\n";
00592         }
00593         else
00594         {
00595             // search for encapsulated IP[v6]Datagram in it
00596             cPacket *encapmsg = PK(msg);
00597             while (encapmsg && dynamic_cast<IPDatagram *>(encapmsg)==NULL && dynamic_cast<IPv6Datagram_Base *>(encapmsg)==NULL)
00598                 encapmsg = encapmsg->getEncapsulatedMsg();
00599                 l2r = msg->arrivedOn("in1");
00600             if (!encapmsg)
00601             {
00602                 //We do not want this to end in an error if EtherAutoconf messages
00603                 //are passed, so just print a warning. -WEI
00604                 EV << "CANNOT DECODE: packet " << msg->getName() << " doesn't contain either IP or IPv6 Datagram\n";
00605             }
00606             else
00607             {
00608                 if (dynamic_cast<IPDatagram *>(encapmsg))
00609                     tcpdump.tcpDump(l2r, "", (IPDatagram *)encapmsg);
00610                 else if (dynamic_cast<IPv6Datagram_Base *>(encapmsg))
00611                     tcpdump.dumpIPv6(l2r, "", (IPv6Datagram_Base *)encapmsg);
00612                 else
00613                 ASSERT(0); // cannot get here
00614             }
00615         }
00616     }
00617 
00618 
00619     if (tcpdump.dumpfile!=NULL && dynamic_cast<IPDatagram *>(msg))
00620     {
00621         stime = simulation.getSimTime();
00622         // Write PCap header
00623 
00624         ph.ts_sec = (int32)stime.dbl();
00625         ph.ts_usec = (uint32)((stime.dbl()-ph.ts_sec)*1000000);
00626          // Write link layer header
00627         hdr = 2; //AF_INET
00628         IPDatagram *ipPacket = check_and_cast<IPDatagram *>(msg);
00629         // IP header:
00630         //struct sockaddr_in *to = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in));
00631         //int32 tosize = sizeof(struct sockaddr_in);
00632         int32 serialized_ip = IPSerializer().serialize(ipPacket,buf, sizeof(buf));
00633         ph.incl_len = serialized_ip+sizeof(uint32);
00634         ph.orig_len = ph.incl_len;
00635         fwrite(&ph, sizeof(ph), 1, tcpdump.dumpfile);
00636         fwrite(&hdr, sizeof(uint32), 1, tcpdump.dumpfile);
00637 
00638         fwrite(buf, serialized_ip, 1, tcpdump.dumpfile);
00639     }
00640 
00641 
00642     // forward
00643     int32 index = msg->getArrivalGate()->getIndex();
00644     int32 id;
00645     if (msg->getArrivalGate()->isName("ifIn"))
00646         id = findGate("out2",index);
00647     else
00648         id = findGate("ifOut",index);
00649 
00650     send(msg, id);
00651 }

void TCPDump::initialize (  )  [virtual]

00506 {
00507 struct pcap_hdr fh;
00508 const char* file = this->par("dumpFile");
00509     if (strcmp(file,"")!=0)
00510     {
00511         tcpdump.dumpfile = fopen(file, "wb");
00512         if (!tcpdump.dumpfile)
00513         {
00514                     fprintf(stderr, "Cannot open file [%s] for writing: %s\n", file, strerror(errno));
00515                     exit(-1);
00516             }
00517 
00518         fh.magic = PCAP_MAGIC;
00519         fh.version_major = 2;
00520         fh.version_minor = 4;
00521         fh.thiszone = 0;
00522         fh.sigfigs = 0;
00523         fh.snaplen = 65535;
00524         fh.network = 0;
00525         fwrite(&fh, sizeof(fh), 1, tcpdump.dumpfile);
00526     }
00527     else
00528         tcpdump.dumpfile = NULL;
00529 }

void TCPDump::finish (  )  [virtual]

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


Member Data Documentation

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


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

Generated on Fri Mar 20 18:51:22 2009 for INET Framework for OMNeT++/OMNEST by  doxygen 1.5.5