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

Implements a generic network protocol that routes generic datagrams through the network. More...

#include <GenericNetworkProtocol.h>

Inheritance diagram for inet::GenericNetworkProtocol:
inet::QueueBase inet::INetfilter inet::INetworkProtocol inet::AbstractQueue

Classes

struct  QueuedDatagramForHook
 Represents an GenericDatagram, queued by a Hook. More...
 

Public Member Functions

 GenericNetworkProtocol ()
 
 ~GenericNetworkProtocol ()
 
virtual void registerHook (int priority, IHook *hook) override
 Adds the provided hook to the list of registered hooks that will be called by the network layer when it processes datagrams. More...
 
virtual void unregisterHook (int priority, IHook *hook) override
 Removes the provided hook from the list of registered hooks. More...
 
virtual void dropQueuedDatagram (const INetworkDatagram *datagram) override
 Requests the network layer to drop the datagram, because it's no longer needed. More...
 
virtual void reinjectQueuedDatagram (const INetworkDatagram *datagram) override
 Requests the network layer to restart the processing of the datagram. More...
 
- Public Member Functions inherited from inet::QueueBase
 QueueBase ()
 
- Public Member Functions inherited from inet::AbstractQueue
 AbstractQueue ()
 
virtual ~AbstractQueue ()
 
- Public Member Functions inherited from inet::INetfilter
virtual ~INetfilter ()
 
- Public Member Functions inherited from inet::INetworkProtocol
virtual ~INetworkProtocol ()
 

Protected Types

typedef std::multimap< int, IHook * > HookList
 
typedef std::list< QueuedDatagramForHookDatagramQueueForHooks
 

Protected Member Functions

virtual const InterfaceEntrygetSourceInterfaceFrom (cPacket *packet)
 
virtual void refreshDisplay () const override
 
virtual void handlePacketFromNetwork (GenericDatagram *datagram)
 Handle GenericDatagram messages arriving from lower layer. More...
 
virtual void handleMessageFromHL (cPacket *packet)
 Handle messages (typically packets to be send in Generic) from transport or ICMP. More...
 
virtual void routePacket (GenericDatagram *datagram, const InterfaceEntry *destIE, const L3Address &nextHop, bool fromHL)
 Performs routing. More...
 
virtual void routeMulticastPacket (GenericDatagram *datagram, const InterfaceEntry *destIE, const InterfaceEntry *fromIE)
 Forwards packets to all multicast destinations, using sendDatagramToOutput(). More...
 
virtual GenericDatagramencapsulate (cPacket *transportPacket, const InterfaceEntry *&destIE)
 Encapsulate packet coming from higher layers into GenericDatagram, using the control info attached to the packet. More...
 
virtual cPacket * decapsulate (GenericDatagram *datagram)
 Decapsulate and return encapsulated packet after attaching GenericNetworkProtocolControlInfo. More...
 
virtual void sendDatagramToHL (GenericDatagram *datagram)
 Send datagrams up to the higher layers. More...
 
virtual void sendDatagramToOutput (GenericDatagram *datagram, const InterfaceEntry *ie, L3Address nextHop)
 Last TTL check, then send datagram on the given interface. More...
 
virtual void datagramPreRouting (GenericDatagram *datagram, const InterfaceEntry *inIE, const InterfaceEntry *destIE, const L3Address &nextHop)
 
virtual void datagramLocalIn (GenericDatagram *datagram, const InterfaceEntry *inIE)
 
virtual void datagramLocalOut (GenericDatagram *datagram, const InterfaceEntry *destIE, const L3Address &nextHop)
 
virtual IHook::Result datagramPreRoutingHook (GenericDatagram *datagram, const InterfaceEntry *inIE, const InterfaceEntry *&outIE, L3Address &nextHop)
 
virtual IHook::Result datagramForwardHook (GenericDatagram *datagram, const InterfaceEntry *inIE, const InterfaceEntry *&outIE, L3Address &nextHop)
 
virtual IHook::Result datagramPostRoutingHook (GenericDatagram *datagram, const InterfaceEntry *inIE, const InterfaceEntry *&outIE, L3Address &nextHop)
 
virtual IHook::Result datagramLocalInHook (GenericDatagram *datagram, const InterfaceEntry *inIE)
 
virtual IHook::Result datagramLocalOutHook (GenericDatagram *datagram, const InterfaceEntry *&outIE, L3Address &nextHop)
 
virtual void initialize () override
 Initialization. More...
 
virtual void handleMessage (cMessage *message) override
 Handle message. More...
 
virtual void endService (cPacket *packet) override
 Processing of generic datagrams. More...
 
- Protected Member Functions inherited from inet::QueueBase
virtual void arrival (cPacket *msg) override
 Functions to (re)define behaviour. More...
 
virtual cPacket * arrivalWhenIdle (cPacket *msg) override
 Called when a message arrives at the module when the queue is empty. More...
 
virtual simtime_t startService (cPacket *msg) override
 Called when a message starts service, and should return the service time. More...
 
- Protected Member Functions inherited from inet::AbstractQueue
virtual cPacket * cancelService ()
 If a message is under service, aborts its service and returns the message. More...
 

Protected Attributes

IInterfaceTableinterfaceTable
 
GenericRoutingTableroutingTable
 
IARParp
 
int queueOutBaseGateId
 
int defaultHopLimit
 
ProtocolMapping mapping
 
HookList hooks
 
DatagramQueueForHooks queuedDatagramsForHooks
 
int numLocalDeliver
 
int numDropped
 
int numUnroutable
 
int numForwarded
 
- Protected Attributes inherited from inet::QueueBase
simtime_t delay
 
- Protected Attributes inherited from inet::AbstractQueue
cPacketQueue queue
 The queue. More...
 

Detailed Description

Implements a generic network protocol that routes generic datagrams through the network.

Routing decisions are based on a generic routing table, but it also supports the netfilter interface to allow routing protocols to kick in. It doesn't provide datagram fragmentation and reassembling.

Member Typedef Documentation

typedef std::multimap<int, IHook *> inet::GenericNetworkProtocol::HookList
protected

Constructor & Destructor Documentation

inet::GenericNetworkProtocol::GenericNetworkProtocol ( )
34  :
35  interfaceTable(nullptr),
36  routingTable(nullptr),
37  arp(nullptr),
39  defaultHopLimit(-1),
40  numLocalDeliver(0),
41  numDropped(0),
42  numUnroutable(0),
43  numForwarded(0)
44 {
45 }
int defaultHopLimit
Definition: GenericNetworkProtocol.h:73
GenericRoutingTable * routingTable
Definition: GenericNetworkProtocol.h:68
int numUnroutable
Definition: GenericNetworkProtocol.h:87
int numDropped
Definition: GenericNetworkProtocol.h:86
int numForwarded
Definition: GenericNetworkProtocol.h:88
IInterfaceTable * interfaceTable
Definition: GenericNetworkProtocol.h:67
IARP * arp
Definition: GenericNetworkProtocol.h:69
int numLocalDeliver
Definition: GenericNetworkProtocol.h:85
int queueOutBaseGateId
Definition: GenericNetworkProtocol.h:70
inet::GenericNetworkProtocol::~GenericNetworkProtocol ( )
48 {
49  for (auto & elem : queuedDatagramsForHooks) {
50  delete elem.datagram;
51  }
52  queuedDatagramsForHooks.clear();
53 }
DatagramQueueForHooks queuedDatagramsForHooks
Definition: GenericNetworkProtocol.h:82

Member Function Documentation

INetfilter::IHook::Result inet::GenericNetworkProtocol::datagramForwardHook ( GenericDatagram datagram,
const InterfaceEntry inIE,
const InterfaceEntry *&  outIE,
L3Address nextHop 
)
protectedvirtual
590 {
591  for (auto & elem : hooks) {
592  IHook::Result r = elem.second->datagramForwardHook(datagram, inIE, outIE, nextHop);
593  switch (r) {
594  case IHook::ACCEPT:
595  break; // continue iteration
596 
597  case IHook::DROP:
598  delete datagram;
599  return r;
600 
601  case IHook::QUEUE:
602  queuedDatagramsForHooks.push_back(QueuedDatagramForHook(datagram, inIE, outIE, nextHop, INetfilter::IHook::FORWARD));
603  return r;
604 
605  case IHook::STOLEN:
606  return r;
607 
608  default:
609  throw cRuntimeError("Unknown Hook::Result value: %d", (int)r);
610  }
611  }
612  return IHook::ACCEPT;
613 }
DatagramQueueForHooks queuedDatagramsForHooks
Definition: GenericNetworkProtocol.h:82
doesn&#39;t allow the datagram to pass to the next hook, will be deleted
Definition: INetfilter.h:51
allows the datagram to pass to the next hook
Definition: INetfilter.h:50
HookList hooks
Definition: GenericNetworkProtocol.h:80
Result
Definition: INetfilter.h:49
doesn&#39;t allow datagram to pass to next hook, but won&#39;t be deleted
Definition: INetfilter.h:53
Definition: INetfilter.h:44
queues the datagram for later re-injection (e.g. when route discovery completes)
Definition: INetfilter.h:52
void inet::GenericNetworkProtocol::datagramLocalIn ( GenericDatagram datagram,
const InterfaceEntry inIE 
)
protectedvirtual

Referenced by reinjectQueuedDatagram().

486 {
487  sendDatagramToHL(datagram);
488 }
virtual void sendDatagramToHL(GenericDatagram *datagram)
Send datagrams up to the higher layers.
Definition: GenericNetworkProtocol.cc:415
INetfilter::IHook::Result inet::GenericNetworkProtocol::datagramLocalInHook ( GenericDatagram datagram,
const InterfaceEntry inIE 
)
protectedvirtual

Referenced by routePacket().

642 {
643  L3Address address;
644  for (auto & elem : hooks) {
645  IHook::Result r = elem.second->datagramLocalInHook(datagram, inIE);
646  switch (r) {
647  case IHook::ACCEPT:
648  break; // continue iteration
649 
650  case IHook::DROP:
651  delete datagram;
652  return r;
653 
654  case IHook::QUEUE:
655  queuedDatagramsForHooks.push_back(QueuedDatagramForHook(datagram, inIE, nullptr, address, INetfilter::IHook::LOCALIN));
656  return r;
657 
658  case IHook::STOLEN:
659  return r;
660 
661  default:
662  throw cRuntimeError("Unknown Hook::Result value: %d", (int)r);
663  }
664  }
665  return IHook::ACCEPT;
666 }
DatagramQueueForHooks queuedDatagramsForHooks
Definition: GenericNetworkProtocol.h:82
doesn&#39;t allow the datagram to pass to the next hook, will be deleted
Definition: INetfilter.h:51
Definition: INetfilter.h:43
allows the datagram to pass to the next hook
Definition: INetfilter.h:50
HookList hooks
Definition: GenericNetworkProtocol.h:80
Result
Definition: INetfilter.h:49
doesn&#39;t allow datagram to pass to next hook, but won&#39;t be deleted
Definition: INetfilter.h:53
queues the datagram for later re-injection (e.g. when route discovery completes)
Definition: INetfilter.h:52
void inet::GenericNetworkProtocol::datagramLocalOut ( GenericDatagram datagram,
const InterfaceEntry destIE,
const L3Address nextHop 
)
protectedvirtual

Referenced by handleMessageFromHL(), and reinjectQueuedDatagram().

491 {
492  // route packet
493  if (!datagram->getDestinationAddress().isMulticast())
494  routePacket(datagram, destIE, nextHop, true);
495  else
496  routeMulticastPacket(datagram, destIE, nullptr);
497 }
virtual void routeMulticastPacket(GenericDatagram *datagram, const InterfaceEntry *destIE, const InterfaceEntry *fromIE)
Forwards packets to all multicast destinations, using sendDatagramToOutput().
Definition: GenericNetworkProtocol.cc:220
virtual void routePacket(GenericDatagram *datagram, const InterfaceEntry *destIE, const L3Address &nextHop, bool fromHL)
Performs routing.
Definition: GenericNetworkProtocol.cc:154
INetfilter::IHook::Result inet::GenericNetworkProtocol::datagramLocalOutHook ( GenericDatagram datagram,
const InterfaceEntry *&  outIE,
L3Address nextHop 
)
protectedvirtual

Referenced by handleMessageFromHL().

669 {
670  for (auto & elem : hooks) {
671  IHook::Result r = elem.second->datagramLocalOutHook(datagram, outIE, nextHop);
672  switch (r) {
673  case IHook::ACCEPT:
674  break; // continue iteration
675 
676  case IHook::DROP:
677  delete datagram;
678  return r;
679 
680  case IHook::QUEUE:
681  queuedDatagramsForHooks.push_back(QueuedDatagramForHook(datagram, nullptr, outIE, nextHop, INetfilter::IHook::LOCALOUT));
682  return r;
683 
684  case IHook::STOLEN:
685  return r;
686 
687  default:
688  throw cRuntimeError("Unknown Hook::Result value: %d", (int)r);
689  }
690  }
691  return IHook::ACCEPT;
692 }
DatagramQueueForHooks queuedDatagramsForHooks
Definition: GenericNetworkProtocol.h:82
doesn&#39;t allow the datagram to pass to the next hook, will be deleted
Definition: INetfilter.h:51
allows the datagram to pass to the next hook
Definition: INetfilter.h:50
HookList hooks
Definition: GenericNetworkProtocol.h:80
Definition: INetfilter.h:46
Result
Definition: INetfilter.h:49
doesn&#39;t allow datagram to pass to next hook, but won&#39;t be deleted
Definition: INetfilter.h:53
queues the datagram for later re-injection (e.g. when route discovery completes)
Definition: INetfilter.h:52
INetfilter::IHook::Result inet::GenericNetworkProtocol::datagramPostRoutingHook ( GenericDatagram datagram,
const InterfaceEntry inIE,
const InterfaceEntry *&  outIE,
L3Address nextHop 
)
protectedvirtual
616 {
617  for (auto & elem : hooks) {
618  IHook::Result r = elem.second->datagramPostRoutingHook(datagram, inIE, outIE, nextHop);
619  switch (r) {
620  case IHook::ACCEPT:
621  break; // continue iteration
622 
623  case IHook::DROP:
624  delete datagram;
625  return r;
626 
627  case IHook::QUEUE:
628  queuedDatagramsForHooks.push_back(QueuedDatagramForHook(datagram, inIE, outIE, nextHop, INetfilter::IHook::POSTROUTING));
629  return r;
630 
631  case IHook::STOLEN:
632  return r;
633 
634  default:
635  throw cRuntimeError("Unknown Hook::Result value: %d", (int)r);
636  }
637  }
638  return IHook::ACCEPT;
639 }
DatagramQueueForHooks queuedDatagramsForHooks
Definition: GenericNetworkProtocol.h:82
doesn&#39;t allow the datagram to pass to the next hook, will be deleted
Definition: INetfilter.h:51
allows the datagram to pass to the next hook
Definition: INetfilter.h:50
Definition: INetfilter.h:45
HookList hooks
Definition: GenericNetworkProtocol.h:80
Result
Definition: INetfilter.h:49
doesn&#39;t allow datagram to pass to next hook, but won&#39;t be deleted
Definition: INetfilter.h:53
queues the datagram for later re-injection (e.g. when route discovery completes)
Definition: INetfilter.h:52
void inet::GenericNetworkProtocol::datagramPreRouting ( GenericDatagram datagram,
const InterfaceEntry inIE,
const InterfaceEntry destIE,
const L3Address nextHop 
)
protectedvirtual

Referenced by handlePacketFromNetwork(), and reinjectQueuedDatagram().

477 {
478  // route packet
479  if (!datagram->getDestinationAddress().isMulticast())
480  routePacket(datagram, destIE, nextHop, false);
481  else
482  routeMulticastPacket(datagram, destIE, inIE);
483 }
virtual void routeMulticastPacket(GenericDatagram *datagram, const InterfaceEntry *destIE, const InterfaceEntry *fromIE)
Forwards packets to all multicast destinations, using sendDatagramToOutput().
Definition: GenericNetworkProtocol.cc:220
virtual void routePacket(GenericDatagram *datagram, const InterfaceEntry *destIE, const L3Address &nextHop, bool fromHL)
Performs routing.
Definition: GenericNetworkProtocol.cc:154
INetfilter::IHook::Result inet::GenericNetworkProtocol::datagramPreRoutingHook ( GenericDatagram datagram,
const InterfaceEntry inIE,
const InterfaceEntry *&  outIE,
L3Address nextHop 
)
protectedvirtual

Referenced by handlePacketFromNetwork().

562 {
563  for (auto & elem : hooks) {
564  IHook::Result r = elem.second->datagramPreRoutingHook(datagram, inIE, outIE, nextHop);
565  switch (r) {
566  case IHook::ACCEPT:
567  break; // continue iteration
568 
569  case IHook::DROP:
570  delete datagram;
571  return r;
572 
573  case IHook::QUEUE:
574  if (datagram->getOwner() != this)
575  throw cRuntimeError("Model error: netfilter hook changed the owner of queued datagram '%s'", datagram->getFullName());
576  queuedDatagramsForHooks.push_back(QueuedDatagramForHook(datagram, inIE, nullptr, nextHop, INetfilter::IHook::PREROUTING));
577  return r;
578 
579  case IHook::STOLEN:
580  return r;
581 
582  default:
583  throw cRuntimeError("Unknown Hook::Result value: %d", (int)r);
584  }
585  }
586  return IHook::ACCEPT;
587 }
DatagramQueueForHooks queuedDatagramsForHooks
Definition: GenericNetworkProtocol.h:82
doesn&#39;t allow the datagram to pass to the next hook, will be deleted
Definition: INetfilter.h:51
Definition: INetfilter.h:42
allows the datagram to pass to the next hook
Definition: INetfilter.h:50
HookList hooks
Definition: GenericNetworkProtocol.h:80
Result
Definition: INetfilter.h:49
doesn&#39;t allow datagram to pass to next hook, but won&#39;t be deleted
Definition: INetfilter.h:53
queues the datagram for later re-injection (e.g. when route discovery completes)
Definition: INetfilter.h:52
cPacket * inet::GenericNetworkProtocol::decapsulate ( GenericDatagram datagram)
protectedvirtual

Decapsulate and return encapsulated packet after attaching GenericNetworkProtocolControlInfo.

Referenced by sendDatagramToHL().

352 {
353  // decapsulate transport packet
354  const InterfaceEntry *fromIE = getSourceInterfaceFrom(datagram);
355  cPacket *packet = datagram->decapsulate();
356 
357  // create and fill in control info
358  GenericNetworkProtocolControlInfo *controlInfo = new GenericNetworkProtocolControlInfo();
359  controlInfo->setProtocol(datagram->getTransportProtocol());
360  controlInfo->setSourceAddress(datagram->getSourceAddress());
361  controlInfo->setDestinationAddress(datagram->getDestinationAddress());
362  controlInfo->setInterfaceId(fromIE ? fromIE->getInterfaceId() : -1);
363  controlInfo->setHopLimit(datagram->getHopLimit());
364 
365  // attach control info
366  packet->setControlInfo(controlInfo);
367 
368  return packet;
369 }
virtual const InterfaceEntry * getSourceInterfaceFrom(cPacket *packet)
Definition: GenericNetworkProtocol.cc:108
void inet::GenericNetworkProtocol::dropQueuedDatagram ( const INetworkDatagram daragram)
overridevirtual

Requests the network layer to drop the datagram, because it's no longer needed.

This function may be used by a reactive routing protocol when it cancels the route discovery process.

Implements inet::INetfilter.

517 {
518  Enter_Method("dropDatagram()");
519  for (auto iter = queuedDatagramsForHooks.begin(); iter != queuedDatagramsForHooks.end(); iter++) {
520  if (iter->datagram == datagram) {
521  delete datagram;
522  queuedDatagramsForHooks.erase(iter);
523  return;
524  }
525  }
526 }
DatagramQueueForHooks queuedDatagramsForHooks
Definition: GenericNetworkProtocol.h:82
GenericDatagram * inet::GenericNetworkProtocol::encapsulate ( cPacket *  transportPacket,
const InterfaceEntry *&  destIE 
)
protectedvirtual

Encapsulate packet coming from higher layers into GenericDatagram, using the control info attached to the packet.

Referenced by handleMessageFromHL().

372 {
373  GenericNetworkProtocolControlInfo *controlInfo = check_and_cast<GenericNetworkProtocolControlInfo *>(transportPacket->removeControlInfo());
374  GenericDatagram *datagram = new GenericDatagram(transportPacket->getName());
375 // datagram->setByteLength(HEADER_BYTES); //TODO parameter
376  datagram->encapsulate(transportPacket);
377 
378  // set source and destination address
379  L3Address dest = controlInfo->getDestinationAddress();
380  datagram->setDestinationAddress(dest);
381 
382  // Generic_MULTICAST_IF option, but allow interface selection for unicast packets as well
383  destIE = interfaceTable->getInterfaceById(controlInfo->getInterfaceId());
384 
385  L3Address src = controlInfo->getSourceAddress();
386 
387  // when source address was given, use it; otherwise it'll get the address
388  // of the outgoing interface after routing
389  if (!src.isUnspecified()) {
390  // if interface parameter does not match existing interface, do not send datagram
391  if (routingTable->getInterfaceByAddress(src) == nullptr)
392  throw cRuntimeError("Wrong source address %s in (%s)%s: no interface with such address",
393  src.str().c_str(), transportPacket->getClassName(), transportPacket->getFullName());
394  datagram->setSourceAddress(src);
395  }
396 
397  // set other fields
398  short ttl;
399  if (controlInfo->getHopLimit() > 0)
400  ttl = controlInfo->getHopLimit();
401  else if (false) //TODO: datagram->getDestinationAddress().isLinkLocalMulticast())
402  ttl = 1;
403  else
404  ttl = defaultHopLimit;
405 
406  datagram->setHopLimit(ttl);
407  datagram->setTransportProtocol(controlInfo->getProtocol());
408 
409  // setting GenericNetworkProtocol options is currently not supported
410 
411  delete controlInfo;
412  return datagram;
413 }
virtual InterfaceEntry * getInterfaceById(int id) const =0
Returns an interface by its Id.
int defaultHopLimit
Definition: GenericNetworkProtocol.h:73
GenericRoutingTable * routingTable
Definition: GenericNetworkProtocol.h:68
virtual InterfaceEntry * getInterfaceByAddress(const L3Address &address) const override
Returns an interface given by its address.
Definition: GenericRoutingTable.cc:253
IInterfaceTable * interfaceTable
Definition: GenericNetworkProtocol.h:67
uint8_t ttl
Definition: TCP_NSC.cc:87
void inet::GenericNetworkProtocol::endService ( cPacket *  packet)
overrideprotectedvirtual

Processing of generic datagrams.

Called when a datagram reaches the front of the queue.

Implements inet::AbstractQueue.

99 {
100  if (pk->getArrivalGate()->isName("transportIn"))
102  else {
103  GenericDatagram *dgram = check_and_cast<GenericDatagram *>(pk);
105  }
106 }
virtual void handlePacketFromNetwork(GenericDatagram *datagram)
Handle GenericDatagram messages arriving from lower layer.
Definition: GenericNetworkProtocol.cc:114
virtual void handleMessageFromHL(cPacket *packet)
Handle messages (typically packets to be send in Generic) from transport or ICMP. ...
Definition: GenericNetworkProtocol.cc:134
const InterfaceEntry * inet::GenericNetworkProtocol::getSourceInterfaceFrom ( cPacket *  packet)
protectedvirtual

Referenced by decapsulate(), handlePacketFromNetwork(), and routePacket().

109 {
110  cGate *g = msg->getArrivalGate();
111  return g ? interfaceTable->getInterfaceByNetworkLayerGateIndex(g->getIndex()) : nullptr;
112 }
virtual InterfaceEntry * getInterfaceByNetworkLayerGateIndex(int index)=0
Returns an interface given by its getNetworkLayerGateIndex().
IInterfaceTable * interfaceTable
Definition: GenericNetworkProtocol.h:67
milli< kg >::type g
Definition: Units.h:900
void inet::GenericNetworkProtocol::handleMessage ( cMessage *  message)
overrideprotectedvirtual

Handle message.

Reimplemented from inet::AbstractQueue.

88 {
89  if (dynamic_cast<RegisterTransportProtocolCommand *>(msg)) {
90  RegisterTransportProtocolCommand *command = check_and_cast<RegisterTransportProtocolCommand *>(msg);
91  mapping.addProtocolMapping(command->getProtocol(), msg->getArrivalGate()->getIndex());
92  delete msg;
93  }
94  else
96 }
ProtocolMapping mapping
Definition: GenericNetworkProtocol.h:76
void addProtocolMapping(int protocol, int gateIndex)
Definition: ProtocolMap.cc:82
virtual void handleMessage(cMessage *msg) override
Definition: AbstractQueue.cc:43
void inet::GenericNetworkProtocol::handleMessageFromHL ( cPacket *  packet)
protectedvirtual

Handle messages (typically packets to be send in Generic) from transport or ICMP.

Invokes encapsulate(), then routePacket().

Referenced by endService().

135 {
136  // if no interface exists, do not send datagram
137  if (interfaceTable->getNumInterfaces() == 0) {
138  EV_INFO << "No interfaces exist, dropping packet\n";
139  delete msg;
140  return;
141  }
142 
143  // encapsulate and send
144  const InterfaceEntry *destIE; // will be filled in by encapsulate()
145  GenericDatagram *datagram = encapsulate(msg, destIE);
146 
147  L3Address nextHop;
148  if (datagramLocalOutHook(datagram, destIE, nextHop) != IHook::ACCEPT)
149  return;
150 
151  datagramLocalOut(datagram, destIE, nextHop);
152 }
virtual void datagramLocalOut(GenericDatagram *datagram, const InterfaceEntry *destIE, const L3Address &nextHop)
Definition: GenericNetworkProtocol.cc:490
allows the datagram to pass to the next hook
Definition: INetfilter.h:50
virtual IHook::Result datagramLocalOutHook(GenericDatagram *datagram, const InterfaceEntry *&outIE, L3Address &nextHop)
Definition: GenericNetworkProtocol.cc:668
virtual int getNumInterfaces() const =0
Returns the number of interfaces.
IInterfaceTable * interfaceTable
Definition: GenericNetworkProtocol.h:67
virtual GenericDatagram * encapsulate(cPacket *transportPacket, const InterfaceEntry *&destIE)
Encapsulate packet coming from higher layers into GenericDatagram, using the control info attached to...
Definition: GenericNetworkProtocol.cc:371
void inet::GenericNetworkProtocol::handlePacketFromNetwork ( GenericDatagram datagram)
protectedvirtual

Handle GenericDatagram messages arriving from lower layer.

Decrements TTL, then invokes routePacket().

Referenced by endService().

115 {
116  if (datagram->hasBitError()) {
117  //TODO discard
118  }
119 
120  delete datagram->removeControlInfo();
121 
122  // hop counter decrement; FIXME but not if it will be locally delivered
123  datagram->setHopLimit(datagram->getHopLimit() - 1);
124 
125  L3Address nextHop;
126  const InterfaceEntry *inIE = getSourceInterfaceFrom(datagram);
127  const InterfaceEntry *destIE = nullptr;
128  if (datagramPreRoutingHook(datagram, inIE, destIE, nextHop) != IHook::ACCEPT)
129  return;
130 
131  datagramPreRouting(datagram, inIE, destIE, nextHop);
132 }
virtual IHook::Result datagramPreRoutingHook(GenericDatagram *datagram, const InterfaceEntry *inIE, const InterfaceEntry *&outIE, L3Address &nextHop)
Definition: GenericNetworkProtocol.cc:561
allows the datagram to pass to the next hook
Definition: INetfilter.h:50
virtual void datagramPreRouting(GenericDatagram *datagram, const InterfaceEntry *inIE, const InterfaceEntry *destIE, const L3Address &nextHop)
Definition: GenericNetworkProtocol.cc:476
virtual const InterfaceEntry * getSourceInterfaceFrom(cPacket *packet)
Definition: GenericNetworkProtocol.cc:108
void inet::GenericNetworkProtocol::initialize ( )
overrideprotectedvirtual

Initialization.

Reimplemented from inet::QueueBase.

56 {
58 
59  interfaceTable = getModuleFromPar<IInterfaceTable>(par("interfaceTableModule"), this);
60  routingTable = getModuleFromPar<GenericRoutingTable>(par("routingTableModule"), this);
61  arp = getModuleFromPar<IARP>(par("arpModule"), this);
62 
63  queueOutBaseGateId = gateSize("queueOut") == 0 ? -1 : gate("queueOut", 0)->getId();
64  defaultHopLimit = par("hopLimit");
66 
67  WATCH(numLocalDeliver);
68  WATCH(numDropped);
69  WATCH(numUnroutable);
70  WATCH(numForwarded);
71 }
int defaultHopLimit
Definition: GenericNetworkProtocol.h:73
GenericRoutingTable * routingTable
Definition: GenericNetworkProtocol.h:68
int numUnroutable
Definition: GenericNetworkProtocol.h:87
int numDropped
Definition: GenericNetworkProtocol.h:86
int numForwarded
Definition: GenericNetworkProtocol.h:88
virtual void initialize() override
Definition: QueueBase.cc:22
IInterfaceTable * interfaceTable
Definition: GenericNetworkProtocol.h:67
IARP * arp
Definition: GenericNetworkProtocol.h:69
int numLocalDeliver
Definition: GenericNetworkProtocol.h:85
int queueOutBaseGateId
Definition: GenericNetworkProtocol.h:70
void inet::GenericNetworkProtocol::refreshDisplay ( ) const
overrideprotectedvirtual
74 {
75  char buf[80] = "";
76  if (numForwarded > 0)
77  sprintf(buf + strlen(buf), "fwd:%d ", numForwarded);
78  if (numLocalDeliver > 0)
79  sprintf(buf + strlen(buf), "up:%d ", numLocalDeliver);
80  if (numDropped > 0)
81  sprintf(buf + strlen(buf), "DROP:%d ", numDropped);
82  if (numUnroutable > 0)
83  sprintf(buf + strlen(buf), "UNROUTABLE:%d ", numUnroutable);
84  getDisplayString().setTagArg("t", 0, buf);
85 }
int numUnroutable
Definition: GenericNetworkProtocol.h:87
int numDropped
Definition: GenericNetworkProtocol.h:86
int numForwarded
Definition: GenericNetworkProtocol.h:88
int numLocalDeliver
Definition: GenericNetworkProtocol.h:85
void inet::GenericNetworkProtocol::registerHook ( int  priority,
IHook hook 
)
overridevirtual

Adds the provided hook to the list of registered hooks that will be called by the network layer when it processes datagrams.

Implements inet::INetfilter.

500 {
501  Enter_Method("registerHook()");
502  hooks.insert(std::pair<int, IHook *>(priority, hook));
503 }
HookList hooks
Definition: GenericNetworkProtocol.h:80
void inet::GenericNetworkProtocol::reinjectQueuedDatagram ( const INetworkDatagram datagram)
overridevirtual

Requests the network layer to restart the processing of the datagram.

This function may be used by a reactive routing protocol when it completes the route discovery process.

Implements inet::INetfilter.

529 {
530  Enter_Method("reinjectDatagram()");
531  for (auto iter = queuedDatagramsForHooks.begin(); iter != queuedDatagramsForHooks.end(); iter++) {
532  if (iter->datagram == datagram) {
533  GenericDatagram *datagram = iter->datagram;
534  const InterfaceEntry *inIE = iter->inIE;
535  const InterfaceEntry *outIE = iter->outIE;
536  const L3Address& nextHop = iter->nextHop;
537  INetfilter::IHook::Type hookType = iter->hookType;
538  switch (hookType) {
540  datagramPreRouting(datagram, inIE, outIE, nextHop);
541  break;
542 
544  datagramLocalIn(datagram, inIE);
545  break;
546 
548  datagramLocalOut(datagram, outIE, nextHop);
549  break;
550 
551  default:
552  throw cRuntimeError("Re-injection of datagram queued for this hook not implemented");
553  break;
554  }
555  queuedDatagramsForHooks.erase(iter);
556  return;
557  }
558  }
559 }
virtual void datagramLocalIn(GenericDatagram *datagram, const InterfaceEntry *inIE)
Definition: GenericNetworkProtocol.cc:485
virtual void datagramLocalOut(GenericDatagram *datagram, const InterfaceEntry *destIE, const L3Address &nextHop)
Definition: GenericNetworkProtocol.cc:490
DatagramQueueForHooks queuedDatagramsForHooks
Definition: GenericNetworkProtocol.h:82
Definition: INetfilter.h:42
Definition: INetfilter.h:43
virtual void datagramPreRouting(GenericDatagram *datagram, const InterfaceEntry *inIE, const InterfaceEntry *destIE, const L3Address &nextHop)
Definition: GenericNetworkProtocol.cc:476
Definition: INetfilter.h:46
Type
Definition: INetfilter.h:41
void inet::GenericNetworkProtocol::routeMulticastPacket ( GenericDatagram datagram,
const InterfaceEntry destIE,
const InterfaceEntry fromIE 
)
protectedvirtual

Forwards packets to all multicast destinations, using sendDatagramToOutput().

Referenced by datagramLocalOut(), and datagramPreRouting().

221 {
222  L3Address destAddr = datagram->getDestinationAddress();
223  // if received from the network...
224  if (fromIE != nullptr) {
225  // check for local delivery
226  if (routingTable->isLocalMulticastAddress(destAddr))
227  sendDatagramToHL(datagram);
228 //
229 // // don't forward if GenericNetworkProtocol forwarding is off
230 // if (!rt->isGenericForwardingEnabled())
231 // {
232 // delete datagram;
233 // return;
234 // }
235 //
236 // // don't forward if dest address is link-scope
237 // if (destAddr.isLinkLocalMulticast())
238 // {
239 // delete datagram;
240 // return;
241 // }
242  }
243  else {
244  //TODO
245  for (int i = 0; i < interfaceTable->getNumInterfaces(); ++i) {
246  const InterfaceEntry *destIE = interfaceTable->getInterface(i);
247  if (!destIE->isLoopback())
248  sendDatagramToOutput(datagram->dup(), destIE, datagram->getDestinationAddress());
249  }
250  delete datagram;
251  }
252 
253 // Address destAddr = datagram->getDestinationAddress();
254 // EV << "Routing multicast datagram `" << datagram->getName() << "' with dest=" << destAddr << "\n";
255 //
256 // numMulticast++;
257 //
258 // // DVMRP: process datagram only if sent locally or arrived on the shortest
259 // // route (provided routing table already contains srcAddr); otherwise
260 // // discard and continue.
261 // const InterfaceEntry *shortestPathIE = rt->getInterfaceForDestinationAddr(datagram->getSourceAddress());
262 // if (fromIE!=nullptr && shortestPathIE!=nullptr && fromIE!=shortestPathIE)
263 // {
264 // // FIXME count dropped
265 // EV << "Packet dropped.\n";
266 // delete datagram;
267 // return;
268 // }
269 //
270 // // if received from the network...
271 // if (fromIE!=nullptr)
272 // {
273 // // check for local delivery
274 // if (rt->isLocalMulticastAddress(destAddr))
275 // {
276 // GenericDatagram *datagramCopy = datagram->dup();
277 //
278 // // FIXME code from the MPLS model: set packet dest address to routerId (???)
279 // datagramCopy->setDestinationAddress(rt->getRouterId());
280 //
281 // reassembleAndDeliver(datagramCopy);
282 // }
283 //
284 // // don't forward if GenericNetworkProtocol forwarding is off
285 // if (!rt->isGenericForwardingEnabled())
286 // {
287 // delete datagram;
288 // return;
289 // }
290 //
291 // // don't forward if dest address is link-scope
292 // if (destAddr.isLinkLocalMulticast())
293 // {
294 // delete datagram;
295 // return;
296 // }
297 //
298 // }
299 //
300 // // routed explicitly via Generic_MULTICAST_IF
301 // if (destIE!=nullptr)
302 // {
303 // ASSERT(datagram->getDestinationAddress().isMulticast());
304 //
305 // EV << "multicast packet explicitly routed via output interface " << destIE->getName() << endl;
306 //
307 // // set datagram source address if not yet set
308 // if (datagram->getSourceAddress().isUnspecified())
309 // datagram->setSourceAddress(destIE->ipv4Data()->getGenericAddress());
310 //
311 // // send
312 // sendDatagramToOutput(datagram, destIE, datagram->getDestinationAddress());
313 //
314 // return;
315 // }
316 //
317 // // now: routing
318 // MulticastRoutes routes = rt->getMulticastRoutesFor(destAddr);
319 // if (routes.size()==0)
320 // {
321 // // no destination: delete datagram
322 // delete datagram;
323 // }
324 // else
325 // {
326 // // copy original datagram for multiple destinations
327 // for (unsigned int i=0; i<routes.size(); i++)
328 // {
329 // const InterfaceEntry *destIE = routes[i].interf;
330 //
331 // // don't forward to input port
332 // if (destIE && destIE!=fromIE)
333 // {
334 // GenericDatagram *datagramCopy = datagram->dup();
335 //
336 // // set datagram source address if not yet set
337 // if (datagramCopy->getSourceAddress().isUnspecified())
338 // datagramCopy->setSourceAddress(destIE->ipv4Data()->getGenericAddress());
339 //
340 // // send
341 // Address nextHop = routes[i].gateway;
342 // sendDatagramToOutput(datagramCopy, destIE, nextHop);
343 // }
344 // }
345 //
346 // // only copies sent, delete original datagram
347 // delete datagram;
348 // }
349 }
virtual void sendDatagramToOutput(GenericDatagram *datagram, const InterfaceEntry *ie, L3Address nextHop)
Last TTL check, then send datagram on the given interface.
Definition: GenericNetworkProtocol.cc:435
GenericRoutingTable * routingTable
Definition: GenericNetworkProtocol.h:68
virtual void sendDatagramToHL(GenericDatagram *datagram)
Send datagrams up to the higher layers.
Definition: GenericNetworkProtocol.cc:415
virtual bool isLocalMulticastAddress(const L3Address &dest) const override
Checks if the address is in one of the local multicast group address list.
Definition: GenericRoutingTable.cc:298
virtual int getNumInterfaces() const =0
Returns the number of interfaces.
IInterfaceTable * interfaceTable
Definition: GenericNetworkProtocol.h:67
virtual InterfaceEntry * getInterface(int pos) const =0
Returns the InterfaceEntry specified by an index 0..numInterfaces-1.
void inet::GenericNetworkProtocol::routePacket ( GenericDatagram datagram,
const InterfaceEntry destIE,
const L3Address nextHop,
bool  fromHL 
)
protectedvirtual

Performs routing.

Based on the routing decision, it dispatches to sendDatagramToHL() for local packets, to sendDatagramToOutput() for forwarded packets, to handleMulticastPacket() for multicast packets, or drops the packet if it's unroutable or forwarding is off.

Referenced by datagramLocalOut(), and datagramPreRouting().

155 {
156  // TBD add option handling code here
157 
158  L3Address destAddr = datagram->getDestinationAddress();
159 
160  EV_INFO << "Routing datagram `" << datagram->getName() << "' with dest=" << destAddr << ": ";
161 
162  // check for local delivery
163  if (routingTable->isLocalAddress(destAddr)) {
164  EV_INFO << "local delivery\n";
165  if (datagram->getSourceAddress().isUnspecified())
166  datagram->setSourceAddress(destAddr); // allows two apps on the same host to communicate
167  numLocalDeliver++;
168 
170  return;
171 
172  sendDatagramToHL(datagram);
173  return;
174  }
175 
176  // if datagram arrived from input gate and Generic_FORWARD is off, delete datagram
177  if (!fromHL && !routingTable->isForwardingEnabled()) {
178  EV_INFO << "forwarding off, dropping packet\n";
179  numDropped++;
180  delete datagram;
181  return;
182  }
183 
184  // if output port was explicitly requested, use that, otherwise use GenericNetworkProtocol routing
185  // TODO: see IPv4, using destIE here leaves nextHope unspecified
186  L3Address nextHop;
187  if (destIE && !requestedNextHop.isUnspecified()) {
188  EV_DETAIL << "using manually specified output interface " << destIE->getName() << "\n";
189  nextHop = requestedNextHop;
190  }
191  else {
192  // use GenericNetworkProtocol routing (lookup in routing table)
193  const GenericRoute *re = routingTable->findBestMatchingRoute(destAddr);
194 
195  // error handling: destination address does not exist in routing table:
196  // throw packet away and continue
197  if (re == nullptr) {
198  EV_INFO << "unroutable, discarding packet\n";
199  numUnroutable++;
200  delete datagram;
201  return;
202  }
203 
204  // extract interface and next-hop address from routing table entry
205  destIE = re->getInterface();
206  nextHop = re->getNextHopAsGeneric();
207  }
208 
209  // set datagram source address if not yet set
210  if (datagram->getSourceAddress().isUnspecified())
211  datagram->setSourceAddress(destIE->getGenericNetworkProtocolData()->getAddress());
212 
213  // default: send datagram to fragmentation
214  EV_INFO << "output interface is " << destIE->getName() << ", next-hop address: " << nextHop << "\n";
215  numForwarded++;
216 
217  sendDatagramToOutput(datagram, destIE, nextHop);
218 }
virtual bool isLocalAddress(const L3Address &dest) const override
Checks if the address is a local one, i.e.
Definition: GenericRoutingTable.cc:240
virtual GenericRoute * findBestMatchingRoute(const L3Address &dest) const override
The routing function.
Definition: GenericRoutingTable.cc:265
virtual void sendDatagramToOutput(GenericDatagram *datagram, const InterfaceEntry *ie, L3Address nextHop)
Last TTL check, then send datagram on the given interface.
Definition: GenericNetworkProtocol.cc:435
GenericRoutingTable * routingTable
Definition: GenericNetworkProtocol.h:68
int numUnroutable
Definition: GenericNetworkProtocol.h:87
virtual bool isForwardingEnabled() const override
Forwarding on/off.
Definition: GenericRoutingTable.cc:225
virtual void sendDatagramToHL(GenericDatagram *datagram)
Send datagrams up to the higher layers.
Definition: GenericNetworkProtocol.cc:415
int numDropped
Definition: GenericNetworkProtocol.h:86
virtual IHook::Result datagramLocalInHook(GenericDatagram *datagram, const InterfaceEntry *inIE)
Definition: GenericNetworkProtocol.cc:641
allows the datagram to pass to the next hook
Definition: INetfilter.h:50
int numForwarded
Definition: GenericNetworkProtocol.h:88
int numLocalDeliver
Definition: GenericNetworkProtocol.h:85
virtual const InterfaceEntry * getSourceInterfaceFrom(cPacket *packet)
Definition: GenericNetworkProtocol.cc:108
void inet::GenericNetworkProtocol::sendDatagramToHL ( GenericDatagram datagram)
protectedvirtual

Send datagrams up to the higher layers.

Referenced by datagramLocalIn(), routeMulticastPacket(), and routePacket().

416 {
417  int protocol = datagram->getTransportProtocol();
418  int gateIndex = mapping.findOutputGateForProtocol(protocol);
419  // check if the transportOut port are connected, otherwise discard the packet
420  if (gateIndex >= 0) {
421  cGate *outGate = gate("transportOut", gateIndex);
422  if (outGate->isPathOK()) {
423  // decapsulate and send on appropriate output gate
424  cPacket *packet = decapsulate(datagram);
425  delete datagram;
426  send(packet, "transportOut", gateIndex);
427  return;
428  }
429  }
430 
431  //TODO send an ICMP error: protocol unreachable
432  delete datagram;
433 }
ProtocolMapping mapping
Definition: GenericNetworkProtocol.h:76
uint8_t protocol
Definition: TCP_NSC.cc:88
virtual cPacket * decapsulate(GenericDatagram *datagram)
Decapsulate and return encapsulated packet after attaching GenericNetworkProtocolControlInfo.
Definition: GenericNetworkProtocol.cc:351
int findOutputGateForProtocol(int protocol) const
find output gate index for protocol ID and returns it.
Definition: ProtocolMap.cc:64
void inet::GenericNetworkProtocol::sendDatagramToOutput ( GenericDatagram datagram,
const InterfaceEntry ie,
L3Address  nextHop 
)
protectedvirtual

Last TTL check, then send datagram on the given interface.

Referenced by routeMulticastPacket(), and routePacket().

436 {
437  if (datagram->getByteLength() > ie->getMTU())
438  throw cRuntimeError("datagram too large"); //TODO refine
439 
440  // hop counter check
441  if (datagram->getHopLimit() <= 0) {
442  EV_INFO << "datagram hopLimit reached zero, discarding\n";
443  delete datagram; //TODO stats counter???
444  return;
445  }
446 
447  if (!ie->isBroadcast()) {
448  EV_DETAIL << "output interface " << ie->getName() << " is not broadcast, skipping ARP\n";
449  send(datagram, queueOutBaseGateId + ie->getNetworkLayerGateIndex());
450  return;
451  }
452 
453  // determine what address to look up in ARP cache
454  if (nextHop.isUnspecified()) {
455  nextHop = datagram->getDestinationAddress();
456  EV_WARN << "no next-hop address, using destination address " << nextHop << " (proxy ARP)\n";
457  }
458 
459  // send out datagram to NIC, with control info attached
460  MACAddress nextHopMAC = arp->resolveL3Address(nextHop, ie);
461  if (nextHopMAC == MACAddress::UNSPECIFIED_ADDRESS) {
462  throw cRuntimeError("ARP couldn't resolve the '%s' address", nextHop.str().c_str());
463  }
464  else {
465  // add control info with MAC address
466  Ieee802Ctrl *controlInfo = new Ieee802Ctrl();
467  controlInfo->setDest(nextHopMAC);
468  controlInfo->setEtherType(ETHERTYPE_INET_GENERIC);
469  datagram->setControlInfo(controlInfo);
470 
471  // send out
472  send(datagram, queueOutBaseGateId + ie->getNetworkLayerGateIndex());
473  }
474 }
Definition: Ieee802Ctrl_m.h:121
virtual MACAddress resolveL3Address(const L3Address &address, const InterfaceEntry *ie)=0
Tries to resolve the given network address to a MAC address.
IARP * arp
Definition: GenericNetworkProtocol.h:69
static const MACAddress UNSPECIFIED_ADDRESS
The unspecified MAC address, 00:00:00:00:00:00.
Definition: MACAddress.h:57
int queueOutBaseGateId
Definition: GenericNetworkProtocol.h:70
void inet::GenericNetworkProtocol::unregisterHook ( int  priority,
IHook hook 
)
overridevirtual

Removes the provided hook from the list of registered hooks.

Implements inet::INetfilter.

506 {
507  Enter_Method("unregisterHook()");
508  for (auto iter = hooks.begin(); iter != hooks.end(); iter++) {
509  if ((iter->first == priority) && (iter->second == hook)) {
510  hooks.erase(iter);
511  return;
512  }
513  }
514 }
HookList hooks
Definition: GenericNetworkProtocol.h:80

Member Data Documentation

IARP* inet::GenericNetworkProtocol::arp
protected

Referenced by initialize(), and sendDatagramToOutput().

int inet::GenericNetworkProtocol::defaultHopLimit
protected

Referenced by encapsulate(), and initialize().

IInterfaceTable* inet::GenericNetworkProtocol::interfaceTable
protected
ProtocolMapping inet::GenericNetworkProtocol::mapping
protected

Referenced by handleMessage(), and sendDatagramToHL().

int inet::GenericNetworkProtocol::numDropped
protected
int inet::GenericNetworkProtocol::numForwarded
protected
int inet::GenericNetworkProtocol::numLocalDeliver
protected
int inet::GenericNetworkProtocol::numUnroutable
protected
int inet::GenericNetworkProtocol::queueOutBaseGateId
protected

Referenced by initialize(), and sendDatagramToOutput().

GenericRoutingTable* inet::GenericNetworkProtocol::routingTable
protected

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