src/common/pcap-file-object.h
changeset 6040 a30eb0e5758a
parent 6035 ecd8840c3573
child 6041 b65c6d6794f8
equal deleted inserted replaced
6039:c4006544d0b8 6040:a30eb0e5758a
    37   static TypeId GetTypeId (void);
    37   static TypeId GetTypeId (void);
    38 
    38 
    39   PcapFileObject ();
    39   PcapFileObject ();
    40   ~PcapFileObject ();
    40   ~PcapFileObject ();
    41 
    41 
       
    42   /**
       
    43    * Create a new pcap file object representing a new or existing pcap file.
       
    44    * Semantics are similar to the C standard library function \c fopen
       
    45    *
       
    46    * Possible modes are:
       
    47    *
       
    48    * \verbatim
       
    49    * "r":   Open a file for reading.  The file must exist.  The pcap header
       
    50    *        is assumed to exist in the file and will be read and checked.
       
    51    *        The file seek position indicator is set to point to the first 
       
    52    *        packet on exit.
       
    53    *
       
    54    * "w":   Create an empty file for writing. If a file with the same name 
       
    55    *        already exists its content is erased and the file is treated as a 
       
    56    *        new empty pcap file.  The file is assumed not to have a pcap 
       
    57    *        header and the caller is responsible for calling Init before saving
       
    58    *        any packet data.  The file seek position indicator is set to point 
       
    59    *        to the beginning of the file on exit since there will be no pcap
       
    60    *        header.
       
    61    *
       
    62    * "a":   Append to an existing file. This mode allows for adding packet data
       
    63    *        to the end of an existing pcap file.  The file must exist and have a
       
    64    *        valid pcap header written (N.B. this is different from standard fopen
       
    65    *        semantics).  The file seek position indicator is set to point 
       
    66    *        to the end of the file on exit.
       
    67    *
       
    68    * "r+":  Open a file for update -- both reading and writing. The file must 
       
    69    *        exist.  The pcap header is assumed to have been written to the 
       
    70    *        file and will be read and checked.  The file seek position indicator
       
    71    *        is set to point to the first packet on exit.
       
    72    *
       
    73    * "w+":  Create an empty file for both reading and writing.  If a file with
       
    74    *        the same name already exists, its content is erased and the file is 
       
    75    *        treated as a new empty pcap file.  Since this new file will not have
       
    76    *        a pcap header, the caller is responsible for calling Init before 
       
    77    *        saving any packet data.  On exit, the file seek position indicator is
       
    78    *        set to point to the beginning of the file.
       
    79    *
       
    80    * "a+"   Open a file for reading and appending.  The file must exist and have a
       
    81    *        valid pcap header written (N.B. this is different from standard fopen
       
    82    *        semantics).  The file seek position indicator is set to point 
       
    83    *        to the end of the file on exit.  Existing content is preserved.
       
    84    * \endverbatim
       
    85    *
       
    86    * Since a pcap file is always a binary file, the file type is automatically 
       
    87    * selected as a binary file.  For example, providing a mode string "a+" 
       
    88    * results in the underlying OS file being opened in "a+b" mode.
       
    89    *
       
    90    * \param filename String containing the name of the file.
       
    91    *
       
    92    * \param mode String containing the access mode for the file.
       
    93    *
       
    94    * \returns Error indication that should be interpreted as, "did an error 
       
    95    * happen"?  That is, the method returns false if the open succeeds, true 
       
    96    * otherwise.  The errno variable will be set by the OS to to provide a 
       
    97    * more descriptive failure indication.
       
    98    */
    42   bool Open (std::string const &filename, std::string const &mode);
    99   bool Open (std::string const &filename, std::string const &mode);
    43 
   100 
       
   101   /**
       
   102    * Close the underlying pcap file.
       
   103    */
    44   void Close (void);
   104   void Close (void);
    45 
   105 
       
   106   /**
       
   107    * Initialize the pcap file associated with this object.  This file must have
       
   108    * been previously opened with write permissions.
       
   109    *
       
   110    * \param dataLinkType A data link type as defined in the pcap library.  If
       
   111    * you want to make resulting pcap files visible in existing tools, the 
       
   112    * data link type must match existing definitions, such as PCAP_ETHERNET,
       
   113    * PCAP_PPP, PCAP_80211, etc.  If you are storing different kinds of packet
       
   114    * data, such as naked TCP headers, you are at liberty to locally define your
       
   115    * own data link types.  According to the pcap-linktype man page, "well-known"
       
   116    * pcap linktypes range from 0 to 177.  If you use a large random number for
       
   117    * your type, chances are small for a collision.
       
   118    *
       
   119    * \param snapLen An optional maximum size for packets written to the file.
       
   120    * Defaults to 65535.  If packets exceed this length they are truncated.
       
   121    *
       
   122    * \param tzCorrection An integer describing the offset of your local
       
   123    * time zone from UTC/GMT.  For example, Pacific Standard Time in the US is
       
   124    * GMT-8, so one would enter -8 for that correction.  Defaults to 0 (UTC).
       
   125    *
       
   126    * \return false if the open succeeds, true otherwise.
       
   127    *
       
   128    * \warning Calling this method on an existing file will result in the loss
       
   129    * any existing data.
       
   130    */
    46   bool Init (uint32_t dataLinkType, 
   131   bool Init (uint32_t dataLinkType, 
    47              uint32_t snapLen = PcapFile::SNAPLEN_DEFAULT, 
   132              uint32_t snapLen = PcapFile::SNAPLEN_DEFAULT, 
    48              int32_t tzCorrection = PcapFile::ZONE_DEFAULT);
   133              int32_t tzCorrection = PcapFile::ZONE_DEFAULT);
    49 
   134 
       
   135   /**
       
   136    * \brief Write the next packet to file
       
   137    * 
       
   138    * \param t Packet timestamp as ns3::Time.
       
   139    * \param p Packet to write to the pcap file.
       
   140    * 
       
   141    * \return true on error, false otherwise
       
   142    */
    50   bool Write (Time t, Ptr<const Packet> p);
   143   bool Write (Time t, Ptr<const Packet> p);
       
   144 
       
   145   /**
       
   146    * \brief Write the provided header along with the packet to the pcap file.
       
   147    *
       
   148    * It is the case that adding a header to a packet prior to writing it to a
       
   149    * file must trigger a deep copy in the Packet.  By providing the header
       
   150    * separately, we can avoid that copy.
       
   151    * 
       
   152    * \param t Packet timestamp as ns3::Time.
       
   153    * \param header The Header to prepend to the packet.
       
   154    * \param p Packet to write to the pcap file.
       
   155    * 
       
   156    * \return true on error, false otherwise
       
   157    */
    51   bool Write (Time t, Header &header, Ptr<const Packet> p);
   158   bool Write (Time t, Header &header, Ptr<const Packet> p);
       
   159 
       
   160   /**
       
   161    * \brief Write the provided data buffer to the pcap file.
       
   162    *
       
   163    * \param t Packet timestamp as ns3::Time.
       
   164    * \param buffer The buffer to write.
       
   165    * \param length The size of the buffer.
       
   166    * 
       
   167    * \return true on error, false otherwise
       
   168    */
    52   bool Write (Time t, uint8_t const *buffer, uint32_t length);
   169   bool Write (Time t, uint8_t const *buffer, uint32_t length);
    53 
   170 
       
   171   /*
       
   172    * \brief Returns the magic number of the pcap file as defined by the magic_number
       
   173    * field in the pcap global header.
       
   174    *
       
   175    * See http://wiki.wireshark.org/Development/LibpcapFileFormat
       
   176    */ 
    54   uint32_t GetMagic (void);
   177   uint32_t GetMagic (void);
       
   178 
       
   179   /*
       
   180    * \brief Returns the major version of the pcap file as defined by the version_major
       
   181    * field in the pcap global header.
       
   182    *
       
   183    * See http://wiki.wireshark.org/Development/LibpcapFileFormat
       
   184    */ 
    55   uint16_t GetVersionMajor (void);
   185   uint16_t GetVersionMajor (void);
       
   186 
       
   187   /*
       
   188    * \brief Returns the minor version of the pcap file as defined by the version_minor
       
   189    * field in the pcap global header.
       
   190    *
       
   191    * See http://wiki.wireshark.org/Development/LibpcapFileFormat
       
   192    */ 
    56   uint16_t GetVersionMinor (void);
   193   uint16_t GetVersionMinor (void);
       
   194 
       
   195   /*
       
   196    * \brief Returns the time zone offset of the pcap file as defined by the thiszone
       
   197    * field in the pcap global header.
       
   198    *
       
   199    * See http://wiki.wireshark.org/Development/LibpcapFileFormat
       
   200    */ 
    57   int32_t GetTimeZoneOffset (void);
   201   int32_t GetTimeZoneOffset (void);
       
   202 
       
   203   /*
       
   204    * \brief Returns the accuracy of timestamps field of the pcap file as defined
       
   205    * by the sigfigs field in the pcap global header.
       
   206    *
       
   207    * See http://wiki.wireshark.org/Development/LibpcapFileFormat
       
   208    */ 
    58   uint32_t GetSigFigs (void);
   209   uint32_t GetSigFigs (void);
       
   210 
       
   211   /*
       
   212    * \brief Returns the max length of saved packets field of the pcap file as 
       
   213    * defined by the snaplen field in the pcap global header.
       
   214    *
       
   215    * See http://wiki.wireshark.org/Development/LibpcapFileFormat
       
   216    */ 
    59   uint32_t GetSnapLen (void);
   217   uint32_t GetSnapLen (void);
       
   218 
       
   219   /*
       
   220    * \brief Returns the data link type field of the pcap file as defined by the 
       
   221    * network field in the pcap global header.
       
   222    *
       
   223    * See http://wiki.wireshark.org/Development/LibpcapFileFormat
       
   224    */ 
    60   uint32_t GetDataLinkType (void);
   225   uint32_t GetDataLinkType (void);
    61   
   226   
    62 private:
   227 private:
    63   PcapFile m_file;
   228   PcapFile m_file;
    64 };
   229 };