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

#include <TunSocket.h>

Inheritance diagram for inet::TunSocket:
inet::ISocket

Classes

class  ICallback
 

Public Member Functions

 TunSocket ()
 
 ~TunSocket ()
 
void setOutputGate (cGate *outputGate)
 Sets the gate on which to send raw packets. More...
 
void setCallback (ICallback *cb)
 Sets a callback object, to be used with processMessage(). More...
 
void * getUserData () const
 
void setUserData (void *userData)
 
virtual int getSocketId () const override
 Returns the internal socket Id. More...
 
void open (int interfaceId)
 
virtual void send (Packet *packet) override
 
virtual void close () override
 Close the socket. More...
 
virtual void destroy () override
 Notify the protocol that the owner of ISocket has destroyed the socket. More...
 
virtual bool isOpen () const override
 
virtual bool belongsToSocket (cMessage *msg) const override
 Returns true if the message belongs to this socket. More...
 
virtual void processMessage (cMessage *msg) override
 Examines the message, takes ownership, and updates socket state. More...
 
- Public Member Functions inherited from inet::ISocket
virtual ~ISocket ()
 

Protected Member Functions

void sendToTun (cMessage *msg)
 

Protected Attributes

int socketId = -1
 
int interfaceId = -1
 
ICallbackcallback = nullptr
 
void * userData = nullptr
 
cGate * outputGate = nullptr
 
bool isOpen_ = false
 

Constructor & Destructor Documentation

◆ TunSocket()

inet::TunSocket::TunSocket ( )
18 {
19  socketId = getEnvir()->getUniqueNumber();
20 }

◆ ~TunSocket()

inet::TunSocket::~TunSocket ( )
inline
39 {}

Member Function Documentation

◆ belongsToSocket()

bool inet::TunSocket::belongsToSocket ( cMessage *  msg) const
overridevirtual

Returns true if the message belongs to this socket.

Implements inet::ISocket.

28 {
29  auto& tags = check_and_cast<ITaggedObject *>(msg)->getTags();
30  int msgSocketId = tags.getTag<SocketInd>()->getSocketId();
31  return socketId == msgSocketId;
32 }

Referenced by processMessage().

◆ close()

void inet::TunSocket::close ( )
overridevirtual

Close the socket.

Implements inet::ISocket.

77 {
78  auto request = new Request("CLOSE", TUN_C_CLOSE);
79  TunCloseCommand *command = new TunCloseCommand();
80  request->setControlInfo(command);
81  sendToTun(request);
82  this->interfaceId = -1;
83 }

◆ destroy()

void inet::TunSocket::destroy ( )
overridevirtual

Notify the protocol that the owner of ISocket has destroyed the socket.

Typically used when the owner of ISocket has crashed.

Implements inet::ISocket.

86 {
87  auto request = new Request("DESTROY", TUN_C_DESTROY);
88  auto command = new TunDestroyCommand();
89  request->setControlInfo(command);
90  sendToTun(request);
91  this->interfaceId = -1;
92 }

◆ getSocketId()

virtual int inet::TunSocket::getSocketId ( ) const
inlineoverridevirtual

Returns the internal socket Id.

Implements inet::ISocket.

69 { return socketId; }

Referenced by belongsToSocket().

◆ getUserData()

void* inet::TunSocket::getUserData ( ) const
inline
63 { return userData; }

◆ isOpen()

virtual bool inet::TunSocket::isOpen ( ) const
inlineoverridevirtual

Implements inet::ISocket.

75 { return isOpen_; }

◆ open()

void inet::TunSocket::open ( int  interfaceId)
57 {
58  isOpen_ = true;
59  this->interfaceId = interfaceId;
60  auto request = new Request("OPEN", TUN_C_OPEN);
61  TunOpenCommand *command = new TunOpenCommand();
62  request->setControlInfo(command);
63  sendToTun(request);
64 }

Referenced by inet::TunLoopbackApp::initialize(), and inet::TunnelApp::initialize().

◆ processMessage()

void inet::TunSocket::processMessage ( cMessage *  msg)
overridevirtual

Examines the message, takes ownership, and updates socket state.

Implements inet::ISocket.

35 {
36  ASSERT(belongsToSocket(msg));
37 
38  switch (msg->getKind()) {
39  case TUN_I_DATA:
40  if (callback)
41  callback->socketDataArrived(this, check_and_cast<Packet *>(msg));
42  else
43  delete msg;
44  break;
45  case TUN_I_CLOSED:
46  isOpen_ = false;
47  if (callback)
48  callback->socketClosed(this);
49  delete msg;
50  break;
51  default:
52  throw cRuntimeError("TunSocket: invalid msg kind %d, one of the TUN_I_xxx constants expected", msg->getKind());
53  }
54 }

◆ send()

void inet::TunSocket::send ( Packet packet)
overridevirtual

Implements inet::ISocket.

67 {
68  if (interfaceId == -1)
69  throw cRuntimeError("Socket is closed");
70 // TunSendCommand *command = new TunSendCommand();
71 // packet->setControlInfo(command);
72  packet->setKind(TUN_C_DATA);
73  sendToTun(packet);
74 }

Referenced by inet::TunLoopbackApp::handleMessage(), and inet::TunnelApp::socketDataArrived().

◆ sendToTun()

void inet::TunSocket::sendToTun ( cMessage *  msg)
protected
95 {
96  if (!outputGate)
97  throw cRuntimeError("TunSocket: setOutputGate() must be invoked before socket can be used");
98  auto& tags = check_and_cast<ITaggedObject *>(msg)->getTags();
99  tags.addTagIfAbsent<SocketReq>()->setSocketId(socketId);
100  tags.addTagIfAbsent<InterfaceReq>()->setInterfaceId(interfaceId);
101  check_and_cast<cSimpleModule *>(outputGate->getOwnerModule())->send(msg, outputGate);
102 }

Referenced by close(), destroy(), open(), and send().

◆ setCallback()

void inet::TunSocket::setCallback ( ICallback cb)

Sets a callback object, to be used with processMessage().

This callback object may be your simple module itself (if it multiply inherits from ICallback too, that is you declared it as

class MyAppModule : public cSimpleModule, public TunSocket::ICallback

and redefined the necessary virtual functions; or you may use dedicated class (and objects) for this purpose.

TunSocket doesn't delete the callback object in the destructor or on any other occasion.

23 {
24  this->callback = callback;
25 }

Referenced by inet::TunnelApp::initialize().

◆ setOutputGate()

void inet::TunSocket::setOutputGate ( cGate *  outputGate)
inline

Sets the gate on which to send raw packets.

Must be invoked before socket can be used. Example: socket.setOutputGate(gate("ipOut"));

45 { this->outputGate = outputGate; }

Referenced by inet::TunLoopbackApp::initialize(), and inet::TunnelApp::initialize().

◆ setUserData()

void inet::TunSocket::setUserData ( void *  userData)
inline
64 { this->userData = userData; }

Member Data Documentation

◆ callback

ICallback* inet::TunSocket::callback = nullptr
protected

Referenced by processMessage(), and setCallback().

◆ interfaceId

int inet::TunSocket::interfaceId = -1
protected

Referenced by close(), destroy(), open(), send(), and sendToTun().

◆ isOpen_

bool inet::TunSocket::isOpen_ = false
protected

Referenced by open(), and processMessage().

◆ outputGate

cGate* inet::TunSocket::outputGate = nullptr
protected

Referenced by sendToTun().

◆ socketId

int inet::TunSocket::socketId = -1
protected

◆ userData

void* inet::TunSocket::userData = nullptr
protected

The documentation for this class was generated from the following files:
inet::TUN_I_CLOSED
@ TUN_I_CLOSED
Definition: TunControlInfo_m.h:79
inet::TunSocket::sendToTun
void sendToTun(cMessage *msg)
Definition: TunSocket.cc:94
inet::TunSocket::socketId
int socketId
Definition: TunSocket.h:27
inet::TUN_C_DATA
@ TUN_C_DATA
Definition: TunControlInfo_m.h:62
inet::TunSocket::ICallback::socketClosed
virtual void socketClosed(TunSocket *socket)=0
InterfaceReq
removed InterfaceReq
Definition: IUdp-gates.txt:11
inet::TunSocket::belongsToSocket
virtual bool belongsToSocket(cMessage *msg) const override
Returns true if the message belongs to this socket.
Definition: TunSocket.cc:27
inet::TUN_C_CLOSE
@ TUN_C_CLOSE
Definition: TunControlInfo_m.h:60
inet::TunSocket::interfaceId
int interfaceId
Definition: TunSocket.h:28
inet::TunSocket::ICallback::socketDataArrived
virtual void socketDataArrived(TunSocket *socket, Packet *packet)=0
inet::TUN_I_DATA
@ TUN_I_DATA
Definition: TunControlInfo_m.h:80
tags
* tags
Definition: IUdp-gates.txt:3
inet::TUN_C_OPEN
@ TUN_C_OPEN
Definition: TunControlInfo_m.h:59
inet::TunSocket::outputGate
cGate * outputGate
Definition: TunSocket.h:31
inet::TUN_C_DESTROY
@ TUN_C_DESTROY
Definition: TunControlInfo_m.h:61
inet::TunSocket::userData
void * userData
Definition: TunSocket.h:30
inet::TunSocket::callback
ICallback * callback
Definition: TunSocket.h:29
inet::TunSocket::getSocketId
virtual int getSocketId() const override
Returns the internal socket Id.
Definition: TunSocket.h:69
inet::TunSocket::isOpen_
bool isOpen_
Definition: TunSocket.h:32