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

#include <TunLoopbackApp.h>

Inheritance diagram for inet::TunLoopbackApp:
inet::LifecycleUnsupported inet::ILifecycle

Protected Member Functions

void initialize (int stage) override
 
virtual int numInitStages () const override
 
void handleMessage (cMessage *msg) override
 
void finish () override
 

Protected Attributes

const char * tunInterface = nullptr
 
unsigned int packetsSent = 0
 
unsigned int packetsReceived = 0
 
TunSocket tunSocket
 

Additional Inherited Members

- Public Member Functions inherited from inet::LifecycleUnsupported
virtual bool handleOperationStage (LifecycleOperation *operation, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 
- Public Member Functions inherited from inet::ILifecycle
virtual ~ILifecycle ()
 

Member Function Documentation

◆ finish()

void inet::TunLoopbackApp::finish ( )
overrideprotected
77 {
78  EV_INFO << "packets sent: " << packetsSent << endl;
79  EV_INFO << "packets received: " << packetsReceived << endl;
80 }

◆ handleMessage()

void inet::TunLoopbackApp::handleMessage ( cMessage *  msg)
overrideprotected
42 {
43  if (message->arrivedOn("socketIn")) {
44  EV_INFO << "Message " << message->getName() << " arrived from tun. " << packetsReceived + 1 << " packets received so far\n";
46 
47  Packet *packet = check_and_cast<Packet *>(message);
48  auto packetProtocol = getPacketProtocol(packet);
49  auto networkProtocol = getNetworkProtocol(packet);
50  if (packetProtocol != networkProtocol)
51  throw cRuntimeError("Cannot handle packet");
52 
53  const auto& networkHeader = removeNetworkProtocolHeader(packet, networkProtocol);
54  const auto& transportProtocol = *networkHeader->getProtocol();
55  const auto& transportHeader = removeTransportProtocolHeader(packet, transportProtocol);
56 
57  unsigned int destPort = transportHeader->getDestinationPort();
58  transportHeader->setDestinationPort(transportHeader->getSourcePort());
59  transportHeader->setSourcePort(destPort);
60  L3Address destAddr = networkHeader->getDestinationAddress();
61  networkHeader->setDestinationAddress(networkHeader->getSourceAddress());
62  networkHeader->setSourceAddress(destAddr);
63 
64  insertTransportProtocolHeader(packet, transportProtocol, transportHeader);
65  insertNetworkProtocolHeader(packet, networkProtocol, networkHeader);
66 
67  delete message->removeControlInfo();
68  packet->clearTags();
69  tunSocket.send(packet);
70  packetsSent++;
71  }
72  else
73  throw cRuntimeError("Unknown message: %s", message->getName());
74 }

◆ initialize()

void inet::TunLoopbackApp::initialize ( int  stage)
overrideprotected
24 {
25  cSimpleModule::initialize(stage);
26  if (stage == INITSTAGE_LOCAL) {
27  tunInterface = par("tunInterface");
28  packetsSent = 0;
29  packetsReceived = 0;
30  }
31  else if (stage == INITSTAGE_APPLICATION_LAYER) {
32  IInterfaceTable *interfaceTable = getModuleFromPar<IInterfaceTable>(par("interfaceTableModule"), this);
33  NetworkInterface *networkInterface = interfaceTable->findInterfaceByName(tunInterface);
34  if (networkInterface == nullptr)
35  throw cRuntimeError("TUN interface not found: %s", tunInterface);
36  tunSocket.setOutputGate(gate("socketOut"));
37  tunSocket.open(networkInterface->getInterfaceId());
38  }
39 }

◆ numInitStages()

virtual int inet::TunLoopbackApp::numInitStages ( ) const
inlineoverrideprotectedvirtual
27 { return NUM_INIT_STAGES; }

Member Data Documentation

◆ packetsReceived

unsigned int inet::TunLoopbackApp::packetsReceived = 0
protected

Referenced by finish(), handleMessage(), and initialize().

◆ packetsSent

unsigned int inet::TunLoopbackApp::packetsSent = 0
protected

Referenced by finish(), handleMessage(), and initialize().

◆ tunInterface

const char* inet::TunLoopbackApp::tunInterface = nullptr
protected

Referenced by initialize().

◆ tunSocket

TunSocket inet::TunLoopbackApp::tunSocket
protected

Referenced by handleMessage(), and initialize().


The documentation for this class was generated from the following files:
inet::TunSocket::open
void open(int interfaceId)
Definition: TunSocket.cc:56
inet::TunLoopbackApp::packetsReceived
unsigned int packetsReceived
Definition: TunLoopbackApp.h:21
inet::getPacketProtocol
const Protocol & getPacketProtocol(Packet *packet)
Definition: ProtocolTools.cc:18
inet::insertNetworkProtocolHeader
void insertNetworkProtocolHeader(Packet *packet, const Protocol &protocol, const Ptr< NetworkHeaderBase > &header)
Definition: L3Tools.cc:70
inet::TunLoopbackApp::tunInterface
const char * tunInterface
Definition: TunLoopbackApp.h:18
inet::insertTransportProtocolHeader
void insertTransportProtocolHeader(Packet *packet, const Protocol &protocol, const Ptr< TransportHeaderBase > &header)
Definition: L4Tools.cc:77
inet::removeTransportProtocolHeader
const Ptr< TransportHeaderBase > removeTransportProtocolHeader(Packet *packet, const Protocol &protocol)
Definition: L4Tools.cc:85
inet::INITSTAGE_LOCAL
INET_API InitStage INITSTAGE_LOCAL
Initialization of local state that don't use or affect other modules includes:
NUM_INIT_STAGES
#define NUM_INIT_STAGES
Definition: InitStageRegistry.h:73
inet::TunLoopbackApp::tunSocket
TunSocket tunSocket
Definition: TunLoopbackApp.h:23
inet::TunLoopbackApp::packetsSent
unsigned int packetsSent
Definition: TunLoopbackApp.h:20
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::removeNetworkProtocolHeader
const Ptr< NetworkHeaderBase > removeNetworkProtocolHeader(Packet *packet, const Protocol &protocol)
Definition: L3Tools.cc:78
inet::getNetworkProtocol
const Protocol & getNetworkProtocol(Packet *packet)
Definition: L3Tools.cc:28
inet::TunSocket::send
virtual void send(Packet *packet) override
Definition: TunSocket.cc:66