src/common/pcap-writer.h
changeset 6068 a2127017ecb4
parent 6067 ccbdc2b19ea5
parent 6062 f62b76f5c92a
child 6069 c21754b56036
equal deleted inserted replaced
6067:ccbdc2b19ea5 6068:a2127017ecb4
     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 channelNumber the channel number, as defined by the
       
   121    * IEEE 802.11 standard. 
       
   122    * @param rate the PHY data rate in units of 500kbps (i.e., the same
       
   123    * units used both for the radiotap and for the prism header) 
       
   124    * @param isShortPreamble true if short preamble is used, false otherwise
       
   125    * @param isTx true if packet is being transmitted, false when
       
   126    * packet is being received
       
   127    * @param signalDbm signal power in dBm
       
   128    * @param noiseDbm  noise power in dBm
       
   129    */
       
   130   void WriteWifiMonitorPacket(Ptr<const Packet> packet, uint16_t channelFreqMhz,  uint16_t channelNumber,
       
   131                               uint32_t rate, bool isShortPreamble, bool isTx, 
       
   132                               double signalDbm, double noiseDbm);
       
   133 
       
   134   /** 
       
   135    * Set the maximum number of bytes to be captured for each packet. 
       
   136    * 
       
   137    * @param size the maximum number of bytes to be captured. If zero
       
   138    * (default), the whole packet will be captured. 
       
   139    */
       
   140   void SetCaptureSize (uint32_t size);
       
   141 
       
   142 
       
   143 private:
       
   144   void WriteData (uint8_t const*buffer, uint32_t size);
       
   145   void Write64 (uint64_t data);
       
   146   void Write32 (uint32_t data);
       
   147   void Write16 (uint16_t data);
       
   148   void Write8 (uint8_t data);
       
   149   void WriteHeader (uint32_t network);
       
   150   int8_t RoundToInt8 (double value);
       
   151   std::ofstream *m_writer;
       
   152   uint32_t m_pcapMode;
       
   153   uint32_t m_captureSize;
       
   154   
       
   155 };
       
   156 
       
   157 } // namespace ns3
       
   158 
       
   159 #endif /* PCAP_WRITER_H */