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

Absolute dropper. More...

#include <MultiFieldClassifier.h>

Inheritance diagram for inet::MultiFieldClassifier:

Classes

struct  Filter
 

Public Member Functions

 MultiFieldClassifier ()
 

Protected Member Functions

void addFilter (const Filter &filter)
 
void configureFilters (cXMLElement *config)
 
virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void refreshDisplay () const override
 
virtual int classifyPacket (cPacket *packet)
 

Protected Attributes

int numOutGates = 0
 
std::vector< Filterfilters
 
int numRcvd = 0
 

Static Protected Attributes

static simsignal_t pkClassSignal = registerSignal("pkClass")
 

Detailed Description

Absolute dropper.

Constructor & Destructor Documentation

inet::MultiFieldClassifier::MultiFieldClassifier ( )
inline
70 {}

Member Function Documentation

void inet::MultiFieldClassifier::addFilter ( const Filter filter)
protected
197 {
198  if (filter.gateIndex < 0 || filter.gateIndex >= numOutGates)
199  throw cRuntimeError("no output gate for gate index %d", filter.gateIndex);
200  if (!filter.srcAddr.isUnspecified() && ((filter.srcAddr.getType() == L3Address::IPv6 && filter.srcPrefixLength > 128) ||
201  (filter.srcAddr.getType() == L3Address::IPv4 && filter.srcPrefixLength > 32)))
202  throw cRuntimeError("srcPrefixLength is invalid");
203  if (!filter.destAddr.isUnspecified() && ((filter.destAddr.getType() == L3Address::IPv6 && filter.destPrefixLength > 128) ||
204  (filter.destAddr.getType() == L3Address::IPv4 && filter.destPrefixLength > 32)))
205  throw cRuntimeError("srcPrefixLength is invalid");
206  if (filter.protocol != -1 && (filter.protocol < 0 || filter.protocol > 0xff))
207  throw cRuntimeError("protocol is not a valid protocol number");
208  if (filter.tos != -1 && (filter.tos < 0 || filter.tos > 0xff))
209  throw cRuntimeError("tos is not valid");
210  if (filter.tosMask < 0 || filter.tosMask > 0xff)
211  throw cRuntimeError("tosMask is not valid");
212  if (filter.srcPortMin != -1 && (filter.srcPortMin < 0 || filter.srcPortMin > 0xffff))
213  throw cRuntimeError("srcPortMin is not a valid port number");
214  if (filter.srcPortMax != -1 && (filter.srcPortMax < 0 || filter.srcPortMax > 0xffff))
215  throw cRuntimeError("srcPortMax is not a valid port number");
216  if (filter.srcPortMin != -1 && filter.srcPortMin > filter.srcPortMax)
217  throw cRuntimeError("srcPortMin > srcPortMax");
218  if (filter.destPortMin != -1 && (filter.destPortMin < 0 || filter.destPortMin > 0xffff))
219  throw cRuntimeError("destPortMin is not a valid port number");
220  if (filter.destPortMax != -1 && (filter.destPortMax < 0 || filter.destPortMax > 0xffff))
221  throw cRuntimeError("destPortMax is not a valid port number");
222  if (filter.destPortMin != -1 && filter.destPortMin > filter.destPortMax)
223  throw cRuntimeError("destPortMin > destPortMax");
224 
225  filters.push_back(filter);
226 }
int numOutGates
Definition: MultiFieldClassifier.h:58
Definition: L3Address.h:47
std::vector< Filter > filters
Definition: MultiFieldClassifier.h:59
Definition: L3Address.h:46
int inet::MultiFieldClassifier::classifyPacket ( cPacket *  packet)
protectedvirtual
169 {
170  for ( ; packet; packet = packet->getEncapsulatedPacket()) {
171 #ifdef WITH_IPv4
172  IPv4Datagram *ipv4Datagram = dynamic_cast<IPv4Datagram *>(packet);
173  if (ipv4Datagram) {
174  for (auto & elem : filters)
175  if (elem.matches(ipv4Datagram))
176  return elem.gateIndex;
177 
178  return -1;
179  }
180 #endif // ifdef WITH_IPv4
181 #ifdef WITH_IPv6
182  IPv6Datagram *ipv6Datagram = dynamic_cast<IPv6Datagram *>(packet);
183  if (ipv6Datagram) {
184  for (auto & elem : filters)
185  if (elem.matches(ipv6Datagram))
186  return elem.gateIndex;
187 
188  return -1;
189  }
190 #endif // ifdef WITH_IPv6
191  }
192 
193  return -1;
194 }
std::vector< Filter > filters
Definition: MultiFieldClassifier.h:59
void inet::MultiFieldClassifier::configureFilters ( cXMLElement *  config)
protected
229 {
230  L3AddressResolver addressResolver;
231  cXMLElementList filterElements = config->getChildrenByTagName("filter");
232  for (auto & filterElements_i : filterElements) {
233  cXMLElement *filterElement = filterElements_i;
234  try {
235  const char *gateAttr = getRequiredAttribute(filterElement, "gate");
236  const char *srcAddrAttr = filterElement->getAttribute("srcAddress");
237  const char *srcPrefixLengthAttr = filterElement->getAttribute("srcPrefixLength");
238  const char *destAddrAttr = filterElement->getAttribute("destAddress");
239  const char *destPrefixLengthAttr = filterElement->getAttribute("destPrefixLength");
240  const char *protocolAttr = filterElement->getAttribute("protocol");
241  const char *tosAttr = filterElement->getAttribute("tos");
242  const char *tosMaskAttr = filterElement->getAttribute("tosMask");
243  const char *srcPortAttr = filterElement->getAttribute("srcPort");
244  const char *srcPortMinAttr = filterElement->getAttribute("srcPortMin");
245  const char *srcPortMaxAttr = filterElement->getAttribute("srcPortMax");
246  const char *destPortAttr = filterElement->getAttribute("destPort");
247  const char *destPortMinAttr = filterElement->getAttribute("destPortMin");
248  const char *destPortMaxAttr = filterElement->getAttribute("destPortMax");
249 
250  Filter filter;
251  filter.gateIndex = parseIntAttribute(gateAttr, "gate");
252  if (srcAddrAttr)
253  filter.srcAddr = addressResolver.resolve(srcAddrAttr);
254  if (srcPrefixLengthAttr)
255  filter.srcPrefixLength = parseIntAttribute(srcPrefixLengthAttr, "srcPrefixLength");
256  else if (srcAddrAttr)
257  filter.srcPrefixLength = filter.srcAddr.getType() == L3Address::IPv6 ? 128 : 32;
258  if (destAddrAttr)
259  filter.destAddr = addressResolver.resolve(destAddrAttr);
260  if (destPrefixLengthAttr)
261  filter.destPrefixLength = parseIntAttribute(destPrefixLengthAttr, "destPrefixLength");
262  else if (destAddrAttr)
263  filter.destPrefixLength = filter.destAddr.getType() == L3Address::IPv6 ? 128 : 32;
264  if (protocolAttr)
265  filter.protocol = parseProtocol(protocolAttr, "protocol");
266  if (tosAttr)
267  filter.tos = parseIntAttribute(tosAttr, "tos");
268  if (tosMaskAttr)
269  filter.tosMask = parseIntAttribute(tosMaskAttr, "tosMask");
270  if (srcPortAttr)
271  filter.srcPortMin = filter.srcPortMax = parseIntAttribute(srcPortAttr, "srcPort");
272  if (srcPortMinAttr)
273  filter.srcPortMin = parseIntAttribute(srcPortMinAttr, "srcPortMin");
274  if (srcPortMaxAttr)
275  filter.srcPortMax = parseIntAttribute(srcPortMaxAttr, "srcPortMax");
276  if (destPortAttr)
277  filter.destPortMin = filter.destPortMax = parseIntAttribute(destPortAttr, "destPort");
278  if (destPortMinAttr)
279  filter.destPortMin = parseIntAttribute(destPortMinAttr, "destPortMin");
280  if (destPortMaxAttr)
281  filter.destPortMax = parseIntAttribute(destPortMaxAttr, "destPortMax");
282 
283  addFilter(filter);
284  }
285  catch (std::exception& e) {
286  throw cRuntimeError("Error in XML <filter> element at %s: %s", filterElement->getSourceLocation(), e.what());
287  }
288  }
289 }
Definition: L3Address.h:47
void addFilter(const Filter &filter)
Definition: MultiFieldClassifier.cc:196
const char * getRequiredAttribute(const cXMLElement &node, const char *attr)
Definition: XMLUtils.cc:150
const value< double, units::C > e(1.602176487e-19)
int parseProtocol(const char *attrValue, const char *attrName)
Parses an IP protocol number.
Definition: DiffservUtil.cc:103
int parseIntAttribute(const char *attrValue, const char *attrName, bool isOptional)
Parses an integer attribute.
Definition: DiffservUtil.cc:78
void inet::MultiFieldClassifier::handleMessage ( cMessage *  msg)
overrideprotectedvirtual
147 {
148  cPacket *packet = check_and_cast<cPacket *>(msg);
149 
150  numRcvd++;
151  int gateIndex = classifyPacket(packet);
152  emit(pkClassSignal, gateIndex);
153 
154  if (gateIndex >= 0)
155  send(packet, "outs", gateIndex);
156  else
157  send(packet, "defaultOut");
158 }
static simsignal_t pkClassSignal
Definition: MultiFieldClassifier.h:63
virtual int classifyPacket(cPacket *packet)
Definition: MultiFieldClassifier.cc:168
int numRcvd
Definition: MultiFieldClassifier.h:61
void inet::MultiFieldClassifier::initialize ( int  stage)
overrideprotectedvirtual
131 {
132  cSimpleModule::initialize(stage);
133 
134  if (stage == INITSTAGE_LOCAL) {
135  numOutGates = gateSize("outs");
136 
137  numRcvd = 0;
138  WATCH(numRcvd);
139  }
140  else if (stage == INITSTAGE_NETWORK_LAYER_3) {
141  cXMLElement *config = par("filters").xmlValue();
142  configureFilters(config);
143  }
144 }
int numOutGates
Definition: MultiFieldClassifier.h:58
Initialization of network-layer protocols, stage 3.
Definition: InitStages.h:84
Local initializations.
Definition: InitStages.h:35
void configureFilters(cXMLElement *config)
Definition: MultiFieldClassifier.cc:228
int numRcvd
Definition: MultiFieldClassifier.h:61
virtual int inet::MultiFieldClassifier::numInitStages ( ) const
inlineoverrideprotectedvirtual
73 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::MultiFieldClassifier::refreshDisplay ( ) const
overrideprotectedvirtual
161 {
162  char buf[20] = "";
163  if (numRcvd > 0)
164  sprintf(buf + strlen(buf), "rcvd:%d ", numRcvd);
165  getDisplayString().setTagArg("t", 0, buf);
166 }
int numRcvd
Definition: MultiFieldClassifier.h:61

Member Data Documentation

std::vector<Filter> inet::MultiFieldClassifier::filters
protected
int inet::MultiFieldClassifier::numOutGates = 0
protected
int inet::MultiFieldClassifier::numRcvd = 0
protected
simsignal_t inet::MultiFieldClassifier::pkClassSignal = registerSignal("pkClass")
staticprotected

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