INET Framework for OMNeT++/OMNEST
inet::PcapWriter 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 <PcapWriter.h>

Inheritance diagram for inet::PcapWriter:
inet::IPcapWriter

Public Member Functions

 PcapWriter ()
 Constructor. More...
 
 ~PcapWriter ()
 Destructor. More...
 
void open (const char *filename, unsigned int snaplen) override
 Opens a PCAP file with the given file name. More...
 
bool isOpen () const override
 Returns true if the pcap file is currently open. More...
 
void writePacket (simtime_t time, const Packet *packet, Direction direction, NetworkInterface *ie, PcapLinkType linkType) override
 Records the given packet into the output file if it is open, and throws an exception otherwise. More...
 
void close () override
 Closes the output file if it is open. More...
 
void setFlush (bool flush) override
 Force flushing of pcap dump. More...
 
- Public Member Functions inherited from inet::IPcapWriter
virtual ~IPcapWriter ()
 

Protected Member Functions

void writeHeader (PcapLinkType linkType)
 

Protected Attributes

std::string fileName
 
FILE * dumpfile = nullptr
 
unsigned int snaplen = 0
 
PcapLinkType network = LINKTYPE_INVALID
 
bool flush = false
 
bool needHeader = true
 

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

◆ PcapWriter()

inet::PcapWriter::PcapWriter ( )
inline

Constructor.

It does not open the output file.

42 {}

◆ ~PcapWriter()

inet::PcapWriter::~PcapWriter ( )

Destructor.

It closes the output file if it is open.

46 {
47  PcapWriter::close(); // NOTE: admitting that this will not call overridden methods from the destructor
48 }

Member Function Documentation

◆ close()

void inet::PcapWriter::close ( )
overridevirtual

Closes the output file if it is open.

Implements inet::IPcapWriter.

125 {
126  if (dumpfile) {
127  fclose(dumpfile);
128  dumpfile = nullptr;
129  }
130 }

Referenced by inet::queueing::PcapFilePacketConsumer::finish(), and ~PcapWriter().

◆ isOpen()

bool inet::PcapWriter::isOpen ( ) const
inlineoverridevirtual

Returns true if the pcap file is currently open.

Implements inet::IPcapWriter.

59 { return dumpfile != nullptr; }

◆ open()

void inet::PcapWriter::open ( const char *  filename,
unsigned int  snaplen 
)
overridevirtual

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.

Implements inet::IPcapWriter.

51 {
52  if (opp_isempty(filename))
53  throw cRuntimeError("Cannot open pcap file: file name is empty");
54 
56  dumpfile = fopen(filename, "wb");
57  fileName = filename;
58 
59  if (!dumpfile)
60  throw cRuntimeError("Cannot open pcap file [%s] for writing: %s", filename, strerror(errno));
61 
62  snaplen = snaplen_par;
63  needHeader = true;
65 
66  flush = false;
67 }

Referenced by inet::queueing::PcapFilePacketConsumer::initialize().

◆ setFlush()

void inet::PcapWriter::setFlush ( bool  flush)
inlineoverridevirtual

Force flushing of pcap dump.

Implements inet::IPcapWriter.

75 { this->flush = flush; }

Referenced by inet::queueing::PcapFilePacketConsumer::initialize().

◆ writeHeader()

void inet::PcapWriter::writeHeader ( PcapLinkType  linkType)
protected
70 {
71  struct pcap_hdr fh;
72 
73  fh.magic = PCAP_MAGIC;
74  fh.version_major = 2;
75  fh.version_minor = 4;
76  fh.thiszone = 0;
77  fh.sigfigs = 0;
78  fh.snaplen = snaplen;
79  fh.network = linkType;
80  fwrite(&fh, sizeof(fh), 1, dumpfile);
81 }

Referenced by writePacket().

◆ writePacket()

void inet::PcapWriter::writePacket ( simtime_t  time,
const Packet packet,
Direction  direction,
NetworkInterface ie,
PcapLinkType  linkType 
)
overridevirtual

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

Implements inet::IPcapWriter.

84 {
85  if (!dumpfile)
86  throw cRuntimeError("Cannot write frame: pcap output file is not open");
87 
88  if (needHeader) {
89  if (linkTypePar == LINKTYPE_INVALID)
90  throw cRuntimeError("invalid linktype arrived");
91  writeHeader(linkTypePar);
92  network = linkTypePar;
93  needHeader = false;
94  }
95  else {
96  if (network != linkTypePar)
97  throw cRuntimeError("linktype mismatch error: required linktype = %d, arrived linktype = %d", network, linkTypePar);
98  }
99 
100  (void)direction; // unused
101  (void)ie; // unused
102 
103  EV_INFO << "Writing packet" << EV_FIELD(packet) << EV_FIELD(fileName) << EV_ENDL;
104  uint8_t buf[MAXBUFLENGTH];
105  memset(buf, 0, sizeof(buf));
106 
107  struct pcaprec_hdr ph;
108  ph.ts_sec = (int32_t)stime.inUnit(SIMTIME_S);
109  ph.ts_usec = (uint32_t)(stime.inUnit(SIMTIME_US) - (uint32_t)1000000 * stime.inUnit(SIMTIME_S));
110  auto data = packet->peekDataAsBytes();
111  auto bytes = data->getBytes();
112  for (size_t i = 0; i < bytes.size(); i++) {
113  buf[i] = bytes[i];
114  }
115  ph.orig_len = B(data->getChunkLength()).get();
116 
117  ph.incl_len = ph.orig_len > snaplen ? snaplen : ph.orig_len;
118  fwrite(&ph, sizeof(ph), 1, dumpfile);
119  fwrite(buf, ph.incl_len, 1, dumpfile);
120  if (flush)
121  fflush(dumpfile);
122 }

Referenced by inet::queueing::PcapFilePacketConsumer::pushPacket().

Member Data Documentation

◆ dumpfile

FILE* inet::PcapWriter::dumpfile = nullptr
protected

Referenced by close(), open(), writeHeader(), and writePacket().

◆ fileName

std::string inet::PcapWriter::fileName
protected

Referenced by open(), and writePacket().

◆ flush

bool inet::PcapWriter::flush = false
protected

Referenced by open(), and writePacket().

◆ needHeader

bool inet::PcapWriter::needHeader = true
protected

Referenced by open(), and writePacket().

◆ network

PcapLinkType inet::PcapWriter::network = LINKTYPE_INVALID
protected

Referenced by open(), and writePacket().

◆ snaplen

unsigned int inet::PcapWriter::snaplen = 0
protected

Referenced by open(), writeHeader(), and writePacket().


The documentation for this class was generated from the following files:
inet::PcapWriter::flush
bool flush
Definition: PcapWriter.h:32
inet::PcapWriter::fileName
std::string fileName
Definition: PcapWriter.h:28
inet::PcapWriter::dumpfile
FILE * dumpfile
Definition: PcapWriter.h:29
inet::LINKTYPE_INVALID
@ LINKTYPE_INVALID
Definition: IPcapWriter.h:18
inet::PcapWriter::network
PcapLinkType network
Definition: PcapWriter.h:31
inet::units::units::B
intscale< b, 1, 8 > B
Definition: Units.h:1168
EV_FIELD
#define EV_FIELD(...)
Definition: INETDefs.h:112
inet::PcapWriter::close
void close() override
Closes the output file if it is open.
Definition: PcapWriter.cc:124
inet::PcapWriter::needHeader
bool needHeader
Definition: PcapWriter.h:33
inet::PcapWriter::snaplen
unsigned int snaplen
Definition: PcapWriter.h:30
MAXBUFLENGTH
#define MAXBUFLENGTH
Definition: PcapWriter.cc:20
inet::PcapWriter::writeHeader
void writeHeader(PcapLinkType linkType)
Definition: PcapWriter.cc:69
EV_ENDL
#define EV_ENDL
Definition: INETDefs.h:114
inet::utils::makePathForFile
void makePathForFile(const char *filename)
Definition: INETUtils.cc:210
PCAP_MAGIC
#define PCAP_MAGIC
Definition: PcapWriter.cc:22