src/common/pcap-writer.h
author Nicola Baldo <nbaldo@cttc.es>
Thu Aug 13 09:36:53 2009 +0200 (2009-08-13)
changeset 4709 b0743dbc4e55
parent 4645 d53223aae797
child 4715 d0041768ff60
permissions -rw-r--r--
bug 639: add configurable capture size to pcap
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2005,2006 INRIA
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License version 2 as
     7  * published by the Free Software Foundation;
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    17  *
    18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    19  */
    20 
    21 #ifndef PCAP_WRITER_H
    22 #define PCAP_WRITER_H
    23 
    24 #include <stdint.h>
    25 #include "ns3/object.h"
    26 
    27 namespace ns3 {
    28 
    29 class Packet;
    30 
    31 /**
    32  * \ingroup common
    33  *
    34  * \brief Pcap output for Packet logger
    35  *
    36  * Log Packets to a file in pcap format which can be
    37  * read by pcap readers.
    38  */
    39 class PcapWriter : public Object
    40 {
    41 public:
    42   static TypeId GetTypeId (void);
    43   PcapWriter ();
    44   ~PcapWriter ();
    45 
    46   /**
    47    * \param name the name of the file to store packet log into.
    48    * This method creates the file if it does not exist. If it
    49    * exists, the file is emptied.
    50    */
    51   void Open (std::string const &name);
    52 
    53   /**
    54    * Write a pcap header in the output file which specifies
    55    * that the content of the file will be Packets with
    56    * Ethernet/LLC/SNAP encapsulation. This method should
    57    * be invoked before ns3::PcapWriter::writePacket and after
    58    * ns3::PcapWriter::open.
    59    */
    60   void WriteEthernetHeader (void);
    61 
    62   /**
    63    * Write a pcap header in the output file which specifies
    64    * that the content of the file will be IPv4 Packets. This 
    65    * method should be invoked before ns3::PcapWriter::WritePacket 
    66    * and after ns3::PcapWriter::Open.
    67    */
    68   void WriteIpHeader (void);
    69 
    70   /**
    71    * Write a pcap header in the output file which specifies
    72    * that the content of the file will be 802.11 Packets. This 
    73    * method should be invoked before ns3::PcapWriter::WritePacket 
    74    * and after ns3::PcapWriter::Open.
    75    */
    76   void WriteWifiHeader (void);
    77 
    78   /**
    79    * Write a pcap header in the output file which specifies
    80    * that the content of the file will be 802.11 Packets preceded by a
    81    * radiotap header providing PHY layer info. This method should be
    82    * invoked before ns3::PcapWriter::WritePacket and after
    83    * ns3::PcapWriter::Open. 
    84    */
    85   void WriteWifiRadiotapHeader (void);
    86 
    87   /**
    88    * Write a pcap header in the output file which specifies
    89    * that the content of the file will be 802.11 Packets preceded by a
    90    * prism header providing PHY layer info. This method should be
    91    * invoked before ns3::PcapWriter::WritePacket and after
    92    * ns3::PcapWriter::Open. 
    93    */
    94   void WriteWifiPrismHeader (void);
    95 
    96   /**
    97    * Write a pcap header in the output file which specifies
    98    * that the content of the file will be ppp Packets. This 
    99    * method should be invoked before ns3::PcapWriter::WritePacket 
   100    * and after ns3::PcapWriter::Open.
   101    */
   102   void WritePppHeader (void);
   103 
   104   /**
   105    * \param packet packet to write to output file
   106    */
   107   void WritePacket (Ptr<const Packet> packet);
   108 
   109   /** 
   110    * Write a Packet, possibly adding wifi PHY layer information to it
   111    *
   112    * @param packet the packet being received
   113    * @param channelFreqMhz the frequency in MHz at which the packet is
   114    * received. Note that in real devices this is normally the
   115    * frequency to which  the receiver is tuned, and this can be
   116    * different than the frequency at which the packet was originally
   117    * transmitted. This is because it is possible to have the receiver
   118    * tuned on a given channel and still to be able to receive packets
   119    * on a nearby channel.
   120    * @param rate the PHY data rate in units of 500kbps (i.e., the same
   121    * units used both for the radiotap and for the prism header) 
   122    * @param isShortPreamble true if short preamble is used, false otherwise
   123    * @param isTx true if packet is being transmitted, false when
   124    * packet is being received
   125    * @param signalDbm signal power in dBm
   126    * @param noiseDbm  noise power in dBm
   127    */
   128   void WriteWifiMonitorPacket(Ptr<const Packet> packet, uint16_t channelFreqMhz, 
   129                               uint32_t rate, bool isShortPreamble, bool isTx, 
   130                               double signalDbm, double noiseDbm);
   131 
   132   /** 
   133    * Set the maximum number of bytes to be captured for each packet. 
   134    * 
   135    * @param size the maximum number of bytes to be captured. If zero
   136    * (default), the whole packet will be captured. 
   137    */
   138   void SetCaptureSize (uint32_t size);
   139 
   140 
   141 private:
   142   void WriteData (uint8_t const*buffer, uint32_t size);
   143   void Write64 (uint64_t data);
   144   void Write32 (uint32_t data);
   145   void Write16 (uint16_t data);
   146   void Write8 (uint8_t data);
   147   void WriteHeader (uint32_t network);
   148   int8_t RoundToInt8 (double value);
   149   std::ofstream *m_writer;
   150   uint32_t m_pcapMode;
   151   uint32_t m_captureSize;
   152   
   153 };
   154 
   155 } // namespace ns3
   156 
   157 #endif /* PCAP_WRITER_H */