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 
35 namespace ns3 {
36 
37 NS_LOG_COMPONENT_DEFINE ("PacketSink");
38 
39 NS_OBJECT_ENSURE_REGISTERED (PacketSink);
40 
41 TypeId
43 {
44  static TypeId tid = TypeId ("ns3::PacketSink")
46  .SetGroupName("Applications")
47  .AddConstructor<PacketSink> ()
48  .AddAttribute ("Local",
49  "The Address on which to Bind the rx socket.",
50  AddressValue (),
53  .AddAttribute ("Protocol",
54  "The type id of the protocol to use for the rx socket.",
58  .AddTraceSource ("Rx",
59  "A packet has been received",
61  "ns3::Packet::AddressTracedCallback")
62  .AddTraceSource ("RxWithAddresses", "A packet has been received",
64  "ns3::Packet::TwoAddressTracedCallback")
65  ;
66  return tid;
67 }
68 
70 {
71  NS_LOG_FUNCTION (this);
72  m_socket = 0;
73  m_totalRx = 0;
74 }
75 
77 {
78  NS_LOG_FUNCTION (this);
79 }
80 
81 uint64_t PacketSink::GetTotalRx () const
82 {
83  NS_LOG_FUNCTION (this);
84  return m_totalRx;
85 }
86 
89 {
90  NS_LOG_FUNCTION (this);
91  return m_socket;
92 }
93 
94 std::list<Ptr<Socket> >
96 {
97  NS_LOG_FUNCTION (this);
98  return m_socketList;
99 }
100 
102 {
103  NS_LOG_FUNCTION (this);
104  m_socket = 0;
105  m_socketList.clear ();
106 
107  // chain up
109 }
110 
111 
112 // Application Methods
113 void PacketSink::StartApplication () // Called at time specified by Start
114 {
115  NS_LOG_FUNCTION (this);
116  // Create the socket if not already
117  if (!m_socket)
118  {
120  if (m_socket->Bind (m_local) == -1)
121  {
122  NS_FATAL_ERROR ("Failed to bind socket");
123  }
124  m_socket->Listen ();
127  {
128  Ptr<UdpSocket> udpSocket = DynamicCast<UdpSocket> (m_socket);
129  if (udpSocket)
130  {
131  // equivalent to setsockopt (MCAST_JOIN_GROUP)
132  udpSocket->MulticastJoinGroup (0, m_local);
133  }
134  else
135  {
136  NS_FATAL_ERROR ("Error: joining multicast on a non-UDP socket");
137  }
138  }
139  }
140 
143  MakeNullCallback<bool, Ptr<Socket>, const Address &> (),
148 }
149 
150 void PacketSink::StopApplication () // Called at time specified by Stop
151 {
152  NS_LOG_FUNCTION (this);
153  while(!m_socketList.empty ()) //these are accepted sockets, close them
154  {
155  Ptr<Socket> acceptedSocket = m_socketList.front ();
156  m_socketList.pop_front ();
157  acceptedSocket->Close ();
158  }
159  if (m_socket)
160  {
161  m_socket->Close ();
163  }
164 }
165 
167 {
168  NS_LOG_FUNCTION (this << socket);
169  Ptr<Packet> packet;
170  Address from;
171  Address localAddress;
172  while ((packet = socket->RecvFrom (from)))
173  {
174  if (packet->GetSize () == 0)
175  { //EOF
176  break;
177  }
178  m_totalRx += packet->GetSize ();
180  {
181  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds ()
182  << "s packet sink received "
183  << packet->GetSize () << " bytes from "
185  << " port " << InetSocketAddress::ConvertFrom (from).GetPort ()
186  << " total Rx " << m_totalRx << " bytes");
187  }
188  else if (Inet6SocketAddress::IsMatchingType (from))
189  {
190  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds ()
191  << "s packet sink received "
192  << packet->GetSize () << " bytes from "
194  << " port " << Inet6SocketAddress::ConvertFrom (from).GetPort ()
195  << " total Rx " << m_totalRx << " bytes");
196  }
197  socket->GetSockName (localAddress);
198  m_rxTrace (packet, from);
199  m_rxTraceWithAddresses (packet, from, localAddress);
200  }
201 }
202 
203 
205 {
206  NS_LOG_FUNCTION (this << socket);
207 }
208 
210 {
211  NS_LOG_FUNCTION (this << socket);
212 }
213 
214 
216 {
217  NS_LOG_FUNCTION (this << s << from);
219  m_socketList.push_back (s);
220 }
221 
222 } // Namespace ns3
static TypeId GetTypeId(void)
Get the type ID.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Address m_local
Local address to bind to.
Definition: packet-sink.h:129
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
virtual int ShutdownSend(void)=0
bool IsMulticast(const Address &ad)
Address family-independent test for a multicast address.
uint64_t GetTotalRx() const
Definition: packet-sink.cc:81
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
virtual int GetSockName(Address &address) const =0
Get socket address.
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
Definition: packet-sink.h:134
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
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:280
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
static TypeId GetTypeId(void)
Get the type ID.
Definition: packet-sink.cc:42
Callback< R > MakeNullCallback(void)
Definition: callback.h:1635
std::list< Ptr< Socket > > GetAcceptedSockets(void) const
Definition: packet-sink.cc:95
a polymophic address class
Definition: address.h:90
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
virtual int Listen(void)=0
Listen for incoming connections.
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:137
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
The base class for all ns3 applications.
Definition: application.h:60
void HandleRead(Ptr< Socket > socket)
Handle a packet received by the application.
Definition: packet-sink.cc:166
AttributeValue implementation for TypeId.
Definition: type-id.h:608
TypeId m_tid
Protocol TypeId.
Definition: packet-sink.h:131
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
virtual void StopApplication(void)
Application specific shutdown code.
Definition: packet-sink.cc:150
void SetRecvCallback(Callback< void, Ptr< Socket > >)
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
void HandlePeerClose(Ptr< Socket > socket)
Handle an connection close.
Definition: packet-sink.cc:204
Ptr< Node > GetNode() const
Definition: application.cc:104
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:83
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
uint16_t GetPort(void) const
uint64_t m_totalRx
Total bytes received.
Definition: packet-sink.h:130
Ptr< const AttributeChecker > MakeAddressChecker(void)
Definition: address.cc:172
virtual ~PacketSink()
Definition: packet-sink.cc:76
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:193
std::list< Ptr< Socket > > m_socketList
the accepted sockets
Definition: packet-sink.h:127
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:608
AttributeValue implementation for Address.
Definition: address.h:278
void HandleAccept(Ptr< Socket > socket, const Address &from)
Handle an incoming connection.
Definition: packet-sink.cc:215
void HandlePeerError(Ptr< Socket > socket)
Handle an connection error.
Definition: packet-sink.cc:209
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
static bool IsMatchingType(const Address &addr)
If the address match.
Ptr< Socket > GetListeningSocket(void) const
Definition: packet-sink.cc:88
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 void DoDispose(void)
Destructor implementation.
Definition: packet-sink.cc:101
virtual void StartApplication(void)
Application specific startup code.
Definition: packet-sink.cc:113
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.
Receive and consume traffic generated to an IP address and port.
Definition: packet-sink.h:68
uint16_t GetPort(void) const
Get the port.
Ptr< Socket > m_socket
Listening socket.
Definition: packet-sink.h:126
virtual int Close(void)=0
Close a socket.
Ptr< const AttributeChecker > MakeTypeIdChecker(void)
Definition: type-id.cc:1225
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
static bool IsMatchingType(const Address &address)
Ipv4Address GetIpv4(void) const