author | Craig Dowell <craigdo@ee.washington.edu> |
Fri, 29 Jan 2010 20:08:50 -0800 | |
changeset 6035 | ecd8840c3573 |
parent 6009 | e1b696a1ed28 |
child 6041 | b65c6d6794f8 |
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" |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
20 |
|
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
21 |
#include "buffer.h" |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
22 |
#include "header.h" |
6009 | 23 |
#include "pcap-file-object.h" |
24 |
||
25 |
NS_LOG_COMPONENT_DEFINE ("PcapFileObject"); |
|
26 |
||
27 |
namespace ns3 { |
|
28 |
||
29 |
NS_OBJECT_ENSURE_REGISTERED (PcapFileObject); |
|
30 |
||
31 |
TypeId |
|
32 |
PcapFileObject::GetTypeId (void) |
|
33 |
{ |
|
34 |
static TypeId tid = TypeId ("ns3::PcapFileObject") |
|
35 |
.SetParent<Object> () |
|
36 |
.AddConstructor<PcapFileObject> () |
|
37 |
; |
|
38 |
return tid; |
|
39 |
} |
|
40 |
||
41 |
||
42 |
PcapFileObject::PcapFileObject () |
|
43 |
{ |
|
44 |
} |
|
45 |
||
46 |
PcapFileObject::~PcapFileObject () |
|
47 |
{ |
|
48 |
Close (); |
|
49 |
} |
|
50 |
||
51 |
void |
|
52 |
PcapFileObject::Close (void) |
|
53 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
54 |
m_file.Close (); |
6009 | 55 |
} |
56 |
||
57 |
bool |
|
58 |
PcapFileObject::Open (std::string const &filename, std::string const &mode) |
|
59 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
60 |
return m_file.Open (filename, mode); |
6009 | 61 |
} |
62 |
||
63 |
bool |
|
64 |
PcapFileObject::Init (uint32_t dataLinkType, uint32_t snapLen, int32_t tzCorrection) |
|
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.Init (dataLinkType, snapLen, tzCorrection); |
6009 | 67 |
} |
68 |
||
69 |
bool |
|
70 |
PcapFileObject::Write (Time t, Ptr<const Packet> p) |
|
71 |
{ |
|
72 |
uint64_t current = t.GetMicroSeconds (); |
|
73 |
uint64_t s = current / 1000000; |
|
74 |
uint64_t us = current % 1000000; |
|
75 |
||
76 |
uint32_t bufferSize = p->GetSize (); |
|
77 |
uint8_t *buffer = new uint8_t[bufferSize]; |
|
78 |
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
|
79 |
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
|
80 |
delete [] buffer; |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
81 |
return rc; |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
82 |
} |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
83 |
|
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
84 |
bool |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
85 |
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
|
86 |
{ |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
87 |
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
|
88 |
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
|
89 |
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
|
90 |
|
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
91 |
Buffer headerBuffer; |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
92 |
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
|
93 |
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
|
94 |
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
|
95 |
|
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
96 |
headerBuffer.AddAtStart (headerSize); |
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
97 |
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
|
98 |
|
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
99 |
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
|
100 |
|
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
101 |
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
|
102 |
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
|
103 |
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
|
104 |
|
6009 | 105 |
delete [] buffer; |
106 |
return rc; |
|
107 |
} |
|
108 |
||
109 |
bool |
|
110 |
PcapFileObject::Write (Time t, uint8_t const *buffer, uint32_t length) |
|
111 |
{ |
|
112 |
uint64_t current = t.GetMicroSeconds (); |
|
113 |
uint64_t s = current / 1000000; |
|
114 |
uint64_t us = current % 1000000; |
|
115 |
||
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
116 |
return m_file.Write (s, us, buffer, length); |
6009 | 117 |
} |
118 |
||
119 |
uint32_t |
|
120 |
PcapFileObject::GetMagic (void) |
|
121 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
122 |
return m_file.GetMagic (); |
6009 | 123 |
} |
124 |
||
125 |
uint16_t |
|
126 |
PcapFileObject::GetVersionMajor (void) |
|
127 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
128 |
return m_file.GetVersionMajor (); |
6009 | 129 |
} |
130 |
||
131 |
uint16_t |
|
132 |
PcapFileObject::GetVersionMinor (void) |
|
133 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
134 |
return m_file.GetVersionMinor (); |
6009 | 135 |
} |
136 |
||
137 |
int32_t |
|
138 |
PcapFileObject::GetTimeZoneOffset (void) |
|
139 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
140 |
return m_file.GetTimeZoneOffset (); |
6009 | 141 |
} |
142 |
||
143 |
uint32_t |
|
144 |
PcapFileObject::GetSigFigs (void) |
|
145 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
146 |
return m_file.GetSigFigs (); |
6009 | 147 |
} |
148 |
||
149 |
uint32_t |
|
150 |
PcapFileObject::GetSnapLen (void) |
|
151 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
152 |
return m_file.GetSnapLen (); |
6009 | 153 |
} |
154 |
||
155 |
uint32_t |
|
156 |
PcapFileObject::GetDataLinkType (void) |
|
157 |
{ |
|
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
158 |
return m_file.GetDataLinkType (); |
6009 | 159 |
} |
160 |
||
161 |
} //namespace ns3 |