Skip to content

Commit

Permalink
PcapFilePacketConsumer: Added fileFormat parameter for choose pcap or…
Browse files Browse the repository at this point in the history
… pcapng file format.
  • Loading branch information
ZoltanBojthe committed Oct 19, 2023
1 parent de5856f commit 2b198df
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/inet/queueing/sink/PcapFilePacketConsumer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "inet/queueing/sink/PcapFilePacketConsumer.h"

#include "inet/common/ModuleAccess.h"
#include "inet/common/packet/recorder/PcapWriter.h"
#include "inet/common/packet/recorder/PcapngWriter.h"

namespace inet {
namespace queueing {
Expand All @@ -18,8 +20,15 @@ void PcapFilePacketConsumer::initialize(int stage)
{
PassivePacketSinkBase::initialize(stage);
if (stage == INITSTAGE_LOCAL) {
pcapWriter.setFlush(par("alwaysFlush"));
pcapWriter.open(par("filename"), par("snaplen"), par("timePrecision"));
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)
Expand All @@ -40,15 +49,15 @@ void PcapFilePacketConsumer::initialize(int stage)

void PcapFilePacketConsumer::finish()
{
pcapWriter.close();
pcapWriter->close();
}

void PcapFilePacketConsumer::pushPacket(Packet *packet, const cGate *gate)
{
Enter_Method("pushPacket");
take(packet);
emit(packetPushedSignal, packet);
pcapWriter.writePacket(simTime(), packet, direction, getContainingNicModule(this), networkType);
pcapWriter->writePacket(simTime(), packet, direction, getContainingNicModule(this), networkType);
numProcessedPackets++;
processedTotalLength += packet->getDataLength();
delete packet;
Expand Down
4 changes: 2 additions & 2 deletions src/inet/queueing/sink/PcapFilePacketConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef __INET_PCAPFILEPACKETCONSUMER_H
#define __INET_PCAPFILEPACKETCONSUMER_H

#include "inet/common/packet/recorder/PcapWriter.h"
#include "inet/common/packet/recorder/IPcapWriter.h"
#include "inet/queueing/base/PassivePacketSinkBase.h"
#include "inet/queueing/contract/IActivePacketSource.h"

Expand All @@ -18,7 +18,7 @@ namespace queueing {
class INET_API PcapFilePacketConsumer : public PassivePacketSinkBase
{
protected:
PcapWriter pcapWriter;
IPcapWriter *pcapWriter = nullptr;
Direction direction = DIRECTION_UNDEFINED;
PcapLinkType networkType = LINKTYPE_INVALID;

Expand Down
1 change: 1 addition & 0 deletions src/inet/queueing/sink/PcapFilePacketConsumer.ned
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import inet.queueing.contract.IPassivePacketSink;
simple PcapFilePacketConsumer extends PassivePacketSinkBase like IPassivePacketSink
{
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
Expand Down

0 comments on commit 2b198df

Please sign in to comment.