src/common/pcap-file-object.cc
changeset 6035 ecd8840c3573
parent 6009 e1b696a1ed28
child 6041 b65c6d6794f8
equal deleted inserted replaced
6034:de2824cc388d 6035:ecd8840c3573
    14  * You should have received a copy of the GNU General Public License
    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
    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
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    17  */
    17  */
    18 
    18 
       
    19 #include "ns3/log.h"
       
    20 
       
    21 #include "buffer.h"
       
    22 #include "header.h"
    19 #include "pcap-file-object.h"
    23 #include "pcap-file-object.h"
    20 #include "ns3/log.h"
       
    21 
    24 
    22 NS_LOG_COMPONENT_DEFINE ("PcapFileObject");
    25 NS_LOG_COMPONENT_DEFINE ("PcapFileObject");
    23 
    26 
    24 namespace ns3 {
    27 namespace ns3 {
    25 
    28 
    46 }
    49 }
    47 
    50 
    48 void
    51 void
    49 PcapFileObject::Close (void)
    52 PcapFileObject::Close (void)
    50 {
    53 {
    51   file.Close ();
    54   m_file.Close ();
    52 }
    55 }
    53 
    56 
    54 bool
    57 bool
    55 PcapFileObject::Open (std::string const &filename, std::string const &mode)
    58 PcapFileObject::Open (std::string const &filename, std::string const &mode)
    56 {
    59 {
    57   return file.Open (filename, mode);
    60   return m_file.Open (filename, mode);
    58 }
    61 }
    59 
    62 
    60 bool
    63 bool
    61 PcapFileObject::Init (uint32_t dataLinkType, uint32_t snapLen, int32_t tzCorrection)
    64 PcapFileObject::Init (uint32_t dataLinkType, uint32_t snapLen, int32_t tzCorrection)
    62 {
    65 {
    63   return file.Init (dataLinkType, snapLen, tzCorrection);
    66   return m_file.Init (dataLinkType, snapLen, tzCorrection);
    64 }
    67 }
    65 
    68 
    66 bool
    69 bool
    67 PcapFileObject::Write (Time t, Ptr<const Packet> p)
    70 PcapFileObject::Write (Time t, Ptr<const Packet> p)
    68 {
    71 {
    71   uint64_t us = current % 1000000;
    74   uint64_t us = current % 1000000;
    72 
    75 
    73   uint32_t bufferSize = p->GetSize ();
    76   uint32_t bufferSize = p->GetSize ();
    74   uint8_t *buffer = new uint8_t[bufferSize];
    77   uint8_t *buffer = new uint8_t[bufferSize];
    75   p->CopyData (buffer, bufferSize);
    78   p->CopyData (buffer, bufferSize);
    76   bool rc = file.Write (s, us, buffer, bufferSize);
    79   bool rc = m_file.Write (s, us, buffer, bufferSize);
       
    80   delete [] buffer;
       
    81   return rc;
       
    82 }
       
    83 
       
    84 bool
       
    85 PcapFileObject::Write (Time t, Header &header, Ptr<const Packet> p)
       
    86 {
       
    87   uint64_t current = t.GetMicroSeconds ();
       
    88   uint64_t s = current / 1000000;
       
    89   uint64_t us = current % 1000000;
       
    90 
       
    91   Buffer headerBuffer;
       
    92   uint32_t headerSize = header.GetSerializedSize ();
       
    93   uint32_t packetSize = p->GetSize ();
       
    94   uint32_t bufferSize = headerSize + packetSize;
       
    95 
       
    96   headerBuffer.AddAtStart (headerSize);
       
    97   header.Serialize (headerBuffer.Begin ());
       
    98 
       
    99   uint8_t *buffer = new uint8_t[bufferSize];
       
   100 
       
   101   headerBuffer.Begin ().Read (buffer, headerSize);
       
   102   p->CopyData (&buffer[headerSize], packetSize);
       
   103   bool rc = m_file.Write (s, us, buffer, bufferSize);
       
   104 
    77   delete [] buffer;
   105   delete [] buffer;
    78   return rc;
   106   return rc;
    79 }
   107 }
    80 
   108 
    81 bool
   109 bool
    83 {
   111 {
    84   uint64_t current = t.GetMicroSeconds ();
   112   uint64_t current = t.GetMicroSeconds ();
    85   uint64_t s = current / 1000000;
   113   uint64_t s = current / 1000000;
    86   uint64_t us = current % 1000000;
   114   uint64_t us = current % 1000000;
    87 
   115 
    88   return file.Write (s, us, buffer, length);
   116   return m_file.Write (s, us, buffer, length);
    89 }
   117 }
    90 
   118 
    91 uint32_t
   119 uint32_t
    92 PcapFileObject::GetMagic (void)
   120 PcapFileObject::GetMagic (void)
    93 {
   121 {
    94   return file.GetMagic ();
   122   return m_file.GetMagic ();
    95 }
   123 }
    96 
   124 
    97 uint16_t
   125 uint16_t
    98 PcapFileObject::GetVersionMajor (void)
   126 PcapFileObject::GetVersionMajor (void)
    99 {
   127 {
   100   return file.GetVersionMajor ();
   128   return m_file.GetVersionMajor ();
   101 }
   129 }
   102 
   130 
   103 uint16_t
   131 uint16_t
   104 PcapFileObject::GetVersionMinor (void)
   132 PcapFileObject::GetVersionMinor (void)
   105 {
   133 {
   106   return file.GetVersionMinor ();
   134   return m_file.GetVersionMinor ();
   107 }
   135 }
   108 
   136 
   109 int32_t
   137 int32_t
   110 PcapFileObject::GetTimeZoneOffset (void)
   138 PcapFileObject::GetTimeZoneOffset (void)
   111 {
   139 {
   112   return file.GetTimeZoneOffset ();
   140   return m_file.GetTimeZoneOffset ();
   113 }
   141 }
   114 
   142 
   115 uint32_t
   143 uint32_t
   116 PcapFileObject::GetSigFigs (void)
   144 PcapFileObject::GetSigFigs (void)
   117 {
   145 {
   118   return file.GetSigFigs ();
   146   return m_file.GetSigFigs ();
   119 }
   147 }
   120 
   148 
   121 uint32_t
   149 uint32_t
   122 PcapFileObject::GetSnapLen (void)
   150 PcapFileObject::GetSnapLen (void)
   123 {
   151 {
   124   return file.GetSnapLen ();
   152   return m_file.GetSnapLen ();
   125 }
   153 }
   126 
   154 
   127 uint32_t
   155 uint32_t
   128 PcapFileObject::GetDataLinkType (void)
   156 PcapFileObject::GetDataLinkType (void)
   129 {
   157 {
   130   return file.GetDataLinkType ();
   158   return m_file.GetDataLinkType ();
   131 }
   159 }
   132 
   160 
   133 } //namespace ns3
   161 } //namespace ns3