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

UdpSocket is a convenience class, to make it easier to send and receive UDP packets from your application models. More...

#include <UdpSocket.h>

Inheritance diagram for inet::UdpSocket:
inet::ISocket

Classes

class  ICallback
 

Public Types

enum  State { CONNECTED, CLOSED }
 

Public Member Functions

 UdpSocket ()
 Constructor. More...
 
 ~UdpSocket ()
 Destructor. More...
 
void * getUserData () const
 
void setUserData (void *userData)
 
State getState () const
 
int getSocketId () const override
 Returns the internal socket Id. More...
 
Opening and closing connections, sending data
void setOutputGate (cGate *toUdp)
 Sets the gate on which to send to UDP. More...
 
void bind (int localPort)
 Bind the socket to a local port number. More...
 
void bind (L3Address localAddr, int localPort)
 Bind the socket to a local port number and IP address (useful with multi-homing or multicast addresses). More...
 
void connect (L3Address remoteAddr, int remotePort)
 Connects to a remote UDP socket. More...
 
void setTimeToLive (int ttl)
 Set the TTL (Ipv6: Hop Limit) field on sent packets. More...
 
void setDscp (short dscp)
 Sets the Ipv4 / Ipv6 dscp fields of packets sent from the UDP socket. More...
 
void setTos (short tos)
 Sets the Ipv4 Type of Service / Ipv6 Traffic Class fields of packets sent from the UDP socket. More...
 
void setBroadcast (bool broadcast)
 Set the Broadcast option on the UDP socket. More...
 
void setMulticastLoop (bool value)
 The boolean value specifies whether sent multicast packets should be looped back to the local sockets (like the Unix IP_MULTICAST_LOOP socket option). More...
 
void setMulticastOutputInterface (int interfaceId)
 Set the output interface for sending multicast packets (like the Unix IP_MULTICAST_IF socket option). More...
 
void setReuseAddress (bool value)
 Set the ReuseAddress option on the UDP socket. More...
 
void joinMulticastGroup (const L3Address &multicastAddr, int interfaceId=-1)
 Adds the socket to the given multicast group, that is, UDP packets arriving to the given multicast address will be passed up to the socket. More...
 
void joinLocalMulticastGroups (MulticastGroupList mgl)
 Joins the socket to each multicast group that are registered with any of the interfaces. More...
 
void leaveMulticastGroup (const L3Address &multicastAddr)
 Causes the socket to leave the given multicast group, i.e. More...
 
void leaveLocalMulticastGroups (MulticastGroupList mgl)
 Causes the socket to leave each multicast groups that are registered with any of the interfaces. More...
 
void blockMulticastSources (int interfaceId, const L3Address &multicastAddr, const std::vector< L3Address > &sourceList)
 Blocks multicast traffic of the specified group address from specific sources. More...
 
void unblockMulticastSources (int interfaceId, const L3Address &multicastAddr, const std::vector< L3Address > &sourceList)
 Unblocks the multicast traffic of the specified group address from the specified sources. More...
 
void joinMulticastSources (int interfaceId, const L3Address &multicastAddr, const std::vector< L3Address > &sourceList)
 Adds the socket to the given multicast group and source addresses, that is, UDP packets arriving from one of the sources and to the given multicast address will be passed up to the socket. More...
 
void leaveMulticastSources (int interfaceId, const L3Address &multicastAddr, const std::vector< L3Address > &sourceList)
 Causes the socket to leave the given multicast group for the specified sources, i.e. More...
 
void setMulticastSourceFilter (int interfaceId, const L3Address &multicastAddr, UdpSourceFilterMode filterMode, const std::vector< L3Address > &sourceList)
 Sets the source filter for the given multicast group. More...
 
void sendTo (Packet *msg, L3Address destAddr, int destPort)
 Sends a data packet to the given address and port. More...
 
virtual void send (Packet *msg) override
 Sends a data packet to the address and port specified previously in a connect() call. More...
 
virtual void close () override
 Unbinds the socket. More...
 
virtual void destroy () override
 Notify the protocol that the owner of ISocket has destroyed the socket. More...
 
- Public Member Functions inherited from inet::ISocket
virtual ~ISocket ()
 

Protected Member Functions

void sendToUDP (cMessage *msg)
 

Protected Attributes

int socketId
 
ICallbackcb = nullptr
 
void * userData = nullptr
 
cGate * gateToUdp = nullptr
 
State sockState = CLOSED
 

Handling of messages arriving from UDP

virtual bool belongsToSocket (cMessage *msg) const override
 Returns true if the message belongs to this socket instance (message has a UdpControlInfo as getControlInfo(), and the socketId in it matches that of the socket.) More...
 
void setCallback (ICallback *cb)
 Sets a callback object, to be used with processMessage(). More...
 
virtual void processMessage (cMessage *msg) override
 Examines the message, takes ownership, and updates socket state. More...
 
virtual bool isOpen () const override
 
static std::string getReceivedPacketInfo (Packet *pk)
 Utility function: returns a line of information about a packet received via UDP. More...
 

Detailed Description

UdpSocket is a convenience class, to make it easier to send and receive UDP packets from your application models.

You'd have one (or more) UdpSocket object(s) in your application simple module class, and call its member functions (bind(), connect(), sendTo(), etc.) to create and configure a socket, and to send datagrams.

UdpSocket chooses and remembers the socketId for you, assembles and sends command packets such as UDP_C_BIND to UDP, and can also help you deal with packets and notification messages arriving from UDP.

Here is a code fragment that creates an UDP socket and sends a 1K packet over it (the code can be placed in your handleMessage() or activity()):

  UdpSocket socket;
  socket.setOutputGate(gate("udpOut"));
  socket.connect(Address("10.0.0.2"), 2000);
  cPacket *pk = new cPacket("dgram");
  pk->setByteLength(1024);
  socket.send(pk);
  socket.close();

Processing messages sent up by the UDP module is relatively straightforward. You only need to distinguish between data packets and error notifications, by checking the message kind (should be either UDP_I_DATA or UDP_I_ERROR), and casting the control info to UDPDataIndication or UdpErrorIndication. USPSocket provides some help for this with the belongsToSocket() and belongsToAnyUDPSocket() methods.

Member Enumeration Documentation

◆ State

Enumerator
CONNECTED 
CLOSED 
77 { CONNECTED, CLOSED };

Constructor & Destructor Documentation

◆ UdpSocket()

inet::UdpSocket::UdpSocket ( )

Constructor.

The getSocketId() method returns a valid Id right after constructor call.

28 {
29  // don't allow user-specified socketIds because they may conflict with
30  // automatically assigned ones.
31  socketId = getEnvir()->getUniqueNumber();
32 }

◆ ~UdpSocket()

inet::UdpSocket::~UdpSocket ( )
inline

Destructor.

99 {}

Member Function Documentation

◆ belongsToSocket()

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

Returns true if the message belongs to this socket instance (message has a UdpControlInfo as getControlInfo(), and the socketId in it matches that of the socket.)

Implements inet::ISocket.

374 {
375  auto& tags = check_and_cast<ITaggedObject *>(msg)->getTags();
376  const auto& socketInd = tags.findTag<SocketInd>();
377  return socketInd != nullptr && socketInd->getSocketId() == socketId;
378 }

Referenced by inet::UdpSocketIo::handleMessageWhenUp(), inet::Ldp::handleMessageWhenUp(), and processMessage().

◆ bind() [1/2]

◆ bind() [2/2]

void inet::UdpSocket::bind ( L3Address  localAddr,
int  localPort 
)

Bind the socket to a local port number and IP address (useful with multi-homing or multicast addresses).

Use port=0 for an ephemeral port.

40 {
41  if (localPort < -1 || localPort > 65535) // -1: ephemeral port
42  throw cRuntimeError("UdpSocket::bind(): invalid port number %d", localPort);
43 
44  UdpBindCommand *ctrl = new UdpBindCommand();
45  ctrl->setLocalAddr(localAddr);
46  ctrl->setLocalPort(localPort);
47  auto request = new Request("bind", UDP_C_BIND);
48  request->setControlInfo(ctrl);
49  sendToUDP(request);
51 }

◆ blockMulticastSources()

void inet::UdpSocket::blockMulticastSources ( int  interfaceId,
const L3Address multicastAddr,
const std::vector< L3Address > &  sourceList 
)

Blocks multicast traffic of the specified group address from specific sources.

Use this method only if joinMulticastGroup() was previously called for that group.

198 {
199  auto request = new Request("blockMulticastSources", UDP_C_SETOPTION);
200  UdpBlockMulticastSourcesCommand *ctrl = new UdpBlockMulticastSourcesCommand();
201  ctrl->setInterfaceId(interfaceId);
202  ctrl->setMulticastAddr(multicastAddr);
203  ctrl->setSourceListArraySize(sourceList.size());
204  for (size_t i = 0; i < sourceList.size(); ++i)
205  ctrl->setSourceList(i, sourceList[i]);
206  request->setControlInfo(ctrl);
207  sendToUDP(request);
208 }

◆ close()

◆ connect()

void inet::UdpSocket::connect ( L3Address  remoteAddr,
int  remotePort 
)

Connects to a remote UDP socket.

This has two effects: (1) this socket will only receive packets from specified address/port, and (2) you can use send() (as opposed to sendTo()) to send packets.

54 {
55  if (addr.isUnspecified())
56  throw cRuntimeError("UdpSocket::connect(): unspecified remote address");
57  if (port <= 0 || port > 65535)
58  throw cRuntimeError("UdpSocket::connect(): invalid remote port number %d", port);
59 
60  UdpConnectCommand *ctrl = new UdpConnectCommand();
61  ctrl->setRemoteAddr(addr);
62  ctrl->setRemotePort(port);
63  auto request = new Request("connect", UDP_C_CONNECT);
64  request->setControlInfo(ctrl);
65  sendToUDP(request);
67 }

Referenced by inet::NetPerfMeter::establishConnection(), inet::UdpSocketIo::handleStartOperation(), and inet::TunnelApp::initialize().

◆ destroy()

void inet::UdpSocket::destroy ( )
overridevirtual

◆ getReceivedPacketInfo()

std::string inet::UdpSocket::getReceivedPacketInfo ( Packet pk)
static

Utility function: returns a line of information about a packet received via UDP.

316 {
317  auto l3Addresses = pk->getTag<L3AddressInd>();
318  auto ports = pk->getTag<L4PortInd>();
319  L3Address srcAddr = l3Addresses->getSrcAddress();
320  L3Address destAddr = l3Addresses->getDestAddress();
321  int srcPort = ports->getSrcPort();
322  int destPort = ports->getDestPort();
323  int interfaceID = pk->getTag<InterfaceInd>()->getInterfaceId();
324  int ttl = pk->getTag<HopLimitInd>()->getHopLimit();
325 
326  std::stringstream os;
327  os << pk << " (" << pk->getByteLength() << " bytes) ";
328  os << srcAddr << ":" << srcPort << " --> " << destAddr << ":" << destPort;
329  os << " TTL=" << ttl;
330  if (auto tosTag = pk->findTag<TosInd>())
331  os << " TOS=" << tosTag->getTos();
332  if (auto dscpTag = pk->findTag<DscpInd>())
333  os << " DSCP=" << dscpTag->getDifferentiatedServicesCodePoint();
334  os << " on ifID=" << interfaceID;
335  return os.str();
336 }

Referenced by inet::UdpSink::processPacket(), inet::UdpBasicApp::processPacket(), inet::UdpBasicBurst::processPacket(), inet::UdpVideoStreamClient::receiveStream(), and inet::UdpSocketIo::socketDataArrived().

◆ getSocketId()

int inet::UdpSocket::getSocketId ( ) const
inlineoverridevirtual

Returns the internal socket Id.

Implements inet::ISocket.

108 { return socketId; }

Referenced by inet::sctp::Sctp::bindPortForUDP(), inet::operator<<(), and inet::Ldp::processLDPHello().

◆ getState()

State inet::UdpSocket::getState ( ) const
inline
103 { return sockState; }

◆ getUserData()

void* inet::UdpSocket::getUserData ( ) const
inline
101 { return userData; }

◆ isOpen()

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

◆ joinLocalMulticastGroups()

void inet::UdpSocket::joinLocalMulticastGroups ( MulticastGroupList  mgl)

Joins the socket to each multicast group that are registered with any of the interfaces.

266 {
267  if (mgl.size() > 0) {
268  UdpJoinMulticastGroupsCommand *ctrl = new UdpJoinMulticastGroupsCommand();
269  ctrl->setMulticastAddrArraySize(mgl.size());
270  ctrl->setInterfaceIdArraySize(mgl.size());
271 
272  for (unsigned int j = 0; j < mgl.size(); ++j) {
273  ctrl->setMulticastAddr(j, mgl[j].multicastAddr);
274  ctrl->setInterfaceId(j, mgl[j].interfaceId);
275  }
276 
277  auto request = new Request("joinMulticastGroups", UDP_C_SETOPTION);
278  request->setControlInfo(ctrl);
279  sendToUDP(request);
280  }
281 }

Referenced by inet::rtp::Rtcp::createSocket(), inet::rtp::Rtp::createSocket(), inet::UdpEchoApp::handleStartOperation(), inet::UdpSocketIo::setSocketOptions(), inet::UdpSink::setSocketOptions(), and inet::UdpBasicApp::setSocketOptions().

◆ joinMulticastGroup()

void inet::UdpSocket::joinMulticastGroup ( const L3Address multicastAddr,
int  interfaceId = -1 
)

Adds the socket to the given multicast group, that is, UDP packets arriving to the given multicast address will be passed up to the socket.

One can also optionally specify the output interface for packets sent to that address.

176 {
177  auto request = new Request("joinMulticastGroups", UDP_C_SETOPTION);
178  UdpJoinMulticastGroupsCommand *ctrl = new UdpJoinMulticastGroupsCommand();
179  ctrl->setMulticastAddrArraySize(1);
180  ctrl->setMulticastAddr(0, multicastAddr);
181  ctrl->setInterfaceIdArraySize(1);
182  ctrl->setInterfaceId(0, interfaceId);
183  request->setControlInfo(ctrl);
184  sendToUDP(request);
185 }

Referenced by inet::UdpSocketIo::setSocketOptions(), inet::UdpSink::setSocketOptions(), and inet::Rip::startRIPRouting().

◆ joinMulticastSources()

void inet::UdpSocket::joinMulticastSources ( int  interfaceId,
const L3Address multicastAddr,
const std::vector< L3Address > &  sourceList 
)

Adds the socket to the given multicast group and source addresses, that is, UDP packets arriving from one of the sources and to the given multicast address will be passed up to the socket.

237 {
238  auto request = new Request("joinMulticastSources", UDP_C_SETOPTION);
239  UdpJoinMulticastSourcesCommand *ctrl = new UdpJoinMulticastSourcesCommand();
240  ctrl->setInterfaceId(interfaceId);
241  ctrl->setMulticastAddr(multicastAddr);
242  ctrl->setSourceListArraySize(sourceList.size());
243  for (size_t i = 0; i < sourceList.size(); ++i)
244  ctrl->setSourceList(i, sourceList[i]);
245  request->setControlInfo(ctrl);
246  sendToUDP(request);
247 }

◆ leaveLocalMulticastGroups()

void inet::UdpSocket::leaveLocalMulticastGroups ( MulticastGroupList  mgl)

Causes the socket to leave each multicast groups that are registered with any of the interfaces.

285 {
286  if (mgl.size() > 0) {
287  UdpLeaveMulticastGroupsCommand *ctrl = new UdpLeaveMulticastGroupsCommand();
288  ctrl->setMulticastAddrArraySize(mgl.size());
289 
290  for (unsigned int j = 0; j < mgl.size(); ++j) {
291  ctrl->setMulticastAddr(j, mgl[j].multicastAddr);
292  }
293 
294  auto request = new Request("leaveMulticastGroups", UDP_C_SETOPTION);
295  request->setControlInfo(ctrl);
296  sendToUDP(request);
297  }
298 }

◆ leaveMulticastGroup()

void inet::UdpSocket::leaveMulticastGroup ( const L3Address multicastAddr)

Causes the socket to leave the given multicast group, i.e.

UDP packets arriving to the given multicast address will no longer passed up to the socket.

188 {
189  auto request = new Request("leaveMulticastGroups", UDP_C_SETOPTION);
190  UdpLeaveMulticastGroupsCommand *ctrl = new UdpLeaveMulticastGroupsCommand();
191  ctrl->setMulticastAddrArraySize(1);
192  ctrl->setMulticastAddr(0, multicastAddr);
193  request->setControlInfo(ctrl);
194  sendToUDP(request);
195 }

Referenced by inet::UdpSink::handleCrashOperation(), inet::UdpSink::handleStopOperation(), and inet::UdpSink::processStop().

◆ leaveMulticastSources()

void inet::UdpSocket::leaveMulticastSources ( int  interfaceId,
const L3Address multicastAddr,
const std::vector< L3Address > &  sourceList 
)

Causes the socket to leave the given multicast group for the specified sources, i.e.

UDP packets arriving from those sources to the given multicast address will no longer passed up to the socket.

224 {
225  auto request = new Request("leaveMulticastSources", UDP_C_SETOPTION);
226  UdpLeaveMulticastSourcesCommand *ctrl = new UdpLeaveMulticastSourcesCommand();
227  ctrl->setInterfaceId(interfaceId);
228  ctrl->setMulticastAddr(multicastAddr);
229  ctrl->setSourceListArraySize(sourceList.size());
230  for (size_t i = 0; i < sourceList.size(); ++i)
231  ctrl->setSourceList(i, sourceList[i]);
232  request->setControlInfo(ctrl);
233  sendToUDP(request);
234 }

◆ processMessage()

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

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

Implements inet::ISocket.

344 {
345  ASSERT(belongsToSocket(msg));
346 
347  switch (msg->getKind()) {
348  case UDP_I_DATA:
349  if (cb)
350  cb->socketDataArrived(this, check_and_cast<Packet *>(msg));
351  else
352  delete msg;
353  break;
354  case UDP_I_ERROR:
355  if (cb)
356  cb->socketErrorArrived(this, check_and_cast<Indication *>(msg));
357  else
358  delete msg;
359  break;
360  case UDP_I_SOCKET_CLOSED:
361  check_and_cast<Indication *>(msg);
362  sockState = CLOSED;
363  if (cb)
364  cb->socketClosed(this);
365  delete msg;
366  break;
367  default:
368  throw cRuntimeError("UdpSocket: invalid msg kind %d, one of the UDP_I_xxx constants expected", msg->getKind());
369  break;
370  }
371 }

Referenced by inet::VoipStreamReceiver::handleMessage(), inet::SimpleVoipReceiver::handleMessage(), inet::UdpSocketIo::handleMessageWhenUp(), inet::UdpEchoApp::handleMessageWhenUp(), inet::UdpVideoStreamClient::handleMessageWhenUp(), inet::UdpSink::handleMessageWhenUp(), inet::UdpBasicApp::handleMessageWhenUp(), inet::DhcpServer::handleMessageWhenUp(), inet::UdpVideoStreamServer::handleMessageWhenUp(), inet::DhcpClient::handleMessageWhenUp(), inet::UdpBasicBurst::handleMessageWhenUp(), inet::aodv::Aodv::handleMessageWhenUp(), and inet::Ldp::handleMessageWhenUp().

◆ send()

void inet::UdpSocket::send ( Packet msg)
overridevirtual

Sends a data packet to the address and port specified previously in a connect() call.

Implements inet::ISocket.

81 {
82  pk->setKind(UDP_C_DATA);
83  sendToUDP(pk);
85 }

Referenced by inet::UdpSocketIo::handleMessageWhenUp(), inet::aodv::Aodv::handleMessageWhenUp(), inet::aodv::Aodv::sendAODVPacket(), inet::TunnelApp::socketDataArrived(), and inet::NetPerfMeter::transmitFrame().

◆ sendTo()

void inet::UdpSocket::sendTo ( Packet msg,
L3Address  destAddr,
int  destPort 
)

Sends a data packet to the given address and port.

Additional options can be passed in a SendOptions struct.

70 {
71  pk->setKind(UDP_C_DATA);
72  auto addressReq = pk->addTagIfAbsent<L3AddressReq>();
73  addressReq->setDestAddress(destAddr);
74  if (destPort != -1)
75  pk->addTagIfAbsent<L4PortReq>()->setDestPort(destPort);
76  sendToUDP(pk);
78 }

Referenced by inet::rtp::Rtcp::createPacket(), inet::rtp::Rtp::dataOut(), inet::UdpBasicBurst::generateBurst(), inet::VoipStreamSender::handleMessage(), inet::Rip::processRequest(), inet::UdpVideoStreamClient::requestStream(), inet::Ldp::sendHelloTo(), inet::UdpBasicApp::sendPacket(), inet::Rip::sendPacket(), inet::UdpVideoStreamServer::sendStreamData(), inet::DhcpServer::sendToUDP(), inet::DhcpClient::sendToUdp(), inet::SimpleVoipSender::sendVoIPPacket(), and inet::UdpEchoApp::socketDataArrived().

◆ sendToUDP()

void inet::UdpSocket::sendToUDP ( cMessage *  msg)
protected
305 {
306  if (!gateToUdp)
307  throw cRuntimeError("UdpSocket: setOutputGate() must be invoked before socket can be used");
308  EV_DEBUG << "Sending to UDP protocol" << EV_FIELD(msg) << EV_ENDL;
309  auto& tags = check_and_cast<ITaggedObject *>(msg)->getTags();
310  tags.addTagIfAbsent<DispatchProtocolReq>()->setProtocol(&Protocol::udp);
311  tags.addTagIfAbsent<SocketReq>()->setSocketId(socketId);
312  check_and_cast<cSimpleModule *>(gateToUdp->getOwnerModule())->send(msg, gateToUdp);
313 }

Referenced by bind(), blockMulticastSources(), close(), connect(), destroy(), joinLocalMulticastGroups(), joinMulticastGroup(), joinMulticastSources(), leaveLocalMulticastGroups(), leaveMulticastGroup(), leaveMulticastSources(), send(), sendTo(), setBroadcast(), setDscp(), setMulticastLoop(), setMulticastOutputInterface(), setMulticastSourceFilter(), setReuseAddress(), setTimeToLive(), setTos(), and unblockMulticastSources().

◆ setBroadcast()

void inet::UdpSocket::setBroadcast ( bool  broadcast)

Set the Broadcast option on the UDP socket.

This will cause the socket to receive broadcast packets as well.

140 {
141  auto request = new Request("setBroadcast", UDP_C_SETOPTION);
142  UdpSetBroadcastCommand *ctrl = new UdpSetBroadcastCommand();
143  ctrl->setBroadcast(broadcast);
144  request->setControlInfo(ctrl);
145  sendToUDP(request);
146 }

Referenced by inet::aodv::Aodv::handleStartOperation(), inet::DhcpServer::openSocket(), inet::DhcpClient::openSocket(), inet::UdpSocketIo::setSocketOptions(), inet::UdpSink::setSocketOptions(), and inet::UdpBasicApp::setSocketOptions().

◆ setCallback()

void inet::UdpSocket::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 UdpSocket::ICallback

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

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

339 {
340  cb = callback;
341 }

Referenced by inet::UdpEchoApp::handleCrashOperation(), inet::UdpVideoStreamServer::handleCrashOperation(), inet::UdpEchoApp::handleStartOperation(), inet::UdpVideoStreamServer::handleStartOperation(), inet::aodv::Aodv::handleStartOperation(), inet::UdpVideoStreamServer::handleStopOperation(), inet::TunnelApp::initialize(), inet::VoipStreamReceiver::initialize(), inet::DhcpServer::initialize(), inet::DhcpClient::initialize(), inet::SimpleVoipReceiver::initialize(), inet::UdpBasicBurst::processStart(), inet::UdpBasicBurst::processStop(), inet::UdpVideoStreamClient::requestStream(), inet::UdpSocketIo::setSocketOptions(), inet::UdpSink::setSocketOptions(), and inet::UdpBasicApp::setSocketOptions().

◆ setDscp()

void inet::UdpSocket::setDscp ( short  dscp)

Sets the Ipv4 / Ipv6 dscp fields of packets sent from the UDP socket.

122 {
123  auto request = new Request("setDscp", UDP_C_SETOPTION);
124  auto *ctrl = new UdpSetDscpCommand();
125  ctrl->setDscp(dscp);
126  request->setControlInfo(ctrl);
127  sendToUDP(request);
128 }

Referenced by inet::UdpVideoStreamServer::handleStartOperation(), inet::VoipStreamSender::initialize(), inet::UdpBasicBurst::processStart(), inet::UdpSocketIo::setSocketOptions(), and inet::UdpBasicApp::setSocketOptions().

◆ setMulticastLoop()

void inet::UdpSocket::setMulticastLoop ( bool  value)

The boolean value specifies whether sent multicast packets should be looped back to the local sockets (like the Unix IP_MULTICAST_LOOP socket option).

158 {
159  auto request = new Request("setMulticastLoop", UDP_C_SETOPTION);
160  UdpSetMulticastLoopCommand *ctrl = new UdpSetMulticastLoopCommand();
161  ctrl->setLoop(value);
162  request->setControlInfo(ctrl);
163  sendToUDP(request);
164 }

Referenced by inet::Rip::startRIPRouting().

◆ setMulticastOutputInterface()

void inet::UdpSocket::setMulticastOutputInterface ( int  interfaceId)

Set the output interface for sending multicast packets (like the Unix IP_MULTICAST_IF socket option).

The argument is the interface's ID in InterfaceTable.

149 {
150  auto request = new Request("setMulticastOutputIf", UDP_C_SETOPTION);
151  UdpSetMulticastInterfaceCommand *ctrl = new UdpSetMulticastInterfaceCommand();
152  ctrl->setInterfaceId(interfaceId);
153  request->setControlInfo(ctrl);
154  sendToUDP(request);
155 }

Referenced by inet::UdpSocketIo::setSocketOptions(), and inet::UdpBasicApp::setSocketOptions().

◆ setMulticastSourceFilter()

void inet::UdpSocket::setMulticastSourceFilter ( int  interfaceId,
const L3Address multicastAddr,
UdpSourceFilterMode  filterMode,
const std::vector< L3Address > &  sourceList 
)

Sets the source filter for the given multicast group.

If filterMode is INCLUDE, then UDP packets arriving from one of the sources and to to given multicast group will be passed up to the socket. If filterMode is EXCLUDE, then all UDP packets arriving to the given multicast group will be passed up except those that arrive from the specified sources.

251 {
252  auto request = new Request("setMulticastSourceFilter", UDP_C_SETOPTION);
253  UdpSetMulticastSourceFilterCommand *ctrl = new UdpSetMulticastSourceFilterCommand();
254  ctrl->setInterfaceId(interfaceId);
255  ctrl->setMulticastAddr(multicastAddr);
256  ctrl->setFilterMode(filterMode);
257  ctrl->setSourceListArraySize(sourceList.size());
258  for (size_t i = 0; i < sourceList.size(); ++i)
259  ctrl->setSourceList(i, sourceList[i]);
260  request->setControlInfo(ctrl);
261  sendToUDP(request);
262 }

◆ setOutputGate()

◆ setReuseAddress()

void inet::UdpSocket::setReuseAddress ( bool  value)

Set the ReuseAddress option on the UDP socket.

It is possible to use an already bound address/port in the bind() command only if both the original and the new socket set the ReuseAddress flag to true. Note that only one socket will receive the packets arrived at that address/port (the last bound one). This option works like REUSE_ADDR socket option in Linux.

167 {
168  auto request = new Request("setReuseAddress", UDP_C_SETOPTION);
169  UdpSetReuseAddressCommand *ctrl = new UdpSetReuseAddressCommand();
170  ctrl->setReuseAddress(value);
171  request->setControlInfo(ctrl);
172  sendToUDP(request);
173 }

◆ setTimeToLive()

void inet::UdpSocket::setTimeToLive ( int  ttl)

Set the TTL (Ipv6: Hop Limit) field on sent packets.

113 {
114  auto request = new Request("setTTL", UDP_C_SETOPTION);
115  UdpSetTimeToLiveCommand *ctrl = new UdpSetTimeToLiveCommand();
116  ctrl->setTtl(ttl);
117  request->setControlInfo(ctrl);
118  sendToUDP(request);
119 }

Referenced by inet::UdpVideoStreamServer::handleStartOperation(), inet::VoipStreamSender::initialize(), inet::UdpBasicBurst::processStart(), inet::Rip::sendPacket(), inet::UdpSocketIo::setSocketOptions(), and inet::UdpBasicApp::setSocketOptions().

◆ setTos()

void inet::UdpSocket::setTos ( short  tos)

Sets the Ipv4 Type of Service / Ipv6 Traffic Class fields of packets sent from the UDP socket.

131 {
132  auto request = new Request("setTos", UDP_C_SETOPTION);
133  auto *ctrl = new UdpSetTosCommand();
134  ctrl->setTos(tos);
135  request->setControlInfo(ctrl);
136  sendToUDP(request);
137 }

Referenced by inet::UdpVideoStreamServer::handleStartOperation(), inet::VoipStreamSender::initialize(), inet::UdpBasicBurst::processStart(), inet::UdpSocketIo::setSocketOptions(), and inet::UdpBasicApp::setSocketOptions().

◆ setUserData()

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

◆ unblockMulticastSources()

void inet::UdpSocket::unblockMulticastSources ( int  interfaceId,
const L3Address multicastAddr,
const std::vector< L3Address > &  sourceList 
)

Unblocks the multicast traffic of the specified group address from the specified sources.

Use this method only if the traffic was previously blocked by calling blockMulticastSources().

211 {
212  auto request = new Request("unblockMulticastSources", UDP_C_SETOPTION);
213  UdpUnblockMulticastSourcesCommand *ctrl = new UdpUnblockMulticastSourcesCommand();
214  ctrl->setInterfaceId(interfaceId);
215  ctrl->setMulticastAddr(multicastAddr);
216  ctrl->setSourceListArraySize(sourceList.size());
217  for (size_t i = 0; i < sourceList.size(); ++i)
218  ctrl->setSourceList(i, sourceList[i]);
219  request->setControlInfo(ctrl);
220  sendToUDP(request);
221 }

Member Data Documentation

◆ cb

ICallback* inet::UdpSocket::cb = nullptr
protected

Referenced by processMessage(), and setCallback().

◆ gateToUdp

cGate* inet::UdpSocket::gateToUdp = nullptr
protected

Referenced by destroy(), and sendToUDP().

◆ socketId

int inet::UdpSocket::socketId
protected

◆ sockState

State inet::UdpSocket::sockState = CLOSED
protected

◆ userData

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

The documentation for this class was generated from the following files:
inet::UdpSocket::ICallback::socketErrorArrived
virtual void socketErrorArrived(UdpSocket *socket, Indication *indication)=0
Notifies about error indication arrival, indication ownership is transferred to the callee.
inet::UdpSocket::bind
void bind(int localPort)
Bind the socket to a local port number.
Definition: UdpSocket.cc:34
inet::UDP_C_CONNECT
@ UDP_C_CONNECT
Definition: UdpControlInfo_m.h:88
L4PortReq
removed L4PortReq
Definition: IUdp-gates.txt:11
DispatchProtocolReq
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd DispatchProtocolReq L4PortInd Ipv4ControlInfo Ipv6ControlInfo down DispatchProtocolReq
Definition: IUdp-gates.txt:25
L3AddressInd
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd L3AddressInd
Definition: IUdp-gates.txt:20
inet::UdpSocket::userData
void * userData
Definition: UdpSocket.h:82
inet::UdpSocket::ICallback::socketDataArrived
virtual void socketDataArrived(UdpSocket *socket, Packet *packet)=0
Notifies about data arrival, packet ownership is transferred to the callee.
inet::UdpSocket::gateToUdp
cGate * gateToUdp
Definition: UdpSocket.h:83
ctrl
removed ctrl
Definition: IUdp-gates.txt:7
inet::UDP_I_SOCKET_CLOSED
@ UDP_I_SOCKET_CLOSED
Definition: UdpControlInfo_m.h:159
EV_FIELD
#define EV_FIELD(...)
Definition: INETDefs.h:112
inet::UdpSocket::cb
ICallback * cb
Definition: UdpSocket.h:81
inet::Protocol::udp
static const Protocol udp
Definition: Protocol.h:117
inet::UdpSocket::sockState
State sockState
Definition: UdpSocket.h:84
inet::UDP_C_DESTROY
@ UDP_C_DESTROY
Definition: UdpControlInfo_m.h:91
inet::UdpSocket::CLOSED
@ CLOSED
Definition: UdpSocket.h:77
inet::UdpSocket::belongsToSocket
virtual bool belongsToSocket(cMessage *msg) const override
Returns true if the message belongs to this socket instance (message has a UdpControlInfo as getContr...
Definition: UdpSocket.cc:373
inet::UDP_C_BIND
@ UDP_C_BIND
Definition: UdpControlInfo_m.h:87
L4PortInd
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd L4PortInd
Definition: IUdp-gates.txt:20
inet::UDP_I_DATA
@ UDP_I_DATA
Definition: UdpControlInfo_m.h:157
inet::UdpSocket::ICallback::socketClosed
virtual void socketClosed(UdpSocket *socket)=0
Notifies about socket closed, indication ownership is transferred to the callee.
inet::UDP_C_DATA
@ UDP_C_DATA
Definition: UdpControlInfo_m.h:86
inet::UdpSocket::CONNECTED
@ CONNECTED
Definition: UdpSocket.h:77
tags
* tags
Definition: IUdp-gates.txt:3
inet::UDP_C_SETOPTION
@ UDP_C_SETOPTION
Definition: UdpControlInfo_m.h:89
inet::UdpSocket::sendToUDP
void sendToUDP(cMessage *msg)
Definition: UdpSocket.cc:304
inet::UdpSocket::socketId
int socketId
Definition: UdpSocket.h:80
EV_ENDL
#define EV_ENDL
Definition: INETDefs.h:114
inet::UDP_I_ERROR
@ UDP_I_ERROR
Definition: UdpControlInfo_m.h:158
inet::UDP_C_CLOSE
@ UDP_C_CLOSE
Definition: UdpControlInfo_m.h:90