-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
queueing: Added new PacketFlowPcapFileRecorder module.
- Loading branch information
Showing
3 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// Copyright (C) 2024 OpenSim Ltd. | ||
// | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
// | ||
|
||
|
||
#include "inet/queueing/flow/PacketFlowPcapFileRecorder.h" | ||
|
||
#include "inet/common/packet/recorder/PcapWriter.h" | ||
#include "inet/common/packet/recorder/PcapngWriter.h" | ||
|
||
namespace inet { | ||
namespace queueing { | ||
|
||
Define_Module(PacketFlowPcapFileRecorder); | ||
|
||
void PacketFlowPcapFileRecorder::initialize(int stage) | ||
{ | ||
PacketFlowBase::initialize(stage); | ||
if (stage == INITSTAGE_LOCAL) { | ||
const char *fileFormat = par("fileFormat"); | ||
if (!strcmp(fileFormat, "pcap")) | ||
pcapWriter = new PcapWriter(); | ||
else if (!strcmp(fileFormat, "pcapng")) | ||
pcapWriter = new PcapngWriter(); | ||
else | ||
throw cRuntimeError("Unknown fileFormat parameter"); | ||
pcapWriter->setFlush(par("alwaysFlush")); | ||
pcapWriter->open(par("filename"), par("snaplen"), par("timePrecision")); | ||
networkType = static_cast<PcapLinkType>(par("networkType").intValue()); | ||
const char *dirString = par("direction"); | ||
if (*dirString == 0) | ||
direction = DIRECTION_UNDEFINED; | ||
else if (!strcmp(dirString, "outbound")) | ||
direction = DIRECTION_OUTBOUND; | ||
else if (!strcmp(dirString, "inbound")) | ||
direction = DIRECTION_INBOUND; | ||
else | ||
throw cRuntimeError("invalid direction parameter value: %s", dirString); | ||
} | ||
} | ||
|
||
cGate *PacketFlowPcapFileRecorder::getRegistrationForwardingGate(cGate *gate) | ||
{ | ||
if (gate == outputGate) | ||
return inputGate; | ||
else if (gate == inputGate) | ||
return outputGate; | ||
else | ||
throw cRuntimeError("Unknown gate"); | ||
} | ||
|
||
void PacketFlowPcapFileRecorder::finish() | ||
{ | ||
pcapWriter->close(); | ||
} | ||
|
||
void PacketFlowPcapFileRecorder::processPacket(Packet *packet) | ||
{ | ||
pcapWriter->writePacket(simTime(), packet, direction, findContainingNicModule(this), networkType); | ||
} | ||
|
||
} // namespace queueing | ||
} // namespace inet | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// Copyright (C) 2024 OpenSim Ltd. | ||
// | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
// | ||
|
||
|
||
#ifndef __INET_PACKETFLOWPCAPFILERECORDER_H | ||
#define __INET_PACKETFLOWPCAPFILERECORDER_H | ||
|
||
#include "inet/common/IProtocolRegistrationListener.h" | ||
#include "inet/common/packet/recorder/IPcapWriter.h" | ||
#include "inet/queueing/base/PacketFlowBase.h" | ||
|
||
namespace inet { | ||
namespace queueing { | ||
|
||
class INET_API PacketFlowPcapFileRecorder : public PacketFlowBase, public TransparentProtocolRegistrationListener | ||
{ | ||
protected: | ||
IPcapWriter *pcapWriter = nullptr; | ||
Direction direction = DIRECTION_UNDEFINED; | ||
PcapLinkType networkType = LINKTYPE_INVALID; | ||
|
||
protected: | ||
virtual void initialize(int stage) override; | ||
virtual void finish() override; | ||
|
||
virtual cGate *getRegistrationForwardingGate(cGate *gate) override; | ||
|
||
public: | ||
virtual void processPacket(Packet *packet) override; | ||
}; | ||
|
||
} // namespace queueing | ||
} // namespace inet | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// Copyright (C) 2024 OpenSim Ltd. | ||
// | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
// | ||
|
||
|
||
package inet.queueing.flow; | ||
|
||
import inet.queueing.base.PacketFlowBase; | ||
import inet.queueing.contract.IPacketFlow; | ||
|
||
simple PacketFlowPcapFileRecorder extends PacketFlowBase like IPacketFlow | ||
{ | ||
parameters: | ||
string fileFormat @enum("pcap","pcapng") = default("pcap"); | ||
string filename; // the PCAP file to be written | ||
int networkType; // the network type header field in the PCAP file, see http://www.tcpdump.org/linktypes.html (1=ethernet, 204=ppp, 105=IEEE 802.11, ...) | ||
int snaplen = default(65535); // maximum number of bytes to record per packet | ||
int timePrecision = default(6); // Time precison in recorded file. pcap supports only 6 (usec) or 9 (nanosec), pcapng supports more values (see 'if_tsresol' option in pcapng file format). | ||
string direction @enum(inbound, outbound); // direction flag | ||
bool alwaysFlush = default(false); // flush the PCAP file after each write to ensure that all packets are captured in case of a crash | ||
@class(PacketFlowPcapFileRecorder); | ||
} | ||
|