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