A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
pcap-file-wrapper.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7#include "pcap-file-wrapper.h"
8
9#include "ns3/boolean.h"
10#include "ns3/buffer.h"
11#include "ns3/header.h"
12#include "ns3/log.h"
13#include "ns3/uinteger.h"
14
15namespace ns3
16{
17
18NS_LOG_COMPONENT_DEFINE("PcapFileWrapper");
19
20NS_OBJECT_ENSURE_REGISTERED(PcapFileWrapper);
21
22TypeId
24{
25 static TypeId tid =
26 TypeId("ns3::PcapFileWrapper")
28 .SetGroupName("Network")
29 .AddConstructor<PcapFileWrapper>()
30 .AddAttribute("CaptureSize",
31 "Maximum length of captured packets (cf. pcap snaplen)",
35 .AddAttribute("NanosecMode",
36 "Whether packet timestamps in the PCAP file are nanoseconds or "
37 "microseconds(default).",
38 BooleanValue(false),
41 return tid;
42}
43
48
54
55bool
57{
58 NS_LOG_FUNCTION(this);
59 return m_file.Fail();
60}
61
62bool
64{
65 NS_LOG_FUNCTION(this);
66 return m_file.Eof();
67}
68
69void
75
76void
82
83void
84PcapFileWrapper::Open(const std::string& filename, std::ios::openmode mode)
85{
86 NS_LOG_FUNCTION(this << filename << mode);
87 m_file.Open(filename, mode);
88}
89
90void
91PcapFileWrapper::Init(uint32_t dataLinkType, uint32_t snapLen, int32_t tzCorrection)
92{
93 //
94 // If the user doesn't provide a snaplen, the default value will come in. If
95 // this happens, we use the "CaptureSize" Attribute. If the user does provide
96 // a snaplen, we use the one provided.
97 //
98 NS_LOG_FUNCTION(this << dataLinkType << snapLen << tzCorrection);
99 if (snapLen != std::numeric_limits<uint32_t>::max())
100 {
101 m_file.Init(dataLinkType, snapLen, tzCorrection, false, m_nanosecMode);
102 }
103 else
104 {
105 m_file.Init(dataLinkType, m_snapLen, tzCorrection, false, m_nanosecMode);
106 }
107}
108
109void
111{
112 NS_LOG_FUNCTION(this << t << p);
113 if (m_file.IsNanoSecMode())
114 {
115 uint64_t current = t.GetNanoSeconds();
116 uint64_t s = current / 1000000000;
117 uint64_t ns = current % 1000000000;
118 m_file.Write(s, ns, p);
119 }
120 else
121 {
122 uint64_t current = t.GetMicroSeconds();
123 uint64_t s = current / 1000000;
124 uint64_t us = current % 1000000;
125 m_file.Write(s, us, p);
126 }
127}
128
129void
131{
132 NS_LOG_FUNCTION(this << t << &header << p);
133 if (m_file.IsNanoSecMode())
134 {
135 uint64_t current = t.GetNanoSeconds();
136 uint64_t s = current / 1000000000;
137 uint64_t ns = current % 1000000000;
138 m_file.Write(s, ns, header, p);
139 }
140 else
141 {
142 uint64_t current = t.GetMicroSeconds();
143 uint64_t s = current / 1000000;
144 uint64_t us = current % 1000000;
145 m_file.Write(s, us, header, p);
146 }
147}
148
149void
150PcapFileWrapper::Write(Time t, const uint8_t* buffer, uint32_t length)
151{
152 NS_LOG_FUNCTION(this << t << &buffer << length);
153 if (m_file.IsNanoSecMode())
154 {
155 uint64_t current = t.GetNanoSeconds();
156 uint64_t s = current / 1000000000;
157 uint64_t ns = current % 1000000000;
158 m_file.Write(s, ns, buffer, length);
159 }
160 else
161 {
162 uint64_t current = t.GetMicroSeconds();
163 uint64_t s = current / 1000000;
164 uint64_t us = current % 1000000;
165 m_file.Write(s, us, buffer, length);
166 }
167}
168
171{
172 uint32_t tsSec;
173 uint32_t tsUsec;
174 uint32_t inclLen;
175 uint32_t origLen;
176 uint32_t readLen;
177
178 uint8_t datbuf[65536];
179
180 m_file.Read(datbuf, 65536, tsSec, tsUsec, inclLen, origLen, readLen);
181
182 if (m_file.Fail())
183 {
184 return nullptr;
185 }
186
187 if (m_file.IsNanoSecMode())
188 {
189 t = NanoSeconds(tsSec * 1000000000ULL + tsUsec);
190 }
191 else
192 {
193 t = MicroSeconds(tsSec * 1000000ULL + tsUsec);
194 }
195
196 return Create<Packet>(datbuf, origLen);
197}
198
201{
202 NS_LOG_FUNCTION(this);
203 return m_file.GetMagic();
204}
205
206uint16_t
212
213uint16_t
219
226
233
240
247
248} // namespace ns3
Protocol header serialization and deserialization.
Definition header.h:33
A base class which provides memory management and object aggregation.
Definition object.h:78
bool IsNanoSecMode()
Get the nanosecond mode of the file.
Definition pcap-file.cc:144
void Close()
Close the underlying file.
Definition pcap-file.cc:81
void Open(const std::string &filename, std::ios::openmode mode)
Create a new pcap file or open an existing pcap file.
Definition pcap-file.cc:320
uint32_t GetDataLinkType()
Returns the data link type field of the pcap file as defined by the network field in the pcap global ...
Definition pcap-file.cc:130
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:468
uint32_t GetMagic()
Returns the magic number of the pcap file as defined by the magic_number field in the pcap global hea...
Definition pcap-file.cc:88
uint16_t GetVersionMajor()
Returns the major version of the pcap file as defined by the version_major field in the pcap global h...
Definition pcap-file.cc:95
uint16_t GetVersionMinor()
Returns the minor version of the pcap file as defined by the version_minor field in the pcap global h...
Definition pcap-file.cc:102
void Clear()
Clear all state bits of the underlying iostream.
Definition pcap-file.cc:74
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:340
void Write(uint32_t tsSec, uint32_t tsUsec, const uint8_t *const data, uint32_t totalLen)
Write next packet to file.
Definition pcap-file.cc:433
bool Eof() const
Definition pcap-file.cc:67
bool Fail() const
Definition pcap-file.cc:60
uint32_t GetSnapLen()
Returns the max length of saved packets field of the pcap file as defined by the snaplen field in the...
Definition pcap-file.cc:123
static const uint32_t SNAPLEN_DEFAULT
Default value for maximum octets to save per packet.
Definition pcap-file.h:35
int32_t GetTimeZoneOffset()
Returns the time zone offset of the pcap file as defined by the thiszone field in the pcap global hea...
Definition pcap-file.cc:109
uint32_t GetSigFigs()
Returns the accuracy of timestamps field of the pcap file as defined by the sigfigs field in the pcap...
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.
static TypeId GetTypeId()
Get the type ID.
PcapFile m_file
Pcap file.
void Open(const std::string &filename, std::ios::openmode mode)
Create a new pcap file or open an existing pcap file.
int32_t GetTimeZoneOffset()
Returns the time zone offset of the pcap file as defined by the thiszone field in the pcap global hea...
uint32_t m_snapLen
max length of saved packets
uint32_t GetSigFigs()
Returns the accuracy of timestamps field of the pcap file as defined by the sigfigs field in the pcap...
void Close()
Close the underlying pcap 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 Clear()
Clear all state bits of the underlying iostream.
uint32_t GetMagic()
Returns the magic number of the pcap file as defined by the magic_number field in the pcap global hea...
uint16_t GetVersionMajor()
Returns the major version of the pcap file as defined by the version_major field in the pcap global h...
uint32_t GetSnapLen()
Returns the max length of saved packets field of the pcap file as defined by the snaplen field in the...
uint16_t GetVersionMinor()
Returns the minor version of the pcap file as defined by the version_minor field in the pcap global h...
uint32_t GetDataLinkType()
Returns the data link type field of the pcap file as defined by the network field in the pcap global ...
bool m_nanosecMode
Timestamps in nanosecond mode.
Smart pointer class similar to boost::intrusive_ptr.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:407
int64_t GetMicroSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:402
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Hold an unsigned integer type.
Definition uinteger.h:34
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1332
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition boolean.cc:113
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition boolean.h:70