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

#include <TunnelApp.h>

Inheritance diagram for inet::TunnelApp:
inet::ApplicationBase inet::UdpSocket::ICallback inet::Ipv4Socket::ICallback inet::TunSocket::ICallback inet::OperationalBase inet::INetworkSocket::ICallback inet::OperationalMixin< cSimpleModule > inet::ILifecycle

Public Member Functions

 TunnelApp ()
 
virtual ~TunnelApp ()
 
- Public Member Functions inherited from inet::ApplicationBase
 ApplicationBase ()
 
- 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::UdpSocket::ICallback
virtual ~ICallback ()
 
- Public Member Functions inherited from inet::Ipv4Socket::ICallback
virtual void socketDataArrived (INetworkSocket *socket, Packet *packet) override
 
virtual void socketClosed (INetworkSocket *socket) override
 Notifies about socket closed, indication ownership is transferred to the callee. More...
 
- Public Member Functions inherited from inet::INetworkSocket::ICallback
virtual ~ICallback ()
 
- Public Member Functions inherited from inet::TunSocket::ICallback
virtual ~ICallback ()
 

Protected Member Functions

virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void handleMessageWhenUp (cMessage *msg) override
 
virtual void socketDataArrived (UdpSocket *socket, Packet *packet) override
 Notifies about data arrival, packet ownership is transferred to the callee. More...
 
virtual void socketErrorArrived (UdpSocket *socket, Indication *indication) override
 Notifies about error indication arrival, indication ownership is transferred to the callee. More...
 
virtual void socketClosed (UdpSocket *socket) override
 Notifies about socket closed, indication ownership is transferred to the callee. More...
 
virtual void socketDataArrived (Ipv4Socket *socket, Packet *packet) override
 
virtual void socketClosed (Ipv4Socket *socket) override
 
virtual void socketDataArrived (TunSocket *socket, Packet *packet) override
 
virtual void socketClosed (TunSocket *socket) override
 
virtual void handleStartOperation (LifecycleOperation *operation) override
 
virtual void handleStopOperation (LifecycleOperation *operation) override
 
virtual void handleCrashOperation (LifecycleOperation *operation) override
 
- Protected Member Functions inherited from inet::ApplicationBase
virtual bool isInitializeStage (int stage) const override
 
virtual bool isModuleStartStage (int stage) const override
 
virtual bool isModuleStopStage (int stage) const override
 
- 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 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

const Protocolprotocol = nullptr
 
const char * interface = nullptr
 
const char * destinationAddress = nullptr
 
int destinationPort = -1
 
int localPort = -1
 
Ipv4Socket ipv4Socket
 
UdpSocket serverSocket
 
UdpSocket clientSocket
 
TunSocket tunSocket
 
SocketMap socketMap
 
- 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

◆ TunnelApp()

inet::TunnelApp::TunnelApp ( )
24 {
25 }

◆ ~TunnelApp()

inet::TunnelApp::~TunnelApp ( )
virtual
28 {
29 }

Member Function Documentation

◆ handleCrashOperation()

void inet::TunnelApp::handleCrashOperation ( LifecycleOperation operation)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

166 {
170  for (auto s : socketMap.getMap())
171  s.second->destroy();
173 }

◆ handleMessageWhenUp()

void inet::TunnelApp::handleMessageWhenUp ( cMessage *  msg)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

75 {
76  if (message->arrivedOn("socketIn")) {
77  ASSERT(message->getControlInfo() == nullptr);
78 
79  if (auto socket = socketMap.findSocketFor(message)) {
80  socket->processMessage(message);
81  }
82  else
83  throw cRuntimeError("Unknown message: %s", message->getName());
84  }
85  else
86  throw cRuntimeError("Message arrived on unknown gate %s", message->getArrivalGate()->getFullName());
87 
88  if (operationalState == State::STOPPING_OPERATION) {
90  return;
91  for (auto s : socketMap.getMap())
92  if (s.second->isOpen())
93  return;
95  startActiveOperationExtraTimeOrFinish(par("stopOperationExtraTime"));
96  }
97 }

◆ handleStartOperation()

virtual void inet::TunnelApp::handleStartOperation ( LifecycleOperation operation)
inlineoverrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

57 {} // TODO implementation

◆ handleStopOperation()

void inet::TunnelApp::handleStopOperation ( LifecycleOperation operation)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

156 {
157  ipv4Socket.close();
160  for (auto s : socketMap.getMap())
161  s.second->close();
162  delayActiveOperationFinish(par("stopOperationTimeout"));
163 }

◆ initialize()

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

Reimplemented from inet::OperationalMixin< cSimpleModule >.

32 {
34  if (stage == INITSTAGE_LOCAL) {
35  interface = par("interface");
36  const char *protocolName = par("protocol");
37  protocol = Protocol::getProtocol(protocolName);
38  destinationAddress = par("destinationAddress");
39  if (protocol == &Protocol::udp) {
40  destinationPort = par("destinationPort");
41  localPort = par("localPort");
42  }
43  }
44  else if (stage == INITSTAGE_APPLICATION_LAYER) {
45  if (protocol == &Protocol::ipv4) {
46  ipv4Socket.setOutputGate(gate("socketOut"));
48  ipv4Socket.setCallback(this);
50  }
51  if (protocol == &Protocol::udp) {
52  serverSocket.setOutputGate(gate("socketOut"));
53  if (localPort != -1)
55  clientSocket.setOutputGate(gate("socketOut"));
56  if (destinationPort != -1)
57  clientSocket.connect(L3AddressResolver().resolve(destinationAddress), destinationPort);
62  }
63  IInterfaceTable *interfaceTable = getModuleFromPar<IInterfaceTable>(par("interfaceTableModule"), this);
64  NetworkInterface *networkInterface = interfaceTable->findInterfaceByName(interface);
65  if (networkInterface == nullptr)
66  throw cRuntimeError("TUN interface not found: %s", interface);
67  tunSocket.setOutputGate(gate("socketOut"));
68  tunSocket.open(networkInterface->getInterfaceId());
69  tunSocket.setCallback(this);
71  }
72 }

◆ numInitStages()

virtual int inet::TunnelApp::numInitStages ( ) const
inlineoverrideprotectedvirtual
39 { return NUM_INIT_STAGES; }

◆ socketClosed() [1/3]

void inet::TunnelApp::socketClosed ( Ipv4Socket socket)
overrideprotectedvirtual

Implements inet::Ipv4Socket::ICallback.

133 {
134  // TODO processing socket closed at stopOperation
135 }

◆ socketClosed() [2/3]

virtual void inet::TunnelApp::socketClosed ( TunSocket socket)
inlineoverrideprotectedvirtual

Implements inet::TunSocket::ICallback.

54 {}

◆ socketClosed() [3/3]

void inet::TunnelApp::socketClosed ( UdpSocket socket)
overrideprotectedvirtual

Notifies about socket closed, indication ownership is transferred to the callee.

Implements inet::UdpSocket::ICallback.

116 {
117  // TODO processing socket closed at stopOperation
118 }

◆ socketDataArrived() [1/3]

void inet::TunnelApp::socketDataArrived ( Ipv4Socket socket,
Packet packet 
)
overrideprotectedvirtual

Implements inet::Ipv4Socket::ICallback.

122 {
123  auto packetProtocol = packet->getTag<NetworkProtocolInd>()->getProtocol();
124  if (protocol == packetProtocol) {
125  packet->clearTags();
126  tunSocket.send(packet);
127  }
128  else
129  throw cRuntimeError("Unknown protocol: %s", packetProtocol->getName());
130 }

◆ socketDataArrived() [2/3]

void inet::TunnelApp::socketDataArrived ( TunSocket socket,
Packet packet 
)
overrideprotectedvirtual

Implements inet::TunSocket::ICallback.

139 {
140  // InterfaceInd says packet is from tunnel interface and socket id is present and equals to tunSocket
141  if (protocol == &Protocol::ipv4) {
142  packet->clearTags();
143  packet->addTag<L3AddressReq>()->setDestAddress(L3AddressResolver().resolve(destinationAddress));
144  packet->addTag<PacketProtocolTag>()->setProtocol(&Protocol::ipv4);
145  ipv4Socket.send(packet);
146  }
147  else if (protocol == &Protocol::udp) {
148  packet->clearTags();
149  clientSocket.send(packet);
150  }
151  else
152  throw cRuntimeError("Unknown protocol: %s", protocol->getName());
153 }

◆ socketDataArrived() [3/3]

void inet::TunnelApp::socketDataArrived ( UdpSocket socket,
Packet packet 
)
overrideprotectedvirtual

Notifies about data arrival, packet ownership is transferred to the callee.

Implements inet::UdpSocket::ICallback.

100 {
101  auto packetProtocol = packet->getTag<TransportProtocolInd>()->getProtocol();
102  if (protocol == packetProtocol) {
103  packet->clearTags();
104  tunSocket.send(packet);
105  }
106  else
107  throw cRuntimeError("Unknown protocol: %s", packetProtocol->getName());
108 }

◆ socketErrorArrived()

void inet::TunnelApp::socketErrorArrived ( UdpSocket socket,
Indication indication 
)
overrideprotectedvirtual

Notifies about error indication arrival, indication ownership is transferred to the callee.

Implements inet::UdpSocket::ICallback.

111 {
112  delete indication;
113 }

Member Data Documentation

◆ clientSocket

UdpSocket inet::TunnelApp::clientSocket
protected

◆ destinationAddress

const char* inet::TunnelApp::destinationAddress = nullptr
protected

Referenced by initialize(), and socketDataArrived().

◆ destinationPort

int inet::TunnelApp::destinationPort = -1
protected

Referenced by initialize().

◆ interface

const char* inet::TunnelApp::interface = nullptr
protected

Referenced by initialize().

◆ ipv4Socket

◆ localPort

int inet::TunnelApp::localPort = -1
protected

Referenced by initialize().

◆ protocol

const Protocol* inet::TunnelApp::protocol = nullptr
protected

Referenced by initialize(), and socketDataArrived().

◆ serverSocket

UdpSocket inet::TunnelApp::serverSocket
protected

◆ socketMap

SocketMap inet::TunnelApp::socketMap
protected

◆ tunSocket

TunSocket inet::TunnelApp::tunSocket
protected

Referenced by initialize(), and socketDataArrived().


The documentation for this class was generated from the following files:
inet::UdpSocket::setOutputGate
void setOutputGate(cGate *toUdp)
Sets the gate on which to send to UDP.
Definition: UdpSocket.h:117
inet::TunnelApp::localPort
int localPort
Definition: TunnelApp.h:26
inet::OperationalMixin< cSimpleModule >::operationalState
State operationalState
Definition: OperationalMixin.h:23
inet::UdpSocket::bind
void bind(int localPort)
Bind the socket to a local port number.
Definition: UdpSocket.cc:34
TransportProtocolInd
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd TransportProtocolInd
Definition: IUdp-gates.txt:20
inet::TunnelApp::protocol
const Protocol * protocol
Definition: TunnelApp.h:22
inet::Protocol::ipv4
static const Protocol ipv4
Definition: Protocol.h:93
inet::TunSocket::open
void open(int interfaceId)
Definition: TunSocket.cc:56
inet::Ipv4Socket::close
virtual void close() override
Closes this socket releasing all resources.
Definition: Ipv4Socket.cc:96
inet::Protocol::getProtocol
static const Protocol * getProtocol(int id)
Definition: Protocol.cc:50
inet::OperationalMixin< cSimpleModule >::initialize
virtual void initialize(int stage) override
Definition: OperationalMixinImpl.h:26
inet::UdpSocket::destroy
virtual void destroy() override
Notify the protocol that the owner of ISocket has destroyed the socket.
Definition: UdpSocket.cc:98
inet::SocketMap::deleteSockets
void deleteSockets()
Deletes the socket objects.
Definition: SocketMap.cc:44
inet::UdpSocket::setCallback
void setCallback(ICallback *cb)
Sets a callback object, to be used with processMessage().
Definition: UdpSocket.cc:338
inet::SocketMap::findSocketFor
ISocket * findSocketFor(cMessage *msg)
Finds the socket for the given message.
Definition: SocketMap.cc:19
PacketProtocolTag
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd DispatchProtocolReq L4PortInd Ipv4ControlInfo Ipv6ControlInfo down PacketProtocolTag
Definition: IUdp-gates.txt:25
inet::UdpSocket::send
virtual void send(Packet *msg) override
Sends a data packet to the address and port specified previously in a connect() call.
Definition: UdpSocket.cc:80
inet::TunnelApp::ipv4Socket
Ipv4Socket ipv4Socket
Definition: TunnelApp.h:28
inet::SocketMap::addSocket
void addSocket(ISocket *socket)
Adds the given socket.
Definition: SocketMap.cc:28
inet::UdpSocket::connect
void connect(L3Address remoteAddr, int remotePort)
Connects to a remote UDP socket.
Definition: UdpSocket.cc:53
inet::units::values::s
value< double, units::s > s
Definition: Units.h:1235
inet::Protocol::udp
static const Protocol udp
Definition: Protocol.h:117
inet::SocketMap::getMap
std::map< int, ISocket * > & getMap()
Returns the socket map.
Definition: SocketMap.h:57
inet::Ipv4Socket::send
virtual void send(Packet *packet) override
Definition: Ipv4Socket.cc:84
inet::INITSTAGE_LOCAL
INET_API InitStage INITSTAGE_LOCAL
Initialization of local state that don't use or affect other modules includes:
inet::UdpSocket::close
virtual void close() override
Unbinds the socket.
Definition: UdpSocket.cc:87
NUM_INIT_STAGES
#define NUM_INIT_STAGES
Definition: InitStageRegistry.h:73
inet::TunSocket::setCallback
void setCallback(ICallback *cb)
Sets a callback object, to be used with processMessage().
Definition: TunSocket.cc:22
inet::TunnelApp::tunSocket
TunSocket tunSocket
Definition: TunnelApp.h:31
inet::TunnelApp::clientSocket
UdpSocket clientSocket
Definition: TunnelApp.h:30
inet::UdpSocket::isOpen
virtual bool isOpen() const override
Definition: UdpSocket.h:293
inet::TunSocket::setOutputGate
void setOutputGate(cGate *outputGate)
Sets the gate on which to send raw packets.
Definition: TunSocket.h:45
inet::INITSTAGE_APPLICATION_LAYER
INET_API InitStage INITSTAGE_APPLICATION_LAYER
Initialization of applications.
inet::Protocol::getName
const char * getName() const
Definition: Protocol.h:38
inet::OperationalMixin< cSimpleModule >::delayActiveOperationFinish
virtual void delayActiveOperationFinish(simtime_t timeout)
Definition: OperationalMixinImpl.h:161
inet::TunnelApp::interface
const char * interface
Definition: TunnelApp.h:23
inet::OperationalMixin< cSimpleModule >::startActiveOperationExtraTimeOrFinish
virtual void startActiveOperationExtraTimeOrFinish(simtime_t extraTime)
Definition: OperationalMixinImpl.h:179
inet::Ipv4Address::UNSPECIFIED_ADDRESS
static const Ipv4Address UNSPECIFIED_ADDRESS
0.0.0.0
Definition: Ipv4Address.h:91
inet::Ipv4Socket::setCallback
virtual void setCallback(INetworkSocket::ICallback *callback) override
Sets a callback object, to be used with processMessage().
Definition: Ipv4Socket.cc:25
inet::TunnelApp::destinationAddress
const char * destinationAddress
Definition: TunnelApp.h:24
inet::Ipv4Socket::setOutputGate
void setOutputGate(cGate *outputGate)
Sets the gate on which to send raw packets.
Definition: Ipv4Socket.h:55
inet::Ipv4Socket::bind
virtual void bind(const Protocol *protocol, Ipv4Address localAddress)
Definition: Ipv4Socket.cc:61
inet::TunSocket::send
virtual void send(Packet *packet) override
Definition: TunSocket.cc:66
inet::TunnelApp::destinationPort
int destinationPort
Definition: TunnelApp.h:25
inet::TunnelApp::socketMap
SocketMap socketMap
Definition: TunnelApp.h:32
inet::Ipv4Socket::isOpen
virtual bool isOpen() const override
Definition: Ipv4Socket.h:73
inet::TunnelApp::serverSocket
UdpSocket serverSocket
Definition: TunnelApp.h:29
inet::Ipv4Socket::destroy
virtual void destroy() override
Notify the protocol that the owner of ISocket has destroyed the socket.
Definition: Ipv4Socket.cc:105