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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tom Henderson (tomhend@u.washington.edu)
7 */
8
9#ifndef PACKET_SINK_H
10#define PACKET_SINK_H
11
12#include "seq-ts-size-header.h"
13
14#include "ns3/address.h"
15#include "ns3/application.h"
16#include "ns3/event-id.h"
17#include "ns3/inet-socket-address.h"
18#include "ns3/inet6-socket-address.h"
19#include "ns3/ptr.h"
20#include "ns3/traced-callback.h"
21
22#include <unordered_map>
23
24namespace ns3
25{
26
27class Address;
28class Socket;
29class Packet;
30
31/**
32 * \ingroup applications
33 * \defgroup packetsink PacketSink
34 *
35 * This application was written to complement OnOffApplication, but it
36 * is more general so a PacketSink name was selected. Functionally it is
37 * important to use in multicast situations, so that reception of the layer-2
38 * multicast frames of interest are enabled, but it is also useful for
39 * unicast as an example of how you can write something simple to receive
40 * packets at the application layer. Also, if an IP stack generates
41 * ICMP Port Unreachable errors, receiving applications will be needed.
42 */
43
44/**
45 * \ingroup packetsink
46 *
47 * \brief Receive and consume traffic generated to an IP address and port
48 *
49 * This application was written to complement OnOffApplication, but it
50 * is more general so a PacketSink name was selected. Functionally it is
51 * important to use in multicast situations, so that reception of the layer-2
52 * multicast frames of interest are enabled, but it is also useful for
53 * unicast as an example of how you can write something simple to receive
54 * packets at the application layer. Also, if an IP stack generates
55 * ICMP Port Unreachable errors, receiving applications will be needed.
56 *
57 * The constructor specifies the Address (IP address and port) and the
58 * transport protocol to use. A virtual Receive () method is installed
59 * as a callback on the receiving socket. By default, when logging is
60 * enabled, it prints out the size of packets and their address.
61 * A tracing source to Receive() is also available.
62 */
63class PacketSink : public Application
64{
65 public:
66 /**
67 * \brief Get the type ID.
68 * \return the object TypeId
69 */
70 static TypeId GetTypeId();
71 PacketSink();
72
73 ~PacketSink() override;
74
75 /**
76 * \return the total bytes received in this sink app
77 */
78 uint64_t GetTotalRx() const;
79
80 /**
81 * \return pointer to listening socket
82 */
84
85 /**
86 * \return list of pointers to accepted sockets
87 */
88 std::list<Ptr<Socket>> GetAcceptedSockets() const;
89
90 /**
91 * TracedCallback signature for a reception with addresses and SeqTsSizeHeader
92 *
93 * \param p The packet received (without the SeqTsSize header)
94 * \param from From address
95 * \param to Local address
96 * \param header The SeqTsSize header
97 */
99 const Address& from,
100 const Address& to,
101 const SeqTsSizeHeader& header);
102
103 protected:
104 void DoDispose() override;
105
106 private:
107 void StartApplication() override;
108 void StopApplication() override;
109
110 /**
111 * \brief Handle a packet received by the application
112 * \param socket the receiving socket
113 */
114 void HandleRead(Ptr<Socket> socket);
115 /**
116 * \brief Handle an incoming connection
117 * \param socket the incoming connection socket
118 * \param from the address the connection is from
119 */
120 void HandleAccept(Ptr<Socket> socket, const Address& from);
121 /**
122 * \brief Handle an connection close
123 * \param socket the connected socket
124 */
125 void HandlePeerClose(Ptr<Socket> socket);
126 /**
127 * \brief Handle an connection error
128 * \param socket the connected socket
129 */
130 void HandlePeerError(Ptr<Socket> socket);
131
132 /**
133 * \brief Packet received: assemble byte stream to extract SeqTsSizeHeader
134 * \param p received packet
135 * \param from from address
136 * \param localAddress local address
137 *
138 * The method assembles a received byte stream and extracts SeqTsSizeHeader
139 * instances from the stream to export in a trace source.
140 */
141 void PacketReceived(const Ptr<Packet>& p, const Address& from, const Address& localAddress);
142
143 /**
144 * \brief Hashing for the Address class
145 */
147 {
148 /**
149 * \brief operator ()
150 * \param x the address of which calculate the hash
151 * \return the hash of x
152 *
153 * Should this method go in address.h?
154 *
155 * It calculates the hash taking the uint32_t hash value of the IPv4 or IPv6 address.
156 * It works only for InetSocketAddresses (IPv4 version) or Inet6SocketAddresses (IPv6
157 * version)
158 */
159 size_t operator()(const Address& x) const
160 {
162 {
164 return Ipv4AddressHash()(a.GetIpv4());
165 }
167 {
169 return Ipv6AddressHash()(a.GetIpv6());
170 }
171
172 NS_ABORT_MSG("PacketSink: unexpected address type, neither IPv4 nor IPv6");
173 return 0; // silence the warnings.
174 }
175 };
176
177 std::unordered_map<Address, Ptr<Packet>, AddressHash> m_buffer; //!< Buffer for received packets
178
179 // In the case of TCP, each socket accept returns a new socket, so the
180 // listening socket is stored separately from the accepted sockets
181 Ptr<Socket> m_socket; //!< Listening socket
182 std::list<Ptr<Socket>> m_socketList; //!< the accepted sockets
183
184 Address m_local; //!< Local address to bind to (address and port)
185 uint16_t m_localPort; //!< Local port to bind to
186 uint64_t m_totalRx; //!< Total bytes received
187 TypeId m_tid; //!< Protocol TypeId
188
189 bool m_enableSeqTsSizeHeader{false}; //!< Enable or disable the export of SeqTsSize header
190
191 /// Traced Callback: received packets, source address.
193 /// Callback for tracing the packet Rx events, includes source and destination addresses
195 /// Callbacks for tracing the packet Rx events, includes source, destination addresses, and
196 /// headers
199};
200
201} // namespace ns3
202
203#endif /* PACKET_SINK_H */
a polymophic address class
Definition address.h:90
The base class for all ns3 applications.
Definition application.h:51
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.
Hash function class for IPv6 addresses.
Receive and consume traffic generated to an IP address and port.
Definition packet-sink.h:64
static TypeId GetTypeId()
Get the type ID.
std::unordered_map< Address, Ptr< Packet >, AddressHash > m_buffer
Buffer for received packets.
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_rxTraceWithAddresses
Callback for tracing the packet Rx events, includes source and destination addresses.
void StopApplication() override
Application specific shutdown code.
TypeId m_tid
Protocol TypeId.
Ptr< Socket > GetListeningSocket() const
uint16_t m_localPort
Local port to bind to.
Address m_local
Local address to bind to (address and port)
std::list< Ptr< Socket > > m_socketList
the accepted sockets
void HandleRead(Ptr< Socket > socket)
Handle a packet received by the application.
uint64_t GetTotalRx() const
void HandleAccept(Ptr< Socket > socket, const Address &from)
Handle an incoming connection.
void HandlePeerError(Ptr< Socket > socket)
Handle an connection error.
TracedCallback< Ptr< const Packet >, const Address &, const Address &, const SeqTsSizeHeader & > m_rxTraceWithSeqTsSize
Callbacks for tracing the packet Rx events, includes source, destination addresses,...
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:98
uint64_t m_totalRx
Total bytes received.
~PacketSink() override
void StartApplication() override
Application specific startup code.
void DoDispose() override
Destructor implementation.
bool m_enableSeqTsSizeHeader
Enable or disable the export of SeqTsSize header.
std::list< Ptr< Socket > > GetAcceptedSockets() const
void HandlePeerClose(Ptr< Socket > socket)
Handle an connection close.
Ptr< Socket > m_socket
Listening socket.
void PacketReceived(const Ptr< Packet > &p, const Address &from, const Address &localAddress)
Packet received: assemble byte stream to extract SeqTsSizeHeader.
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
Smart pointer class similar to boost::intrusive_ptr.
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:48
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hashing for the Address class.
size_t operator()(const Address &x) const
operator ()