INET Framework for OMNeT++/OMNEST
inet::PcapDump Class Reference

Dumps packets into a PCAP file; see the "pcap-savefile" man page or http://www.tcpdump.org/ for details on the file format. More...

#include <PcapDump.h>

Public Member Functions

 PcapDump ()
 Constructor. More...
 
 ~PcapDump ()
 Destructor. More...
 
void openPcap (const char *filename, unsigned int snaplen)
 Opens a PCAP file with the given file name. More...
 
bool isOpen () const
 Returns true if the pcap file is currently open. More...
 
void writeFrame (simtime_t time, const IPv4Datagram *ipPacket)
 Records the given packet into the output file if it is open, and throws an exception otherwise. More...
 
void writeIPv6Frame (simtime_t stime, const IPv6Datagram *ipPacket)
 
void closePcap ()
 Closes the output file if it is open. More...
 
void setFlushParameter (bool doFlush)
 Force flushing of pcap dump. More...
 

Protected Attributes

FILE * dumpfile = nullptr
 
unsigned int snaplen = 0
 
bool flush = false
 

Detailed Description

Dumps packets into a PCAP file; see the "pcap-savefile" man page or http://www.tcpdump.org/ for details on the file format.

Note: The file is currently recorded in the "classic" format, not in the "Next Generation" file format also on tcpdump.org.

Constructor & Destructor Documentation

inet::PcapDump::PcapDump ( )
inline

Constructor.

It does not open the output file.

49 {}
inet::PcapDump::~PcapDump ( )

Destructor.

It closes the output file if it is open.

71 {
72  closePcap();
73 }
void closePcap()
Closes the output file if it is open.
Definition: PcapDump.cc:171

Member Function Documentation

void inet::PcapDump::closePcap ( )

Closes the output file if it is open.

Referenced by inet::TCPDump::finish(), and inet::PcapRecorder::finish().

172 {
173  if (dumpfile) {
174  fclose(dumpfile);
175  dumpfile = nullptr;
176  }
177 }
FILE * dumpfile
Definition: PcapDump.h:41
bool inet::PcapDump::isOpen ( ) const
inline

Returns true if the pcap file is currently open.

Referenced by inet::TCPDump::handleMessage(), and inet::PcapRecorder::recordPacket().

66 { return dumpfile != nullptr; }
FILE * dumpfile
Definition: PcapDump.h:41
void inet::PcapDump::openPcap ( const char *  filename,
unsigned int  snaplen 
)

Opens a PCAP file with the given file name.

The snaplen parameter is the length that packets will be truncated to. Throws an exception if the file cannot be opened.

Referenced by inet::TCPDump::initialize(), and inet::PcapRecorder::initialize().

76 {
77  struct pcap_hdr fh;
78 
79  if (!filename || !filename[0])
80  throw cRuntimeError("Cannot open pcap file: file name is empty");
81 
82  dumpfile = fopen(filename, "wb");
83 
84  if (!dumpfile)
85  throw cRuntimeError("Cannot open pcap file [%s] for writing: %s", filename, strerror(errno));
86 
87  snaplen = snaplen_par;
88 
89  flush = false;
90 
91  fh.magic = PCAP_MAGIC;
92  fh.version_major = 2;
93  fh.version_minor = 4;
94  fh.thiszone = 0;
95  fh.sigfigs = 0;
96  fh.snaplen = snaplen;
97  fh.network = 0;
98  fwrite(&fh, sizeof(fh), 1, dumpfile);
99 }
#define PCAP_MAGIC
Definition: PcapDump.cc:47
bool flush
Definition: PcapDump.h:43
unsigned int snaplen
Definition: PcapDump.h:42
FILE * dumpfile
Definition: PcapDump.h:41
void inet::PcapDump::setFlushParameter ( bool  doFlush)
inline

Force flushing of pcap dump.

Referenced by inet::PcapRecorder::initialize().

83 { flush = doFlush; };
bool flush
Definition: PcapDump.h:43
void inet::PcapDump::writeFrame ( simtime_t  time,
const IPv4Datagram ipPacket 
)

Records the given packet into the output file if it is open, and throws an exception otherwise.

Referenced by inet::TCPDump::handleMessage(), and inet::PcapRecorder::recordPacket().

102 {
103  if (!dumpfile)
104  throw cRuntimeError("Cannot write frame: pcap output file is not open");
105 
106 #ifdef WITH_IPv4
107  uint8 buf[MAXBUFLENGTH];
108  memset((void *)&buf, 0, sizeof(buf));
109 
110  struct pcaprec_hdr ph;
111  ph.ts_sec = (int32)stime.inUnit(SIMTIME_S);
112  ph.ts_usec = (uint32)(stime.inUnit(SIMTIME_US) - (uint32)1000000 * stime.inUnit(SIMTIME_S));
113  // Write Ethernet header
114  uint32 hdr = 2; //AF_INET
115 
116  serializer::Buffer b(buf, sizeof(buf));
117  serializer::Context c;
118  c.throwOnSerializerNotFound = false;
119  serializer::IPv4Serializer().serializePacket(ipPacket, b, c);
120  int32 serialized_ip = b.getPos();
121 
122  ph.orig_len = serialized_ip + sizeof(uint32);
123 
124  ph.incl_len = ph.orig_len > snaplen ? snaplen : ph.orig_len;
125  fwrite(&ph, sizeof(ph), 1, dumpfile);
126  fwrite(&hdr, sizeof(uint32), 1, dumpfile);
127  fwrite(buf, ph.incl_len - sizeof(uint32), 1, dumpfile);
128  if (flush)
129  fflush(dumpfile);
130 #else // ifdef WITH_IPv4
131  throw cRuntimeError("Cannot write frame: INET compiled without IPv4 feature");
132 #endif // ifdef WITH_IPv4
133 }
uint8_t uint8
Definition: Compat.h:34
bool flush
Definition: PcapDump.h:43
unsigned int snaplen
Definition: PcapDump.h:42
#define MAXBUFLENGTH
Definition: PcapDump.cc:45
const value< double, compose< units::m, pow< units::s,-1 > > > c(299792458)
FILE * dumpfile
Definition: PcapDump.h:41
int32_t int32
Definition: Compat.h:31
uint32_t uint32
Definition: Compat.h:30
value< double, units::m > b
Definition: Units.h:1054
void inet::PcapDump::writeIPv6Frame ( simtime_t  stime,
const IPv6Datagram ipPacket 
)

Referenced by inet::PcapRecorder::recordPacket().

136 {
137  if (!dumpfile)
138  throw cRuntimeError("Cannot write frame: pcap output file is not open");
139 
140 #ifdef WITH_IPv6
141  uint8 buf[MAXBUFLENGTH];
142  memset((void *)&buf, 0, sizeof(buf));
143 
144  struct pcaprec_hdr ph;
145  ph.ts_sec = (int32)stime.inUnit(SIMTIME_S);
146  ph.ts_usec = (uint32)(stime.inUnit(SIMTIME_US) - (uint32)1000000 * stime.inUnit(SIMTIME_S));
147  // Write Ethernet header
148  uint32 hdr = 2; //AF_INET
149 
150  serializer::Buffer b(buf, sizeof(buf));
151  serializer::Context c;
152  c.throwOnSerializerNotFound = false;
153  serializer::IPv6Serializer().serializePacket(ipPacket, b, c);
154  int32 serialized_ip = b.getPos();
155 
156  if (serialized_ip > 0) {
157  ph.orig_len = serialized_ip + sizeof(uint32);
158 
159  ph.incl_len = ph.orig_len > snaplen ? snaplen : ph.orig_len;
160  fwrite(&ph, sizeof(ph), 1, dumpfile);
161  fwrite(&hdr, sizeof(uint32), 1, dumpfile);
162  fwrite(buf, ph.incl_len - sizeof(uint32), 1, dumpfile);
163  if (flush)
164  fflush(dumpfile);
165  }
166 #else // ifdef WITH_IPv6
167  throw cRuntimeError("Cannot write frame: INET compiled without IPv6 feature");
168 #endif // ifdef WITH_IPv6
169 }
uint8_t uint8
Definition: Compat.h:34
bool flush
Definition: PcapDump.h:43
unsigned int snaplen
Definition: PcapDump.h:42
#define MAXBUFLENGTH
Definition: PcapDump.cc:45
const value< double, compose< units::m, pow< units::s,-1 > > > c(299792458)
FILE * dumpfile
Definition: PcapDump.h:41
int32_t int32
Definition: Compat.h:31
uint32_t uint32
Definition: Compat.h:30
value< double, units::m > b
Definition: Units.h:1054

Member Data Documentation

FILE* inet::PcapDump::dumpfile = nullptr
protected
bool inet::PcapDump::flush = false
protected
unsigned int inet::PcapDump::snaplen = 0
protected

The documentation for this class was generated from the following files: