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/uinteger.h"
21 #include "ns3/buffer.h"
22 #include "ns3/header.h"
23 #include "pcap-file-wrapper.h"
24 
25 namespace ns3 {
26 
27 NS_LOG_COMPONENT_DEFINE ("PcapFileWrapper");
28 
29 NS_OBJECT_ENSURE_REGISTERED (PcapFileWrapper);
30 
31 TypeId
33 {
34  static TypeId tid = TypeId ("ns3::PcapFileWrapper")
35  .SetParent<Object> ()
36  .SetGroupName("Network")
37  .AddConstructor<PcapFileWrapper> ()
38  .AddAttribute ("CaptureSize",
39  "Maximum length of captured packets (cf. pcap snaplen)",
42  MakeUintegerChecker<uint32_t> (0, PcapFile::SNAPLEN_DEFAULT))
43  ;
44  return tid;
45 }
46 
47 
49 {
50  NS_LOG_FUNCTION (this);
51 }
52 
54 {
55  NS_LOG_FUNCTION (this);
56  Close ();
57 }
58 
59 
60 bool
62 {
63  NS_LOG_FUNCTION (this);
64  return m_file.Fail ();
65 }
66 bool
68 {
69  NS_LOG_FUNCTION (this);
70  return m_file.Eof ();
71 }
72 void
74 {
75  NS_LOG_FUNCTION (this);
76  m_file.Clear ();
77 }
78 
79 void
81 {
82  NS_LOG_FUNCTION (this);
83  m_file.Close ();
84 }
85 
86 void
87 PcapFileWrapper::Open (std::string const &filename, std::ios::openmode mode)
88 {
89  NS_LOG_FUNCTION (this << filename << mode);
90  m_file.Open (filename, mode);
91 }
92 
93 void
94 PcapFileWrapper::Init (uint32_t dataLinkType, uint32_t snapLen, int32_t tzCorrection)
95 {
96  //
97  // If the user doesn't provide a snaplen, the default value will come in. If
98  // this happens, we use the "CaptureSize" Attribute. If the user does provide
99  // a snaplen, we use the one provided.
100  //
101  NS_LOG_FUNCTION (this << dataLinkType << snapLen << tzCorrection);
102  if (snapLen != std::numeric_limits<uint32_t>::max ())
103  {
104  m_file.Init (dataLinkType, snapLen, tzCorrection);
105  }
106  else
107  {
108  m_file.Init (dataLinkType, m_snapLen, tzCorrection);
109  }
110 }
111 
112 void
114 {
115  NS_LOG_FUNCTION (this << t << p);
116  uint64_t current = t.GetMicroSeconds ();
117  uint64_t s = current / 1000000;
118  uint64_t us = current % 1000000;
119 
120  m_file.Write (s, us, p);
121 }
122 
123 void
125 {
126  NS_LOG_FUNCTION (this << t << &header << p);
127  uint64_t current = t.GetMicroSeconds ();
128  uint64_t s = current / 1000000;
129  uint64_t us = current % 1000000;
130 
131  m_file.Write (s, us, header, p);
132 }
133 
134 void
135 PcapFileWrapper::Write (Time t, uint8_t const *buffer, uint32_t length)
136 {
137  NS_LOG_FUNCTION (this << t << &buffer << length);
138  uint64_t current = t.GetMicroSeconds ();
139  uint64_t s = current / 1000000;
140  uint64_t us = current % 1000000;
141 
142  m_file.Write (s, us, buffer, length);
143 }
144 
145 uint32_t
147 {
148  NS_LOG_FUNCTION (this);
149  return m_file.GetMagic ();
150 }
151 
152 uint16_t
154 {
155  NS_LOG_FUNCTION (this);
156  return m_file.GetVersionMajor ();
157 }
158 
159 uint16_t
161 {
162  NS_LOG_FUNCTION (this);
163  return m_file.GetVersionMinor ();
164 }
165 
166 int32_t
168 {
169  NS_LOG_FUNCTION (this);
170  return m_file.GetTimeZoneOffset ();
171 }
172 
173 uint32_t
175 {
176  NS_LOG_FUNCTION (this);
177  return m_file.GetSigFigs ();
178 }
179 
180 uint32_t
182 {
183  NS_LOG_FUNCTION (this);
184  return m_file.GetSnapLen ();
185 }
186 
187 uint32_t
189 {
190  NS_LOG_FUNCTION (this);
191  return m_file.GetDataLinkType ();
192 }
193 
194 } // namespace ns3
Protocol header serialization and deserialization.
Definition: header.h:42
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
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:107
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.
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:201
bool Eof(void) const
Definition: pcap-file.cc:72
uint32_t GetDataLinkType(void)
Returns the data link type field of the pcap file as defined by the network field in the pcap global ...
int64_t GetMicroSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:342
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.
bool Eof(void) const
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:93
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void Clear(void)
Clear all state bits of the underlying iostream.
Definition: pcap-file.cc:78
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...
void Init(uint32_t dataLinkType, uint32_t snapLen=SNAPLEN_DEFAULT, int32_t timeZoneCorrection=ZONE_DEFAULT, bool swapMode=false)
Initialize the pcap file associated with this object.
Definition: pcap-file.cc:329
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:121
void Close(void)
Close the underlying file.
Definition: pcap-file.cc:86
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:100
uint32_t GetSigFigs(void)
Returns the accuracy of timestamps field of the pcap file as defined by the sigfigs field in the pcap...
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:310
uint32_t m_snapLen
max length of saved packets
bool Fail(void) const
Definition: pcap-file.cc:66
bool Fail(void) const
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:135
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:404
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:57
TypeId SetParent(TypeId tid)
Definition: type-id.cc:638
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:128
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:114