author | Vedran Miletić <rivanvx@gmail.com> |
Sat, 01 Sep 2012 20:57:21 +0200 | |
changeset 9063 | 32755d0516f4 |
parent 7386 | 2310ed220a61 |
child 10657 | 6531a8817def |
permissions | -rw-r--r-- |
7385
10beb0e53130
standardize emacs c++ mode comments
Vedran Miletić <rivanvx@gmail.com>
parents:
7353
diff
changeset
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
6009 | 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 |
||
6052
e146b9c9c7e7
PcapFileObject to PcapFileWrapper
Craig Dowell <craigdo@ee.washington.edu>
parents:
6041
diff
changeset
|
19 |
#ifndef PCAP_FILE_WRAPPER_H |
e146b9c9c7e7
PcapFileObject to PcapFileWrapper
Craig Dowell <craigdo@ee.washington.edu>
parents:
6041
diff
changeset
|
20 |
#define PCAP_FILE_WRAPPER_H |
6009 | 21 |
|
9063
32755d0516f4
Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents:
7386
diff
changeset
|
22 |
#include <cstring> |
6056
27b8cb0963bf
add missing include
Craig Dowell <craigdo@ee.washington.edu>
parents:
6052
diff
changeset
|
23 |
#include <limits> |
6206
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
24 |
#include <fstream> |
6009 | 25 |
#include "ns3/ptr.h" |
26 |
#include "ns3/packet.h" |
|
6455
e17da5733c0b
Change nix-vector to use SimpleRefCount instead of Object
Josh Pelkey <jpelkey@gatech.edu>
parents:
6206
diff
changeset
|
27 |
#include "ns3/object.h" |
6009 | 28 |
#include "ns3/nstime.h" |
29 |
#include "pcap-file.h" |
|
30 |
||
31 |
namespace ns3 { |
|
32 |
||
33 |
/* |
|
6101
6e16bf523c55
some doxygen updates got missed
Craig Dowell <craigdo@ee.washington.edu>
parents:
6056
diff
changeset
|
34 |
* A class that wraps a PcapFile as an ns3::Object and provides a higher-layer |
6e16bf523c55
some doxygen updates got missed
Craig Dowell <craigdo@ee.washington.edu>
parents:
6056
diff
changeset
|
35 |
* ns-3 interface to the low-level public methods of PcapFile. Users are |
6e16bf523c55
some doxygen updates got missed
Craig Dowell <craigdo@ee.washington.edu>
parents:
6056
diff
changeset
|
36 |
* encouraged to use this object instead of class ns3::PcapFile in ns-3 |
6e16bf523c55
some doxygen updates got missed
Craig Dowell <craigdo@ee.washington.edu>
parents:
6056
diff
changeset
|
37 |
* public APIs. |
6009 | 38 |
*/ |
6052
e146b9c9c7e7
PcapFileObject to PcapFileWrapper
Craig Dowell <craigdo@ee.washington.edu>
parents:
6041
diff
changeset
|
39 |
class PcapFileWrapper : public Object |
6009 | 40 |
{ |
41 |
public: |
|
42 |
static TypeId GetTypeId (void); |
|
43 |
||
6052
e146b9c9c7e7
PcapFileObject to PcapFileWrapper
Craig Dowell <craigdo@ee.washington.edu>
parents:
6041
diff
changeset
|
44 |
PcapFileWrapper (); |
e146b9c9c7e7
PcapFileObject to PcapFileWrapper
Craig Dowell <craigdo@ee.washington.edu>
parents:
6041
diff
changeset
|
45 |
~PcapFileWrapper (); |
6009 | 46 |
|
6206
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
47 |
|
6040 | 48 |
/** |
6206
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
49 |
* \return true if the 'fail' bit is set in the underlying iostream, false otherwise. |
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
50 |
*/ |
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
51 |
bool Fail (void) const; |
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
52 |
/** |
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
53 |
* \return true if the 'eof' bit is set in the underlying iostream, false otherwise. |
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
54 |
*/ |
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
55 |
bool Eof (void) const; |
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
56 |
/** |
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
57 |
* Clear all state bits of the underlying iostream. |
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
58 |
*/ |
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
59 |
void Clear (void); |
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
60 |
|
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
61 |
/** |
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
62 |
* Create a new pcap file or open an existing pcap file. Semantics are |
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
63 |
* similar to the stdc++ io stream classes. |
6040 | 64 |
* |
65 |
* Since a pcap file is always a binary file, the file type is automatically |
|
6206
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
66 |
* selected as a binary file (fstream::binary is automatically ored with the mode |
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
67 |
* field). |
6040 | 68 |
* |
69 |
* \param filename String containing the name of the file. |
|
70 |
* |
|
71 |
* \param mode String containing the access mode for the file. |
|
72 |
* |
|
73 |
*/ |
|
6206
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
74 |
void Open (std::string const &filename, std::ios::openmode mode); |
6009 | 75 |
|
6040 | 76 |
/** |
77 |
* Close the underlying pcap file. |
|
78 |
*/ |
|
6009 | 79 |
void Close (void); |
80 |
||
6040 | 81 |
/** |
6052
e146b9c9c7e7
PcapFileObject to PcapFileWrapper
Craig Dowell <craigdo@ee.washington.edu>
parents:
6041
diff
changeset
|
82 |
* Initialize the pcap file associated with this wrapper. This file must have |
6040 | 83 |
* been previously opened with write permissions. |
84 |
* |
|
85 |
* \param dataLinkType A data link type as defined in the pcap library. If |
|
86 |
* you want to make resulting pcap files visible in existing tools, the |
|
87 |
* data link type must match existing definitions, such as PCAP_ETHERNET, |
|
88 |
* PCAP_PPP, PCAP_80211, etc. If you are storing different kinds of packet |
|
89 |
* data, such as naked TCP headers, you are at liberty to locally define your |
|
90 |
* own data link types. According to the pcap-linktype man page, "well-known" |
|
91 |
* pcap linktypes range from 0 to 177. If you use a large random number for |
|
92 |
* your type, chances are small for a collision. |
|
93 |
* |
|
94 |
* \param snapLen An optional maximum size for packets written to the file. |
|
95 |
* Defaults to 65535. If packets exceed this length they are truncated. |
|
96 |
* |
|
97 |
* \param tzCorrection An integer describing the offset of your local |
|
98 |
* time zone from UTC/GMT. For example, Pacific Standard Time in the US is |
|
99 |
* GMT-8, so one would enter -8 for that correction. Defaults to 0 (UTC). |
|
100 |
* |
|
101 |
* \warning Calling this method on an existing file will result in the loss |
|
102 |
* any existing data. |
|
103 |
*/ |
|
6206
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
104 |
void Init (uint32_t dataLinkType, |
6041 | 105 |
uint32_t snapLen = std::numeric_limits<uint32_t>::max (), |
6009 | 106 |
int32_t tzCorrection = PcapFile::ZONE_DEFAULT); |
107 |
||
6040 | 108 |
/** |
109 |
* \brief Write the next packet to file |
|
110 |
* |
|
111 |
* \param t Packet timestamp as ns3::Time. |
|
112 |
* \param p Packet to write to the pcap file. |
|
113 |
* |
|
114 |
*/ |
|
6206
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
115 |
void Write (Time t, Ptr<const Packet> p); |
6040 | 116 |
|
117 |
/** |
|
118 |
* \brief Write the provided header along with the packet to the pcap file. |
|
119 |
* |
|
120 |
* It is the case that adding a header to a packet prior to writing it to a |
|
121 |
* file must trigger a deep copy in the Packet. By providing the header |
|
122 |
* separately, we can avoid that copy. |
|
123 |
* |
|
124 |
* \param t Packet timestamp as ns3::Time. |
|
125 |
* \param header The Header to prepend to the packet. |
|
126 |
* \param p Packet to write to the pcap file. |
|
127 |
* |
|
128 |
*/ |
|
6206
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
129 |
void Write (Time t, Header &header, Ptr<const Packet> p); |
6040 | 130 |
|
131 |
/** |
|
132 |
* \brief Write the provided data buffer to the pcap file. |
|
133 |
* |
|
134 |
* \param t Packet timestamp as ns3::Time. |
|
135 |
* \param buffer The buffer to write. |
|
136 |
* \param length The size of the buffer. |
|
137 |
* |
|
138 |
*/ |
|
6206
9787dc9fdd84
bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6101
diff
changeset
|
139 |
void Write (Time t, uint8_t const *buffer, uint32_t length); |
6009 | 140 |
|
6040 | 141 |
/* |
142 |
* \brief Returns the magic number of the pcap file as defined by the magic_number |
|
143 |
* field in the pcap global header. |
|
144 |
* |
|
145 |
* See http://wiki.wireshark.org/Development/LibpcapFileFormat |
|
146 |
*/ |
|
6009 | 147 |
uint32_t GetMagic (void); |
6040 | 148 |
|
149 |
/* |
|
150 |
* \brief Returns the major version of the pcap file as defined by the version_major |
|
151 |
* field in the pcap global header. |
|
152 |
* |
|
153 |
* See http://wiki.wireshark.org/Development/LibpcapFileFormat |
|
154 |
*/ |
|
6009 | 155 |
uint16_t GetVersionMajor (void); |
6040 | 156 |
|
157 |
/* |
|
158 |
* \brief Returns the minor version of the pcap file as defined by the version_minor |
|
159 |
* field in the pcap global header. |
|
160 |
* |
|
161 |
* See http://wiki.wireshark.org/Development/LibpcapFileFormat |
|
162 |
*/ |
|
6009 | 163 |
uint16_t GetVersionMinor (void); |
6040 | 164 |
|
165 |
/* |
|
166 |
* \brief Returns the time zone offset of the pcap file as defined by the thiszone |
|
167 |
* field in the pcap global header. |
|
168 |
* |
|
169 |
* See http://wiki.wireshark.org/Development/LibpcapFileFormat |
|
170 |
*/ |
|
6009 | 171 |
int32_t GetTimeZoneOffset (void); |
6040 | 172 |
|
173 |
/* |
|
174 |
* \brief Returns the accuracy of timestamps field of the pcap file as defined |
|
175 |
* by the sigfigs field in the pcap global header. |
|
176 |
* |
|
177 |
* See http://wiki.wireshark.org/Development/LibpcapFileFormat |
|
178 |
*/ |
|
6009 | 179 |
uint32_t GetSigFigs (void); |
6040 | 180 |
|
181 |
/* |
|
182 |
* \brief Returns the max length of saved packets field of the pcap file as |
|
183 |
* defined by the snaplen field in the pcap global header. |
|
184 |
* |
|
185 |
* See http://wiki.wireshark.org/Development/LibpcapFileFormat |
|
186 |
*/ |
|
6009 | 187 |
uint32_t GetSnapLen (void); |
6040 | 188 |
|
189 |
/* |
|
190 |
* \brief Returns the data link type field of the pcap file as defined by the |
|
191 |
* network field in the pcap global header. |
|
192 |
* |
|
193 |
* See http://wiki.wireshark.org/Development/LibpcapFileFormat |
|
194 |
*/ |
|
6009 | 195 |
uint32_t GetDataLinkType (void); |
7182
5ecfee5d17de
network coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6823
diff
changeset
|
196 |
|
6009 | 197 |
private: |
6035
ecd8840c3573
add ability to pass a header to pcap file object
Craig Dowell <craigdo@ee.washington.edu>
parents:
6009
diff
changeset
|
198 |
PcapFile m_file; |
6041 | 199 |
uint32_t m_snapLen; |
6009 | 200 |
}; |
201 |
||
7386
2310ed220a61
standardize ns-3 namespace declaration format
Vedran Miletić <rivanvx@gmail.com>
parents:
7385
diff
changeset
|
202 |
} // namespace ns3 |
6009 | 203 |
|
7353
09fccf6195ea
bug 1203: Inconsistently named ifndef/define macros in ns-3 headers
Vedran Miletic <rivanvx@gmail.com>
parents:
7182
diff
changeset
|
204 |
#endif /* PCAP_FILE_WRAPPER_H */ |