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