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 
32 TypeId
34 {
35  static TypeId tid = TypeId ("ns3::PcapFileWrapper")
36  .SetParent<Object> ()
37  .AddConstructor<PcapFileWrapper> ()
38  .AddAttribute ("CaptureSize",
39  "Maximum length of captured packets (cf. pcap snaplen)",
41  MakeUintegerAccessor (&PcapFileWrapper::m_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
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
uint16_t GetVersionMinor(void)
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)
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
NS_LOG_COMPONENT_DEFINE("PcapFileWrapper")
bool Eof(void) const
Definition: pcap-file.cc:72
uint32_t GetDataLinkType(void)
int64_t GetMicroSeconds(void) const
Definition: nstime.h:291
Hold an unsigned integer type.
Definition: uinteger.h:46
static TypeId GetTypeId(void)
Ptr< SampleEmitter > s
int32_t GetTimeZoneOffset(void)
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)
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)
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)
Definition: pcap-file.cc:121
void Close(void)
Close the underlying file.
Definition: pcap-file.cc:86
uint16_t GetVersionMajor(void)
Definition: pcap-file.cc:100
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
bool Fail(void) const
Definition: pcap-file.cc:66
bool Fail(void) const
uint32_t GetDataLinkType(void)
Definition: pcap-file.cc:135
a base class which provides memory management and object aggregation
Definition: object.h:63
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:611
uint32_t GetSnapLen(void)
Definition: pcap-file.cc:128
int32_t GetTimeZoneOffset(void)
Definition: pcap-file.cc:114