TCPDump.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2005 Michael Tuexen
00003 //                    2008 Irene Ruengeler
00004 //                    2009 Thomas Dreibholz
00005 //
00006 // This program is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public License
00008 // as published by the Free Software Foundation; either version 2
00009 // of the License, or (at your option) any later version.
00010 //
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014 // GNU Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public License
00017 // along with this program; if not, see <http://www.gnu.org/licenses/>.
00018 //
00019 
00020 #ifndef __TCPDUMP_H
00021 #define __TCPDUMP_H
00022 
00023 #include <omnetpp.h>
00024 #include <assert.h>
00025 #include "IPAddress.h"
00026 //#include "IPDatagram_m.h"
00027 #include "IPDatagram.h"
00028 #include "SCTPMessage.h"
00029 #include "TCPSegment.h"
00030 #include "IPv6Datagram_m.h"
00031 
00032 #define PCAP_MAGIC           0xa1b2c3d4
00033 #define RBUFFER_SIZE 65535
00034 
00035 /* "libpcap" file header (minus magic number). */
00036 struct pcap_hdr {
00037      uint32 magic;      /* magic */
00038      uint16 version_major;   /* major version number */
00039      uint16 version_minor;   /* minor version number */
00040      uint32 thiszone;   /* GMT to local correction */
00041      uint32 sigfigs;        /* accuracy of timestamps */
00042      uint32 snaplen;        /* max length of captured packets, in octets */
00043      uint32 network;        /* data link type */
00044 };
00045 
00046 /* "libpcap" record header. */
00047 struct pcaprec_hdr {
00048      int32  ts_sec;     /* timestamp seconds */
00049      uint32 ts_usec;        /* timestamp microseconds */
00050      uint32 incl_len;   /* number of octets of packet saved in file */
00051      uint32 orig_len;   /* actual length of packet */
00052 };
00053 
00054 typedef struct {
00055      uint8  dest_addr[6];
00056      uint8  src_addr[6];
00057      uint16 l3pid;
00058 } hdr_ethernet_t;
00059 
00060 /* T.D. 22.09.09: commented this out, since it is not used anywhere.
00061 static hdr_ethernet_t HDR_ETHERNET = {
00062      {0x02, 0x02, 0x02, 0x02, 0x02, 0x02},
00063      {0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
00064      0};
00065 */
00066 
00070 class TCPDumper
00071 {
00072     protected:
00073         int32 seq;
00074         std::ostream *outp;
00075     public:
00076         TCPDumper(std::ostream& o);
00077         ~TCPDumper();
00078         inline void setVerbosity(const int32 verbosityLevel) {
00079             verbosity = verbosityLevel;
00080         }
00081         void ipDump(const char *label, IPDatagram *dgram, const char *comment=NULL);
00082         void sctpDump(const char *label, SCTPMessage *sctpmsg, const std::string& srcAddr, const std::string& destAddr, const char *comment=NULL);
00083         // dumps arbitary text
00084         void dump(const char *label, const char *msg);
00085         void tcpDump(bool l2r, const char *label, IPDatagram *dgram, const char *comment=NULL);
00086         void tcpDump(bool l2r, const char *label, TCPSegment *tcpseg, const std::string& srcAddr, const std::string& destAddr, const char *comment=NULL);
00087         void dumpIPv6(bool l2r, const char *label, IPv6Datagram_Base *dgram, const char *comment=NULL);//FIXME: Temporary hack
00088         void udpDump(bool l2r, const char *label, IPDatagram *dgram, const char *comment);
00089         const char* intToChunk(int32 type);
00090         FILE *dumpfile;
00091     private:
00092         int verbosity;
00093 };
00094 
00095 
00099 class INET_API TCPDump : public cSimpleModule
00100 {
00101     protected:
00102         unsigned char* ringBuffer[RBUFFER_SIZE];
00103         TCPDumper tcpdump;
00104         unsigned int snaplen;
00105         unsigned long first, last, space;
00106 
00107     public:
00108 
00109         TCPDump();
00110         ~TCPDump();
00111         virtual void handleMessage(cMessage *msg);
00112         virtual void initialize();
00113         virtual void finish();
00114 };
00115 
00116 #endif
00117 
00118