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

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

#include <Ipv6Socket.h>

Inheritance diagram for inet::Ipv6Socket:
inet::INetworkSocket inet::ISocket

Classes

class  ICallback
 

Public Member Functions

 Ipv6Socket (cGate *outputGate=nullptr)
 
virtual ~Ipv6Socket ()
 
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, Ipv6Address localAddress)
 
virtual void connect (Ipv6Address remoteAddress)
 
virtual void send (Packet *packet) override
 
virtual void sendTo (Packet *packet, Ipv6Address 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 IPv6 socket.

Constructor & Destructor Documentation

◆ Ipv6Socket()

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

◆ ~Ipv6Socket()

virtual inet::Ipv6Socket::~Ipv6Socket ( )
inlinevirtual
48 {}

Member Function Documentation

◆ belongsToSocket()

bool inet::Ipv6Socket::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::Ipv6Socket::bind ( const Protocol protocol,
Ipv6Address  localAddress 
)
virtual
61 {
62  ASSERT(!bound);
63  auto *command = new Ipv6SocketBindCommand();
64  command->setProtocol(protocol);
65  command->setLocalAddress(localAddress);
66  auto request = new Request("bind", IPv6_C_BIND);
67  request->setControlInfo(command);
68  sendToOutput(request);
69  bound = true;
70  isOpen_ = true;
71 }

◆ bind() [2/2]

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

75 { bind(protocol, localAddress.toIpv6()); }

Referenced by bind().

◆ close()

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

96 {
97  ASSERT(bound);
98  Ipv6SocketCloseCommand *command = new Ipv6SocketCloseCommand();
99  auto request = new Request("close", IPv6_C_CLOSE);
100  request->setControlInfo(command);
101  sendToOutput(request);
102 }

◆ connect() [1/2]

void inet::Ipv6Socket::connect ( Ipv6Address  remoteAddress)
virtual
74 {
75  isOpen_ = true;
76  auto *command = new Ipv6SocketConnectCommand();
77  command->setRemoteAddress(remoteAddress);
78  auto request = new Request("connect", IPv6_C_CONNECT);
79  request->setControlInfo(command);
80  sendToOutput(request);
81 }

◆ connect() [2/2]

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

76 { connect(remoteAddress.toIpv6()); }

Referenced by connect().

◆ destroy()

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

105 {
106  auto *command = new Ipv6SocketDestroyCommand();
107  auto request = new Request("destroy", IPv6_C_DESTROY);
108  request->setControlInfo(command);
109  sendToOutput(request);
110 }

◆ getNetworkProtocol()

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

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

Implements inet::INetworkSocket.

61 { return &Protocol::ipv6; }

◆ getSocketId()

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

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

Implements inet::ISocket.

60 { return socketId; }

Referenced by belongsToSocket().

◆ getUserData()

void* inet::Ipv6Socket::getUserData ( ) const
inline
57 { return userData; }

◆ isOpen()

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

Implements inet::ISocket.

72 { return isOpen_; }

◆ processMessage()

void inet::Ipv6Socket::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 IPv6_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  bound = isOpen_ = false;
50  if (callback)
51  callback->socketClosed(this);
52  delete msg;
53  break;
54  default:
55  throw cRuntimeError("Ipv6Socket: invalid msg kind %d, one of the IPv6_I_xxx constants expected", msg->getKind());
56  break;
57  }
58 }

◆ send()

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

Implements inet::ISocket.

84 {
85  sendToOutput(packet);
86 }

Referenced by sendTo().

◆ sendTo() [1/2]

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

◆ sendTo() [2/2]

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

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

Implements inet::INetworkSocket.

77 { sendTo(packet, destAddress.toIpv6()); }

Referenced by sendTo().

◆ sendToOutput()

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

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

◆ setCallback()

void inet::Ipv6Socket::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 }

◆ setOutputGate()

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

54 { this->outputGate = outputGate; }

◆ setUserData()

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

Member Data Documentation

◆ bound

bool inet::Ipv6Socket::bound = false
protected

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

◆ callback

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

Referenced by processMessage(), and setCallback().

◆ isOpen_

bool inet::Ipv6Socket::isOpen_ = false
protected

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

◆ outputGate

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

Referenced by sendToOutput().

◆ socketId

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

Referenced by belongsToSocket(), and sendToOutput().

◆ userData

void* inet::Ipv6Socket::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::Ipv6Socket::send
virtual void send(Packet *packet) override
Definition: Ipv6Socket.cc:83
inet::Ipv6Socket::userData
void * userData
Definition: Ipv6Socket.h:40
inet::Protocol::ipv6
static const Protocol ipv6
Definition: Protocol.h:94
inet::Ipv6Socket::callback
INetworkSocket::ICallback * callback
Definition: Ipv6Socket.h:39
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::Ipv6Socket::outputGate
cGate * outputGate
Definition: Ipv6Socket.h:41
inet::Ipv6Socket::getSocketId
virtual int getSocketId() const override
Returns the socket Id which is unique within the network node.
Definition: Ipv6Socket.h:60
inet::Ipv6Socket::belongsToSocket
virtual bool belongsToSocket(cMessage *msg) const override
Returns true if the message belongs to this socket.
Definition: Ipv6Socket.cc:30
inet::IPv6_I_DATA
@ IPv6_I_DATA
Definition: Ipv6SocketCommand_m.h:84
inet::IPv6_C_CLOSE
@ IPv6_C_CLOSE
Definition: Ipv6SocketCommand_m.h:66
inet::Ipv6Socket::connect
virtual void connect(Ipv6Address remoteAddress)
Definition: Ipv6Socket.cc:73
inet::INetworkSocket::ICallback::socketDataArrived
virtual void socketDataArrived(INetworkSocket *socket, Packet *packet)=0
inet::Ipv6Socket::sendTo
virtual void sendTo(Packet *packet, Ipv6Address destAddress)
Definition: Ipv6Socket.cc:88
inet::IPv6_C_CONNECT
@ IPv6_C_CONNECT
Definition: Ipv6SocketCommand_m.h:65
inet::Ipv6Socket::socketId
int socketId
Definition: Ipv6Socket.h:38
inet::Ipv6Socket::bound
bool bound
Definition: Ipv6Socket.h:36
inet::IPv6_C_DESTROY
@ IPv6_C_DESTROY
Definition: Ipv6SocketCommand_m.h:67
inet::IPv6_C_BIND
@ IPv6_C_BIND
Definition: Ipv6SocketCommand_m.h:64
tags
* tags
Definition: IUdp-gates.txt:3
inet::Ipv6Socket::isOpen_
bool isOpen_
Definition: Ipv6Socket.h:37
inet::Ipv6Socket::sendToOutput
void sendToOutput(cMessage *message)
Definition: Ipv6Socket.cc:112
inet::Ipv6Socket::bind
virtual void bind(const Protocol *protocol, Ipv6Address localAddress)
Definition: Ipv6Socket.cc:60
inet::IPv6_I_SOCKET_CLOSED
@ IPv6_I_SOCKET_CLOSED
Definition: Ipv6SocketCommand_m.h:85