A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("PcapFileWrapper");
26 
27 namespace ns3 {
28 
29 NS_OBJECT_ENSURE_REGISTERED (PcapFileWrapper);
30 
31 TypeId
33 {
34  static TypeId tid = TypeId ("ns3::PcapFileWrapper")
35  .SetParent<Object> ()
36  .AddConstructor<PcapFileWrapper> ()
37  .AddAttribute ("CaptureSize",
38  "Maximum length of captured packets (cf. pcap snaplen)",
40  MakeUintegerAccessor (&PcapFileWrapper::m_snapLen),
41  MakeUintegerChecker<uint32_t> (0, PcapFile::SNAPLEN_DEFAULT))
42  ;
43  return tid;
44 }
45 
46 
48 {
49  NS_LOG_FUNCTION (this);
50 }
51 
53 {
54  NS_LOG_FUNCTION (this);
55  Close ();
56 }
57 
58 
59 bool
61 {
62  NS_LOG_FUNCTION (this);
63  return m_file.Fail ();
64 }
65 bool
67 {
68  NS_LOG_FUNCTION (this);
69  return m_file.Eof ();
70 }
71 void
73 {
74  NS_LOG_FUNCTION (this);
75  m_file.Clear ();
76 }
77 
78 void
80 {
81  NS_LOG_FUNCTION (this);
82  m_file.Close ();
83 }
84 
85 void
86 PcapFileWrapper::Open (std::string const &filename, std::ios::openmode mode)
87 {
88  NS_LOG_FUNCTION (this << filename << mode);
89  m_file.Open (filename, mode);
90 }
91 
92 void
93 PcapFileWrapper::Init (uint32_t dataLinkType, uint32_t snapLen, int32_t tzCorrection)
94 {
95  //
96  // If the user doesn't provide a snaplen, the default value will come in. If
97  // this happens, we use the "CaptureSize" Attribute. If the user does provide
98  // a snaplen, we use the one provided.
99  //
100  NS_LOG_FUNCTION (this << dataLinkType << snapLen << tzCorrection);
101  if (snapLen != std::numeric_limits<uint32_t>::max ())
102  {
103  m_file.Init (dataLinkType, snapLen, tzCorrection);
104  }
105  else
106  {
107  m_file.Init (dataLinkType, m_snapLen, tzCorrection);
108  }
109 }
110 
111 void
113 {
114  NS_LOG_FUNCTION (this << t << p);
115  uint64_t current = t.GetMicroSeconds ();
116  uint64_t s = current / 1000000;
117  uint64_t us = current % 1000000;
118 
119  m_file.Write (s, us, p);
120 }
121 
122 void
124 {
125  NS_LOG_FUNCTION (this << t << &header << p);
126  uint64_t current = t.GetMicroSeconds ();
127  uint64_t s = current / 1000000;
128  uint64_t us = current % 1000000;
129 
130  m_file.Write (s, us, header, p);
131 }
132 
133 void
134 PcapFileWrapper::Write (Time t, uint8_t const *buffer, uint32_t length)
135 {
136  NS_LOG_FUNCTION (this << t << &buffer << length);
137  uint64_t current = t.GetMicroSeconds ();
138  uint64_t s = current / 1000000;
139  uint64_t us = current % 1000000;
140 
141  m_file.Write (s, us, buffer, length);
142 }
143 
144 uint32_t
146 {
147  NS_LOG_FUNCTION (this);
148  return m_file.GetMagic ();
149 }
150 
151 uint16_t
153 {
154  NS_LOG_FUNCTION (this);
155  return m_file.GetVersionMajor ();
156 }
157 
158 uint16_t
160 {
161  NS_LOG_FUNCTION (this);
162  return m_file.GetVersionMinor ();
163 }
164 
165 int32_t
167 {
168  NS_LOG_FUNCTION (this);
169  return m_file.GetTimeZoneOffset ();
170 }
171 
172 uint32_t
174 {
175  NS_LOG_FUNCTION (this);
176  return m_file.GetSigFigs ();
177 }
178 
179 uint32_t
181 {
182  NS_LOG_FUNCTION (this);
183  return m_file.GetSnapLen ();
184 }
185 
186 uint32_t
188 {
189  NS_LOG_FUNCTION (this);
190  return m_file.GetDataLinkType ();
191 }
192 
193 } // namespace ns3
Protocol header serialization and deserialization.
Definition: header.h:42
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
#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 the class in the ns-3 factory.
Definition: object-base.h:38
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:170
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
Definition: nstime.h:289
Hold an unsigned integer type.
Definition: uinteger.h:46
static TypeId GetTypeId(void)
Get the type ID.
Ptr< SampleEmitter > s
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
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
Time current
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:64
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 unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
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