/usr/include/ns3.27/ns3/pcap-file.h is in libns3-dev 3.27+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2009 University of Washington
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Craig Dowell (craigdo@ee.washington.edu)
*/
#ifndef PCAP_FILE_H
#define PCAP_FILE_H
#include <string>
#include <fstream>
#include <stdint.h>
#include "ns3/ptr.h"
namespace ns3 {
class Packet;
class Header;
/**
* \brief A class representing a pcap file
*
* A class representing a pcap file. This allows easy creation, writing and
* reading of files composed of stored packets; which may be viewed using
* standard tools.
*/
class PcapFile
{
public:
static const int32_t ZONE_DEFAULT = 0; /**< Time zone offset for current location */
static const uint32_t SNAPLEN_DEFAULT = 65535; /**< Default value for maximum octets to save per packet */
public:
PcapFile ();
~PcapFile ();
/**
* \return true if the 'fail' bit is set in the underlying iostream, false otherwise.
*/
bool Fail (void) const;
/**
* \return true if the 'eof' bit is set in the underlying iostream, false otherwise.
*/
bool Eof (void) const;
/**
* Clear all state bits of the underlying iostream.
*/
void Clear (void);
/**
* Create a new pcap file or open an existing pcap file. Semantics are
* similar to the stdc++ io stream classes, but differ in that
* positions in the file are based on packets not characters. For example
* if the file is opened for reading, the file position indicator (seek
* position) points to the beginning of the first packet in the file, not
* zero (which would point to the start of the pcap header).
*
* Since a pcap file is always a binary file, the file type is automatically
* selected as a binary file (fstream::binary is automatically ored with the mode
* field).
*
* \param filename String containing the name of the file.
*
* \param mode the access mode for the file.
*/
void Open (std::string const &filename, std::ios::openmode mode);
/**
* Close the underlying file.
*/
void Close (void);
/**
* Initialize the pcap file associated with this object. This file must have
* been previously opened with write permissions.
*
* \param dataLinkType A data link type as defined in the pcap library. If
* you want to make resulting pcap files visible in existing tools, the
* data link type must match existing definitions, such as PCAP_ETHERNET,
* PCAP_PPP, PCAP_80211, etc. If you are storing different kinds of packet
* data, such as naked TCP headers, you are at liberty to locally define your
* own data link types. According to the pcap-linktype man page, "well-known"
* pcap linktypes range from 0 to 263. If you use a large random number for
* your type, chances are small for a collision.
*
* \param snapLen An optional maximum size for packets written to the file.
* Defaults to 65535. If packets exceed this length they are truncated.
*
* \param timeZoneCorrection An integer describing the offset of your local
* time zone from UTC/GMT. For example, Pacific Standard Time in the US is
* GMT-8, so one would enter -8 for that correction. Defaults to 0 (UTC).
*
* \param swapMode Flag indicating a difference in endianness of the
* writing system. Defaults to false.
*
* \param nanosecMode Flag indicating the time resolution of the writing
* system. Default to false.
*
* \return false if the open succeeds, true otherwise.
*
* \warning Calling this method on an existing file will result in the loss
* any existing data.
*/
void Init (uint32_t dataLinkType,
uint32_t snapLen = SNAPLEN_DEFAULT,
int32_t timeZoneCorrection = ZONE_DEFAULT,
bool swapMode = false,
bool nanosecMode = false);
/**
* \brief Write next packet to file
*
* \param tsSec Packet timestamp, seconds
* \param tsUsec Packet timestamp, microseconds
* \param data Data buffer
* \param totalLen Total packet length
*
*/
void Write (uint32_t tsSec, uint32_t tsUsec, uint8_t const * const data, uint32_t totalLen);
/**
* \brief Write next packet to file
*
* \param tsSec Packet timestamp, seconds
* \param tsUsec Packet timestamp, microseconds
* \param p Packet to write
*
*/
void Write (uint32_t tsSec, uint32_t tsUsec, Ptr<const Packet> p);
/**
* \brief Write next packet to file
*
* \param tsSec Packet timestamp, seconds
* \param tsUsec Packet timestamp, microseconds
* \param header Header to write, in front of packet
* \param p Packet to write
*
*/
void Write (uint32_t tsSec, uint32_t tsUsec, const Header &header, Ptr<const Packet> p);
/**
* \brief Read next packet from file
*
* \param data [out] Data buffer
* \param maxBytes Allocated data buffer size
* \param tsSec [out] Packet timestamp, seconds
* \param tsUsec [out] Packet timestamp, microseconds
* \param inclLen [out] Included length
* \param origLen [out] Original length
* \param readLen [out] Number of bytes read
*
*/
void Read (uint8_t * const data,
uint32_t maxBytes,
uint32_t &tsSec,
uint32_t &tsUsec,
uint32_t &inclLen,
uint32_t &origLen,
uint32_t &readLen);
/**
* \brief Get the swap mode of the file.
*
* Pcap files use a magic number that is overloaded to identify both the
* format of the file itself and the byte ordering of the file. The magic
* number (and all data) is written into the file according to the native
* byte ordering of the writing system. If a reading application reads
* the magic number identically (for example 0xa1b2c3d4) then no byte
* swapping is required to correctly interpret the file data. If the reading
* application sees the magic number is byte swapped (for example 0xd4c3b2a1)
* then it knows that it needs to byteswap appropriate fields in the format.
*
* GetSWapMode returns a value indicating whether or not the fields are being
* byteswapped. Used primarily for testing the class itself, but may be
* useful as a flag indicating a difference in endianness of the writing
* system.
*
* \returns swap mode of the file
*/
bool GetSwapMode (void);
/**
* \brief Get the nanosecond mode of the file.
*
* \returns true if the packet timestamps in the PCAP
* file have nanosecond resolution.
*/
bool IsNanoSecMode (void);
/**
* \brief Returns the magic number of the pcap file as defined by the magic_number
* field in the pcap global header.
*
* See http://wiki.wireshark.org/Development/LibpcapFileFormat
*
* \returns magic number
*/
uint32_t GetMagic (void);
/**
* \brief Returns the major version of the pcap file as defined by the version_major
* field in the pcap global header.
*
* See http://wiki.wireshark.org/Development/LibpcapFileFormat
*
* \returns major version
*/
uint16_t GetVersionMajor (void);
/**
* \brief Returns the minor version of the pcap file as defined by the version_minor
* field in the pcap global header.
*
* See http://wiki.wireshark.org/Development/LibpcapFileFormat
*
* \returns minor version
*/
uint16_t GetVersionMinor (void);
/**
* \brief Returns the time zone offset of the pcap file as defined by the thiszone
* field in the pcap global header.
*
* See http://wiki.wireshark.org/Development/LibpcapFileFormat
*
* \returns time zone offset
*/
int32_t GetTimeZoneOffset (void);
/**
* \brief Returns the accuracy of timestamps field of the pcap file as defined
* by the sigfigs field in the pcap global header.
*
* See http://wiki.wireshark.org/Development/LibpcapFileFormat
*
* \returns accuracy of timestamps
*/
uint32_t GetSigFigs (void);
/**
* \brief Returns the max length of saved packets field of the pcap file as
* defined by the snaplen field in the pcap global header.
*
* See http://wiki.wireshark.org/Development/LibpcapFileFormat
*
* \returns max length of saved packets field
*/
uint32_t GetSnapLen (void);
/**
* \brief Returns the data link type field of the pcap file as defined by the
* network field in the pcap global header.
*
* See http://wiki.wireshark.org/Development/LibpcapFileFormat
*
* \returns data link type field
*/
uint32_t GetDataLinkType (void);
/**
* \brief Compare two PCAP files packet-by-packet
*
* \return true if files are different, false otherwise
*
* \param f1 First PCAP file name
* \param f2 Second PCAP file name
* \param sec [out] Time stamp of first different packet, seconds. Undefined if files doesn't differ.
* \param usec [out] Time stamp of first different packet, microseconds. Undefined if files doesn't differ.
* \param packets [out] Number of first different packet. Total number of parsed packets if files doesn't differ.
* \param snapLen Snap length (if used)
*/
static bool Diff (std::string const & f1, std::string const & f2,
uint32_t & sec, uint32_t & usec, uint32_t & packets,
uint32_t snapLen = SNAPLEN_DEFAULT);
private:
/**
* \brief Pcap file header
*/
typedef struct {
uint32_t m_magicNumber; /**< Magic number identifying this as a pcap file */
uint16_t m_versionMajor; /**< Major version identifying the version of pcap used in this file */
uint16_t m_versionMinor; /**< Minor version identifying the version of pcap used in this file */
int32_t m_zone; /**< Time zone correction to be applied to timestamps of packets */
uint32_t m_sigFigs; /**< Unused by pretty much everybody */
uint32_t m_snapLen; /**< Maximum length of packet data stored in records */
uint32_t m_type; /**< Data link type of packet data */
} PcapFileHeader;
/**
* \brief Pcap record header
*/
typedef struct {
uint32_t m_tsSec; /**< seconds part of timestamp */
uint32_t m_tsUsec; /**< microseconds part of timestamp (nsecs for PCAP_NSEC_MAGIC) */
uint32_t m_inclLen; /**< number of octets of packet saved in file */
uint32_t m_origLen; /**< actual length of original packet */
} PcapRecordHeader;
/**
* \brief Swap a value byte order
* \param val the value
* \returns the value with byte order swapped
*/
uint8_t Swap (uint8_t val);
/**
* \brief Swap a value byte order
* \param val the value
* \returns the value with byte order swapped
*/
uint16_t Swap (uint16_t val);
/**
* \brief Swap a value byte order
* \param val the value
* \returns the value with byte order swapped
*/
uint32_t Swap (uint32_t val);
/**
* \brief Swap the byte order of a Pcap file header
* \param from original file header
* \param to swapped file header
*/
void Swap (PcapFileHeader *from, PcapFileHeader *to);
/**
* \brief Swap the byte order of a Pcap record header
* \param from original record header
* \param to swapped record header
*/
void Swap (PcapRecordHeader *from, PcapRecordHeader *to);
/**
* \brief Write a Pcap file header
*/
void WriteFileHeader (void);
/**
* \brief Write a Pcap packet header
*
* The pcap header has a fixed length of 24 bytes. The last 4 bytes
* represent the link-layer type
*
* \param tsSec Time stamp (seconds part)
* \param tsUsec Time stamp (microseconds part)
* \param totalLen total packet length
* \returns the length of the packet to write in the Pcap file
*/
uint32_t WritePacketHeader (uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen);
/**
* \brief Read and verify a Pcap file header
*/
void ReadAndVerifyFileHeader (void);
std::string m_filename; //!< file name
std::fstream m_file; //!< file stream
PcapFileHeader m_fileHeader; //!< file header
bool m_swapMode; //!< swap mode
bool m_nanosecMode; //!< nanosecond timestamp mode
};
} // namespace ns3
#endif /* PCAP_FILE_H */
|