Skip to content

Commit

Permalink
queueing: Added new PacketFlowPcapFileRecorder module.
Browse files Browse the repository at this point in the history
  • Loading branch information
levy committed May 14, 2024
1 parent 9d13ba4 commit d542106
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/inet/queueing/flow/PacketFlowPcapFileRecorder.cc
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

39 changes: 39 additions & 0 deletions src/inet/queueing/flow/PacketFlowPcapFileRecorder.h
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

25 changes: 25 additions & 0 deletions src/inet/queueing/flow/PacketFlowPcapFileRecorder.ned
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);
}

0 comments on commit d542106

Please sign in to comment.