A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 "seq-ts-size-header.h"
24 
25 #include "ns3/address.h"
26 #include "ns3/application.h"
27 #include "ns3/event-id.h"
28 #include "ns3/inet-socket-address.h"
29 #include "ns3/inet6-socket-address.h"
30 #include "ns3/ptr.h"
31 #include "ns3/traced-callback.h"
32 
33 #include <unordered_map>
34 
35 namespace ns3
36 {
37 
38 class Address;
39 class Socket;
40 class Packet;
41 
74 class PacketSink : public Application
75 {
76  public:
81  static TypeId GetTypeId();
82  PacketSink();
83 
84  ~PacketSink() override;
85 
89  uint64_t GetTotalRx() const;
90 
95 
99  std::list<Ptr<Socket>> GetAcceptedSockets() const;
100 
110  const Address& from,
111  const Address& to,
112  const SeqTsSizeHeader& header);
113 
114  protected:
115  void DoDispose() override;
116 
117  private:
118  // inherited from Application base class.
119  void StartApplication() override; // Called at time specified by Start
120  void StopApplication() override; // Called at time specified by Stop
121 
126  void HandleRead(Ptr<Socket> socket);
132  void HandleAccept(Ptr<Socket> socket, const Address& from);
137  void HandlePeerClose(Ptr<Socket> socket);
142  void HandlePeerError(Ptr<Socket> socket);
143 
153  void PacketReceived(const Ptr<Packet>& p, const Address& from, const Address& localAddress);
154 
158  struct AddressHash
159  {
171  size_t operator()(const Address& x) const
172  {
174  {
176  return Ipv4AddressHash()(a.GetIpv4());
177  }
179  {
181  return Ipv6AddressHash()(a.GetIpv6());
182  }
183 
184  NS_ABORT_MSG("PacketSink: unexpected address type, neither IPv4 nor IPv6");
185  return 0; // silence the warnings.
186  }
187  };
188 
189  std::unordered_map<Address, Ptr<Packet>, AddressHash> m_buffer;
190 
191  // In the case of TCP, each socket accept returns a new socket, so the
192  // listening socket is stored separately from the accepted sockets
194  std::list<Ptr<Socket>> m_socketList;
195 
197  uint16_t m_localPort;
198  uint64_t m_totalRx;
200 
202 
211 };
212 
213 } // namespace ns3
214 
215 #endif /* PACKET_SINK_H */
a polymophic address class
Definition: address.h:101
The base class for all ns3 applications.
Definition: application.h:62
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: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:189
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:206
void StopApplication() override
Application specific shutdown code.
Definition: packet-sink.cc:178
TypeId m_tid
Protocol TypeId.
Definition: packet-sink.h:199
Ptr< Socket > GetListeningSocket() const
Definition: packet-sink.cc:103
uint16_t m_localPort
Local port to bind to.
Definition: packet-sink.h:197
Address m_local
Local address to bind to (address and port)
Definition: packet-sink.h:196
std::list< Ptr< Socket > > m_socketList
the accepted sockets
Definition: packet-sink.h:194
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:210
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:109
uint64_t m_totalRx
Total bytes received.
Definition: packet-sink.h:198
~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:201
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:193
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:204
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:159
size_t operator()(const Address &x) const
operator ()
Definition: packet-sink.h:171