36#include "ns3/inet-socket-address.h"
37#include "ns3/inet6-socket-address.h"
40#include "ns3/trace-source-accessor.h"
61 TypeId(
"ns3::UdpSocketImpl")
63 .SetGroupName(
"Internet")
65 .AddTraceSource(
"Drop",
66 "Drop UDP packet due to receive buffer overflow",
68 "ns3::Packet::TracedCallback")
69 .AddAttribute(
"IcmpCallback",
70 "Callback invoked whenever an icmp error is received on this socket.",
74 .AddAttribute(
"IcmpCallback6",
75 "Callback invoked whenever an icmpv6 error is received on this socket.",
83 : m_endPoint(nullptr),
87 m_errno(ERROR_NOTERROR),
88 m_shutdownSend(false),
89 m_shutdownRecv(false),
181 m_udp->RemoveSocket(
this);
192 m_udp->RemoveSocket(
this);
350 ipv6l3->AddMulticastAddress(ipv6);
537 p->ReplacePacketTag(ipTosTag);
545 p->ReplacePacketTag(priorityTag);
562 p->AddPacketTag(tag);
568 p->AddPacketTag(tag);
572 bool found = p->RemovePacketTag(tag);
583 p->AddPacketTag(tag);
597 for (
uint32_t i = 0; i < ipv4->GetNInterfaces(); i++)
614 NS_LOG_LOGIC(
"Sending one copy from " << addri <<
" to " << dest);
624 m_udp->Send(p->Copy(),
634 else if (ipv4->GetRoutingProtocol())
643 route = ipv4->GetRoutingProtocol()->RouteOutput(p, header, oif, errno_);
650 uint32_t outputIfIndex = ipv4->GetInterfaceForDevice(route->GetOutputDevice());
651 uint32_t ifNAddr = ipv4->GetNAddresses(outputIfIndex);
652 for (
uint32_t addrI = 0; addrI < ifNAddr; ++addrI)
664 m_udp->Send(p->Copy(),
729 p->AddPacketTag(ipTclassTag);
737 p->ReplacePacketTag(priorityTag);
754 p->AddPacketTag(tag);
760 p->AddPacketTag(tag);
770 m_udp->Send(p->Copy(),
780 else if (ipv6->GetRoutingProtocol())
789 route = ipv6->GetRoutingProtocol()->RouteOutput(p, header, oif, errno_);
794 m_udp->Send(p->Copy(),
842 uint8_t tos = transport.
GetTos();
887 if (p->GetSize() <= maxSize)
1004 if (oldBoundNetDevice)
1006 uint32_t index = ipv6l3->GetInterfaceForDevice(oldBoundNetDevice);
1016 uint32_t index = ipv6l3->GetInterfaceForDevice(netdevice);
1045 packet->RemovePacketTag(tag);
1048 tag.
SetRecvIf(incomingInterface->GetDevice()->GetIfIndex());
1049 packet->AddPacketTag(tag);
1057 packet->AddPacketTag(ipTosTag);
1064 packet->AddPacketTag(ipTtlTag);
1069 packet->RemovePacketTag(priorityTag);
1085 NS_LOG_WARN(
"No receive buffer space available. Drop.");
1107 packet->RemovePacketTag(tag);
1111 tag.
SetRecvIf(incomingInterface->GetDevice()->GetIfIndex());
1112 packet->AddPacketTag(tag);
1120 packet->AddPacketTag(ipTclassTag);
1127 packet->AddPacketTag(ipHopLimitTag);
1132 packet->RemovePacketTag(priorityTag);
1148 NS_LOG_WARN(
"No receive buffer space available. Drop.");
1161 << (
uint32_t)icmpCode << icmpInfo);
1164 m_icmpCallback(icmpSource, icmpTtl, icmpType, icmpCode, icmpInfo);
1176 << (
uint32_t)icmpCode << icmpInfo);
1259 std::vector<Ipv6Address> sourceAddresses)
1265 "Can join only one IPv6 multicast group.");
1272 if (filterMode ==
INCLUDE && sourceAddresses.empty())
1278 NS_ASSERT_MSG(index >= 0,
"Interface without a valid index");
1279 ipv6l3->RemoveMulticastAddress(address, index);
1283 ipv6l3->RemoveMulticastAddress(address);
1292 NS_ASSERT_MSG(index >= 0,
"Interface without a valid index");
1293 ipv6l3->AddMulticastAddress(address, index);
1297 ipv6l3->AddMulticastAddress(address);
a polymophic address class
AttributeValue implementation for Callback.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
uint16_t GetPort() const
Get the port.
static bool IsMatchingType(const Address &addr)
If the address match.
Ipv6Address GetIpv6() const
Get the IPv6 address.
static bool IsMatchingType(const Address &address)
Ipv4Address GetIpv4() const
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
Ipv4 addresses are stored in host order in this class.
static Ipv4Address ConvertFrom(const Address &address)
static Ipv4Address GetZero()
static bool IsMatchingType(const Address &address)
static Ipv4Address GetAny()
void BindToNetDevice(Ptr< NetDevice > netdevice)
Bind a socket to specific device.
void SetDestroyCallback(Callback< void > callback)
Set the default destroy callback.
Ipv4Address GetLocalAddress() const
Get the local address.
uint16_t GetLocalPort() const
Get the local port.
void SetRxEnabled(bool enabled)
Enable or Disable the endpoint Rx capability.
void SetIcmpCallback(Callback< void, Ipv4Address, uint8_t, uint8_t, uint8_t, uint32_t > callback)
Set the ICMP callback.
void SetRxCallback(Callback< void, Ptr< Packet >, Ipv4Header, uint16_t, Ptr< Ipv4Interface > > callback)
Set the reception callback.
Access to the IPv4 forwarding table, interfaces, and configuration.
a class to store IPv4 address information on an interface
Ipv4Address GetLocal() const
Get the local address.
Ipv4Address GetBroadcast() const
Get the broadcast address.
This class implements Linux struct pktinfo in order to deliver ancillary information to the socket in...
void SetRecvIf(uint32_t ifindex)
Set the tag's receiving interface.
void SetAddress(Ipv4Address addr)
Set the tag's address.
void SetTtl(uint8_t ttl)
Set the tag's Time to Live Implemented, but not used in the stack yet.
Describes an IPv6 address.
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
bool IsMulticast() const
If the IPv6 address is multicast (ff00::/8).
bool IsIpv4MappedAddress() const
If the address is an IPv4-mapped address.
bool IsAny() const
If the IPv6 address is the "Any" address.
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.
Ipv4Address GetIpv4MappedAddress() const
Return the Ipv4 address.
static bool IsMatchingType(const Address &address)
If the Address matches the type.
uint16_t GetLocalPort() const
Get the local port.
void SetIcmpCallback(Callback< void, Ipv6Address, uint8_t, uint8_t, uint8_t, uint32_t > callback)
Set the ICMP callback.
Ipv6Address GetLocalAddress() const
Get the local address.
void BindToNetDevice(Ptr< NetDevice > netdevice)
Bind a socket to specific device.
void SetRxEnabled(bool enabled)
Enable or Disable the endpoint Rx capability.
void SetRxCallback(Callback< void, Ptr< Packet >, Ipv6Header, uint16_t, Ptr< Ipv6Interface > > callback)
Set the reception callback.
void SetDestroyCallback(Callback< void > callback)
Set the default destroy callback.
Access to the IPv6 forwarding table, interfaces, and configuration.
IPv6 layer implementation.
This class implements a tag that carries socket ancillary data to the socket interface.
void SetTrafficClass(uint8_t tclass)
Set the tag's Traffic Class.
void SetRecvIf(uint32_t ifindex)
Set the tag's receiving interface.
void SetHoplimit(uint8_t ttl)
Set the tag's Hop Limit.
void SetAddress(Ipv6Address addr)
Set the tag's address.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Smart pointer class similar to boost::intrusive_ptr.
Ptr< NetDevice > GetBoundNetDevice()
Returns socket's bound NetDevice, if any.
bool IsIpRecvTtl() const
Ask if the socket is currently passing information about IP_TTL up the stack.
Ptr< Packet > Recv()
Read a single packet from the socket.
virtual void Ipv6LeaveGroup()
Leaves IPv6 multicast group this socket is joined to.
bool IsManualIpTtl() const
Checks if the socket has a specific IPv4 TTL set.
void SetIpTos(uint8_t ipTos)
Manually set IP Type of Service field.
void NotifySend(uint32_t spaceAvailable)
Notify through the callback (if set) that some data have been sent.
virtual uint8_t GetIpTtl() const
Query the value of IP Time to Live field of this socket.
bool IsRecvPktInfo() const
Get status indicating whether enable/disable packet information to socket.
uint8_t GetIpTos() const
Query the value of IP Type of Service of this socket.
Ipv6Address m_ipv6MulticastGroupAddress
IPv6 multicast group address.
SocketType
Enumeration of the possible socket types.
static uint8_t IpTos2Priority(uint8_t ipTos)
Return the priority corresponding to a given TOS value.
void NotifyDataRecv()
Notify through the callback (if set) that some data have been received.
Ipv6MulticastFilterMode
Enumeration of the possible filter of a socket.
Ptr< NetDevice > m_boundnetdevice
the device this socket is bound to (might be null).
virtual void BindToNetDevice(Ptr< NetDevice > netdevice)
Bind a socket to specific device.
bool IsIpv6RecvTclass() const
Ask if the socket is currently passing information about IPv6 Traffic Class up the stack.
bool IsIpv6RecvHopLimit() const
Ask if the socket is currently passing information about IPv6 Hop Limit up the stack.
virtual uint8_t GetIpv6HopLimit() const
Query the value of IP Hop Limit field of this socket.
SocketErrno
Enumeration of the possible errors returned by a socket.
bool IsIpRecvTos() const
Ask if the socket is currently passing information about IP Type of Service up the stack.
void NotifyDataSent(uint32_t size)
Notify through the callback (if set) that some data have been sent.
void NotifyConnectionSucceeded()
Notify through the callback (if set) that the connection has been established.
uint8_t GetPriority() const
Query the priority value of this socket.
uint8_t GetIpv6Tclass() const
Query the value of IPv6 Traffic Class field of this socket.
bool IsManualIpv6HopLimit() const
Checks if the socket has a specific IPv6 Hop Limit set.
bool IsManualIpv6Tclass() const
Checks if the socket has a specific IPv6 Tclass set.
void NotifyConnectionFailed()
Notify through the callback (if set) that the connection has not been established due to an error.
indicates whether the socket has IP_TOS set.
void SetTos(uint8_t tos)
Set the tag's TOS.
This class implements a tag that carries the socket-specific TTL of a packet to the IP layer.
void SetTtl(uint8_t ttl)
Set the tag's TTL.
This class implements a tag that carries the socket-specific HOPLIMIT of a packet to the IPv6 layer.
void SetHopLimit(uint8_t hopLimit)
Set the tag's Hop Limit.
indicates whether the socket has IPV6_TCLASS set.
void SetTclass(uint8_t tclass)
Set the tag's Tclass.
indicates whether the socket has a priority set.
void SetPriority(uint8_t priority)
Set the tag's priority.
indicates whether packets should be sent out with the DF (Don't Fragment) flag set.
void Enable()
Enables the DF (Don't Fragment) flag.
void Disable()
Disables the DF (Don't Fragment) flag.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
static const uint8_t PROT_NUMBER
protocol number (0x11)
(abstract) base class of all UdpSockets
A sockets interface to UDP.
void SetUdp(Ptr< UdpL4Protocol > udp)
Set the associated UDP L4 protocol.
uint32_t GetTxAvailable() const override
Returns the number of bytes which can be sent in a single call to Send.
void SetIpMulticastTtl(uint8_t ipTtl) override
Set the IP multicast TTL.
bool m_allowBroadcast
Allow send broadcast packets.
int MulticastJoinGroup(uint32_t interfaceIndex, const Address &groupAddress) override
Corresponds to socket option MCAST_JOIN_GROUP.
static TypeId GetTypeId()
Get the type ID.
bool GetIpMulticastLoop() const override
Get the IP multicast loop capability.
bool GetMtuDiscover() const override
Get the MTU discover capability.
int GetPeerName(Address &address) const override
Get the peer address of a connected socket.
void Destroy()
Kill this socket by zeroing its attributes (IPv4)
void ForwardUp6(Ptr< Packet > packet, Ipv6Header header, uint16_t port, Ptr< Ipv6Interface > incomingInterface)
Called by the L3 protocol when it received a packet to pass on to TCP.
int DoSendTo(Ptr< Packet > p, Ipv4Address daddr, uint16_t dport, uint8_t tos)
Send a packet to a specific destination and port (IPv4)
Callback< void, Ipv6Address, uint8_t, uint8_t, uint8_t, uint32_t > m_icmpCallback6
ICMPv6 callback.
void BindToNetDevice(Ptr< NetDevice > netdevice) override
Bind a socket to specific device.
int Close() override
Close a socket.
int FinishBind()
Finish the binding process.
bool m_connected
Connection established.
void ForwardIcmp(Ipv4Address icmpSource, uint8_t icmpTtl, uint8_t icmpType, uint8_t icmpCode, uint32_t icmpInfo)
Called by the L3 protocol when it received an ICMP packet to pass on to TCP.
int Listen() override
Listen for incoming connections.
Callback< void, Ipv4Address, uint8_t, uint8_t, uint8_t, uint32_t > m_icmpCallback
ICMP callback.
SocketErrno m_errno
Socket error code.
uint8_t m_ipMulticastTtl
Multicast TTL.
int GetSockName(Address &address) const override
Get socket address.
Ipv4EndPoint * m_endPoint
the IPv4 endpoint
int32_t m_ipMulticastIf
Multicast Interface.
void SetIpMulticastIf(int32_t ipIf) override
Set the IP multicast interface.
void SetRcvBufSize(uint32_t size) override
Set the receiving buffer size.
uint8_t GetIpMulticastTtl() const override
Get the IP multicast TTL.
SocketType GetSocketType() const override
std::queue< std::pair< Ptr< Packet >, Address > > m_deliveryQueue
Queue for incoming packets.
uint32_t m_rxAvailable
Number of available bytes to be received.
Ptr< Node > GetNode() const override
Return the node this socket is associated with.
TracedCallback< Ptr< const Packet > > m_dropTrace
Trace for dropped packets.
void Ipv6JoinGroup(Ipv6Address address, Socket::Ipv6MulticastFilterMode filterMode, std::vector< Ipv6Address > sourceAddresses) override
Joins a IPv6 multicast group.
uint32_t m_rcvBufSize
Receive buffer size.
Address m_defaultAddress
Default address.
UdpSocketImpl()
Create an unbound udp socket.
int Bind6() override
Allocate a local IPv6 endpoint for this socket.
~UdpSocketImpl() override
uint16_t m_defaultPort
Default port.
bool m_shutdownSend
Send no longer allowed.
int Send(Ptr< Packet > p, uint32_t flags) override
Send data (or dummy data) to the remote host.
bool m_ipMulticastLoop
Allow multicast loop.
int SendTo(Ptr< Packet > p, uint32_t flags, const Address &address) override
Send data to a specified peer.
int32_t GetIpMulticastIf() const override
Get the IP multicast interface.
Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress) override
Read a single packet from the socket and retrieve the sender address.
bool m_mtuDiscover
Allow MTU discovery.
bool m_shutdownRecv
Receive no longer allowed.
void SetNode(Ptr< Node > node)
Set the associated node.
bool GetAllowBroadcast() const override
Query whether broadcast datagram transmissions are allowed.
void ForwardUp(Ptr< Packet > packet, Ipv4Header header, uint16_t port, Ptr< Ipv4Interface > incomingInterface)
Called by the L3 protocol when it received a packet to pass on to TCP.
int Connect(const Address &address) override
Initiate a connection to a remote host.
Ptr< UdpL4Protocol > m_udp
the associated UDP L4 protocol
void SetMtuDiscover(bool discover) override
Set the MTU discover capability.
Ipv6EndPoint * m_endPoint6
the IPv6 endpoint
int MulticastLeaveGroup(uint32_t interfaceIndex, const Address &groupAddress) override
Corresponds to socket option MCAST_LEAVE_GROUP.
void Destroy6()
Kill this socket by zeroing its attributes (IPv6)
void DeallocateEndPoint()
Deallocate m_endPoint and m_endPoint6.
int Bind() override
Allocate a local IPv4 endpoint for this socket.
int ShutdownSend() override
SocketErrno GetErrno() const override
Get last error number.
int DoSend(Ptr< Packet > p)
Send a packet.
uint32_t GetRxAvailable() const override
Return number of bytes which can be returned from one or multiple calls to Recv.
bool SetAllowBroadcast(bool allowBroadcast) override
Configure whether broadcast datagram transmissions are allowed.
void SetIpMulticastLoop(bool loop) override
Set the IP multicast loop capability.
Ptr< Node > m_node
the associated node
void ForwardIcmp6(Ipv6Address icmpSource, uint8_t icmpTtl, uint8_t icmpType, uint8_t icmpCode, uint32_t icmpInfo)
Called by the L3 protocol when it received an ICMPv6 packet to pass on to TCP.
uint32_t GetRcvBufSize() const override
Get the receiving buffer size.
int ShutdownRecv() override
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Ptr< const AttributeAccessor > MakeCallbackAccessor(T1 a1)
Ptr< const AttributeChecker > MakeCallbackChecker()
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#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_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
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...
static const uint32_t MAX_IPV4_UDP_DATAGRAM_SIZE
Maximum UDP datagram size.