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
26namespace ns3 {
27
28NS_LOG_COMPONENT_DEFINE ("PcapFileWrapper");
29
30NS_OBJECT_ENSURE_REGISTERED (PcapFileWrapper);
31
32TypeId
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
65bool
67{
68 NS_LOG_FUNCTION (this);
69 return m_file.Fail ();
70}
71
72bool
74{
75 NS_LOG_FUNCTION (this);
76 return m_file.Eof ();
77}
78void
80{
81 NS_LOG_FUNCTION (this);
82 m_file.Clear ();
83}
84
85void
87{
88 NS_LOG_FUNCTION (this);
89 m_file.Close ();
90}
91
92void
93PcapFileWrapper::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
99void
100PcapFileWrapper::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
118void
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
138void
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
158void
159PcapFileWrapper::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 uint8_t datbuf[65536];
188
189 m_file.Read (datbuf,65536,tsSec,tsUsec,inclLen,origLen,readLen);
190
191 if (m_file.Fail())
192 {
193 return 0;
194 }
195
196 if (m_file.IsNanoSecMode())
197 {
198 t = NanoSeconds(tsSec*1000000000ULL+tsUsec);
199 }
200 else
201 {
202 t = MicroSeconds(tsSec*1000000ULL+tsUsec);
203 }
204
205 return Create<Packet> (datbuf,origLen);
206
207}
208
211{
212 NS_LOG_FUNCTION (this);
213 return m_file.GetMagic ();
214}
215
216uint16_t
218{
219 NS_LOG_FUNCTION (this);
220 return m_file.GetVersionMajor ();
221}
222
223uint16_t
225{
226 NS_LOG_FUNCTION (this);
227 return m_file.GetVersionMinor ();
228}
229
232{
233 NS_LOG_FUNCTION (this);
234 return m_file.GetTimeZoneOffset ();
235}
236
239{
240 NS_LOG_FUNCTION (this);
241 return m_file.GetSigFigs ();
242}
243
246{
247 NS_LOG_FUNCTION (this);
248 return m_file.GetSnapLen ();
249}
250
253{
254 NS_LOG_FUNCTION (this);
255 return m_file.GetDataLinkType ();
256}
257
258} // namespace ns3
#define max(a, b)
Definition: 80211b.c:43
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Protocol header serialization and deserialization.
Definition: header.h:43
A base class which provides memory management and object aggregation.
Definition: object.h:88
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
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
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
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
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
void Clear(void)
Clear all state bits of the underlying iostream.
Definition: pcap-file.cc:80
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 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
bool Fail(void) const
Definition: pcap-file.cc:68
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
bool IsNanoSecMode(void)
Get the nanosecond mode of the file.
Definition: pcap-file.cc:151
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
static const uint32_t SNAPLEN_DEFAULT
Default value for maximum octets to save per packet.
Definition: pcap-file.h:46
bool Eof(void) const
Definition: pcap-file.cc:74
void Close(void)
Close the underlying file.
Definition: pcap-file.cc:88
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
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
A class that wraps a PcapFile as an ns3::Object and provides a higher-layer ns-3 interface to the low...
Ptr< Packet > Read(Time &t)
Read the next packet from the file.
void Write(Time t, Ptr< const Packet > p)
Write the next packet to file.
PcapFile m_file
Pcap file.
uint32_t GetDataLinkType(void)
Returns the data link type field of the pcap file as defined by the network field in the pcap global ...
void Clear(void)
Clear all state bits of the underlying iostream.
void Open(std::string const &filename, std::ios::openmode mode)
Create a new pcap file or open an existing pcap file.
uint32_t GetSnapLen(void)
Returns the max length of saved packets field of the pcap file as defined by the snaplen field in the...
void Close(void)
Close the underlying pcap file.
uint32_t m_snapLen
max length of saved packets
bool Fail(void) const
uint32_t GetSigFigs(void)
Returns the accuracy of timestamps field of the pcap file as defined by the sigfigs field in the pcap...
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.
static TypeId GetTypeId(void)
Get the type ID.
uint16_t GetVersionMajor(void)
Returns the major version of the pcap file as defined by the version_major field in the pcap global h...
bool Eof(void) const
uint16_t GetVersionMinor(void)
Returns the minor version of the pcap file as defined by the version_minor field in the pcap global h...
uint32_t GetMagic(void)
Returns the magic number of the pcap file as defined by the magic_number field in the pcap global hea...
int32_t GetTimeZoneOffset(void)
Returns the time zone offset of the pcap file as defined by the thiszone field in the pcap global hea...
bool m_nanosecMode
Timestamps in nanosecond mode.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
int64_t GetMicroSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:387
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:391
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
Hold an unsigned integer type.
Definition: uinteger.h:44
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:85
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:45
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1260
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1268
Every class exported by the ns3 library is enclosed in the ns3 namespace.