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

This class implements a raw L3 socket. More...

#include <L3Socket.h>

Inheritance diagram for inet::L3Socket:
inet::INetworkSocket inet::ISocket

Classes

class  ICallback
 

Public Member Functions

 L3Socket (const Protocol *l3Protocol, cGate *outputGate=nullptr)
 
virtual ~L3Socket ()
 
void setOutputGate (cGate *outputGate)
 Sets the gate on which to send raw packets. More...
 
virtual void setCallback (INetworkSocket::ICallback *callback) override
 Sets a callback object, to be used with processMessage(). More...
 
void * getUserData () const
 
void setUserData (void *userData)
 
virtual int getSocketId () const override
 Returns the socket Id which is unique within the network node. More...
 
virtual const ProtocolgetNetworkProtocol () const override
 Returns the associated network protocol used to deliver datagrams by this socket. More...
 
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...
 
virtual void bind (const Protocol *protocol, L3Address localAddress) override
 Binds this socket to the given protocol and local address. More...
 
virtual void connect (L3Address remoteAddress) override
 Connects to a remote socket. More...
 
virtual void send (Packet *packet) override
 
virtual void sendTo (Packet *packet, L3Address destAddress) override
 Sends a packet to the given remote address using the associated network protocol. More...
 
virtual void close () override
 Closes this socket releasing all resources. More...
 
virtual void destroy () override
 Notify the protocol that the owner of ISocket has destroyed the socket. More...
 
virtual bool isOpen () const override
 
- Public Member Functions inherited from inet::ISocket
virtual ~ISocket ()
 

Protected Member Functions

void sendToOutput (cMessage *message)
 

Protected Attributes

bool bound = false
 
bool isOpen_ = false
 
const Protocoll3Protocol = nullptr
 
int socketId = -1
 
INetworkSocket::ICallbackcallback = nullptr
 
void * userData = nullptr
 
cGate * outputGate = nullptr
 

Detailed Description

This class implements a raw L3 socket.

Constructor & Destructor Documentation

◆ L3Socket()

inet::L3Socket::L3Socket ( const Protocol l3Protocol,
cGate *  outputGate = nullptr 
)
19  :
21  socketId(getEnvir()->getUniqueNumber()),
23 {
24 }

◆ ~L3Socket()

virtual inet::L3Socket::~L3Socket ( )
inlinevirtual
50 {}

Member Function Documentation

◆ belongsToSocket()

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

Returns true if the message belongs to this socket.

Implements inet::ISocket.

32 {
33  auto& tags = check_and_cast<ITaggedObject *>(msg)->getTags();
34  int msgSocketId = tags.getTag<SocketInd>()->getSocketId();
35  return socketId == msgSocketId;
36 }

Referenced by processMessage().

◆ bind()

void inet::L3Socket::bind ( const Protocol protocol,
L3Address  localAddress 
)
overridevirtual

Binds this socket to the given protocol and local address.

All incoming packets matching the given parameters will be delivered via the callback interface.

Implements inet::INetworkSocket.

62 {
63  ASSERT(!bound);
64  ASSERT(l3Protocol != nullptr);
65  L3SocketBindCommand *command = new L3SocketBindCommand();
66  command->setProtocol(protocol);
67  auto request = new Request("bind", L3_C_BIND);
68  request->setControlInfo(command);
69  sendToOutput(request);
70  bound = true;
71  isOpen_ = true;
72 }

◆ close()

void inet::L3Socket::close ( )
overridevirtual

Closes this socket releasing all resources.

Once closed, a closed socket may be bound to another (or the same) protocol, and reused.

Implements inet::INetworkSocket.

97 {
98  ASSERT(bound);
99  ASSERT(l3Protocol != nullptr);
100  L3SocketCloseCommand *command = new L3SocketCloseCommand();
101  auto request = new Request("close", L3_C_CLOSE);
102  request->setControlInfo(command);
103  sendToOutput(request);
104 }

◆ connect()

void inet::L3Socket::connect ( L3Address  remoteAddress)
overridevirtual

Connects to a remote socket.

The socket will only receive packets from the specified address, and you can use send() as opposed to sendTo() to send packets.

Implements inet::INetworkSocket.

75 {
76  isOpen_ = true;
77  auto *command = new L3SocketConnectCommand();
78  command->setRemoteAddress(remoteAddress);
79  auto request = new Request("connect", L3_C_CONNECT);
80  request->setControlInfo(command);
81  sendToOutput(request);
82 }

◆ destroy()

void inet::L3Socket::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.

107 {
108  ASSERT(l3Protocol != nullptr);
109  auto *command = new L3SocketDestroyCommand();
110  auto request = new Request("destroy", L3_C_DESTROY);
111  request->setControlInfo(command);
112  sendToOutput(request);
113 }

◆ getNetworkProtocol()

virtual const Protocol* inet::L3Socket::getNetworkProtocol ( ) const
inlineoverridevirtual

Returns the associated network protocol used to deliver datagrams by this socket.

Implements inet::INetworkSocket.

63 { return l3Protocol; }

◆ getSocketId()

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

Returns the socket Id which is unique within the network node.

Implements inet::ISocket.

62 { return socketId; }

Referenced by belongsToSocket().

◆ getUserData()

void* inet::L3Socket::getUserData ( ) const
inline
59 { return userData; }

◆ isOpen()

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

Implements inet::ISocket.

74 { return isOpen_; }

◆ processMessage()

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

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

Implements inet::ISocket.

39 {
40  ASSERT(belongsToSocket(msg));
41  switch (msg->getKind()) {
42  case L3_I_DATA:
43  if (callback)
44  callback->socketDataArrived(this, check_and_cast<Packet *>(msg));
45  else
46  delete msg;
47  break;
48  case L3_I_SOCKET_CLOSED:
49  check_and_cast<Indication *>(msg);
50  bound = isOpen_ = false;
51  if (callback)
52  callback->socketClosed(this);
53  delete msg;
54  break;
55  default:
56  throw cRuntimeError("L3Socket: invalid msg kind %d, one of the L3_I_xxx constants expected", msg->getKind());
57  break;
58  }
59 }

◆ send()

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

Implements inet::ISocket.

85 {
86  sendToOutput(packet);
87 }

Referenced by sendTo().

◆ sendTo()

void inet::L3Socket::sendTo ( Packet packet,
L3Address  remoteAddress 
)
overridevirtual

Sends a packet to the given remote address using the associated network protocol.

Implements inet::INetworkSocket.

90 {
91  auto addressReq = packet->addTagIfAbsent<L3AddressReq>();
92  addressReq->setDestAddress(destAddress);
93  send(packet);
94 }

◆ sendToOutput()

void inet::L3Socket::sendToOutput ( cMessage *  message)
protected
116 {
117  if (!outputGate)
118  throw cRuntimeError("L3Socket: setOutputGate() must be invoked before the socket can be used");
119  auto& tags = check_and_cast<ITaggedObject *>(message)->getTags();
120  tags.addTagIfAbsent<DispatchProtocolReq>()->setProtocol(l3Protocol);
121  tags.addTagIfAbsent<SocketReq>()->setSocketId(socketId);
122  check_and_cast<cSimpleModule *>(outputGate->getOwnerModule())->send(message, outputGate);
123 }

Referenced by bind(), close(), connect(), destroy(), and send().

◆ setCallback()

void inet::L3Socket::setCallback ( INetworkSocket::ICallback callback)
overridevirtual

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 ICallback

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

Sockets don't delete the callback object in the destructor or on any other occasion.

Implements inet::INetworkSocket.

27 {
28  this->callback = callback;
29 }

◆ setOutputGate()

void inet::L3Socket::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"));

56 { this->outputGate = outputGate; }

◆ setUserData()

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

Member Data Documentation

◆ bound

bool inet::L3Socket::bound = false
protected

Referenced by bind(), close(), and processMessage().

◆ callback

INetworkSocket::ICallback* inet::L3Socket::callback = nullptr
protected

Referenced by processMessage(), and setCallback().

◆ isOpen_

bool inet::L3Socket::isOpen_ = false
protected

Referenced by bind(), connect(), and processMessage().

◆ l3Protocol

const Protocol* inet::L3Socket::l3Protocol = nullptr
protected

Referenced by bind(), close(), destroy(), and sendToOutput().

◆ outputGate

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

Referenced by sendToOutput().

◆ socketId

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

Referenced by belongsToSocket(), and sendToOutput().

◆ userData

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

The documentation for this class was generated from the following files:
inet::L3Socket::outputGate
cGate * outputGate
Definition: L3Socket.h:43
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::L3Socket::isOpen_
bool isOpen_
Definition: L3Socket.h:38
inet::L3_C_CONNECT
@ L3_C_CONNECT
Definition: L3SocketCommand_m.h:65
inet::L3Socket::send
virtual void send(Packet *packet) override
Definition: L3Socket.cc:84
inet::INetworkSocket::ICallback::socketClosed
virtual void socketClosed(INetworkSocket *socket)=0
inet::L3Socket::belongsToSocket
virtual bool belongsToSocket(cMessage *msg) const override
Returns true if the message belongs to this socket.
Definition: L3Socket.cc:31
DispatchProtocolReq
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd DispatchProtocolReq L4PortInd Ipv4ControlInfo Ipv6ControlInfo down DispatchProtocolReq
Definition: IUdp-gates.txt:25
inet::L3_C_BIND
@ L3_C_BIND
Definition: L3SocketCommand_m.h:64
inet::L3Socket::callback
INetworkSocket::ICallback * callback
Definition: L3Socket.h:41
inet::L3Socket::bound
bool bound
Definition: L3Socket.h:37
inet::L3Socket::getSocketId
virtual int getSocketId() const override
Returns the socket Id which is unique within the network node.
Definition: L3Socket.h:62
inet::L3Socket::l3Protocol
const Protocol * l3Protocol
Definition: L3Socket.h:39
inet::L3_C_DESTROY
@ L3_C_DESTROY
Definition: L3SocketCommand_m.h:67
inet::INetworkSocket::ICallback::socketDataArrived
virtual void socketDataArrived(INetworkSocket *socket, Packet *packet)=0
inet::L3Socket::socketId
int socketId
Definition: L3Socket.h:40
tags
* tags
Definition: IUdp-gates.txt:3
inet::L3_I_SOCKET_CLOSED
@ L3_I_SOCKET_CLOSED
Definition: L3SocketCommand_m.h:85
inet::L3Socket::userData
void * userData
Definition: L3Socket.h:42
inet::L3_C_CLOSE
@ L3_C_CLOSE
Definition: L3SocketCommand_m.h:66
inet::L3Socket::sendToOutput
void sendToOutput(cMessage *message)
Definition: L3Socket.cc:115