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

Dumps every packet using the PcapDump and PacketDump classes. More...

#include <PcapRecorder.h>

Inheritance diagram for inet::PcapRecorder:

Public Member Functions

 PcapRecorder ()
 
 ~PcapRecorder ()
 

Protected Types

typedef std::map< simsignal_t, bool > SignalList
 

Protected Member Functions

virtual void initialize () override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void finish () override
 
virtual void receiveSignal (cComponent *source, simsignal_t signalID, cObject *obj, cObject *details) override
 
virtual void recordPacket (cPacket *msg, bool l2r)
 

Protected Attributes

SignalList signalList
 
PacketDump packetDumper
 
PcapDump pcapDumper
 
unsigned int snaplen = 0
 
bool dumpBadFrames = false
 

Detailed Description

Dumps every packet using the PcapDump and PacketDump classes.

Member Typedef Documentation

typedef std::map<simsignal_t, bool> inet::PcapRecorder::SignalList
protected

Constructor & Destructor Documentation

inet::PcapRecorder::PcapRecorder ( )
42  : cSimpleModule(), pcapDumper()
43 {
44 }
PcapDump pcapDumper
Definition: PcapRecorder.h:40
inet::PcapRecorder::~PcapRecorder ( )
39 {
40 }

Member Function Documentation

void inet::PcapRecorder::finish ( )
overrideprotectedvirtual
173 {
174  packetDumper.dump("", "pcapRecorder finished");
176 }
void closePcap()
Closes the output file if it is open.
Definition: PcapDump.cc:171
PacketDump packetDumper
Definition: PcapRecorder.h:39
PcapDump pcapDumper
Definition: PcapRecorder.h:40
void dump(const char *label, const char *msg)
Writes the given text on the output stream.
Definition: PacketDump.cc:355
void inet::PcapRecorder::handleMessage ( cMessage *  msg)
overrideprotectedvirtual
108 {
109  throw cRuntimeError("This module does not handle messages");
110 }
void inet::PcapRecorder::initialize ( )
overrideprotectedvirtual
47 {
48  const char *file = par("pcapFile");
49  snaplen = this->par("snaplen");
50  dumpBadFrames = par("dumpBadFrames").boolValue();
51  packetDumper.setVerbose(par("verbose").boolValue());
53  signalList.clear();
54 
55  {
56  cStringTokenizer signalTokenizer(par("sendingSignalNames"));
57 
58  while (signalTokenizer.hasMoreTokens())
59  signalList[registerSignal(signalTokenizer.nextToken())] = true;
60  }
61 
62  {
63  cStringTokenizer signalTokenizer(par("receivingSignalNames"));
64 
65  while (signalTokenizer.hasMoreTokens())
66  signalList[registerSignal(signalTokenizer.nextToken())] = false;
67  }
68 
69  const char *moduleNames = par("moduleNamePatterns");
70  cStringTokenizer moduleTokenizer(moduleNames);
71 
72  while (moduleTokenizer.hasMoreTokens()) {
73  bool found = false;
74  std::string mname(moduleTokenizer.nextToken());
75  bool isAllIndex = (mname.length() > 3) && mname.rfind("[*]") == mname.length() - 3;
76 
77  if (isAllIndex)
78  mname.replace(mname.length() - 3, 3, "");
79 
80  for (cModule::SubmoduleIterator i(getParentModule()); !i.end(); i++) {
81  cModule *submod = *i;
82  if (0 == strcmp(isAllIndex ? submod->getName() : submod->getFullName(), mname.c_str())) {
83  found = true;
84 
85  for (auto & elem : signalList) {
86  if (!submod->isSubscribed(elem.first, this)) {
87  submod->subscribe(elem.first, this);
88  EV << "PcapRecorder " << getFullPath() << " subscribed to "
89  << submod->getFullPath() << ":" << getSignalName(elem.first) << endl;
90  }
91  }
92  }
93  }
94 
95  if (!found) {
96  EV << "The module " << mname << (isAllIndex ? "[*]" : "")
97  << " not found for PcapRecorder " << getFullPath() << endl;
98  }
99  }
100 
101  if (*file) {
102  pcapDumper.openPcap(file, snaplen);
103  pcapDumper.setFlushParameter((bool)par("alwaysFlush"));
104  }
105 }
void openPcap(const char *filename, unsigned int snaplen)
Opens a PCAP file with the given file name.
Definition: PcapDump.cc:75
void setOutStream(std::ostream &o)
Sets the output stream.
Definition: PacketDump.h:62
void setVerbose(bool verb)
Enable/disable verbose output.
Definition: PacketDump.h:72
void setFlushParameter(bool doFlush)
Force flushing of pcap dump.
Definition: PcapDump.h:83
unsigned int snaplen
Definition: PcapRecorder.h:41
#define EVSTREAM
Definition: Compat.h:36
PacketDump packetDumper
Definition: PcapRecorder.h:39
PcapDump pcapDumper
Definition: PcapRecorder.h:40
bool dumpBadFrames
Definition: PcapRecorder.h:42
SignalList signalList
Definition: PcapRecorder.h:38
void inet::PcapRecorder::receiveSignal ( cComponent *  source,
simsignal_t  signalID,
cObject *  obj,
cObject *  details 
)
overrideprotectedvirtual
113 {
114  Enter_Method_Silent();
115  cPacket *packet = dynamic_cast<cPacket *>(obj);
116 
117  if (packet) {
118  SignalList::const_iterator i = signalList.find(signalID);
119  bool l2r = (i != signalList.end()) ? i->second : true;
120  recordPacket(packet, l2r);
121  }
122 }
virtual void recordPacket(cPacket *msg, bool l2r)
Definition: PcapRecorder.cc:124
SignalList signalList
Definition: PcapRecorder.h:38
void inet::PcapRecorder::recordPacket ( cPacket *  msg,
bool  l2r 
)
protectedvirtual

Referenced by receiveSignal().

125 {
126  EV << "PcapRecorder::recordPacket(" << msg->getFullPath() << ", " << l2r << ")\n";
127  packetDumper.dumpPacket(l2r, msg);
128 
129 #if defined(WITH_IPv4) || defined(WITH_IPv6)
130  if (!pcapDumper.isOpen())
131  return;
132 
133  bool hasBitError = false;
134 
135 #ifdef WITH_IPv4
136  IPv4Datagram *ip4Packet = nullptr;
137 #endif // ifdef WITH_IPv4
138 #ifdef WITH_IPv6
139  IPv6Datagram *ip6Packet = nullptr;
140 #endif // ifdef WITH_IPv6
141  while (msg) {
142  if (msg->hasBitError())
143  hasBitError = true;
144 #ifdef WITH_IPv4
145  if (nullptr != (ip4Packet = dynamic_cast<IPv4Datagram *>(msg))) {
146  break;
147  }
148 #endif // ifdef WITH_IPv4
149 #ifdef WITH_IPv6
150  if (nullptr != (ip6Packet = dynamic_cast<IPv6Datagram *>(msg))) {
151  break;
152  }
153 #endif // ifdef WITH_IPv6
154 
155  msg = msg->getEncapsulatedPacket();
156  }
157 #endif // if defined(WITH_IPv4) || defined(WITH_IPv6)
158 #ifdef WITH_IPv4
159  if (ip4Packet && (dumpBadFrames || !hasBitError)) {
160  const simtime_t stime = simTime();
161  pcapDumper.writeFrame(stime, ip4Packet);
162  }
163 #endif // ifdef WITH_IPv4
164 #ifdef WITH_IPv6
165  if (ip6Packet && (dumpBadFrames || !hasBitError)) {
166  const simtime_t stime = simTime();
167  pcapDumper.writeIPv6Frame(stime, ip6Packet);
168  }
169 #endif // ifdef WITH_IPv6
170 }
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...
Definition: PcapDump.cc:101
void dumpPacket(bool l2r, cPacket *packet)
Dumps info about the given packet.
Definition: PacketDump.cc:366
void writeIPv6Frame(simtime_t stime, const IPv6Datagram *ipPacket)
Definition: PcapDump.cc:135
PacketDump packetDumper
Definition: PcapRecorder.h:39
PcapDump pcapDumper
Definition: PcapRecorder.h:40
bool dumpBadFrames
Definition: PcapRecorder.h:42
bool isOpen() const
Returns true if the pcap file is currently open.
Definition: PcapDump.h:66

Member Data Documentation

bool inet::PcapRecorder::dumpBadFrames = false
protected

Referenced by initialize(), and recordPacket().

PacketDump inet::PcapRecorder::packetDumper
protected

Referenced by finish(), initialize(), and recordPacket().

PcapDump inet::PcapRecorder::pcapDumper
protected

Referenced by finish(), initialize(), and recordPacket().

SignalList inet::PcapRecorder::signalList
protected

Referenced by initialize(), and receiveSignal().

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

Referenced by initialize().


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