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

An example packet classifier based on the UDP/TCP port number. More...

#include <ExampleQosClassifier.h>

Inheritance diagram for inet::ExampleQosClassifier:
inet::DefaultProtocolRegistrationListener inet::IProtocolRegistrationListener

Public Member Functions

 ExampleQosClassifier ()
 
virtual void initialize () override
 
virtual void handleMessage (cMessage *msg) override
 
- Public Member Functions inherited from inet::DefaultProtocolRegistrationListener
virtual void handleRegisterServiceGroup (const ProtocolGroup &protocolGroup, cGate *gate, ServicePrimitive servicePrimitive) override
 
virtual void handleRegisterProtocolGroup (const ProtocolGroup &protocolGroup, cGate *gate, ServicePrimitive servicePrimitive) override
 
virtual void handleRegisterAnyService (cGate *gate, ServicePrimitive servicePrimitive) override
 
virtual void handleRegisterAnyProtocol (cGate *gate, ServicePrimitive servicePrimitive) override
 

Protected Member Functions

virtual int getUserPriority (cMessage *msg)
 
virtual void handleRegisterService (const Protocol &protocol, cGate *gate, ServicePrimitive servicePrimitive) override
 
virtual void handleRegisterProtocol (const Protocol &protocol, cGate *gate, ServicePrimitive servicePrimitive) override
 

Detailed Description

An example packet classifier based on the UDP/TCP port number.

Constructor & Destructor Documentation

◆ ExampleQosClassifier()

inet::ExampleQosClassifier::ExampleQosClassifier ( )
inline
25 {}

Member Function Documentation

◆ getUserPriority()

int inet::ExampleQosClassifier::getUserPriority ( cMessage *  msg)
protectedvirtual
53 {
54  int ipProtocol = -1;
55 
56 #if defined(INET_WITH_ETHERNET) || defined(INET_WITH_IPv4) || defined(INET_WITH_IPv6) || defined(INET_WITH_UDP) || defined(INET_WITH_TCP_COMMON)
57  auto packet = check_and_cast<Packet *>(msg);
58  auto packetProtocol = packet->getTag<PacketProtocolTag>()->getProtocol();
59  int ethernetMacProtocol = -1;
60  b ethernetMacHeaderLength = b(0);
61  b ipHeaderLength = b(-1);
62 #endif
63 
64 #ifdef INET_WITH_ETHERNET
65  if (packetProtocol == &Protocol::ethernetMac) {
66  const auto& ethernetMacHeader = packet->peekAtFront<EthernetMacHeader>();
67  ethernetMacProtocol = ethernetMacHeader->getTypeOrLength();
68  ethernetMacHeaderLength = ethernetMacHeader->getChunkLength();
69  }
70 #endif
71 
72 #ifdef INET_WITH_IPv4
73  if (ethernetMacProtocol == ETHERTYPE_IPv4 || packetProtocol == &Protocol::ipv4) {
74  const auto& ipv4Header = packet->peekDataAt<Ipv4Header>(ethernetMacHeaderLength);
75  if (ipv4Header->getProtocolId() == IP_PROT_ICMP)
76  return UP_BE; // ICMP class
77  ipProtocol = ipv4Header->getProtocolId();
78  ipHeaderLength = ipv4Header->getChunkLength();
79  }
80 #endif
81 
82 #ifdef INET_WITH_IPv6
83  if (ethernetMacProtocol == ETHERTYPE_IPv6 || packetProtocol == &Protocol::ipv6) {
84  const auto& ipv6Header = packet->peekDataAt<Ipv6Header>(ethernetMacHeaderLength);
85  if (ipv6Header->getProtocolId() == IP_PROT_IPv6_ICMP)
86  return UP_BE; // ICMPv6 class
87  ipProtocol = ipv6Header->getProtocolId();
88  ipHeaderLength = ipv6Header->getChunkLength();
89  }
90 #endif
91 
92  if (ipProtocol == -1)
93  return UP_BE;
94 
95 #ifdef INET_WITH_UDP
96  if (ipProtocol == IP_PROT_UDP) {
97  const auto& udpHeader = packet->peekDataAt<UdpHeader>(ethernetMacHeaderLength + ipHeaderLength);
98  unsigned int srcPort = udpHeader->getSourcePort();
99  unsigned int destPort = udpHeader->getDestinationPort();
100  if (destPort == 21 || srcPort == 21)
101  return UP_BK;
102  if (destPort == 80 || srcPort == 80)
103  return UP_BE;
104  if (destPort == 4000 || srcPort == 4000)
105  return UP_VI;
106  if (destPort == 5000 || srcPort == 5000)
107  return UP_VO;
108  if (destPort == 6000 || srcPort == 6000) // not classified
109  return -1;
110  }
111 #endif
112 
113 #ifdef INET_WITH_TCP_COMMON
114  if (ipProtocol == IP_PROT_TCP) {
115  const auto& tcpHeader = packet->peekDataAt<tcp::TcpHeader>(ethernetMacHeaderLength + ipHeaderLength);
116  unsigned int srcPort = tcpHeader->getSourcePort();
117  unsigned int destPort = tcpHeader->getDestinationPort();
118  if (destPort == 21 || srcPort == 21)
119  return UP_BK;
120  if (destPort == 80 || srcPort == 80)
121  return UP_BE;
122  if (destPort == 4000 || srcPort == 4000)
123  return UP_VI;
124  if (destPort == 5000 || srcPort == 5000)
125  return UP_VO;
126  if (destPort == 6000 || srcPort == 6000) // not classified
127  return -1;
128  }
129 #endif
130 
131  return UP_BE;
132 }

Referenced by handleMessage().

◆ handleMessage()

void inet::ExampleQosClassifier::handleMessage ( cMessage *  msg)
overridevirtual
46 {
47  auto packet = check_and_cast<Packet *>(msg);
48  packet->addTagIfAbsent<UserPriorityReq>()->setUserPriority(getUserPriority(msg));
49  send(msg, "out");
50 }

◆ handleRegisterProtocol()

void inet::ExampleQosClassifier::handleRegisterProtocol ( const Protocol protocol,
cGate *  gate,
ServicePrimitive  servicePrimitive 
)
overrideprotectedvirtual

Reimplemented from inet::DefaultProtocolRegistrationListener.

146 {
147  Enter_Method("handleRegisterProtocol");
148  if (!strcmp("in", g->getName()))
149  registerProtocol(protocol, gate("out"), servicePrimitive);
150  else
151  throw cRuntimeError("Unknown gate: %s", g->getName());
152 }

◆ handleRegisterService()

void inet::ExampleQosClassifier::handleRegisterService ( const Protocol protocol,
cGate *  gate,
ServicePrimitive  servicePrimitive 
)
overrideprotectedvirtual

Reimplemented from inet::DefaultProtocolRegistrationListener.

135 {
136  Enter_Method("handleRegisterService");
137  if (!strcmp("in", g->getName()))
138  registerService(protocol, gate("out"), servicePrimitive);
139  else if (!strcmp("out", g->getName()))
140  registerService(protocol, gate("in"), servicePrimitive);
141  else
142  throw cRuntimeError("Unknown gate: %s", g->getName());
143 }

◆ initialize()

void inet::ExampleQosClassifier::initialize ( )
overridevirtual
41 {
42  // TODO parameters
43 }

The documentation for this class was generated from the following files:
protocol
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd DispatchProtocolReq L4PortInd Ipv4ControlInfo Ipv6ControlInfo down protocol
Definition: IUdp-gates.txt:25
inet::Protocol::ipv4
static const Protocol ipv4
Definition: Protocol.h:93
inet::IP_PROT_ICMP
@ IP_PROT_ICMP
Definition: IpProtocolId_m.h:91
inet::UP_BE
@ UP_BE
Definition: UserPriority.h:21
inet::Protocol::ipv6
static const Protocol ipv6
Definition: Protocol.h:94
inet::UP_BK
@ UP_BK
Definition: UserPriority.h:19
inet::IP_PROT_IPv6_ICMP
@ IP_PROT_IPv6_ICMP
Definition: IpProtocolId_m.h:101
inet::registerService
void registerService(const Protocol &protocol, cGate *gate, ServicePrimitive servicePrimitive)
Registers a service primitive (SDU processing) at the given gate.
Definition: IProtocolRegistrationListener.cc:14
PacketProtocolTag
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd DispatchProtocolReq L4PortInd Ipv4ControlInfo Ipv6ControlInfo down PacketProtocolTag
Definition: IUdp-gates.txt:25
inet::units::units::g
milli< kg >::type g
Definition: Units.h:1071
inet::IP_PROT_UDP
@ IP_PROT_UDP
Definition: IpProtocolId_m.h:97
inet::Protocol::ethernetMac
static const Protocol ethernetMac
Definition: Protocol.h:65
inet::units::values::b
value< int64_t, units::b > b
Definition: Units.h:1241
inet::UP_VI
@ UP_VI
Definition: UserPriority.h:24
inet::IP_PROT_TCP
@ IP_PROT_TCP
Definition: IpProtocolId_m.h:94
Enter_Method
#define Enter_Method(...)
Definition: SelfDoc.h:71
inet::ExampleQosClassifier::getUserPriority
virtual int getUserPriority(cMessage *msg)
Definition: ExampleQosClassifier.cc:52
inet::ETHERTYPE_IPv4
@ ETHERTYPE_IPv4
Definition: EtherType_m.h:77
inet::registerProtocol
void registerProtocol(const Protocol &protocol, cGate *gate, ServicePrimitive servicePrimitive)
Registers a protocol primitive (PDU processing) at the given gate.
Definition: IProtocolRegistrationListener.cc:83
inet::UP_VO
@ UP_VO
Definition: UserPriority.h:25
inet::ETHERTYPE_IPv6
@ ETHERTYPE_IPv6
Definition: EtherType_m.h:87