A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
pcap-file-wrapper.cc
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
32 PcapFileWrapper::GetTypeId (void)
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)",
39  UintegerValue (PcapFile::SNAPLEN_DEFAULT),
40  MakeUintegerAccessor (&PcapFileWrapper::m_snapLen),
41  MakeUintegerChecker<uint32_t> (0, PcapFile::SNAPLEN_DEFAULT))
42  ;
43  return tid;
44 }
45 
46 
47 PcapFileWrapper::PcapFileWrapper ()
48 {
49  NS_LOG_FUNCTION (this);
50 }
51 
52 PcapFileWrapper::~PcapFileWrapper ()
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
145 PcapFileWrapper::GetMagic (void)
146 {
147  NS_LOG_FUNCTION (this);
148  return m_file.GetMagic ();
149 }
150 
151 uint16_t
152 PcapFileWrapper::GetVersionMajor (void)
153 {
154  NS_LOG_FUNCTION (this);
155  return m_file.GetVersionMajor ();
156 }
157 
158 uint16_t
159 PcapFileWrapper::GetVersionMinor (void)
160 {
161  NS_LOG_FUNCTION (this);
162  return m_file.GetVersionMinor ();
163 }
164 
165 int32_t
166 PcapFileWrapper::GetTimeZoneOffset (void)
167 {
168  NS_LOG_FUNCTION (this);
169  return m_file.GetTimeZoneOffset ();
170 }
171 
172 uint32_t
173 PcapFileWrapper::GetSigFigs (void)
174 {
175  NS_LOG_FUNCTION (this);
176  return m_file.GetSigFigs ();
177 }
178 
179 uint32_t
180 PcapFileWrapper::GetSnapLen (void)
181 {
182  NS_LOG_FUNCTION (this);
183  return m_file.GetSnapLen ();
184 }
185 
186 uint32_t
187 PcapFileWrapper::GetDataLinkType (void)
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
keep track of time unit.
Definition: nstime.h:149
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
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)
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
bool Eof(void) const
Definition: pcap-file.cc:73
int64_t GetMicroSeconds(void) const
Definition: nstime.h:279
bool Eof(void) const
void Open(std::string const &filename, std::ios::openmode mode)
void Clear(void)
Definition: pcap-file.cc:79
static const uint32_t SNAPLEN_DEFAULT
Definition: pcap-file.h:44
void Init(uint32_t dataLinkType, uint32_t snapLen=SNAPLEN_DEFAULT, int32_t timeZoneCorrection=ZONE_DEFAULT, bool swapMode=false)
Definition: pcap-file.cc:330
void Close(void)
Definition: pcap-file.cc:87
void Open(std::string const &filename, std::ios::openmode mode)
Definition: pcap-file.cc:311
bool Fail(void) const
Definition: pcap-file.cc:67
bool Fail(void) const
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:405