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; -*- */
3 * Copyright (c) 2005,2006 INRIA
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;
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.
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
18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
22 * Documentation kindly pointed out by Tom Henderson:
23 * http://wiki.ethereal.com/Development/LibpcapFileFormat
28 #include "ns3/simulator.h"
29 #include "pcap-writer.h"
41 PcapWriter::PcapWriter ()
45 PcapWriter::~PcapWriter ()
51 PcapWriter::Open (std::string const &name)
53 m_writer = new std::ofstream ();
54 m_writer->open (name.c_str ());
58 PcapWriter::WriteEthernetHeader (void)
60 WriteHeader (PCAP_ETHERNET);
64 PcapWriter::WriteIpHeader (void)
66 WriteHeader (PCAP_RAW_IP);
70 PcapWriter::WriteWifiHeader (void)
72 WriteHeader (PCAP_80211);
76 PcapWriter::WriteHeader (uint32_t network)
91 PcapWriter::WritePacket (Ptr<const Packet> packet)
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 ());
107 PcapWriter::WriteData (uint8_t const*buffer, uint32_t size)
109 m_writer->write ((char const *)buffer, size);
112 PcapWriter::Write32 (uint32_t data)
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);
122 PcapWriter::Write16 (uint16_t data)
125 buffer[0] = (data >> 0) & 0xff;
126 buffer[1] = (data >> 8) & 0xff;
127 WriteData (buffer, 2);