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

This class provides a generic filter for packets. More...

#include <PacketFilter.h>

Classes

class  DynamicExpressionResolver
 
class  PacketDissectorCallback
 

Public Member Functions

 PacketFilter ()
 
virtual ~PacketFilter ()
 
void setPattern (const char *pattern)
 
void setExpression (const char *expression)
 
void setExpression (cDynamicExpression *expression)
 
void setExpression (cOwnedDynamicExpression *expression)
 
void setExpression (cValueHolder *expression)
 
void setExpression (cObject *expression)
 
void setExpression (cValue &expression)
 
bool matches (const cPacket *packet) const
 

Protected Member Functions

const cObject * findPacketTag (const char *className) const
 

Protected Attributes

cDynamicExpression * filterExpression = nullptr
 
cMatchExpression * matchExpression = nullptr
 
PacketDissectorCallbackpacketDissectorCallback = nullptr
 
const cPacket * cpacket = nullptr
 
std::multimap< const Protocol *, Chunk * > protocolToChunkMap
 
std::multimap< std::string, Chunk * > classNameToChunkMap
 

Detailed Description

This class provides a generic filter for packets.

The filter is expressed as two patterns using the cMatchExpression format. One filter is applied to the Packet the other one is applied to each Chunk in the packet using the PacketDissector.

Constructor & Destructor Documentation

◆ PacketFilter()

inet::PacketFilter::PacketFilter ( )
17 {
18  packetDissectorCallback = new PacketDissectorCallback(this);
19 }

◆ ~PacketFilter()

inet::PacketFilter::~PacketFilter ( )
virtual
22 {
23  delete filterExpression;
24  delete matchExpression;
26 }

Member Function Documentation

◆ findPacketTag()

const cObject * inet::PacketFilter::findPacketTag ( const char *  className) const
protected
307 {
308  if (auto packet = dynamic_cast<const Packet *>(cpacket)) {
309  for (int i = 0; i < packet->getNumTags(); i++) {
310  const auto& tag = packet->getTag(i);
311  std::string tagClassName = tag->getClassName();
312  auto index = tagClassName.rfind("::");
313  if (index != std::string::npos)
314  tagClassName = tagClassName.substr(index + 2);
315  if (!strcmp(tagClassName.c_str(), className))
316  return tag.get();
317  }
318  return nullptr;
319  }
320  else
321  return nullptr;
322 }

◆ matches()

bool inet::PacketFilter::matches ( const cPacket *  packet) const
93 {
94  this->cpacket = cpacket;
95  protocolToChunkMap.clear();
96  classNameToChunkMap.clear();
97  if (matchExpression == nullptr && filterExpression == nullptr)
98  return true;
99  else {
100  bool result = true;
101  if (matchExpression != nullptr) {
102  cMatchableString matchableString(cpacket->getFullName());
103  result &= matchExpression->matches(&matchableString);
104  }
105  if (filterExpression != nullptr) {
106  if (auto packet = dynamic_cast<const Packet *>(cpacket)) {
107  PacketDissector packetDissector(ProtocolDissectorRegistry::globalRegistry, *packetDissectorCallback);
108  packetDissector.dissectPacket(const_cast<Packet *>(packet));
109  }
110  result &= filterExpression->evaluate().boolValue();
111  }
112  return result;
113  }
114 }

Referenced by inet::PacketEmitter::emitPacket(), inet::StreamIdentifier::processPacket(), and inet::PcapRecorder::recordPacket().

◆ setExpression() [1/6]

void inet::PacketFilter::setExpression ( cDynamicExpression *  expression)
50 {
51  delete filterExpression;
52  filterExpression = expression;
53  filterExpression->setResolver(new DynamicExpressionResolver(this));
54 }

◆ setExpression() [2/6]

void inet::PacketFilter::setExpression ( cObject *  expression)
73 {
74  if (auto ownedDynamicExpression = dynamic_cast<cOwnedDynamicExpression *>(expression))
75  setExpression(ownedDynamicExpression);
76  else if (auto dynamicExpression = dynamic_cast<cDynamicExpression *>(expression))
77  setExpression(dynamicExpression);
78  else if (auto valueHolder = dynamic_cast<cValueHolder *>(expression))
79  setExpression(valueHolder);
80  else
81  throw cRuntimeError("Unknown expression type");
82 }

◆ setExpression() [3/6]

void inet::PacketFilter::setExpression ( const char *  expression)
40 {
41  delete filterExpression;
42  filterExpression = nullptr;
43  if (strcmp(expression, "true")) {
44  filterExpression = new cDynamicExpression();
45  filterExpression->parse(expression, new DynamicExpressionResolver(this));
46  }
47 }

Referenced by inet::StreamDecoder::configureMappings(), inet::PacketEmitter::initialize(), inet::PcapRecorder::initialize(), inet::Ipv4NatTable::parseConfig(), inet::StreamIdentifier::processPacket(), and setExpression().

◆ setExpression() [4/6]

void inet::PacketFilter::setExpression ( cOwnedDynamicExpression *  expression)
57 {
58  delete filterExpression;
59  filterExpression = expression->dup();
60  filterExpression->setResolver(new DynamicExpressionResolver(this));
61 }

◆ setExpression() [5/6]

void inet::PacketFilter::setExpression ( cValue &  expression)
85 {
86  if (expression.getType() == cValue::POINTER)
87  setExpression(expression.objectValue());
88  else
89  setPattern(expression.stringValue());
90 }

◆ setExpression() [6/6]

void inet::PacketFilter::setExpression ( cValueHolder *  expression)
64 {
65  auto& value = expression->get();
66  if (value.getType() == cValue::POINTER)
67  setExpression(check_and_cast<cDynamicExpression *>(value.objectValue())->dup());
68  else
69  setPattern(value.stringValue());
70 }

◆ setPattern()

void inet::PacketFilter::setPattern ( const char *  pattern)
30 {
31  delete matchExpression;
32  matchExpression = nullptr;
33  if (strcmp(pattern, "*")) {
34  matchExpression = new cMatchExpression();
35  matchExpression->setPattern(pattern, false, true, true);
36  }
37 }

Referenced by setExpression().

Member Data Documentation

◆ classNameToChunkMap

◆ cpacket

const cPacket* inet::PacketFilter::cpacket = nullptr
mutableprotected

Referenced by findPacketTag(), and matches().

◆ filterExpression

cDynamicExpression* inet::PacketFilter::filterExpression = nullptr
protected

◆ matchExpression

cMatchExpression* inet::PacketFilter::matchExpression = nullptr
protected

Referenced by matches(), setPattern(), and ~PacketFilter().

◆ packetDissectorCallback

PacketDissectorCallback* inet::PacketFilter::packetDissectorCallback = nullptr
protected

◆ protocolToChunkMap


The documentation for this class was generated from the following files:
inet::PacketFilter::setExpression
void setExpression(const char *expression)
Definition: common/packet/PacketFilter.cc:39
inet::PacketFilter::filterExpression
cDynamicExpression * filterExpression
Definition: common/packet/PacketFilter.h:57
inet::PacketFilter::setPattern
void setPattern(const char *pattern)
Definition: common/packet/PacketFilter.cc:29
inet::PacketFilter::protocolToChunkMap
std::multimap< const Protocol *, Chunk * > protocolToChunkMap
Definition: common/packet/PacketFilter.h:61
inet::PacketFilter::matchExpression
cMatchExpression * matchExpression
Definition: common/packet/PacketFilter.h:58
inet::PacketFilter::packetDissectorCallback
PacketDissectorCallback * packetDissectorCallback
Definition: common/packet/PacketFilter.h:59
inet::PacketFilter::cpacket
const cPacket * cpacket
Definition: common/packet/PacketFilter.h:60
inet::PacketFilter::classNameToChunkMap
std::multimap< std::string, Chunk * > classNameToChunkMap
Definition: common/packet/PacketFilter.h:62
inet::ProtocolDissectorRegistry::globalRegistry
static ProtocolDissectorRegistry globalRegistry
Definition: ProtocolDissectorRegistry.h:21