author | Craig Dowell <craigdo@ee.washington.edu> |
Wed, 03 Feb 2010 13:26:39 -0800 | |
changeset 6041 | b65c6d6794f8 |
parent 6035 | ecd8840c3573 |
child 6043 | fe4ec1033fec |
permissions | -rw-r--r-- |
6009 | 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 |
||
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
19 |
#include "ns3/log.h" |
6041 | 20 |
#include "ns3/uinteger.h" |
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
21 |
|
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
22 |
#include "buffer.h" |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
23 |
#include "header.h" |
6009 | 24 |
#include "pcap-file-object.h" |
25 |
||
26 |
NS_LOG_COMPONENT_DEFINE ("PcapFileObject"); |
|
27 |
||
28 |
namespace ns3 { |
|
29 |
||
30 |
NS_OBJECT_ENSURE_REGISTERED (PcapFileObject); |
|
31 |
||
32 |
TypeId |
|
33 |
PcapFileObject::GetTypeId (void) |
|
34 |
{ |
|
35 |
static TypeId tid = TypeId ("ns3::PcapFileObject") |
|
36 |
.SetParent<Object> () |
|
37 |
.AddConstructor<PcapFileObject> () |
|
6041 | 38 |
.AddAttribute ("CaptureSize", |
39 |
"Maximum length of captured packets (cf. pcap snaplen)", |
|
40 |
UintegerValue (PcapFile::SNAPLEN_DEFAULT), |
|
41 |
MakeUintegerAccessor (&PcapFileObject::m_snapLen), |
|
42 |
MakeUintegerChecker<uint32_t> (0, PcapFile::SNAPLEN_DEFAULT)) |
|
6009 | 43 |
; |
44 |
return tid; |
|
45 |
} |
|
46 |
||
47 |
||
48 |
PcapFileObject::PcapFileObject () |
|
49 |
{ |
|
50 |
} |
|
51 |
||
52 |
PcapFileObject::~PcapFileObject () |
|
53 |
{ |
|
54 |
Close (); |
|
55 |
} |
|
56 |
||
57 |
void |
|
58 |
PcapFileObject::Close (void) |
|
59 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
60 |
m_file.Close (); |
6009 | 61 |
} |
62 |
||
63 |
bool |
|
64 |
PcapFileObject::Open (std::string const &filename, std::string const &mode) |
|
65 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
66 |
return m_file.Open (filename, mode); |
6009 | 67 |
} |
68 |
||
69 |
bool |
|
70 |
PcapFileObject::Init (uint32_t dataLinkType, uint32_t snapLen, int32_t tzCorrection) |
|
71 |
{ |
|
6041 | 72 |
// |
73 |
// If the user doesn't provide a snaplen, the default value will come in. If |
|
74 |
// this happens, we use the "CaptureSize" Attribute. If the user does provide |
|
75 |
// a snaplen, we use the one provided. |
|
76 |
// |
|
77 |
if (snapLen != std::numeric_limits<uint32_t>::max ()) |
|
78 |
{ |
|
79 |
return m_file.Init (dataLinkType, snapLen, tzCorrection); |
|
80 |
} |
|
81 |
else |
|
82 |
{ |
|
83 |
return m_file.Init (dataLinkType, m_snapLen, tzCorrection); |
|
84 |
} |
|
85 |
||
86 |
// |
|
87 |
// Quiet the compiler |
|
88 |
// |
|
89 |
return true; |
|
6009 | 90 |
} |
91 |
||
92 |
bool |
|
93 |
PcapFileObject::Write (Time t, Ptr<const Packet> p) |
|
94 |
{ |
|
95 |
uint64_t current = t.GetMicroSeconds (); |
|
96 |
uint64_t s = current / 1000000; |
|
97 |
uint64_t us = current % 1000000; |
|
98 |
||
99 |
uint32_t bufferSize = p->GetSize (); |
|
100 |
uint8_t *buffer = new uint8_t[bufferSize]; |
|
101 |
p->CopyData (buffer, bufferSize); |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
102 |
bool rc = m_file.Write (s, us, buffer, bufferSize); |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
103 |
delete [] buffer; |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
104 |
return rc; |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
105 |
} |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
106 |
|
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
107 |
bool |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
108 |
PcapFileObject::Write (Time t, Header &header, Ptr<const Packet> p) |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
109 |
{ |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
110 |
uint64_t current = t.GetMicroSeconds (); |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
111 |
uint64_t s = current / 1000000; |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
112 |
uint64_t us = current % 1000000; |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
113 |
|
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
114 |
Buffer headerBuffer; |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
115 |
uint32_t headerSize = header.GetSerializedSize (); |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
116 |
uint32_t packetSize = p->GetSize (); |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
117 |
uint32_t bufferSize = headerSize + packetSize; |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
118 |
|
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
119 |
headerBuffer.AddAtStart (headerSize); |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
120 |
header.Serialize (headerBuffer.Begin ()); |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
121 |
|
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
122 |
uint8_t *buffer = new uint8_t[bufferSize]; |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
123 |
|
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
124 |
headerBuffer.Begin ().Read (buffer, headerSize); |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
125 |
p->CopyData (&buffer[headerSize], packetSize); |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
126 |
bool rc = m_file.Write (s, us, buffer, bufferSize); |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
127 |
|
6009 | 128 |
delete [] buffer; |
129 |
return rc; |
|
130 |
} |
|
131 |
||
132 |
bool |
|
133 |
PcapFileObject::Write (Time t, uint8_t const *buffer, uint32_t length) |
|
134 |
{ |
|
135 |
uint64_t current = t.GetMicroSeconds (); |
|
136 |
uint64_t s = current / 1000000; |
|
137 |
uint64_t us = current % 1000000; |
|
138 |
||
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
139 |
return m_file.Write (s, us, buffer, length); |
6009 | 140 |
} |
141 |
||
142 |
uint32_t |
|
143 |
PcapFileObject::GetMagic (void) |
|
144 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
145 |
return m_file.GetMagic (); |
6009 | 146 |
} |
147 |
||
148 |
uint16_t |
|
149 |
PcapFileObject::GetVersionMajor (void) |
|
150 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
151 |
return m_file.GetVersionMajor (); |
6009 | 152 |
} |
153 |
||
154 |
uint16_t |
|
155 |
PcapFileObject::GetVersionMinor (void) |
|
156 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
157 |
return m_file.GetVersionMinor (); |
6009 | 158 |
} |
159 |
||
160 |
int32_t |
|
161 |
PcapFileObject::GetTimeZoneOffset (void) |
|
162 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
163 |
return m_file.GetTimeZoneOffset (); |
6009 | 164 |
} |
165 |
||
166 |
uint32_t |
|
167 |
PcapFileObject::GetSigFigs (void) |
|
168 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
169 |
return m_file.GetSigFigs (); |
6009 | 170 |
} |
171 |
||
172 |
uint32_t |
|
173 |
PcapFileObject::GetSnapLen (void) |
|
174 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
175 |
return m_file.GetSnapLen (); |
6009 | 176 |
} |
177 |
||
178 |
uint32_t |
|
179 |
PcapFileObject::GetDataLinkType (void) |
|
180 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
181 |
return m_file.GetDataLinkType (); |
6009 | 182 |
} |
183 |
||
184 |
} //namespace ns3 |