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 }
50 
52 {
53  Close ();
54 }
55 
56 
57 bool
59 {
60  return m_file.Fail ();
61 }
62 bool
64 {
65  return m_file.Eof ();
66 }
67 void
69 {
70  m_file.Clear ();
71 }
72 
73 void
75 {
76  m_file.Close ();
77 }
78 
79 void
80 PcapFileWrapper::Open (std::string const &filename, std::ios::openmode mode)
81 {
82  m_file.Open (filename, mode);
83 }
84 
85 void
86 PcapFileWrapper::Init (uint32_t dataLinkType, uint32_t snapLen, int32_t tzCorrection)
87 {
88  //
89  // If the user doesn't provide a snaplen, the default value will come in. If
90  // this happens, we use the "CaptureSize" Attribute. If the user does provide
91  // a snaplen, we use the one provided.
92  //
93  if (snapLen != std::numeric_limits<uint32_t>::max ())
94  {
95  m_file.Init (dataLinkType, snapLen, tzCorrection);
96  }
97  else
98  {
99  m_file.Init (dataLinkType, m_snapLen, tzCorrection);
100  }
101 }
102 
103 void
105 {
106  uint64_t current = t.GetMicroSeconds ();
107  uint64_t s = current / 1000000;
108  uint64_t us = current % 1000000;
109 
110  m_file.Write (s, us, p);
111 }
112 
113 void
115 {
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, header, p);
121 }
122 
123 void
124 PcapFileWrapper::Write (Time t, uint8_t const *buffer, uint32_t length)
125 {
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, buffer, length);
131 }
132 
133 uint32_t
135 {
136  return m_file.GetMagic ();
137 }
138 
139 uint16_t
141 {
142  return m_file.GetVersionMajor ();
143 }
144 
145 uint16_t
147 {
148  return m_file.GetVersionMinor ();
149 }
150 
151 int32_t
153 {
154  return m_file.GetTimeZoneOffset ();
155 }
156 
157 uint32_t
159 {
160  return m_file.GetSigFigs ();
161 }
162 
163 uint32_t
165 {
166  return m_file.GetSnapLen ();
167 }
168 
169 uint32_t
171 {
172  return m_file.GetDataLinkType ();
173 }
174 
175 } // namespace ns3