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

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

#include <Ipv4Socket.h>

Inheritance diagram for inet::Ipv4Socket:
inet::INetworkSocket inet::ISocket

Classes

class  ICallback
 

Public Member Functions

 Ipv4Socket (cGate *outputGate=nullptr)
 
virtual ~Ipv4Socket ()
 
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, Ipv4Address localAddress)
 
virtual void connect (Ipv4Address remoteAddress)
 
virtual void send (Packet *packet) override
 
virtual void sendTo (Packet *packet, Ipv4Address destAddress)
 
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)
 
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 sendTo (Packet *packet, L3Address destAddress) override
 Sends a packet to the given remote address using the associated network protocol. More...
 

Protected Attributes

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

Detailed Description

This class implements a raw IPv4 socket.

Constructor & Destructor Documentation

◆ Ipv4Socket()

inet::Ipv4Socket::Ipv4Socket ( cGate *  outputGate = nullptr)
19  :
20  socketId(getEnvir()->getUniqueNumber()),
22 {
23 }

◆ ~Ipv4Socket()

virtual inet::Ipv4Socket::~Ipv4Socket ( )
inlinevirtual
49 {}

Member Function Documentation

◆ belongsToSocket()

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

Returns true if the message belongs to this socket.

Implements inet::ISocket.

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

Referenced by processMessage().

◆ bind() [1/2]

void inet::Ipv4Socket::bind ( const Protocol protocol,
Ipv4Address  localAddress 
)
virtual
62 {
63  ASSERT(!bound);
64  Ipv4SocketBindCommand *command = new Ipv4SocketBindCommand();
65  command->setProtocol(protocol);
66  command->setLocalAddress(localAddress);
67  auto request = new Request("bind", IPv4_C_BIND);
68  request->setControlInfo(command);
69  sendToOutput(request);
70  bound = true;
71  isOpen_ = true;
72 }

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

◆ bind() [2/2]

virtual void inet::Ipv4Socket::bind ( const Protocol protocol,
L3Address  localAddress 
)
inlineoverrideprotectedvirtual

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.

76 { bind(protocol, localAddress.toIpv4()); }

Referenced by bind().

◆ close()

void inet::Ipv4Socket::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  Ipv4SocketCloseCommand *command = new Ipv4SocketCloseCommand();
100  auto request = new Request("close", IPv4_C_CLOSE);
101  request->setControlInfo(command);
102  sendToOutput(request);
103 }

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

◆ connect() [1/2]

void inet::Ipv4Socket::connect ( Ipv4Address  remoteAddress)
virtual
75 {
76  Ipv4SocketConnectCommand *command = new Ipv4SocketConnectCommand();
77  command->setRemoteAddress(remoteAddress);
78  auto request = new Request("connect", IPv4_C_CONNECT);
79  request->setControlInfo(command);
80  sendToOutput(request);
81  isOpen_ = true;
82 }

◆ connect() [2/2]

virtual void inet::Ipv4Socket::connect ( L3Address  remoteAddress)
inlineoverrideprotectedvirtual

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.

77 { connect(remoteAddress.toIpv4()); }

Referenced by connect().

◆ destroy()

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

106 {
107  ASSERT(bound);
108  auto command = new Ipv4SocketDestroyCommand();
109  auto request = new Request("destroy", IPv4_C_DESTROY);
110  request->setControlInfo(command);
111  sendToOutput(request);
112 }

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

◆ getNetworkProtocol()

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

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

Implements inet::INetworkSocket.

62 { return &Protocol::ipv4; }

◆ getSocketId()

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

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

Implements inet::ISocket.

61 { return socketId; }

Referenced by belongsToSocket().

◆ getUserData()

void* inet::Ipv4Socket::getUserData ( ) const
inline
58 { return userData; }

◆ isOpen()

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

Implements inet::ISocket.

73 { return isOpen_; }

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

◆ processMessage()

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

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

Implements inet::ISocket.

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

◆ send()

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

Implements inet::ISocket.

85 {
86  sendToOutput(packet);
87 }

Referenced by sendTo(), and inet::TunnelApp::socketDataArrived().

◆ sendTo() [1/2]

void inet::Ipv4Socket::sendTo ( Packet packet,
Ipv4Address  destAddress 
)
virtual
90 {
91  auto addressReq = packet->addTagIfAbsent<L3AddressReq>();
92  addressReq->setDestAddress(destAddress);
93  send(packet);
94 }

◆ sendTo() [2/2]

virtual void inet::Ipv4Socket::sendTo ( Packet packet,
L3Address  remoteAddress 
)
inlineoverrideprotectedvirtual

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

Implements inet::INetworkSocket.

78 { sendTo(packet, destAddress.toIpv4()); }

Referenced by sendTo().

◆ sendToOutput()

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

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

◆ setCallback()

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

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

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

◆ setOutputGate()

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

55 { this->outputGate = outputGate; }

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

◆ setUserData()

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

Member Data Documentation

◆ bound

bool inet::Ipv4Socket::bound = false
protected

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

◆ callback

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

Referenced by processMessage(), and setCallback().

◆ isOpen_

bool inet::Ipv4Socket::isOpen_ = false
protected

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

◆ outputGate

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

Referenced by sendToOutput().

◆ socketId

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

Referenced by belongsToSocket(), and sendToOutput().

◆ userData

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

The documentation for this class was generated from the following files:
protocol
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd DispatchProtocolReq L4PortInd Ipv4ControlInfo Ipv6ControlInfo down protocol
Definition: IUdp-gates.txt:25
inet::Ipv4Socket::getSocketId
virtual int getSocketId() const override
Returns the socket Id which is unique within the network node.
Definition: Ipv4Socket.h:61
inet::Protocol::ipv4
static const Protocol ipv4
Definition: Protocol.h:93
inet::Ipv4Socket::bound
bool bound
Definition: Ipv4Socket.h:37
inet::INetworkSocket::ICallback::socketClosed
virtual void socketClosed(INetworkSocket *socket)=0
DispatchProtocolReq
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd DispatchProtocolReq L4PortInd Ipv4ControlInfo Ipv6ControlInfo down DispatchProtocolReq
Definition: IUdp-gates.txt:25
inet::IPv4_C_BIND
@ IPv4_C_BIND
Definition: Ipv4SocketCommand_m.h:64
inet::INetworkSocket::ICallback::socketDataArrived
virtual void socketDataArrived(INetworkSocket *socket, Packet *packet)=0
inet::IPv4_C_DESTROY
@ IPv4_C_DESTROY
Definition: Ipv4SocketCommand_m.h:67
inet::Ipv4Socket::sendTo
virtual void sendTo(Packet *packet, Ipv4Address destAddress)
Definition: Ipv4Socket.cc:89
inet::Ipv4Socket::send
virtual void send(Packet *packet) override
Definition: Ipv4Socket.cc:84
inet::Ipv4Socket::sendToOutput
void sendToOutput(cMessage *message)
Definition: Ipv4Socket.cc:114
inet::Ipv4Socket::callback
INetworkSocket::ICallback * callback
Definition: Ipv4Socket.h:40
inet::IPv4_C_CLOSE
@ IPv4_C_CLOSE
Definition: Ipv4SocketCommand_m.h:66
inet::IPv4_I_SOCKET_CLOSED
@ IPv4_I_SOCKET_CLOSED
Definition: Ipv4SocketCommand_m.h:85
inet::Ipv4Socket::userData
void * userData
Definition: Ipv4Socket.h:41
inet::Ipv4Socket::belongsToSocket
virtual bool belongsToSocket(cMessage *msg) const override
Returns true if the message belongs to this socket.
Definition: Ipv4Socket.cc:30
inet::IPv4_I_DATA
@ IPv4_I_DATA
Definition: Ipv4SocketCommand_m.h:84
inet::Ipv4Socket::connect
virtual void connect(Ipv4Address remoteAddress)
Definition: Ipv4Socket.cc:74
inet::Ipv4Socket::socketId
int socketId
Definition: Ipv4Socket.h:39
tags
* tags
Definition: IUdp-gates.txt:3
inet::Ipv4Socket::bind
virtual void bind(const Protocol *protocol, Ipv4Address localAddress)
Definition: Ipv4Socket.cc:61
inet::Ipv4Socket::isOpen_
bool isOpen_
Definition: Ipv4Socket.h:38
inet::IPv4_C_CONNECT
@ IPv4_C_CONNECT
Definition: Ipv4SocketCommand_m.h:65
inet::Ipv4Socket::outputGate
cGate * outputGate
Definition: Ipv4Socket.h:42