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