23#include "ns3/assert.h"
24#include "ns3/boolean.h"
27#include "ns3/object-map.h"
28#include "ns3/packet.h"
30#include <unordered_map>
43 TypeId(
"ns3::UdpL4Protocol")
45 .SetGroupName(
"Internet")
47 .AddAttribute(
"SocketList",
48 "A container of sockets associated to this protocol. "
49 "The underlying type is an unordered map, the attribute name "
50 "is kept for backward compatibility.",
90 if (node && (ipv4 || ipv6))
94 udpFactory->SetUdp(
this);
95 node->AggregateObject(udpFactory);
158 socket->SetUdp(
this);
198 NS_LOG_FUNCTION(
this << boundNetDevice << localAddress << localPort << peerAddress << peerPort);
199 return m_endPoints->
Allocate(boundNetDevice, localAddress, localPort, peerAddress, peerPort);
244 NS_LOG_FUNCTION(
this << boundNetDevice << localAddress << localPort << peerAddress << peerPort);
263 const uint8_t payload[8])
265 NS_LOG_FUNCTION(
this << icmpSource << icmpTtl << icmpType << icmpCode << icmpInfo
266 << payloadSource << payloadDestination);
269 src = payload[0] << 8;
271 dst = payload[2] << 8;
275 if (endPoint !=
nullptr)
277 endPoint->
ForwardIcmp(icmpSource, icmpTtl, icmpType, icmpCode, icmpInfo);
281 NS_LOG_DEBUG(
"no endpoint found source=" << payloadSource
282 <<
", destination=" << payloadDestination
283 <<
", src=" << src <<
", dst=" << dst);
295 const uint8_t payload[8])
297 NS_LOG_FUNCTION(
this << icmpSource << icmpTtl << icmpType << icmpCode << icmpInfo
298 << payloadSource << payloadDestination);
301 src = payload[0] << 8;
303 dst = payload[2] << 8;
308 if (endPoint !=
nullptr)
310 endPoint->
ForwardIcmp(icmpSource, icmpTtl, icmpType, icmpCode, icmpInfo);
314 NS_LOG_DEBUG(
"no endpoint found source=" << payloadSource
315 <<
", destination=" << payloadDestination
316 <<
", src=" << src <<
", dst=" << dst);
337 packet->PeekHeader(udpHeader);
352 if (endPoints.empty())
356 NS_LOG_LOGIC(
" No Ipv4 endpoints matched on UdpL4Protocol, trying Ipv6 " <<
this);
363 return (this->
Receive(packet, ipv6Header, fakeInterface));
370 packet->RemoveHeader(udpHeader);
371 for (
auto endPoint = endPoints.begin(); endPoint != endPoints.end(); endPoint++)
373 (*endPoint)->ForwardUp(packet->Copy(), header, udpHeader.
GetSourcePort(), interface);
390 packet->RemoveHeader(udpHeader);
405 if (endPoints.empty())
410 for (
auto endPoint = endPoints.begin(); endPoint != endPoints.end(); endPoint++)
412 (*endPoint)->ForwardUp(packet->Copy(), header, udpHeader.
GetSourcePort(), interface);
435 packet->AddHeader(udpHeader);
448 NS_LOG_FUNCTION(
this << packet << saddr << daddr << sport << dport << route);
459 packet->AddHeader(udpHeader);
482 packet->AddHeader(udpHeader);
495 NS_LOG_FUNCTION(
this << packet << saddr << daddr << sport << dport << route);
506 packet->AddHeader(udpHeader);
544 if (socketItem.second == socket)
546 socketItem.second =
nullptr;
void Nullify()
Discard the implementation, set it to null.
bool IsNull() const
Check for null implementation.
L4 Protocol abstract base class.
Ipv4 addresses are stored in host order in this class.
Demultiplexes packets to various transport layer endpoints.
Ipv4EndPoint * SimpleLookup(Ipv4Address daddr, uint16_t dport, Ipv4Address saddr, uint16_t sport)
simple lookup for a match with all the parameters.
EndPoints Lookup(Ipv4Address daddr, uint16_t dport, Ipv4Address saddr, uint16_t sport, Ptr< Ipv4Interface > incomingInterface)
lookup for a match with all the parameters.
void DeAllocate(Ipv4EndPoint *endPoint)
Remove a end point.
Ipv4EndPoint * Allocate()
Allocate a Ipv4EndPoint.
std::list< Ipv4EndPoint * > EndPoints
Container of the IPv4 endpoints.
A representation of an internet endpoint/connection.
void ForwardIcmp(Ipv4Address icmpSource, uint8_t icmpTtl, uint8_t icmpType, uint8_t icmpCode, uint32_t icmpInfo)
Forward the ICMP packet to the upper level.
virtual void Send(Ptr< Packet > packet, Ipv4Address source, Ipv4Address destination, uint8_t protocol, Ptr< Ipv4Route > route)=0
Describes an IPv6 address.
bool IsIpv4MappedAddress() const
If the address is an IPv4-mapped address.
static Ipv6Address MakeIpv4MappedAddress(Ipv4Address addr)
Make the Ipv4-mapped IPv6 address.
Demultiplexer for end points.
EndPoints Lookup(Ipv6Address dst, uint16_t dport, Ipv6Address src, uint16_t sport, Ptr< Ipv6Interface > incomingInterface)
lookup for a match with all the parameters.
Ipv6EndPoint * Allocate()
Allocate a Ipv6EndPoint.
Ipv6EndPoint * SimpleLookup(Ipv6Address dst, uint16_t dport, Ipv6Address src, uint16_t sport)
Simple lookup for a four-tuple match.
void DeAllocate(Ipv6EndPoint *endPoint)
Remove a end point.
std::list< Ipv6EndPoint * > EndPoints
Container of the IPv6 endpoints.
A representation of an IPv6 endpoint/connection.
void ForwardIcmp(Ipv6Address src, uint8_t ttl, uint8_t type, uint8_t code, uint32_t info)
Forward the ICMP packet to the upper level.
Access to the IPv6 forwarding table, interfaces, and configuration.
virtual void Send(Ptr< Packet > packet, Ipv6Address source, Ipv6Address destination, uint8_t protocol, Ptr< Ipv6Route > route)=0
Higher-level layers call this method to send a packet down the stack to the MAC and PHY layers.
static bool ChecksumEnabled()
virtual void NotifyNewAggregate()
Notify all Objects aggregated to this one of a new Object being aggregated.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
virtual void DoDispose()
Destructor implementation.
Smart pointer class similar to boost::intrusive_ptr.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Implementation of the UDP protocol.
void SetDownTarget6(IpL4Protocol::DownTargetCallback6 cb) override
This method allows a caller to set the current down target callback set for this L4 protocol (IPv6 ca...
Ptr< Socket > CreateSocket()
void Send(Ptr< Packet > packet, Ipv4Address saddr, Ipv4Address daddr, uint16_t sport, uint16_t dport)
Send a packet via UDP (IPv4)
Ipv6EndPoint * Allocate6()
Allocate an IPv6 Endpoint.
void SetDownTarget(IpL4Protocol::DownTargetCallback cb) override
This method allows a caller to set the current down target callback set for this L4 protocol (IPv4 ca...
std::unordered_map< uint64_t, Ptr< UdpSocketImpl > > m_sockets
Unordered map of socket IDs and corresponding sockets.
~UdpL4Protocol() override
Ipv6EndPointDemux * m_endPoints6
A list of IPv6 end points.
int GetProtocolNumber() const override
Returns the protocol number of this protocol.
Ptr< Node > m_node
The node this stack is associated with.
Ipv4EndPointDemux * m_endPoints
A list of IPv4 end points.
void ReceiveIcmp(Ipv4Address icmpSource, uint8_t icmpTtl, uint8_t icmpType, uint8_t icmpCode, uint32_t icmpInfo, Ipv4Address payloadSource, Ipv4Address payloadDestination, const uint8_t payload[8]) override
Called from lower-level layers to send the ICMP packet up in the stack.
void SetNode(Ptr< Node > node)
Set node associated with this stack.
IpL4Protocol::DownTargetCallback GetDownTarget() const override
This method allows a caller to get the current down target callback set for this L4 protocol (IPv4 ca...
static TypeId GetTypeId()
Get the type ID.
IpL4Protocol::DownTargetCallback6 GetDownTarget6() const override
This method allows a caller to get the current down target callback set for this L4 protocol (IPv6 ca...
Ipv4EndPoint * Allocate()
Allocate an IPv4 Endpoint.
IpL4Protocol::RxStatus Receive(Ptr< Packet > p, const Ipv4Header &header, Ptr< Ipv4Interface > interface) override
Called from lower-level layers to send the packet up in the stack.
bool RemoveSocket(Ptr< UdpSocketImpl > socket)
Remove a socket from the internal list.
IpL4Protocol::DownTargetCallback m_downTarget
Callback to send packets over IPv4.
IpL4Protocol::DownTargetCallback6 m_downTarget6
Callback to send packets over IPv6.
void DoDispose() override
Destructor implementation.
void NotifyNewAggregate() override
Notify all Objects aggregated to this one of a new Object being aggregated.
void DeAllocate(Ipv4EndPoint *endPoint)
Remove an IPv4 Endpoint.
static constexpr uint8_t PROT_NUMBER
Protocol number (see http://www.iana.org/assignments/protocol-numbers)
uint64_t m_socketIndex
Index of the next socket to be created.
ObjectPtrContainerValue ObjectMapValue
ObjectMapValue is an alias for ObjectPtrContainerValue.
Ptr< const AttributeAccessor > MakeObjectMapAccessor(U T::*memberVariable)
MakeAccessorHelper implementation for ObjectVector.
Ptr< const AttributeChecker > MakeObjectMapChecker()
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...