INET Framework for OMNeT++/OMNEST
|
UDPSocket is a convenience class, to make it easier to send and receive UDP packets from your application models. More...
#include <UDPSocket.h>
Classes | |
struct | SendOptions |
Public Member Functions | |
UDPSocket () | |
Constructor. More... | |
~UDPSocket () | |
Destructor. More... | |
int | getSocketId () const |
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 | setTypeOfService (unsigned char 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 (cPacket *msg, L3Address destAddr, int destPort, const SendOptions *options=nullptr) |
Sends a data packet to the given address and port. More... | |
void | send (cPacket *msg) |
Sends a data packet to the address and port specified previously in a connect() call. More... | |
void | close () |
Unbinds the socket. More... | |
Static Public Member Functions | |
static int | generateSocketId () |
Generates a new socket id. More... | |
Protected Member Functions | |
void | sendToUDP (cMessage *msg) |
Protected Attributes | |
int | sockId |
cGate * | gateToUdp |
Handling of messages arriving from UDP | |
bool | belongsToSocket (cMessage *msg) |
Returns true if the message belongs to this socket instance (message has a UDPControlInfo as getControlInfo(), and the sockId in it matches that of the socket.) More... | |
static bool | belongsToAnyUDPSocket (cMessage *msg) |
Returns true if the message belongs to any UDPSocket instance. More... | |
static std::string | getReceivedPacketInfo (cPacket *pk) |
Utility function: returns a line of information about a packet received via UDP. More... | |
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 sockId 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.
inet::UDPSocket::UDPSocket | ( | ) |
Constructor.
The getSocketId() method returns a valid Id right after constructor call.
|
static |
Returns true if the message belongs to any UDPSocket instance.
(This basically checks if the message has an UDPControlInfo attached to it as getControlInfo().)
bool inet::UDPSocket::belongsToSocket | ( | cMessage * | msg | ) |
Returns true if the message belongs to this socket instance (message has a UDPControlInfo as getControlInfo(), and the sockId in it matches that of the socket.)
void inet::UDPSocket::bind | ( | int | localPort | ) |
Bind the socket to a local port number.
Use port=0 for ephemeral port.
Referenced by inet::sctp::SCTP::bindPortForUDP(), inet::NetPerfMeter::createAndBindSocket(), inet::rtp::RTCP::createSocket(), inet::rtp::RTP::createSocket(), inet::UDPEchoApp::handleNodeStart(), inet::UDPVideoStreamSvr::handleNodeStart(), inet::VoIPStreamReceiver::initialize(), inet::SimpleVoIPSender::initialize(), inet::VoIPStreamSender::initialize(), inet::SimpleVoIPReceiver::initialize(), inet::LDP::initialize(), inet::DHCPServer::openSocket(), inet::DHCPClient::openSocket(), inet::UDPSink::processStart(), inet::UDPBasicApp::processStart(), inet::UDPBasicBurst::processStart(), and inet::UDPVideoStreamCli::requestStream().
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.
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.
void inet::UDPSocket::close | ( | ) |
Unbinds the socket.
Once closed, a closed socket may be bound to another (or the same) port, and reused.
Referenced by inet::UDPSink::processStop(), inet::UDPBasicApp::processStop(), and inet::UDPBasicBurst::processStop().
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.
Referenced by inet::NetPerfMeter::establishConnection().
|
static |
Generates a new socket id.
Referenced by UDPSocket().
|
static |
Utility function: returns a line of information about a packet received via UDP.
Referenced by inet::UDPSink::processPacket(), inet::UDPBasicApp::processPacket(), inet::UDPBasicBurst::processPacket(), and inet::UDPVideoStreamCli::receiveStream().
|
inline |
Returns the internal socket Id.
void inet::UDPSocket::joinLocalMulticastGroups | ( | MulticastGroupList | mgl | ) |
Joins the socket to each multicast group that are registered with any of the interfaces.
Referenced by inet::rtp::RTCP::createSocket(), inet::rtp::RTP::createSocket(), inet::UDPEchoApp::handleNodeStart(), inet::UDPSink::setSocketOptions(), and inet::UDPBasicApp::setSocketOptions().
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.
Referenced by inet::UDPSink::setSocketOptions().
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.
void inet::UDPSocket::leaveLocalMulticastGroups | ( | MulticastGroupList | mgl | ) |
Causes the socket to leave each multicast groups that are registered with any of the interfaces.
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.
Referenced by inet::UDPSink::processStop().
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.
void inet::UDPSocket::send | ( | cPacket * | msg | ) |
Sends a data packet to the address and port specified previously in a connect() call.
Referenced by sendToUDP(), and inet::NetPerfMeter::transmitFrame().
void inet::UDPSocket::sendTo | ( | cPacket * | msg, |
L3Address | destAddr, | ||
int | destPort, | ||
const SendOptions * | options = nullptr |
||
) |
Sends a data packet to the given address and port.
Additional options can be passed in a SendOptions struct.
Referenced by inet::rtp::RTCP::createPacket(), inet::rtp::RTP::dataOut(), inet::UDPBasicBurst::generateBurst(), inet::VoIPStreamSender::handleMessage(), inet::UDPEchoApp::handleMessageWhenUp(), inet::UDPVideoStreamCli::requestStream(), inet::sctp::SCTP::sendAbortFromMain(), inet::LDP::sendHelloTo(), inet::UDPBasicApp::sendPacket(), inet::UDPVideoStreamSvr::sendStreamData(), inet::sctp::SCTPAssociation::sendToIP(), inet::DHCPServer::sendToUDP(), inet::DHCPClient::sendToUDP(), and inet::SimpleVoIPSender::sendVoIPPacket().
|
protected |
Referenced by bind(), blockMulticastSources(), close(), connect(), joinLocalMulticastGroups(), joinMulticastGroup(), joinMulticastSources(), leaveLocalMulticastGroups(), leaveMulticastGroup(), leaveMulticastSources(), send(), sendTo(), setBroadcast(), setMulticastLoop(), setMulticastOutputInterface(), setMulticastSourceFilter(), setReuseAddress(), setTimeToLive(), setTypeOfService(), and unblockMulticastSources().
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.
Referenced by inet::DHCPServer::openSocket(), inet::DHCPClient::openSocket(), inet::UDPSink::setSocketOptions(), and inet::UDPBasicApp::setSocketOptions().
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).
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.
Referenced by inet::UDPBasicApp::setSocketOptions().
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.
|
inline |
Sets the gate on which to send to UDP.
Must be invoked before socket can be used. Example: socket.setOutputGate(gate("udpOut"));
Referenced by inet::sctp::SCTP::bindPortForUDP(), inet::NetPerfMeter::createAndBindSocket(), inet::UDPEchoApp::handleNodeStart(), inet::UDPVideoStreamSvr::handleNodeStart(), inet::rtp::RTP::initialize(), inet::rtp::RTCP::initialize(), inet::VoIPStreamReceiver::initialize(), inet::SimpleVoIPSender::initialize(), inet::VoIPStreamSender::initialize(), inet::SimpleVoIPReceiver::initialize(), inet::LDP::initialize(), inet::DHCPServer::openSocket(), inet::DHCPClient::openSocket(), inet::UDPSink::processStart(), inet::UDPBasicApp::processStart(), inet::UDPBasicBurst::processStart(), and inet::UDPVideoStreamCli::requestStream().
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.
void inet::UDPSocket::setTimeToLive | ( | int | ttl | ) |
Set the TTL (IPv6: Hop Limit) field on sent packets.
Referenced by inet::UDPBasicApp::setSocketOptions().
void inet::UDPSocket::setTypeOfService | ( | unsigned char | tos | ) |
Sets the IPv4 Type of Service / IPv6 Traffic Class fields of packets sent from the UDP socket.
Referenced by inet::UDPBasicApp::setSocketOptions().
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().
|
protected |
Referenced by sendToUDP(), and UDPSocket().
|
protected |
Referenced by belongsToSocket(), bind(), blockMulticastSources(), close(), connect(), joinLocalMulticastGroups(), joinMulticastGroup(), joinMulticastSources(), leaveLocalMulticastGroups(), leaveMulticastGroup(), leaveMulticastSources(), send(), sendTo(), setBroadcast(), setMulticastLoop(), setMulticastOutputInterface(), setMulticastSourceFilter(), setReuseAddress(), setTimeToLive(), setTypeOfService(), UDPSocket(), and unblockMulticastSources().