11#include "ns3/address-utils.h"
12#include "ns3/boolean.h"
13#include "ns3/inet-socket-address.h"
14#include "ns3/inet6-socket-address.h"
15#include "ns3/ipv4-packet-info-tag.h"
16#include "ns3/ipv6-packet-info-tag.h"
19#include "ns3/packet.h"
20#include "ns3/simulator.h"
21#include "ns3/socket-factory.h"
22#include "ns3/socket.h"
23#include "ns3/trace-source-accessor.h"
24#include "ns3/udp-socket-factory.h"
25#include "ns3/udp-socket.h"
40 .SetGroupName(
"Applications")
42 .AddAttribute(
"Protocol",
43 "The type id of the protocol to use for the rx socket.",
47 .AddAttribute(
"EnableSeqTsSizeHeader",
48 "Enable optional header tracing of SeqTsSizeHeader",
52 .AddTraceSource(
"RxWithAddresses",
53 "A packet has been received",
55 "ns3::Packet::TwoAddressTracedCallback")
56 .AddTraceSource(
"RxWithSeqTsSize",
57 "A packet with SeqTsSize header has been received",
59 "ns3::PacketSink::SeqTsSizeCallback");
111 if (local.IsInvalid())
120 NS_LOG_INFO(
this <<
" Binding on " << ipv4 <<
" port " <<
m_port <<
" / " << local <<
".");
126 NS_LOG_INFO(
this <<
" Binding on " << ipv6 <<
" port " <<
m_port <<
" / " << local <<
".");
139 udpSocket->MulticastJoinGroup(0, local);
167 udpSocket->MulticastJoinGroup(0, local);
191 acceptedSocket->Close();
200 while (
auto packet = socket->RecvFrom(from))
202 if (packet->GetSize() == 0)
210 << packet->GetSize() <<
" bytes from "
218 << packet->GetSize() <<
" bytes from "
221 <<
" total Rx " <<
m_totalRx <<
" bytes");
231 if (packet->RemovePacketTag(interfaceInfo))
235 else if (packet->RemovePacketTag(interface6Info))
241 socket->GetSockName(localAddress);
258 auto itBuffer =
m_buffer.find(from);
264 auto buffer = itBuffer->second;
268 buffer->PeekHeader(header);
272 while (buffer->GetSize() >= header.
GetSize())
275 << buffer->GetSize());
276 auto complete = buffer->CreateFragment(0,
static_cast<uint32_t>(header.
GetSize()));
279 complete->RemoveHeader(header);
285 buffer->PeekHeader(header);
a polymophic address class
AttributeValue implementation for Boolean.
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.
static Ipv4Address GetAny()
This class implements Linux struct pktinfo in order to deliver ancillary information to the socket in...
Ipv4Address GetAddress() const
Get the tag's address.
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
This class implements a tag that carries socket ancillary data to the socket interface.
Ipv6Address GetAddress() const
Get the tag's address.
Receive and consume traffic generated to an IP address and port.
static TypeId GetTypeId()
Get the type ID.
std::unordered_map< Address, Ptr< Packet >, AddressHash > m_buffer
Buffer for received packets.
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_rxTraceWithAddresses
Callback for tracing the packet Rx events, includes source and destination addresses.
void DoStartApplication() override
Application specific startup code for child subclasses.
Ptr< Socket > GetListeningSocket() const
std::list< Ptr< Socket > > m_socketList
the accepted sockets
void HandleRead(Ptr< Socket > socket)
Handle a packet received by the application.
uint64_t GetTotalRx() const
void HandleAccept(Ptr< Socket > socket, const Address &from)
Handle an incoming connection.
void DoStopApplication() override
Application specific shutdown code for child subclasses.
void HandlePeerError(Ptr< Socket > socket)
Handle an connection error.
TracedCallback< Ptr< const Packet >, const Address &, const Address &, const SeqTsSizeHeader & > m_rxTraceWithSeqTsSize
Callbacks for tracing the packet Rx events, includes source, destination addresses,...
uint64_t m_totalRx
Total bytes received.
void DoDispose() override
Destructor implementation.
bool m_enableSeqTsSizeHeader
Enable or disable the export of SeqTsSize header.
std::list< Ptr< Socket > > GetAcceptedSockets() const
void HandlePeerClose(Ptr< Socket > socket)
Handle an connection close.
void PacketReceived(const Ptr< Packet > &p, const Address &from, const Address &localAddress)
Packet received: assemble byte stream to extract SeqTsSizeHeader.
Smart pointer class similar to boost::intrusive_ptr.
static Time Now()
Return the current simulation virtual time.
ns3::TracedCallback< Ptr< const Packet > > m_rxTraceWithoutAddress
Callbacks for tracing the packet Rx events.
Ptr< Socket > m_socket6
IPv6 Socket (used if only port is specified).
void DoDispose() override
Destructor implementation.
TypeId m_protocolTid
Protocol TypeId value.
SinkApplication(uint16_t defaultPort=0)
Constructor.
Address m_local
Local address to bind to (address and port).
Ptr< Socket > m_socket
Socket (IPv4 or IPv6, depending on local address).
uint32_t m_port
Local port to bind to.
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
AttributeValue implementation for TypeId.
static TypeId GetTypeId()
Get the type ID.
Ptr< const AttributeChecker > MakeBooleanChecker()
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeChecker > MakeTypeIdChecker()
Ptr< const AttributeAccessor > MakeTypeIdAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Callback< R, Args... > MakeNullCallback()
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
#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_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.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
bool IsMulticast(const Address &ad)
Address family-independent test for a multicast address.
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...
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.