00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2005,2006 INRIA 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> 00019 */ 00020 00021 #ifndef PCAP_WRITER_H 00022 #define PCAP_WRITER_H 00023 00024 #include <stdint.h> 00025 #include "ns3/ref-count-base.h" 00026 00027 namespace ns3 { 00028 00029 class Packet; 00030 00031 /** 00032 * \ingroup common 00033 * 00034 * \brief Pcap output for Packet logger 00035 * 00036 * Log Packets to a file in pcap format which can be 00037 * read by pcap readers. 00038 */ 00039 class PcapWriter : public RefCountBase 00040 { 00041 public: 00042 PcapWriter (); 00043 ~PcapWriter (); 00044 00045 /** 00046 * \param name the name of the file to store packet log into. 00047 * This method creates the file if it does not exist. If it 00048 * exists, the file is emptied. 00049 */ 00050 void Open (std::string const &name); 00051 00052 /** 00053 * Write a pcap header in the output file which specifies 00054 * that the content of the file will be Packets with 00055 * Ethernet/LLC/SNAP encapsulation. This method should 00056 * be invoked before ns3::PcapWriter::writePacket and after 00057 * ns3::PcapWriter::open. 00058 */ 00059 void WriteEthernetHeader (void); 00060 00061 /** 00062 * Write a pcap header in the output file which specifies 00063 * that the content of the file will be IPv4 Packets. This 00064 * method should be invoked before ns3::PcapWriter::WritePacket 00065 * and after ns3::PcapWriter::Open. 00066 */ 00067 void WriteIpHeader (void); 00068 00069 /** 00070 * Write a pcap header in the output file which specifies 00071 * that the content of the file will be 802.11 Packets. This 00072 * method should be invoked before ns3::PcapWriter::WritePacket 00073 * and after ns3::PcapWriter::Open. 00074 */ 00075 void WriteWifiHeader (void); 00076 00077 /** 00078 * Write a pcap header in the output file which specifies 00079 * that the content of the file will be ppp Packets. This 00080 * method should be invoked before ns3::PcapWriter::WritePacket 00081 * and after ns3::PcapWriter::Open. 00082 */ 00083 void WritePppHeader (void); 00084 00085 /** 00086 * \param packet packet to write to output file 00087 */ 00088 void WritePacket (Ptr<const Packet> packet); 00089 00090 private: 00091 void WriteData (uint8_t const*buffer, uint32_t size); 00092 void Write32 (uint32_t data); 00093 void Write16 (uint16_t data); 00094 void WriteHeader (uint32_t network); 00095 std::ofstream *m_writer; 00096 }; 00097 00098 } // namespace ns3 00099 00100 #endif /* PCAP_WRITER_H */