INET Framework for OMNeT++/OMNEST
inet::NetworkProtocolBase Class Referenceabstract

#include <NetworkProtocolBase.h>

Inheritance diagram for inet::NetworkProtocolBase:
inet::LayeredProtocolBase inet::DefaultProtocolRegistrationListener inet::OperationalBase inet::IProtocolRegistrationListener inet::OperationalMixin< cSimpleModule > inet::ILifecycle inet::Flooding inet::ProbabilisticBroadcast inet::WiseRoute inet::AdaptiveProbabilisticBroadcast

Classes

struct  SocketDescriptor
 

Public Member Functions

virtual void handleRegisterService (const Protocol &protocol, cGate *gate, ServicePrimitive servicePrimitive) override
 
virtual void handleRegisterProtocol (const Protocol &protocol, cGate *gate, ServicePrimitive servicePrimitive) override
 
- Public Member Functions inherited from inet::OperationalMixin< cSimpleModule >
virtual ~OperationalMixin ()
 }@ More...
 
- Public Member Functions inherited from inet::ILifecycle
virtual ~ILifecycle ()
 
- 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

 NetworkProtocolBase ()
 
virtual ~NetworkProtocolBase ()
 
virtual void initialize (int stage) override
 
virtual void sendUp (cMessage *message)
 
virtual void sendDown (cMessage *message, int interfaceId=-1)
 
virtual bool isUpperMessage (cMessage *message) const override
 
virtual bool isLowerMessage (cMessage *message) const override
 
virtual bool isInitializeStage (int stage) const override
 
virtual bool isModuleStartStage (int stage) const override
 
virtual bool isModuleStopStage (int stage) const override
 
virtual void handleUpperCommand (cMessage *msg) override
 
virtual const ProtocolgetProtocol () const =0
 
- Protected Member Functions inherited from inet::LayeredProtocolBase
virtual void handleMessageWhenUp (cMessage *message) override
 
virtual void handleSelfMessage (cMessage *message)
 
virtual void handleUpperMessage (cMessage *message)
 
virtual void handleLowerMessage (cMessage *message)
 
virtual void handleLowerCommand (cMessage *message)
 
virtual void handleUpperPacket (Packet *packet)
 
virtual void handleLowerPacket (Packet *packet)
 
- Protected Member Functions inherited from inet::OperationalMixin< cSimpleModule >
virtual int numInitStages () const override
 
virtual void refreshDisplay () const override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void handleMessageWhenDown (cMessage *msg)
 
virtual bool handleOperationStage (LifecycleOperation *operation, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 
virtual void handleStartOperation (LifecycleOperation *operation)=0
 
virtual void handleStopOperation (LifecycleOperation *operation)=0
 
virtual void handleCrashOperation (LifecycleOperation *operation)=0
 
virtual State getInitialOperationalState () const
 Returns initial operational state: OPERATING or NOT_OPERATING. More...
 
virtual void handleActiveOperationTimeout (cMessage *message)
 
virtual bool isUp () const
 utility functions More...
 
virtual bool isDown () const
 
virtual void setOperationalState (State newState)
 
virtual void scheduleOperationTimeout (simtime_t timeout)
 
virtual void setupActiveOperation (LifecycleOperation *operation, IDoneCallback *doneCallback, State)
 
virtual void delayActiveOperationFinish (simtime_t timeout)
 
virtual void startActiveOperationExtraTime (simtime_t delay=SIMTIME_ZERO)
 
virtual void startActiveOperationExtraTimeOrFinish (simtime_t extraTime)
 
virtual void finishActiveOperation ()
 

Protected Attributes

ModuleRefByPar< IInterfaceTableinterfaceTable
 
std::set< const Protocol * > upperProtocols
 
std::map< int, SocketDescriptor * > socketIdToSocketDescriptor
 
- Protected Attributes inherited from inet::OperationalMixin< cSimpleModule >
State operationalState
 
simtime_t lastChange
 
Operation activeOperation
 
cMessage * activeOperationTimeout
 
cMessage * activeOperationExtraTimer
 

Additional Inherited Members

- Protected Types inherited from inet::OperationalMixin< cSimpleModule >
enum  State
 

Constructor & Destructor Documentation

◆ NetworkProtocolBase()

inet::NetworkProtocolBase::NetworkProtocolBase ( )
protected
25 {
26 }

◆ ~NetworkProtocolBase()

virtual inet::NetworkProtocolBase::~NetworkProtocolBase ( )
inlineprotectedvirtual
42 { for (auto entry : socketIdToSocketDescriptor) delete entry.second; }

Member Function Documentation

◆ getProtocol()

virtual const Protocol& inet::NetworkProtocolBase::getProtocol ( ) const
protectedpure virtual

◆ handleRegisterProtocol()

void inet::NetworkProtocolBase::handleRegisterProtocol ( const Protocol protocol,
cGate *  gate,
ServicePrimitive  servicePrimitive 
)
overridevirtual

Reimplemented from inet::DefaultProtocolRegistrationListener.

45 {
46  Enter_Method("handleRegisterProtocol");
47  if (gate->isName("transportOut"))
48  upperProtocols.insert(&protocol);
49 }

◆ handleRegisterService()

void inet::NetworkProtocolBase::handleRegisterService ( const Protocol protocol,
cGate *  gate,
ServicePrimitive  servicePrimitive 
)
overridevirtual

Reimplemented from inet::DefaultProtocolRegistrationListener.

40 {
41  Enter_Method("handleRegisterService");
42 }

◆ handleUpperCommand()

void inet::NetworkProtocolBase::handleUpperCommand ( cMessage *  msg)
overrideprotectedvirtual

Reimplemented from inet::LayeredProtocolBase.

136 {
137  auto request = dynamic_cast<Request *>(msg);
138  if (auto *command = dynamic_cast<L3SocketBindCommand *>(msg->getControlInfo())) {
139  int socketId = request->getTag<SocketReq>()->getSocketId();
140  SocketDescriptor *descriptor = new SocketDescriptor(socketId, command->getProtocol()->getId(), command->getLocalAddress());
141  socketIdToSocketDescriptor[socketId] = descriptor;
142  delete msg;
143  }
144  else if (auto *command = dynamic_cast<L3SocketConnectCommand *>(msg->getControlInfo())) {
145  int socketId = request->getTag<SocketReq>()->getSocketId();
146  if (!containsKey(socketIdToSocketDescriptor, socketId))
147  throw cRuntimeError("Ipv4Socket: should use bind() before connect()");
148  socketIdToSocketDescriptor[socketId]->remoteAddress = command->getRemoteAddress();
149  delete msg;
150  }
151  else if (dynamic_cast<L3SocketCloseCommand *>(msg->getControlInfo()) != nullptr) {
152  int socketId = request->getTag<SocketReq>()->getSocketId();
153  auto it = socketIdToSocketDescriptor.find(socketId);
154  if (it != socketIdToSocketDescriptor.end()) {
155  delete it->second;
156  socketIdToSocketDescriptor.erase(it);
157  auto indication = new Indication("closed", L3_I_SOCKET_CLOSED);
158  auto ctrl = new L3SocketClosedIndication();
159  indication->setControlInfo(ctrl);
160  indication->addTag<SocketInd>()->setSocketId(socketId);
161  send(indication, "transportOut");
162  }
163  delete msg;
164  }
165  else
167 }

◆ initialize()

void inet::NetworkProtocolBase::initialize ( int  stage)
overrideprotectedvirtual

Reimplemented from inet::OperationalMixin< cSimpleModule >.

Reimplemented in inet::Flooding, inet::WiseRoute, inet::ProbabilisticBroadcast, and inet::AdaptiveProbabilisticBroadcast.

29 {
31  if (stage == INITSTAGE_LOCAL)
32  interfaceTable.reference(this, "interfaceTableModule", true);
33  else if (stage == INITSTAGE_NETWORK_LAYER) {
34  registerService(getProtocol(), gate("transportIn"), gate("transportOut"));
35  registerProtocol(getProtocol(), gate("queueOut"), gate("queueIn"));
36  }
37 }

Referenced by inet::ProbabilisticBroadcast::initialize(), and inet::Flooding::initialize().

◆ isInitializeStage()

virtual bool inet::NetworkProtocolBase::isInitializeStage ( int  stage) const
inlineoverrideprotectedvirtual

◆ isLowerMessage()

bool inet::NetworkProtocolBase::isLowerMessage ( cMessage *  message) const
overrideprotectedvirtual

Implements inet::LayeredProtocolBase.

131 {
132  return message->getArrivalGate()->isName("queueIn");
133 }

◆ isModuleStartStage()

virtual bool inet::NetworkProtocolBase::isModuleStartStage ( int  stage) const
inlineoverrideprotectedvirtual

◆ isModuleStopStage()

virtual bool inet::NetworkProtocolBase::isModuleStopStage ( int  stage) const
inlineoverrideprotectedvirtual

◆ isUpperMessage()

bool inet::NetworkProtocolBase::isUpperMessage ( cMessage *  message) const
overrideprotectedvirtual

Implements inet::LayeredProtocolBase.

126 {
127  return message->getArrivalGate()->isName("transportIn");
128 }

◆ sendDown()

void inet::NetworkProtocolBase::sendDown ( cMessage *  message,
int  interfaceId = -1 
)
protectedvirtual
92 {
93  if (message->isPacket())
94  emit(packetSentToLowerSignal, message);
95  if (interfaceId != -1) {
96  auto& tags = check_and_cast<ITaggedObject *>(message)->getTags();
97  tags.addTagIfAbsent<InterfaceReq>()->setInterfaceId(interfaceId);
98  auto networkInterface = interfaceTable->getInterfaceById(interfaceId);
99  auto protocol = networkInterface->getProtocol();
100  if (protocol != nullptr)
101  tags.addTagIfAbsent<DispatchProtocolReq>()->setProtocol(protocol);
102  else
103  tags.removeTagIfPresent<DispatchProtocolReq>();
104  send(message, "queueOut");
105  }
106  else {
107  for (int i = 0; i < interfaceTable->getNumInterfaces(); i++) {
108  NetworkInterface *networkInterface = interfaceTable->getInterface(i);
109  if (networkInterface && !networkInterface->isLoopback()) {
110  cMessage *duplicate = utils::dupPacketAndControlInfo(message);
111  auto& tags = check_and_cast<ITaggedObject *>(duplicate)->getTags();
112  tags.addTagIfAbsent<InterfaceReq>()->setInterfaceId(networkInterface->getInterfaceId());
113  auto protocol = networkInterface->getProtocol();
114  if (protocol != nullptr)
115  tags.addTagIfAbsent<DispatchProtocolReq>()->setProtocol(protocol);
116  else
117  tags.removeTagIfPresent<DispatchProtocolReq>();
118  send(duplicate, "queueOut");
119  }
120  }
121  delete message;
122  }
123 }

Referenced by inet::Flooding::handleLowerPacket(), inet::ProbabilisticBroadcast::handleSelfMessage(), and inet::Flooding::handleUpperPacket().

◆ sendUp()

void inet::NetworkProtocolBase::sendUp ( cMessage *  message)
protectedvirtual
52 {
53  if (Packet *packet = dynamic_cast<Packet *>(message)) {
54  const Protocol *protocol = packet->getTag<PacketProtocolTag>()->getProtocol();
55  const auto& addr = packet->getTag<L3AddressInd>();
56  auto remoteAddress(addr->getSrcAddress());
57  auto localAddress(addr->getDestAddress());
58  bool hasSocket = false;
59  for (const auto& elem : socketIdToSocketDescriptor) {
60  if (elem.second->protocolId == protocol->getId() &&
61  (elem.second->localAddress.isUnspecified() || elem.second->localAddress == localAddress) &&
62  (elem.second->remoteAddress.isUnspecified() || elem.second->remoteAddress == remoteAddress))
63  {
64  auto *packetCopy = packet->dup();
65  packetCopy->setKind(L3_I_DATA);
66  packetCopy->addTagIfAbsent<SocketInd>()->setSocketId(elem.second->socketId);
67  EV_INFO << "Passing up to socket " << elem.second->socketId << "\n";
68  emit(packetSentToUpperSignal, packetCopy);
69  send(packetCopy, "transportOut");
70  hasSocket = true;
71  }
72  }
74  EV_INFO << "Passing up to protocol " << protocol->getName() << "\n";
75  emit(packetSentToUpperSignal, packet);
76  send(packet, "transportOut");
77  }
78  else {
79  if (!hasSocket) {
80  EV_ERROR << "Transport protocol '" << protocol->getName() << "' not connected, discarding packet\n";
81  // TODO send an ICMP error: protocol unreachable
82 // sendToIcmp(datagram, inputInterfaceId, ICMP_DESTINATION_UNREACHABLE, ICMP_DU_PROTOCOL_UNREACHABLE);
83  }
84  delete packet;
85  }
86  }
87  else
88  send(message, "transportOut");
89 }

Referenced by inet::ProbabilisticBroadcast::handleLowerPacket(), and inet::Flooding::handleLowerPacket().

Member Data Documentation

◆ interfaceTable

◆ socketIdToSocketDescriptor

std::map<int, SocketDescriptor *> inet::NetworkProtocolBase::socketIdToSocketDescriptor
protected

Referenced by handleUpperCommand(), and sendUp().

◆ upperProtocols

std::set<const Protocol *> inet::NetworkProtocolBase::upperProtocols
protected

Referenced by handleRegisterProtocol(), and sendUp().


The documentation for this class was generated from the following files:
inet::NetworkProtocolBase::socketIdToSocketDescriptor
std::map< int, SocketDescriptor * > socketIdToSocketDescriptor
Definition: NetworkProtocolBase.h:38
inet::L3_I_DATA
@ L3_I_DATA
Definition: L3SocketCommand_m.h:84
protocol
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd DispatchProtocolReq L4PortInd Ipv4ControlInfo Ipv6ControlInfo down protocol
Definition: IUdp-gates.txt:25
inet::ModuleStopOperation::STAGE_NETWORK_LAYER
@ STAGE_NETWORK_LAYER
Definition: ModuleOperations.h:53
inet::OperationalMixin< cSimpleModule >::initialize
virtual void initialize(int stage) override
Definition: OperationalMixinImpl.h:26
inet::ModuleStartOperation::STAGE_NETWORK_LAYER
@ STAGE_NETWORK_LAYER
Definition: ModuleOperations.h:29
InterfaceReq
removed InterfaceReq
Definition: IUdp-gates.txt:11
DispatchProtocolReq
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd DispatchProtocolReq L4PortInd Ipv4ControlInfo Ipv6ControlInfo down DispatchProtocolReq
Definition: IUdp-gates.txt:25
inet::INITSTAGE_NETWORK_LAYER
INET_API InitStage INITSTAGE_NETWORK_LAYER
Initialization of network layer protocols.
L3AddressInd
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd L3AddressInd
Definition: IUdp-gates.txt:20
inet::NetworkProtocolBase::upperProtocols
std::set< const Protocol * > upperProtocols
Definition: NetworkProtocolBase.h:37
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::LayeredProtocolBase::handleUpperCommand
virtual void handleUpperCommand(cMessage *message)
Definition: LayeredProtocolBase.cc:51
inet::NetworkProtocolBase::getProtocol
virtual const Protocol & getProtocol() const =0
ctrl
removed ctrl
Definition: IUdp-gates.txt:7
inet::contains
bool contains(const std::vector< T > &v, const Tk &a)
Definition: stlutils.h:65
inet::NetworkProtocolBase::interfaceTable
ModuleRefByPar< IInterfaceTable > interfaceTable
Definition: NetworkProtocolBase.h:35
inet::INITSTAGE_LOCAL
INET_API InitStage INITSTAGE_LOCAL
Initialization of local state that don't use or affect other modules includes:
Enter_Method
#define Enter_Method(...)
Definition: SelfDoc.h:71
tags
* tags
Definition: IUdp-gates.txt:3
inet::utils::dupPacketAndControlInfo
T * dupPacketAndControlInfo(T *packet)
Duplicate a packet together with its control info.
Definition: INETUtils.h:87
inet::packetSentToUpperSignal
simsignal_t packetSentToUpperSignal
Definition: Simsignals.cc:87
inet::L3_I_SOCKET_CLOSED
@ L3_I_SOCKET_CLOSED
Definition: L3SocketCommand_m.h:85
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::containsKey
bool containsKey(const std::map< K, V, _C > &m, const Tk &a)
Definition: stlutils.h:80
inet::packetSentToLowerSignal
simsignal_t packetSentToLowerSignal
Definition: Simsignals.cc:90