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.
     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 /*
    22  * Documentation kindly pointed out by Tom Henderson:
    23  * http://wiki.ethereal.com/Development/LibpcapFileFormat
    24  */
    25 
    26 #include <fstream>
    27 
    28 #include "ns3/simulator.h"
    29 #include "pcap-writer.h"
    30 #include "packet.h"
    31 
    32 
    33 namespace ns3 {
    34 
    35 enum {
    36   PCAP_ETHERNET = 1,
    37   PCAP_RAW_IP   = 101,
    38   PCAP_80211    = 105,
    39 };
    40 
    41 PcapWriter::PcapWriter ()
    42 {
    43   m_writer = 0;
    44 }
    45 PcapWriter::~PcapWriter ()
    46 {
    47   delete m_writer;
    48 }
    49 
    50 void
    51 PcapWriter::Open (std::string const &name)
    52 {
    53   m_writer = new std::ofstream ();
    54   m_writer->open (name.c_str ());
    55 }
    56 
    57 void 
    58 PcapWriter::WriteEthernetHeader (void)
    59 {
    60   WriteHeader (PCAP_ETHERNET);
    61 }
    62 
    63 void 
    64 PcapWriter::WriteIpHeader (void)
    65 {
    66   WriteHeader (PCAP_RAW_IP);
    67 }
    68 
    69 void
    70 PcapWriter::WriteWifiHeader (void)
    71 {
    72   WriteHeader (PCAP_80211);
    73 }
    74 
    75 void 
    76 PcapWriter::WriteHeader (uint32_t network)
    77 {
    78   Write32 (0xa1b2c3d4);
    79   Write16 (2);
    80   Write16 (4);
    81   Write32 (0);
    82   Write32 (0);
    83   Write32 (0xffff);
    84   Write32 (network);
    85 }
    86 
    87 
    88 
    89 
    90 void 
    91 PcapWriter::WritePacket (Ptr<const Packet> packet)
    92 {
    93   if (m_writer != 0) 
    94     {
    95       uint64_t current = Simulator::Now ().GetMicroSeconds ();
    96       uint64_t s = current / 1000000;
    97       uint64_t us = current % 1000000;
    98       Write32 (s & 0xffffffff);
    99       Write32 (us & 0xffffffff);
   100       Write32 (packet->GetSize ());
   101       Write32 (packet->GetSize ());
   102       WriteData (packet->PeekData (), packet->GetSize ());
   103     }
   104 }
   105 
   106 void
   107 PcapWriter::WriteData (uint8_t const*buffer, uint32_t size)
   108 {
   109   m_writer->write ((char const *)buffer, size);
   110 }
   111 void
   112 PcapWriter::Write32 (uint32_t data)
   113 {
   114   uint8_t buffer[4];
   115   buffer[0] = (data >> 0) & 0xff;
   116   buffer[1] = (data >> 8) & 0xff;
   117   buffer[2] = (data >> 16) & 0xff;
   118   buffer[3] = (data >> 24) & 0xff;
   119   WriteData (buffer, 4);
   120 }
   121 void
   122 PcapWriter::Write16 (uint16_t data)
   123 {
   124   uint8_t buffer[2];
   125   buffer[0] = (data >> 0) & 0xff;
   126   buffer[1] = (data >> 8) & 0xff;
   127   WriteData (buffer, 2);
   128 }
   129 
   130 } // namespace ns3