src/common/pcap-writer.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Fri Apr 11 11:19:54 2008 -0700 (2008-04-11)
changeset 2906 5a7ae076410e
parent 2834 1aab57845b07
child 3012 56a4c75f9422
permissions -rw-r--r--
ensure that out PcapTraces are generated in a consistant format to allow simple comparison of traces against a single set of reference traces.
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
/*
mathieu@9
    22
 * Documentation kindly pointed out by Tom Henderson:
mathieu@9
    23
 * http://wiki.ethereal.com/Development/LibpcapFileFormat
mathieu@9
    24
 */
mathieu@9
    25
mathieu@458
    26
#include <fstream>
mathieu@458
    27
mathieu@14
    28
#include "ns3/simulator.h"
mathieu@9
    29
#include "pcap-writer.h"
mathieu@9
    30
#include "packet.h"
mathieu@9
    31
mathieu@9
    32
mathieu@16
    33
namespace ns3 {
mathieu@9
    34
mathieu@9
    35
enum {
mathieu@454
    36
  PCAP_ETHERNET = 1,
mathieu@454
    37
  PCAP_RAW_IP   = 101,
mathieu@455
    38
  PCAP_80211    = 105,
mathieu@9
    39
};
mathieu@9
    40
mathieu@9
    41
PcapWriter::PcapWriter ()
mathieu@9
    42
{
mathieu@150
    43
  m_writer = 0;
mathieu@9
    44
}
mathieu@9
    45
PcapWriter::~PcapWriter ()
mathieu@9
    46
{
mathieu@150
    47
  delete m_writer;
mathieu@9
    48
}
mathieu@9
    49
mathieu@9
    50
void
mathieu@456
    51
PcapWriter::Open (std::string const &name)
mathieu@9
    52
{
mathieu@458
    53
  m_writer = new std::ofstream ();
mathieu@458
    54
  m_writer->open (name.c_str ());
mathieu@9
    55
}
mathieu@9
    56
mathieu@9
    57
void 
mathieu@454
    58
PcapWriter::WriteEthernetHeader (void)
mathieu@454
    59
{
mathieu@454
    60
  WriteHeader (PCAP_ETHERNET);
mathieu@454
    61
}
mathieu@454
    62
mathieu@454
    63
void 
mathieu@454
    64
PcapWriter::WriteIpHeader (void)
mathieu@454
    65
{
mathieu@454
    66
  WriteHeader (PCAP_RAW_IP);
mathieu@454
    67
}
mathieu@454
    68
mathieu@455
    69
void
mathieu@455
    70
PcapWriter::WriteWifiHeader (void)
mathieu@455
    71
{
mathieu@455
    72
  WriteHeader (PCAP_80211);
mathieu@455
    73
}
mathieu@455
    74
mathieu@454
    75
void 
mathieu@454
    76
PcapWriter::WriteHeader (uint32_t network)
mathieu@9
    77
{
mathieu@150
    78
  Write32 (0xa1b2c3d4);
mathieu@150
    79
  Write16 (2);
mathieu@150
    80
  Write16 (4);
mathieu@150
    81
  Write32 (0);
mathieu@150
    82
  Write32 (0);
mathieu@150
    83
  Write32 (0xffff);
mathieu@454
    84
  Write32 (network);
mathieu@9
    85
}
mathieu@9
    86
mathieu@454
    87
mathieu@454
    88
mathieu@454
    89
mathieu@9
    90
void 
mathieu@1866
    91
PcapWriter::WritePacket (Ptr<const Packet> packet)
mathieu@9
    92
{
mathieu@150
    93
  if (m_writer != 0) 
mathieu@150
    94
    {
mathieu@163
    95
      uint64_t current = Simulator::Now ().GetMicroSeconds ();
mathieu@150
    96
      uint64_t s = current / 1000000;
mathieu@150
    97
      uint64_t us = current % 1000000;
mathieu@150
    98
      Write32 (s & 0xffffffff);
mathieu@150
    99
      Write32 (us & 0xffffffff);
mathieu@1866
   100
      Write32 (packet->GetSize ());
mathieu@1866
   101
      Write32 (packet->GetSize ());
mathieu@1866
   102
      WriteData (packet->PeekData (), packet->GetSize ());
mathieu@150
   103
    }
mathieu@9
   104
}
mathieu@9
   105
mathieu@9
   106
void
mathieu@458
   107
PcapWriter::WriteData (uint8_t const*buffer, uint32_t size)
mathieu@9
   108
{
mathieu@458
   109
  m_writer->write ((char const *)buffer, size);
mathieu@9
   110
}
mathieu@9
   111
void
mathieu@122
   112
PcapWriter::Write32 (uint32_t data)
mathieu@9
   113
{
mathieu@2906
   114
  uint8_t buffer[4];
mathieu@2906
   115
  buffer[0] = (data >> 0) & 0xff;
mathieu@2906
   116
  buffer[1] = (data >> 8) & 0xff;
mathieu@2906
   117
  buffer[2] = (data >> 16) & 0xff;
mathieu@2906
   118
  buffer[3] = (data >> 24) & 0xff;
mathieu@2906
   119
  WriteData (buffer, 4);
mathieu@9
   120
}
mathieu@9
   121
void
mathieu@122
   122
PcapWriter::Write16 (uint16_t data)
mathieu@9
   123
{
mathieu@2906
   124
  uint8_t buffer[2];
mathieu@2906
   125
  buffer[0] = (data >> 0) & 0xff;
mathieu@2906
   126
  buffer[1] = (data >> 8) & 0xff;
mathieu@2906
   127
  WriteData (buffer, 2);
mathieu@9
   128
}
mathieu@9
   129
mathieu@2773
   130
} // namespace ns3