A Discrete-Event Network Simulator
API
pcap-file-wrapper.cc
Go to the documentation of this file.
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 #include "ns3/log.h"
20 #include "ns3/boolean.h"
21 #include "ns3/uinteger.h"
22 #include "ns3/buffer.h"
23 #include "ns3/header.h"
24 #include "pcap-file-wrapper.h"
25 
26 namespace ns3 {
27 
28 NS_LOG_COMPONENT_DEFINE ("PcapFileWrapper");
29 
30 NS_OBJECT_ENSURE_REGISTERED (PcapFileWrapper);
31 
32 TypeId
34 {
35  static TypeId tid = TypeId ("ns3::PcapFileWrapper")
36  .SetParent<Object> ()
37  .SetGroupName("Network")
38  .AddConstructor<PcapFileWrapper> ()
39  .AddAttribute ("CaptureSize",
40  "Maximum length of captured packets (cf. pcap snaplen)",
43  MakeUintegerChecker<uint32_t> (0, PcapFile::SNAPLEN_DEFAULT))
44  .AddAttribute ("NanosecMode",
45  "Whether packet timestamps in the PCAP file are nanoseconds or microseconds(default).",
46  BooleanValue (false),
49  ;
50  return tid;
51 }
52 
53 
55 {
56  NS_LOG_FUNCTION (this);
57 }
58 
60 {
61  NS_LOG_FUNCTION (this);
62  Close ();
63 }
64 
65 bool
67 {
68  NS_LOG_FUNCTION (this);
69  return m_file.Fail ();
70 }
71 
72 bool
74 {
75  NS_LOG_FUNCTION (this);
76  return m_file.Eof ();
77 }
78 void
80 {
81  NS_LOG_FUNCTION (this);
82  m_file.Clear ();
83 }
84 
85 void
87 {
88  NS_LOG_FUNCTION (this);
89  m_file.Close ();
90 }
91 
92 void
93 PcapFileWrapper::Open (std::string const &filename, std::ios::openmode mode)
94 {
95  NS_LOG_FUNCTION (this << filename << mode);
96  m_file.Open (filename, mode);
97 }
98 
99 void
100 PcapFileWrapper::Init (uint32_t dataLinkType, uint32_t snapLen, int32_t tzCorrection)
101 {
102  //
103  // If the user doesn't provide a snaplen, the default value will come in. If
104  // this happens, we use the "CaptureSize" Attribute. If the user does provide
105  // a snaplen, we use the one provided.
106  //
107  NS_LOG_FUNCTION (this << dataLinkType << snapLen << tzCorrection);
108  if (snapLen != std::numeric_limits<uint32_t>::max ())
109  {
110  m_file.Init (dataLinkType, snapLen, tzCorrection, false, m_nanosecMode);
111  }
112  else
113  {
114  m_file.Init (dataLinkType, m_snapLen, tzCorrection, false, m_nanosecMode);
115  }
116 }
117 
118 void
120 {
121  NS_LOG_FUNCTION (this << t << p);
122  if (m_file.IsNanoSecMode())
123  {
124  uint64_t current = t.GetNanoSeconds ();
125  uint64_t s = current / 1000000000;
126  uint64_t ns = current % 1000000000;
127  m_file.Write (s, ns, p);
128  }
129  else
130  {
131  uint64_t current = t.GetMicroSeconds ();
132  uint64_t s = current / 1000000;
133  uint64_t us = current % 1000000;
134  m_file.Write (s, us, p);
135  }
136 }
137 
138 void
140 {
141  NS_LOG_FUNCTION (this << t << &header << p);
142  if (m_file.IsNanoSecMode())
143  {
144  uint64_t current = t.GetNanoSeconds ();
145  uint64_t s = current / 1000000000;
146  uint64_t ns = current % 1000000000;
147  m_file.Write (s, ns, header, p);
148  }
149  else
150  {
151  uint64_t current = t.GetMicroSeconds ();
152  uint64_t s = current / 1000000;
153  uint64_t us = current % 1000000;
154  m_file.Write (s, us, header, p);
155  }
156 }
157 
158 void
159 PcapFileWrapper::Write (Time t, uint8_t const *buffer, uint32_t length)
160 {
161  NS_LOG_FUNCTION (this << t << &buffer << length);
162  if (m_file.IsNanoSecMode())
163  {
164  uint64_t current = t.GetNanoSeconds ();
165  uint64_t s = current / 1000000000;
166  uint64_t ns = current % 1000000000;
167  m_file.Write (s, ns, buffer, length);
168  }
169  else
170  {
171  uint64_t current = t.GetMicroSeconds ();
172  uint64_t s = current / 1000000;
173  uint64_t us = current % 1000000;
174  m_file.Write (s, us, buffer, length);
175  }
176 }
177 
180 {
181  uint32_t tsSec;
182  uint32_t tsUsec;
183  uint32_t inclLen;
184  uint32_t origLen;
185  uint32_t readLen;
186 
187  uint32_t maxBytes=65536;
188  uint8_t datbuf[maxBytes];
189 
190  m_file.Read (datbuf,maxBytes,tsSec,tsUsec,inclLen,origLen,readLen);
191 
192  if (m_file.Fail())
193  {
194  return 0;
195  }
196 
197  if (m_file.IsNanoSecMode())
198  {
199  t = NanoSeconds(tsSec*1000000000ULL+tsUsec);
200  }
201  else
202  {
203  t = MicroSeconds(tsSec*1000000ULL+tsUsec);
204  }
205 
206  return Create<Packet> (datbuf,origLen);
207 
208 }
209 
210 uint32_t
212 {
213  NS_LOG_FUNCTION (this);
214  return m_file.GetMagic ();
215 }
216 
217 uint16_t
219 {
220  NS_LOG_FUNCTION (this);
221  return m_file.GetVersionMajor ();
222 }
223 
224 uint16_t
226 {
227  NS_LOG_FUNCTION (this);
228  return m_file.GetVersionMinor ();
229 }
230 
231 int32_t
233 {
234  NS_LOG_FUNCTION (this);
235  return m_file.GetTimeZoneOffset ();
236 }
237 
238 uint32_t
240 {
241  NS_LOG_FUNCTION (this);
242  return m_file.GetSigFigs ();
243 }
244 
245 uint32_t
247 {
248  NS_LOG_FUNCTION (this);
249  return m_file.GetSnapLen ();
250 }
251 
252 uint32_t
254 {
255  NS_LOG_FUNCTION (this);
256  return m_file.GetDataLinkType ();
257 }
258 
259 } // namespace ns3
Protocol header serialization and deserialization.
Definition: header.h:42
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
AttributeValue implementation for Boolean.
Definition: boolean.h:36
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
uint32_t GetMagic(void)
Returns the magic number of the pcap file as defined by the magic_number field in the pcap global hea...
uint16_t GetVersionMinor(void)
Returns the minor version of the pcap file as defined by the version_minor field in the pcap global h...
Definition: pcap-file.cc:109
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: boolean.h:85
void Write(Time t, Ptr< const Packet > p)
Write the next packet to file.
void Init(uint32_t dataLinkType, uint32_t snapLen=std::numeric_limits< uint32_t >::max(), int32_t tzCorrection=PcapFile::ZONE_DEFAULT)
Initialize the pcap file associated with this wrapper.
void Init(uint32_t dataLinkType, uint32_t snapLen=SNAPLEN_DEFAULT, int32_t timeZoneCorrection=ZONE_DEFAULT, bool swapMode=false, bool nanosecMode=false)
Initialize the pcap file associated with this object.
Definition: pcap-file.cc:345
uint16_t GetVersionMajor(void)
Returns the major version of the pcap file as defined by the version_major field in the pcap global h...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
uint32_t GetDataLinkType(void)
Returns the data link type field of the pcap file as defined by the network field in the pcap global ...
#define max(a, b)
Definition: 80211b.c:43
bool Fail(void) const
Definition: pcap-file.cc:68
bool Eof(void) const
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1302
void Read(uint8_t *const data, uint32_t maxBytes, uint32_t &tsSec, uint32_t &tsUsec, uint32_t &inclLen, uint32_t &origLen, uint32_t &readLen)
Read next packet from file.
Definition: pcap-file.cc:469
Hold an unsigned integer type.
Definition: uinteger.h:44
static TypeId GetTypeId(void)
Get the type ID.
int32_t GetTimeZoneOffset(void)
Returns the time zone offset of the pcap file as defined by the thiszone field in the pcap global hea...
void Clear(void)
Clear all state bits of the underlying iostream.
Ptr< Packet > Read(Time &t)
Read the next packet from the file.
int64_t GetMicroSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:387
void Open(std::string const &filename, std::ios::openmode mode)
Create a new pcap file or open an existing pcap file.
uint32_t GetMagic(void)
Returns the magic number of the pcap file as defined by the magic_number field in the pcap global hea...
Definition: pcap-file.cc:95
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:391
void Clear(void)
Clear all state bits of the underlying iostream.
Definition: pcap-file.cc:80
static const uint32_t SNAPLEN_DEFAULT
Default value for maximum octets to save per packet.
Definition: pcap-file.h:46
uint16_t GetVersionMinor(void)
Returns the minor version of the pcap file as defined by the version_minor field in the pcap global h...
bool Eof(void) const
Definition: pcap-file.cc:74
uint32_t GetSigFigs(void)
Returns the accuracy of timestamps field of the pcap file as defined by the sigfigs field in the pcap...
Definition: pcap-file.cc:123
void Close(void)
Close the underlying file.
Definition: pcap-file.cc:88
uint16_t GetVersionMajor(void)
Returns the major version of the pcap file as defined by the version_major field in the pcap global h...
Definition: pcap-file.cc:102
uint32_t GetSigFigs(void)
Returns the accuracy of timestamps field of the pcap file as defined by the sigfigs field in the pcap...
bool m_nanosecMode
Timestamps in nanosecond mode.
PcapFile m_file
Pcap file.
void Open(std::string const &filename, std::ios::openmode mode)
Create a new pcap file or open an existing pcap file.
Definition: pcap-file.cc:325
uint32_t m_snapLen
max length of saved packets
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1294
uint32_t GetDataLinkType(void)
Returns the data link type field of the pcap file as defined by the network field in the pcap global ...
Definition: pcap-file.cc:137
A base class which provides memory management and object aggregation.
Definition: object.h:87
void Close(void)
Close the underlying pcap file.
void Write(uint32_t tsSec, uint32_t tsUsec, uint8_t const *const data, uint32_t totalLen)
Write next packet to file.
Definition: pcap-file.cc:434
bool Fail(void) const
A class that wraps a PcapFile as an ns3::Object and provides a higher-layer ns-3 interface to the low...
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
a unique identifier for an interface.
Definition: type-id.h:58
bool IsNanoSecMode(void)
Get the nanosecond mode of the file.
Definition: pcap-file.cc:151
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
uint32_t GetSnapLen(void)
Returns the max length of saved packets field of the pcap file as defined by the snaplen field in the...
Definition: pcap-file.cc:130
uint32_t GetSnapLen(void)
Returns the max length of saved packets field of the pcap file as defined by the snaplen field in the...
int32_t GetTimeZoneOffset(void)
Returns the time zone offset of the pcap file as defined by the thiszone field in the pcap global hea...
Definition: pcap-file.cc:116