1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 2 /* 3 * Copyright (c) 2009 University of Washington 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 19 #ifndef PCAP_FILE_WRAPPER_H 20 #define PCAP_FILE_WRAPPER_H 21 22 #include <cstring> 23 #include <limits> 24 #include <fstream> 25 #include "ns3/ptr.h" 26 #include "ns3/packet.h" 27 #include "ns3/object.h" 28 #include "ns3/nstime.h" 29 #include "pcap-file.h" 30 31 namespace ns3 { 32 33 /** 34 * A class that wraps a PcapFile as an ns3::Object and provides a higher-layer 35 * ns-3 interface to the low-level public methods of PcapFile. Users are 36 * encouraged to use this object instead of class ns3::PcapFile in ns-3 37 * public APIs. 38 */ 39 class PcapFileWrapper : public Object 40 { 41 public: 42 /** 43 * \brief Get the type ID. 44 * \return the object TypeId 45 */ 46 static TypeId GetTypeId (void); 47 48 PcapFileWrapper (); 49 ~PcapFileWrapper (); 50 51 52 /** 53 * \return true if the 'fail' bit is set in the underlying iostream, false otherwise. 54 */ 55 bool Fail (void) const; 56 /** 57 * \return true if the 'eof' bit is set in the underlying iostream, false otherwise. 58 */ 59 bool Eof (void) const; 60 /** 61 * Clear all state bits of the underlying iostream. 62 */ 63 void Clear (void); 64 65 /** 66 * Create a new pcap file or open an existing pcap file. Semantics are 67 * similar to the stdc++ io stream classes. 68 * 69 * Since a pcap file is always a binary file, the file type is automatically 70 * selected as a binary file (fstream::binary is automatically ored with the mode 71 * field). 72 * 73 * \param filename String containing the name of the file. 74 * 75 * \param mode String containing the access mode for the file. 76 * 77 */ 78 void Open (std::string const &filename, std::ios::openmode mode); 79 80 /** 81 * Close the underlying pcap file. 82 */ 83 void Close (void); 84 85 /** 86 * Initialize the pcap file associated with this wrapper. This file must have 87 * been previously opened with write permissions. 88 * 89 * \param dataLinkType A data link type as defined in the pcap library. If 90 * you want to make resulting pcap files visible in existing tools, the 91 * data link type must match existing definitions, such as PCAP_ETHERNET, 92 * PCAP_PPP, PCAP_80211, etc. If you are storing different kinds of packet 93 * data, such as naked TCP headers, you are at liberty to locally define your 94 * own data link types. According to the pcap-linktype man page, "well-known" 95 * pcap linktypes range from 0 to 177. If you use a large random number for 96 * your type, chances are small for a collision. 97 * 98 * \param snapLen An optional maximum size for packets written to the file. 99 * Defaults to 65535. If packets exceed this length they are truncated. 100 * 101 * \param tzCorrection An integer describing the offset of your local 102 * time zone from UTC/GMT. For example, Pacific Standard Time in the US is 103 * GMT-8, so one would enter -8 for that correction. Defaults to 0 (UTC). 104 * 105 * \warning Calling this method on an existing file will result in the loss 106 * any existing data. 107 */ 108 void Init (uint32_t dataLinkType, 109 uint32_t snapLen = std::numeric_limits<uint32_t>::max (), 110 int32_t tzCorrection = PcapFile::ZONE_DEFAULT); 111 112 /** 113 * \brief Write the next packet to file 114 * 115 * \param t Packet timestamp as ns3::Time. 116 * \param p Packet to write to the pcap file. 117 * 118 */ 119 void Write (Time t, Ptr<const Packet> p); 120 121 /** 122 * \brief Write the provided header along with the packet to the pcap file. 123 * 124 * It is the case that adding a header to a packet prior to writing it to a 125 * file must trigger a deep copy in the Packet. By providing the header 126 * separately, we can avoid that copy. 127 * 128 * \param t Packet timestamp as ns3::Time. 129 * \param header The Header to prepend to the packet. 130 * \param p Packet to write to the pcap file. 131 * 132 */ 133 void Write (Time t, const Header &header, Ptr<const Packet> p); 134 135 /** 136 * \brief Write the provided data buffer to the pcap file. 137 * 138 * \param t Packet timestamp as ns3::Time. 139 * \param buffer The buffer to write. 140 * \param length The size of the buffer. 141 * 142 */ 143 void Write (Time t, uint8_t const *buffer, uint32_t length); 144 145 /** 146 * \brief Read the next packet from the file. 147 * 148 * \param t Reference to packet timestamp as ns3::Time. 149 * \returns a pointer to ns3::Packet. 150 */ 151 Ptr<Packet> Read (Time &t); 152 153 /** 154 * \brief Returns the magic number of the pcap file as defined by the magic_number 155 * field in the pcap global header. 156 * 157 * See http://wiki.wireshark.org/Development/LibpcapFileFormat 158 * 159 * \returns magic number 160 */ 161 uint32_t GetMagic (void); 162 163 /** 164 * \brief Returns the major version of the pcap file as defined by the version_major 165 * field in the pcap global header. 166 * 167 * See http://wiki.wireshark.org/Development/LibpcapFileFormat 168 * 169 * \returns major version 170 */ 171 uint16_t GetVersionMajor (void); 172 173 /** 174 * \brief Returns the minor version of the pcap file as defined by the version_minor 175 * field in the pcap global header. 176 * 177 * See http://wiki.wireshark.org/Development/LibpcapFileFormat 178 * 179 * \returns minor version 180 */ 181 uint16_t GetVersionMinor (void); 182 183 /** 184 * \brief Returns the time zone offset of the pcap file as defined by the thiszone 185 * field in the pcap global header. 186 * 187 * See http://wiki.wireshark.org/Development/LibpcapFileFormat 188 * 189 * \returns time zone offset 190 */ 191 int32_t GetTimeZoneOffset (void); 192 193 /** 194 * \brief Returns the accuracy of timestamps field of the pcap file as defined 195 * by the sigfigs field in the pcap global header. 196 * 197 * See http://wiki.wireshark.org/Development/LibpcapFileFormat 198 * 199 * \returns accuracy of timestamps 200 */ 201 uint32_t GetSigFigs (void); 202 203 /** 204 * \brief Returns the max length of saved packets field of the pcap file as 205 * defined by the snaplen field in the pcap global header. 206 * 207 * See http://wiki.wireshark.org/Development/LibpcapFileFormat 208 * 209 * \returns max length of saved packets field 210 */ 211 uint32_t GetSnapLen (void); 212 213 /** 214 * \brief Returns the data link type field of the pcap file as defined by the 215 * network field in the pcap global header. 216 * 217 * See http://wiki.wireshark.org/Development/LibpcapFileFormat 218 * 219 * \returns data link type field 220 */ 221 uint32_t GetDataLinkType (void); 222 223 private: 224 PcapFile m_file; //!< Pcap file 225 uint32_t m_snapLen; //!< max length of saved packets 226 bool m_nanosecMode; //!< Timestamps in nanosecond mode 227 }; 228 229 } // namespace ns3 230 231 #endif /* PCAP_FILE_WRAPPER_H */ 232