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
a polymophic address class
Definition: address.h:91
AttributeValue implementation for Address.
Definition: address.h:278
The base class for all ns3 applications.
Definition: application.h:61
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:83
Ptr< Node > GetNode() const
Definition: application.cc:104
AttributeValue implementation for Boolean.
Definition: boolean.h:37
An Inet6 address class.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
static bool IsMatchingType(const Address &addr)
If the address match.
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
uint16_t GetPort(void) const
Get the port.
an Inet address class
uint16_t GetPort(void) const
Ipv4Address GetIpv4(void) const
static bool IsMatchingType(const Address &address)
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
This class implements Linux struct pktinfo in order to deliver ancillary information to the socket in...
Ipv4Address GetAddress(void) const
Get the tag's address.
This class implements a tag that carries socket ancillary data to the socket interface.
Ipv6Address GetAddress(void) const
Get the tag's address.
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:963
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Definition: packet.cc:335
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Definition: packet.cc:362
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:290
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
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
Receive and consume traffic generated to an IP address and port.
Definition: packet-sink.h:72
virtual void DoDispose(void)
Destructor implementation.
Definition: packet-sink.cc:113
std::unordered_map< Address, Ptr< Packet >, AddressHash > m_buffer
Buffer for received packets.
Definition: packet-sink.h:172
Ptr< Socket > GetListeningSocket(void) const
Definition: packet-sink.cc:100
TypeId m_tid
Protocol TypeId.
Definition: packet-sink.h:182
static TypeId GetTypeId(void)
Get the type ID.
Definition: packet-sink.cc:45
uint16_t m_localPort
Local port to bind to.
Definition: packet-sink.h:180
Address m_local
Local address to bind to (address and port)
Definition: packet-sink.h:179
std::list< Ptr< Socket > > GetAcceptedSockets(void) const
Definition: packet-sink.cc:107
virtual void StartApplication(void)
Application specific startup code.
Definition: packet-sink.cc:125
std::list< Ptr< Socket > > m_socketList
the accepted sockets
Definition: packet-sink.h:177
virtual void StopApplication(void)
Application specific shutdown code.
Definition: packet-sink.cc:175
void HandleRead(Ptr< Socket > socket)
Handle a packet received by the application.
Definition: packet-sink.cc:191
uint64_t GetTotalRx() const
Definition: packet-sink.cc:93
void HandleAccept(Ptr< Socket > socket, const Address &from)
Handle an incoming connection.
Definition: packet-sink.cc:301
void HandlePeerError(Ptr< Socket > socket)
Handle an connection error.
Definition: packet-sink.cc:296
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
Definition: packet-sink.h:187
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
uint64_t m_totalRx
Total bytes received.
Definition: packet-sink.h:181
bool m_enableSeqTsSizeHeader
Enable or disable the export of SeqTsSize header.
Definition: packet-sink.h:184
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
void HandlePeerClose(Ptr< Socket > socket)
Handle an connection close.
Definition: packet-sink.cc:291
Ptr< Socket > m_socket
Listening socket.
Definition: packet-sink.h:176
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
virtual ~PacketSink()
Definition: packet-sink.cc:88
Header with a sequence, a timestamp, and a "size" attribute.
virtual uint32_t GetSerializedSize(void) const override
uint64_t GetSize(void) const
Get the size information that the header is carrying.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
void SetRecvPktInfo(bool flag)
Enable/Disable receive packet information to socket.
Definition: socket.cc:358
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
virtual int Listen(void)=0
Listen for incoming connections.
virtual int ShutdownSend(void)=0
virtual int GetSockName(Address &address) const =0
Get socket address.
virtual int Close(void)=0
Close a socket.
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
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
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
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
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.
@ S
second
Definition: nstime.h:114
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
AttributeValue implementation for TypeId.
Definition: type-id.h:595
static TypeId GetTypeId(void)
Get the type ID.
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
Ptr< const AttributeChecker > MakeAddressChecker(void)
Definition: address.cc:172
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
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
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
Ptr< const AttributeChecker > MakeTypeIdChecker(void)
Definition: type-id.cc:1226
Callback< R, Ts... > MakeNullCallback(void)
Definition: callback.h:1688
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition: abort.h:77
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
#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.
Definition: log.h:281
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
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, 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:1648