A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
packet-sink.h
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
20#ifndef PACKET_SINK_H
21#define PACKET_SINK_H
22
23#include "ns3/address.h"
24#include "ns3/application.h"
25#include "ns3/event-id.h"
26#include "ns3/inet-socket-address.h"
27#include "ns3/inet6-socket-address.h"
28#include "ns3/ptr.h"
29#include "ns3/seq-ts-size-header.h"
30#include "ns3/traced-callback.h"
31
32#include <unordered_map>
33
34namespace ns3
35{
36
37class Address;
38class Socket;
39class Packet;
40
73class PacketSink : public Application
74{
75 public:
80 static TypeId GetTypeId();
81 PacketSink();
82
83 ~PacketSink() override;
84
88 uint64_t GetTotalRx() const;
89
94
98 std::list<Ptr<Socket>> GetAcceptedSockets() const;
99
109 const Address& from,
110 const Address& to,
111 const SeqTsSizeHeader& header);
112
113 protected:
114 void DoDispose() override;
115
116 private:
117 // inherited from Application base class.
118 void StartApplication() override; // Called at time specified by Start
119 void StopApplication() override; // Called at time specified by Stop
120
125 void HandleRead(Ptr<Socket> socket);
131 void HandleAccept(Ptr<Socket> socket, const Address& from);
136 void HandlePeerClose(Ptr<Socket> socket);
141 void HandlePeerError(Ptr<Socket> socket);
142
152 void PacketReceived(const Ptr<Packet>& p, const Address& from, const Address& localAddress);
153
158 {
170 size_t operator()(const Address& x) const
171 {
173 {
175 return Ipv4AddressHash()(a.GetIpv4());
176 }
178 {
180 return Ipv6AddressHash()(a.GetIpv6());
181 }
182
183 NS_ABORT_MSG("PacketSink: unexpected address type, neither IPv4 nor IPv6");
184 return 0; // silence the warnings.
185 }
186 };
187
188 std::unordered_map<Address, Ptr<Packet>, AddressHash> m_buffer;
189
190 // In the case of TCP, each socket accept returns a new socket, so the
191 // listening socket is stored separately from the accepted sockets
193 std::list<Ptr<Socket>> m_socketList;
194
196 uint16_t m_localPort;
197 uint64_t m_totalRx;
199
201
210};
211
212} // namespace ns3
213
214#endif /* PACKET_SINK_H */
a polymophic address class
Definition: address.h:100
The base class for all ns3 applications.
Definition: application.h:61
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() 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.
Class providing an hash for IPv4 addresses.
Definition: ipv4-address.h:401
Hash function class for IPv6 addresses.
Definition: ipv6-address.h:689
Receive and consume traffic generated to an IP address and port.
Definition: packet-sink.h:74
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:178
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:195
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:306
void HandlePeerError(Ptr< Socket > socket)
Handle an connection error.
Definition: packet-sink.cc:300
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
void(* SeqTsSizeCallback)(Ptr< const Packet > p, const Address &from, const Address &to, const SeqTsSizeHeader &header)
TracedCallback signature for a reception with addresses and SeqTsSizeHeader.
Definition: packet-sink.h:108
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:294
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:254
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:78
Header with a sequence, a timestamp, and a "size" attribute.
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:49
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hashing for the Address class.
Definition: packet-sink.h:158
size_t operator()(const Address &x) const
operator ()
Definition: packet-sink.h:170