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 "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
35namespace ns3
36{
37
38class Address;
39class Socket;
40class Packet;
41
42/**
43 * \ingroup applications
44 * \defgroup packetsink PacketSink
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
55/**
56 * \ingroup packetsink
57 *
58 * \brief Receive and consume traffic generated to an IP address and port
59 *
60 * This application was written to complement OnOffApplication, but it
61 * is more general so a PacketSink name was selected. Functionally it is
62 * important to use in multicast situations, so that reception of the layer-2
63 * multicast frames of interest are enabled, but it is also useful for
64 * unicast as an example of how you can write something simple to receive
65 * packets at the application layer. Also, if an IP stack generates
66 * ICMP Port Unreachable errors, receiving applications will be needed.
67 *
68 * The constructor specifies the Address (IP address and port) and the
69 * transport protocol to use. A virtual Receive () method is installed
70 * as a callback on the receiving socket. By default, when logging is
71 * enabled, it prints out the size of packets and their address.
72 * A tracing source to Receive() is also available.
73 */
74class PacketSink : public Application
75{
76 public:
77 /**
78 * \brief Get the type ID.
79 * \return the object TypeId
80 */
81 static TypeId GetTypeId();
82 PacketSink();
83
84 ~PacketSink() override;
85
86 /**
87 * \return the total bytes received in this sink app
88 */
89 uint64_t GetTotalRx() const;
90
91 /**
92 * \return pointer to listening socket
93 */
95
96 /**
97 * \return list of pointers to accepted sockets
98 */
99 std::list<Ptr<Socket>> GetAcceptedSockets() const;
100
101 /**
102 * TracedCallback signature for a reception with addresses and SeqTsSizeHeader
103 *
104 * \param p The packet received (without the SeqTsSize header)
105 * \param from From address
106 * \param to Local address
107 * \param header The SeqTsSize header
108 */
110 const Address& from,
111 const Address& to,
112 const SeqTsSizeHeader& header);
113
114 protected:
115 void DoDispose() override;
116
117 private:
118 void StartApplication() override;
119 void StopApplication() override;
120
121 /**
122 * \brief Handle a packet received by the application
123 * \param socket the receiving socket
124 */
125 void HandleRead(Ptr<Socket> socket);
126 /**
127 * \brief Handle an incoming connection
128 * \param socket the incoming connection socket
129 * \param from the address the connection is from
130 */
131 void HandleAccept(Ptr<Socket> socket, const Address& from);
132 /**
133 * \brief Handle an connection close
134 * \param socket the connected socket
135 */
136 void HandlePeerClose(Ptr<Socket> socket);
137 /**
138 * \brief Handle an connection error
139 * \param socket the connected socket
140 */
141 void HandlePeerError(Ptr<Socket> socket);
142
143 /**
144 * \brief Packet received: assemble byte stream to extract SeqTsSizeHeader
145 * \param p received packet
146 * \param from from address
147 * \param localAddress local address
148 *
149 * The method assembles a received byte stream and extracts SeqTsSizeHeader
150 * instances from the stream to export in a trace source.
151 */
152 void PacketReceived(const Ptr<Packet>& p, const Address& from, const Address& localAddress);
153
154 /**
155 * \brief Hashing for the Address class
156 */
158 {
159 /**
160 * \brief operator ()
161 * \param x the address of which calculate the hash
162 * \return the hash of x
163 *
164 * Should this method go in address.h?
165 *
166 * It calculates the hash taking the uint32_t hash value of the IPv4 or IPv6 address.
167 * It works only for InetSocketAddresses (IPv4 version) or Inet6SocketAddresses (IPv6
168 * version)
169 */
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; //!< Buffer for received packets
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
192 Ptr<Socket> m_socket; //!< Listening socket
193 std::list<Ptr<Socket>> m_socketList; //!< the accepted sockets
194
195 Address m_local; //!< Local address to bind to (address and port)
196 uint16_t m_localPort; //!< Local port to bind to
197 uint64_t m_totalRx; //!< Total bytes received
198 TypeId m_tid; //!< Protocol TypeId
199
200 bool m_enableSeqTsSizeHeader{false}; //!< Enable or disable the export of SeqTsSize header
201
202 /// Traced Callback: received packets, source address.
204 /// Callback for tracing the packet Rx events, includes source and destination addresses
206 /// Callbacks for tracing the packet Rx events, includes source, destination addresses, and
207 /// headers
210};
211
212} // namespace ns3
213
214#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: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:179
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:196
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:307
void HandlePeerError(Ptr< Socket > socket)
Handle an connection error.
Definition: packet-sink.cc:301
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:109
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:295
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:255
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:77
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