A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
packet-sink.cc
Go to the documentation of this file.
1/*
2 * Copyright 2007 University of Washington
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Tom Henderson (tomhend@u.washington.edu)
18 */
19#include "packet-sink.h"
20
21#include "ns3/address-utils.h"
22#include "ns3/address.h"
23#include "ns3/boolean.h"
24#include "ns3/inet-socket-address.h"
25#include "ns3/inet6-socket-address.h"
26#include "ns3/ipv4-packet-info-tag.h"
27#include "ns3/ipv6-packet-info-tag.h"
28#include "ns3/log.h"
29#include "ns3/node.h"
30#include "ns3/packet.h"
31#include "ns3/simulator.h"
32#include "ns3/socket-factory.h"
33#include "ns3/socket.h"
34#include "ns3/trace-source-accessor.h"
35#include "ns3/udp-socket-factory.h"
36#include "ns3/udp-socket.h"
37
38namespace ns3
39{
40
41NS_LOG_COMPONENT_DEFINE("PacketSink");
42
44
45TypeId
47{
48 static TypeId tid =
49 TypeId("ns3::PacketSink")
51 .SetGroupName("Applications")
52 .AddConstructor<PacketSink>()
53 .AddAttribute("Local",
54 "The Address on which to Bind the rx socket.",
58 .AddAttribute("Protocol",
59 "The type id of the protocol to use for the rx socket.",
63 .AddAttribute("EnableSeqTsSizeHeader",
64 "Enable optional header tracing of SeqTsSizeHeader",
65 BooleanValue(false),
68 .AddTraceSource("Rx",
69 "A packet has been received",
71 "ns3::Packet::AddressTracedCallback")
72 .AddTraceSource("RxWithAddresses",
73 "A packet has been received",
75 "ns3::Packet::TwoAddressTracedCallback")
76 .AddTraceSource("RxWithSeqTsSize",
77 "A packet with SeqTsSize header has been received",
79 "ns3::PacketSink::SeqTsSizeCallback");
80 return tid;
81}
82
84{
85 NS_LOG_FUNCTION(this);
86 m_socket = nullptr;
87 m_totalRx = 0;
88}
89
91{
92 NS_LOG_FUNCTION(this);
93}
94
95uint64_t
97{
98 NS_LOG_FUNCTION(this);
99 return m_totalRx;
100}
101
104{
105 NS_LOG_FUNCTION(this);
106 return m_socket;
107}
108
109std::list<Ptr<Socket>>
111{
112 NS_LOG_FUNCTION(this);
113 return m_socketList;
114}
115
116void
118{
119 NS_LOG_FUNCTION(this);
120 m_socket = nullptr;
121 m_socketList.clear();
122
123 // chain up
125}
126
127// Application Methods
128void
129PacketSink::StartApplication() // Called at time specified by Start
130{
131 NS_LOG_FUNCTION(this);
132 // Create the socket if not already
133 if (!m_socket)
134 {
136 NS_ABORT_MSG_IF(m_local.IsInvalid(), "'Local' attribute not properly set");
137 if (m_socket->Bind(m_local) == -1)
138 {
139 NS_FATAL_ERROR("Failed to bind socket");
140 }
141 m_socket->Listen();
144 {
145 Ptr<UdpSocket> udpSocket = DynamicCast<UdpSocket>(m_socket);
146 if (udpSocket)
147 {
148 // equivalent to setsockopt (MCAST_JOIN_GROUP)
149 udpSocket->MulticastJoinGroup(0, m_local);
150 }
151 else
152 {
153 NS_FATAL_ERROR("Error: joining multicast on a non-UDP socket");
154 }
155 }
156 }
157
159 {
161 }
163 {
165 }
166 else
167 {
168 m_localPort = 0;
169 }
176}
177
178void
179PacketSink::StopApplication() // Called at time specified by Stop
180{
181 NS_LOG_FUNCTION(this);
182 while (!m_socketList.empty()) // these are accepted sockets, close them
183 {
184 Ptr<Socket> acceptedSocket = m_socketList.front();
185 m_socketList.pop_front();
186 acceptedSocket->Close();
187 }
188 if (m_socket)
189 {
190 m_socket->Close();
192 }
193}
194
195void
197{
198 NS_LOG_FUNCTION(this << socket);
199 Ptr<Packet> packet;
200 Address from;
201 Address localAddress;
202 while ((packet = socket->RecvFrom(from)))
203 {
204 if (packet->GetSize() == 0)
205 { // EOF
206 break;
207 }
208 m_totalRx += packet->GetSize();
210 {
211 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " packet sink received "
212 << packet->GetSize() << " bytes from "
213 << InetSocketAddress::ConvertFrom(from).GetIpv4() << " port "
214 << InetSocketAddress::ConvertFrom(from).GetPort() << " total Rx "
215 << m_totalRx << " bytes");
216 }
218 {
219 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " packet sink received "
220 << packet->GetSize() << " bytes from "
221 << Inet6SocketAddress::ConvertFrom(from).GetIpv6() << " port "
223 << " total Rx " << m_totalRx << " bytes");
224 }
225
226 if (!m_rxTrace.IsEmpty() || !m_rxTraceWithAddresses.IsEmpty() ||
228 {
229 Ipv4PacketInfoTag interfaceInfo;
230 Ipv6PacketInfoTag interface6Info;
231 if (packet->RemovePacketTag(interfaceInfo))
232 {
233 localAddress = InetSocketAddress(interfaceInfo.GetAddress(), m_localPort);
234 }
235 else if (packet->RemovePacketTag(interface6Info))
236 {
237 localAddress = Inet6SocketAddress(interface6Info.GetAddress(), m_localPort);
238 }
239 else
240 {
241 socket->GetSockName(localAddress);
242 }
243 m_rxTrace(packet, from);
244 m_rxTraceWithAddresses(packet, from, localAddress);
245
247 {
248 PacketReceived(packet, from, localAddress);
249 }
250 }
251 }
252}
253
254void
255PacketSink::PacketReceived(const Ptr<Packet>& p, const Address& from, const Address& localAddress)
256{
257 SeqTsSizeHeader header;
258 Ptr<Packet> buffer;
259
260 auto itBuffer = m_buffer.find(from);
261 if (itBuffer == m_buffer.end())
262 {
263 itBuffer = m_buffer.insert(std::make_pair(from, Create<Packet>(0))).first;
264 }
265
266 buffer = itBuffer->second;
267 buffer->AddAtEnd(p);
268 buffer->PeekHeader(header);
269
270 NS_ABORT_IF(header.GetSize() == 0);
271
272 while (buffer->GetSize() >= header.GetSize())
273 {
274 NS_LOG_DEBUG("Removing packet of size " << header.GetSize() << " from buffer of size "
275 << buffer->GetSize());
276 Ptr<Packet> complete = buffer->CreateFragment(0, static_cast<uint32_t>(header.GetSize()));
277 buffer->RemoveAtStart(static_cast<uint32_t>(header.GetSize()));
278
279 complete->RemoveHeader(header);
280
281 m_rxTraceWithSeqTsSize(complete, from, localAddress, header);
282
283 if (buffer->GetSize() > header.GetSerializedSize())
284 {
285 buffer->PeekHeader(header);
286 }
287 else
288 {
289 break;
290 }
291 }
292}
293
294void
296{
297 NS_LOG_FUNCTION(this << socket);
298}
299
300void
302{
303 NS_LOG_FUNCTION(this << socket);
304}
305
306void
308{
309 NS_LOG_FUNCTION(this << s << from);
310 s->SetRecvCallback(MakeCallback(&PacketSink::HandleRead, this));
311 m_socketList.push_back(s);
312}
313
314} // Namespace ns3
a polymophic address class
Definition: address.h:101
bool IsInvalid() const
Definition: address.cc:71
AttributeValue implementation for Address.
Definition: address.h:286
The base class for all ns3 applications.
Definition: application.h:62
void DoDispose() override
Destructor implementation.
Definition: application.cc:86
Ptr< Node > GetNode() const
Definition: application.cc:108
AttributeValue implementation for Boolean.
Definition: boolean.h:37
An Inet6 address class.
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.
an Inet address class
static bool IsMatchingType(const Address &address)
Ipv4Address GetIpv4() const
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() const
Get the tag's address.
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.
Definition: packet-sink.h:75
static TypeId GetTypeId()
Get the type ID.
Definition: packet-sink.cc:46
std::unordered_map< Address, Ptr< Packet >, AddressHash > m_buffer
Buffer for received packets.
Definition: packet-sink.h:188
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:205
void StopApplication() override
Application specific shutdown code.
Definition: packet-sink.cc:179
TypeId m_tid
Protocol TypeId.
Definition: packet-sink.h:198
Ptr< Socket > GetListeningSocket() const
Definition: packet-sink.cc:103
uint16_t m_localPort
Local port to bind to.
Definition: packet-sink.h:196
Address m_local
Local address to bind to (address and port)
Definition: packet-sink.h:195
std::list< Ptr< Socket > > m_socketList
the accepted sockets
Definition: packet-sink.h:193
void HandleRead(Ptr< Socket > socket)
Handle a packet received by the application.
Definition: packet-sink.cc:196
uint64_t GetTotalRx() const
Definition: packet-sink.cc:96
void HandleAccept(Ptr< Socket > socket, const Address &from)
Handle an incoming connection.
Definition: packet-sink.cc:307
void HandlePeerError(Ptr< Socket > socket)
Handle an connection error.
Definition: packet-sink.cc:301
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:209
uint64_t m_totalRx
Total bytes received.
Definition: packet-sink.h:197
~PacketSink() override
Definition: packet-sink.cc:90
void StartApplication() override
Application specific startup code.
Definition: packet-sink.cc:129
void DoDispose() override
Destructor implementation.
Definition: packet-sink.cc:117
bool m_enableSeqTsSizeHeader
Enable or disable the export of SeqTsSize header.
Definition: packet-sink.h:200
std::list< Ptr< Socket > > GetAcceptedSockets() const
Definition: packet-sink.cc:110
void HandlePeerClose(Ptr< Socket > socket)
Handle an connection close.
Definition: packet-sink.cc:295
Ptr< Socket > m_socket
Listening socket.
Definition: packet-sink.h:192
void PacketReceived(const Ptr< Packet > &p, const Address &from, const Address &localAddress)
Packet received: assemble byte stream to extract SeqTsSizeHeader.
Definition: packet-sink.cc:255
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
Definition: packet-sink.h:203
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Header with a sequence, a timestamp, and a "size" attribute.
uint32_t GetSerializedSize() const override
uint64_t GetSize() const
Get the size information that the header is carrying.
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
void SetRecvPktInfo(bool flag)
Enable/Disable receive packet information to socket.
Definition: socket.cc:354
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:105
virtual int ShutdownSend()=0
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:96
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:72
virtual int Close()=0
Close a socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
virtual int Listen()=0
Listen for incoming connections.
@ S
second
Definition: nstime.h:116
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
AttributeValue implementation for TypeId.
Definition: type-id.h:598
static TypeId GetTypeId()
Get the type ID.
Ptr< const AttributeChecker > MakeAddressChecker()
Definition: address.cc:180
Ptr< const AttributeAccessor > MakeAddressAccessor(T1 a1)
Definition: address.h:286
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:81
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
Ptr< const AttributeChecker > MakeTypeIdChecker()
Definition: type-id.cc:1251
Ptr< const AttributeAccessor > MakeTypeIdAccessor(T1 a1)
Definition: type-id.h:598
Callback< R, Args... > MakeNullCallback()
Definition: callback.h:747
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition: abort.h:76
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#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:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
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...
Definition: callback.h:704