A Discrete-Event Network Simulator
API
packet-sink.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright 2007 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Tom Henderson (tomhend@u.washington.edu)
19  */
20 #include "ns3/address.h"
21 #include "ns3/address-utils.h"
22 #include "ns3/log.h"
23 #include "ns3/inet-socket-address.h"
24 #include "ns3/inet6-socket-address.h"
25 #include "ns3/node.h"
26 #include "ns3/socket.h"
27 #include "ns3/udp-socket.h"
28 #include "ns3/simulator.h"
29 #include "ns3/socket-factory.h"
30 #include "ns3/packet.h"
31 #include "ns3/trace-source-accessor.h"
32 #include "ns3/udp-socket-factory.h"
33 #include "packet-sink.h"
34 #include "ns3/boolean.h"
35 #include "ns3/ipv4-packet-info-tag.h"
36 #include "ns3/ipv6-packet-info-tag.h"
37 
38 namespace ns3 {
39 
40 NS_LOG_COMPONENT_DEFINE ("PacketSink");
41 
42 NS_OBJECT_ENSURE_REGISTERED (PacketSink);
43 
44 TypeId
46 {
47  static TypeId tid = TypeId ("ns3::PacketSink")
49  .SetGroupName("Applications")
50  .AddConstructor<PacketSink> ()
51  .AddAttribute ("Local",
52  "The Address on which to Bind the rx socket.",
53  AddressValue (),
56  .AddAttribute ("Protocol",
57  "The type id of the protocol to use for the rx socket.",
61  .AddAttribute ("EnableSeqTsSizeHeader",
62  "Enable optional header tracing of SeqTsSizeHeader",
63  BooleanValue (false),
66  .AddTraceSource ("Rx",
67  "A packet has been received",
69  "ns3::Packet::AddressTracedCallback")
70  .AddTraceSource ("RxWithAddresses", "A packet has been received",
72  "ns3::Packet::TwoAddressTracedCallback")
73  .AddTraceSource ("RxWithSeqTsSize",
74  "A packet with SeqTsSize header has been received",
76  "ns3::PacketSink::SeqTsSizeCallback")
77  ;
78  return tid;
79 }
80 
82 {
83  NS_LOG_FUNCTION (this);
84  m_socket = 0;
85  m_totalRx = 0;
86 }
87 
89 {
90  NS_LOG_FUNCTION (this);
91 }
92 
93 uint64_t PacketSink::GetTotalRx () const
94 {
95  NS_LOG_FUNCTION (this);
96  return m_totalRx;
97 }
98 
101 {
102  NS_LOG_FUNCTION (this);
103  return m_socket;
104 }
105 
106 std::list<Ptr<Socket> >
108 {
109  NS_LOG_FUNCTION (this);
110  return m_socketList;
111 }
112 
114 {
115  NS_LOG_FUNCTION (this);
116  m_socket = 0;
117  m_socketList.clear ();
118 
119  // chain up
121 }
122 
123 
124 // Application Methods
125 void PacketSink::StartApplication () // Called at time specified by Start
126 {
127  NS_LOG_FUNCTION (this);
128  // Create the socket if not already
129  if (!m_socket)
130  {
132  if (m_socket->Bind (m_local) == -1)
133  {
134  NS_FATAL_ERROR ("Failed to bind socket");
135  }
136  m_socket->Listen ();
139  {
140  Ptr<UdpSocket> udpSocket = DynamicCast<UdpSocket> (m_socket);
141  if (udpSocket)
142  {
143  // equivalent to setsockopt (MCAST_JOIN_GROUP)
144  udpSocket->MulticastJoinGroup (0, m_local);
145  }
146  else
147  {
148  NS_FATAL_ERROR ("Error: joining multicast on a non-UDP socket");
149  }
150  }
151  }
152 
154  {
156  }
158  {
160  }
161  else
162  {
163  m_localPort = 0;
164  }
166  m_socket->SetRecvPktInfo (true);
168  MakeNullCallback<bool, Ptr<Socket>, const Address &> (),
173 }
174 
175 void PacketSink::StopApplication () // Called at time specified by Stop
176 {
177  NS_LOG_FUNCTION (this);
178  while(!m_socketList.empty ()) //these are accepted sockets, close them
179  {
180  Ptr<Socket> acceptedSocket = m_socketList.front ();
181  m_socketList.pop_front ();
182  acceptedSocket->Close ();
183  }
184  if (m_socket)
185  {
186  m_socket->Close ();
188  }
189 }
190 
192 {
193  NS_LOG_FUNCTION (this << socket);
194  Ptr<Packet> packet;
195  Address from;
196  Address localAddress;
197  while ((packet = socket->RecvFrom (from)))
198  {
199  if (packet->GetSize () == 0)
200  { //EOF
201  break;
202  }
203  m_totalRx += packet->GetSize ();
205  {
206  NS_LOG_INFO ("At time " << Simulator::Now ().As (Time::S)
207  << " packet sink received "
208  << packet->GetSize () << " bytes from "
210  << " port " << InetSocketAddress::ConvertFrom (from).GetPort ()
211  << " total Rx " << m_totalRx << " bytes");
212  }
213  else if (Inet6SocketAddress::IsMatchingType (from))
214  {
215  NS_LOG_INFO ("At time " << Simulator::Now ().As (Time::S)
216  << " packet sink received "
217  << packet->GetSize () << " bytes from "
219  << " port " << Inet6SocketAddress::ConvertFrom (from).GetPort ()
220  << " total Rx " << m_totalRx << " bytes");
221  }
222 
223  if (!m_rxTrace.IsEmpty () || !m_rxTraceWithAddresses.IsEmpty () ||
225  {
226  Ipv4PacketInfoTag interfaceInfo;
227  Ipv6PacketInfoTag interface6Info;
228  if (packet->RemovePacketTag (interfaceInfo))
229  {
230  localAddress = InetSocketAddress (interfaceInfo.GetAddress (), m_localPort);
231  }
232  else if (packet->RemovePacketTag (interface6Info))
233  {
234  localAddress = Inet6SocketAddress (interface6Info.GetAddress (), m_localPort);
235  }
236  else
237  {
238  socket->GetSockName (localAddress);
239  }
240  m_rxTrace (packet, from);
241  m_rxTraceWithAddresses (packet, from, localAddress);
242 
244  {
245  PacketReceived (packet, from, localAddress);
246  }
247  }
248  }
249 }
250 
251 void
253  const Address &localAddress)
254 {
255  SeqTsSizeHeader header;
256  Ptr<Packet> buffer;
257 
258  auto itBuffer = m_buffer.find (from);
259  if (itBuffer == m_buffer.end ())
260  {
261  itBuffer = m_buffer.insert (std::make_pair (from, Create<Packet> (0))).first;
262  }
263 
264  buffer = itBuffer->second;
265  buffer->AddAtEnd (p);
266  buffer->PeekHeader (header);
267 
268  NS_ABORT_IF (header.GetSize () == 0);
269 
270  while (buffer->GetSize () >= header.GetSize ())
271  {
272  NS_LOG_DEBUG ("Removing packet of size " << header.GetSize () << " from buffer of size " << buffer->GetSize ());
273  Ptr<Packet> complete = buffer->CreateFragment (0, static_cast<uint32_t> (header.GetSize ()));
274  buffer->RemoveAtStart (static_cast<uint32_t> (header.GetSize ()));
275 
276  complete->RemoveHeader (header);
277 
278  m_rxTraceWithSeqTsSize (complete, from, localAddress, header);
279 
280  if (buffer->GetSize () > header.GetSerializedSize ())
281  {
282  buffer->PeekHeader (header);
283  }
284  else
285  {
286  break;
287  }
288  }
289 }
290 
292 {
293  NS_LOG_FUNCTION (this << socket);
294 }
295 
297 {
298  NS_LOG_FUNCTION (this << socket);
299 }
300 
302 {
303  NS_LOG_FUNCTION (this << s << from);
305  m_socketList.push_back (s);
306 }
307 
308 } // Namespace ns3
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::SeqTsSizeHeader::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const override
Definition: seq-ts-size-header.cc:75
ns3::InetSocketAddress
an Inet address class
Definition: inet-socket-address.h:41
ns3::Inet6SocketAddress::IsMatchingType
static bool IsMatchingType(const Address &addr)
If the address match.
Definition: inet6-socket-address.cc:89
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
ns3::SeqTsSizeHeader
Header with a sequence, a timestamp, and a "size" attribute.
Definition: seq-ts-size-header.h:38
ns3::InetSocketAddress::IsMatchingType
static bool IsMatchingType(const Address &address)
Definition: inet-socket-address.cc:103
ns3::PacketSink::m_local
Address m_local
Local address to bind to (address and port)
Definition: packet-sink.h:179
ns3::BooleanValue
AttributeValue implementation for Boolean.
Definition: boolean.h:37
ns3::Packet::PeekHeader
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:290
ns3::Socket::Bind
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
ns3::Packet::GetSize
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
ns3::InetSocketAddress::GetPort
uint16_t GetPort(void) const
Definition: inet-socket-address.cc:65
ns3::Socket::SetCloseCallbacks
void SetCloseCallbacks(Callback< void, Ptr< Socket > > normalClose, Callback< void, Ptr< Socket > > errorClose)
Detect socket recv() events such as graceful shutdown or error.
Definition: socket.cc:94
ns3::PacketSink::m_totalRx
uint64_t m_totalRx
Total bytes received.
Definition: packet-sink.h:181
ns3::MakeAddressAccessor
Ptr< const AttributeAccessor > MakeAddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: address.h:278
ns3::Packet::CreateFragment
Ptr< Packet > CreateFragment(uint32_t start, uint32_t length) const
Create a new packet which contains a fragment of the original packet.
Definition: packet.cc:227
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
packet-sink.h
ns3::SeqTsSizeHeader::GetSize
uint64_t GetSize(void) const
Get the size information that the header is carrying.
Definition: seq-ts-size-header.cc:61
ns3::PacketSink::HandleAccept
void HandleAccept(Ptr< Socket > socket, const Address &from)
Handle an incoming connection.
Definition: packet-sink.cc:301
ns3::Socket::SetRecvPktInfo
void SetRecvPktInfo(bool flag)
Enable/Disable receive packet information to socket.
Definition: socket.cc:358
ns3::AddressValue
AttributeValue implementation for Address.
Definition: address.h:278
ns3::PacketSink::m_rxTraceWithSeqTsSize
TracedCallback< Ptr< const Packet >, const Address &, const Address &, const SeqTsSizeHeader & > m_rxTraceWithSeqTsSize
Callbacks for tracing the packet Rx events, includes source, destination addresses,...
Definition: packet-sink.h:191
ns3::PacketSink::PacketReceived
void PacketReceived(const Ptr< Packet > &p, const Address &from, const Address &localAddress)
Packet received: assemble byte stream to extract SeqTsSizeHeader.
Definition: packet-sink.cc:252
ns3::PacketSink::PacketSink
PacketSink()
Definition: packet-sink.cc:81
ns3::Socket::SetAcceptCallback
void SetAcceptCallback(Callback< bool, Ptr< Socket >, const Address & > connectionRequest, Callback< void, Ptr< Socket >, const Address & > newConnectionCreated)
Accept connection requests from remote hosts.
Definition: socket.cc:104
ns3::PacketSink::m_tid
TypeId m_tid
Protocol TypeId.
Definition: packet-sink.h:182
ns3::addressUtils::IsMulticast
bool IsMulticast(const Address &ad)
Address family-independent test for a multicast address.
Definition: address-utils.cc:114
ns3::Application::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:83
ns3::MakeBooleanAccessor
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: boolean.h:85
ns3::InetSocketAddress::ConvertFrom
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
Definition: inet-socket-address.cc:126
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::PacketSink::HandlePeerClose
void HandlePeerClose(Ptr< Socket > socket)
Handle an connection close.
Definition: packet-sink.cc:291
ns3::MakeNullCallback
Callback< R, Ts... > MakeNullCallback(void)
Definition: callback.h:1682
ns3::MakeTraceSourceAccessor
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Definition: trace-source-accessor.h:202
ns3::Inet6SocketAddress::GetPort
uint16_t GetPort(void) const
Get the port.
Definition: inet6-socket-address.cc:65
ns3::Inet6SocketAddress::ConvertFrom
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
Definition: inet6-socket-address.cc:110
ns3::PacketSink::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: packet-sink.cc:113
ns3::Ptr< Socket >
NS_FATAL_ERROR
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
ns3::MakeAddressChecker
Ptr< const AttributeChecker > MakeAddressChecker(void)
Definition: address.cc:172
ns3::Ipv4PacketInfoTag::GetAddress
Ipv4Address GetAddress(void) const
Get the tag's address.
Definition: ipv4-packet-info-tag.cc:47
ns3::Socket::SetRecvCallback
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
ns3::TypeIdValue
AttributeValue implementation for TypeId.
Definition: type-id.h:595
ns3::PacketSink::HandlePeerError
void HandlePeerError(Ptr< Socket > socket)
Handle an connection error.
Definition: packet-sink.cc:296
ns3::Packet::RemoveAtStart
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Definition: packet.cc:362
ns3::PacketSink::m_socketList
std::list< Ptr< Socket > > m_socketList
the accepted sockets
Definition: packet-sink.h:177
ns3::Ipv6PacketInfoTag
This class implements a tag that carries socket ancillary data to the socket interface.
Definition: ipv6-packet-info-tag.h:49
ns3::Address
a polymophic address class
Definition: address.h:91
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
ns3::PacketSink::StartApplication
virtual void StartApplication(void)
Application specific startup code.
Definition: packet-sink.cc:125
ns3::Socket::RecvFrom
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
ns3::InetSocketAddress::GetIpv4
Ipv4Address GetIpv4(void) const
Definition: inet-socket-address.cc:71
ns3::PacketSink::GetTotalRx
uint64_t GetTotalRx() const
Definition: packet-sink.cc:93
ns3::Packet::RemoveHeader
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
ns3::MakeBooleanChecker
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
ns3::Socket::Close
virtual int Close(void)=0
Close a socket.
ns3::PacketSink::m_localPort
uint16_t m_localPort
Local port to bind to.
Definition: packet-sink.h:180
ns3::Socket::ShutdownSend
virtual int ShutdownSend(void)=0
ns3::Ipv4PacketInfoTag
This class implements Linux struct pktinfo in order to deliver ancillary information to the socket in...
Definition: ipv4-packet-info-tag.h:50
ns3::PacketSink::GetListeningSocket
Ptr< Socket > GetListeningSocket(void) const
Definition: packet-sink.cc:100
ns3::MakeCallback
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642
ns3::Inet6SocketAddress::GetIpv6
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
Definition: inet6-socket-address.cc:77
ns3::UdpSocketFactory::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: udp-socket-factory.cc:27
NS_ABORT_IF
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition: abort.h:77
ns3::PacketSink::m_rxTraceWithAddresses
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_rxTraceWithAddresses
Callback for tracing the packet Rx events, includes source and destination addresses.
Definition: packet-sink.h:189
ns3::PacketSink::StopApplication
virtual void StopApplication(void)
Application specific shutdown code.
Definition: packet-sink.cc:175
ns3::PacketSink::m_enableSeqTsSizeHeader
bool m_enableSeqTsSizeHeader
Enable or disable the export of SeqTsSize header.
Definition: packet-sink.h:184
ns3::Socket::Listen
virtual int Listen(void)=0
Listen for incoming connections.
ns3::Ipv6PacketInfoTag::GetAddress
Ipv6Address GetAddress(void) const
Get the tag's address.
Definition: ipv6-packet-info-tag.cc:43
ns3::PacketSink::m_socket
Ptr< Socket > m_socket
Listening socket.
Definition: packet-sink.h:176
ns3::Packet::AddAtEnd
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Definition: packet.cc:335
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
ns3::PacketSink::GetAcceptedSockets
std::list< Ptr< Socket > > GetAcceptedSockets(void) const
Definition: packet-sink.cc:107
ns3::Inet6SocketAddress
An Inet6 address class.
Definition: inet6-socket-address.h:37
ns3::Time::S
@ S
second
Definition: nstime.h:115
ns3::Packet::RemovePacketTag
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:963
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::PacketSink::m_rxTrace
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
Definition: packet-sink.h:187
ns3::PacketSink::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: packet-sink.cc:45
ns3::PacketSink
Receive and consume traffic generated to an IP address and port.
Definition: packet-sink.h:72
ns3::Application::GetNode
Ptr< Node > GetNode() const
Definition: application.cc:104
ns3::Socket::CreateSocket
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:71
ns3::Application
The base class for all ns3 applications.
Definition: application.h:61
ns3::PacketSink::m_buffer
std::unordered_map< Address, Ptr< Packet >, AddressHash > m_buffer
Buffer for received packets.
Definition: packet-sink.h:172
ns3::Socket::GetSockName
virtual int GetSockName(Address &address) const =0
Get socket address.
ns3::PacketSink::HandleRead
void HandleRead(Ptr< Socket > socket)
Handle a packet received by the application.
Definition: packet-sink.cc:191
ns3::MakeTypeIdChecker
Ptr< const AttributeChecker > MakeTypeIdChecker(void)
Definition: type-id.cc:1227
ns3::MakeTypeIdAccessor
Ptr< const AttributeAccessor > MakeTypeIdAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: type-id.h:595
ns3::PacketSink::~PacketSink
virtual ~PacketSink()
Definition: packet-sink.cc:88