A Discrete-Event Network Simulator
API
packet-sink.h
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright 2007 University of Washington
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Tom Henderson (tomhend@u.washington.edu)
19 */
20
21#ifndef PACKET_SINK_H
22#define PACKET_SINK_H
23
24#include "ns3/application.h"
25#include "ns3/event-id.h"
26#include "ns3/ptr.h"
27#include "ns3/traced-callback.h"
28#include "ns3/address.h"
29#include "ns3/inet-socket-address.h"
30#include "ns3/inet6-socket-address.h"
31#include "ns3/seq-ts-size-header.h"
32#include <unordered_map>
33
34namespace ns3 {
35
36class Address;
37class Socket;
38class Packet;
39
72class PacketSink : public Application
73{
74public:
79 static TypeId GetTypeId (void);
80 PacketSink ();
81
82 virtual ~PacketSink ();
83
87 uint64_t GetTotalRx () const;
88
93
97 std::list<Ptr<Socket> > GetAcceptedSockets (void) const;
98
107 typedef void (* SeqTsSizeCallback)(Ptr<const Packet> p, const Address &from, const Address & to,
108 const SeqTsSizeHeader &header);
109
110protected:
111 virtual void DoDispose (void);
112private:
113 // inherited from Application base class.
114 virtual void StartApplication (void); // Called at time specified by Start
115 virtual void StopApplication (void); // Called at time specified by Stop
116
121 void HandleRead (Ptr<Socket> socket);
127 void HandleAccept (Ptr<Socket> socket, const Address& from);
132 void HandlePeerClose (Ptr<Socket> socket);
137 void HandlePeerError (Ptr<Socket> socket);
138
148 void PacketReceived (const Ptr<Packet> &p, const Address &from, const Address &localAddress);
149
154 {
165 size_t operator() (const Address &x) const
166 {
168 {
170 return Ipv4AddressHash()(a.GetIpv4 ());
171 }
173 {
175 return Ipv6AddressHash()(a.GetIpv6 ());
176 }
177
178 NS_ABORT_MSG ("PacketSink: unexpected address type, neither IPv4 nor IPv6");
179 return 0; //silence the warnings.
180 }
181 };
182
183 std::unordered_map<Address, Ptr<Packet>, AddressHash> m_buffer;
184
185 // In the case of TCP, each socket accept returns a new socket, so the
186 // listening socket is stored separately from the accepted sockets
188 std::list<Ptr<Socket> > m_socketList;
189
191 uint16_t m_localPort;
192 uint64_t m_totalRx;
194
196
203};
204
205} // namespace ns3
206
207#endif /* PACKET_SINK_H */
208
a polymophic address class
Definition: address.h:91
The base class for all ns3 applications.
Definition: application.h:61
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(void) const
Get the IPv6 address.
an Inet address class
Ipv4Address GetIpv4(void) const
static bool IsMatchingType(const Address &address)
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:397
Hash function class for IPv6 addresses.
Definition: ipv6-address.h:684
Receive and consume traffic generated to an IP address and port.
Definition: packet-sink.h:73
virtual void DoDispose(void)
Destructor implementation.
Definition: packet-sink.cc:113
std::unordered_map< Address, Ptr< Packet >, AddressHash > m_buffer
Buffer for received packets.
Definition: packet-sink.h:183
Ptr< Socket > GetListeningSocket(void) const
Definition: packet-sink.cc:100
TypeId m_tid
Protocol TypeId.
Definition: packet-sink.h:193
static TypeId GetTypeId(void)
Get the type ID.
Definition: packet-sink.cc:45
uint16_t m_localPort
Local port to bind to.
Definition: packet-sink.h:191
Address m_local
Local address to bind to (address and port)
Definition: packet-sink.h:190
std::list< Ptr< Socket > > GetAcceptedSockets(void) const
Definition: packet-sink.cc:107
virtual void StartApplication(void)
Application specific startup code.
Definition: packet-sink.cc:125
std::list< Ptr< Socket > > m_socketList
the accepted sockets
Definition: packet-sink.h:188
virtual void StopApplication(void)
Application specific shutdown code.
Definition: packet-sink.cc:175
void HandleRead(Ptr< Socket > socket)
Handle a packet received by the application.
Definition: packet-sink.cc:191
uint64_t GetTotalRx() const
Definition: packet-sink.cc:93
void HandleAccept(Ptr< Socket > socket, const Address &from)
Handle an incoming connection.
Definition: packet-sink.cc:301
void HandlePeerError(Ptr< Socket > socket)
Handle an connection error.
Definition: packet-sink.cc:296
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
Definition: packet-sink.h:198
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:200
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:107
uint64_t m_totalRx
Total bytes received.
Definition: packet-sink.h:192
bool m_enableSeqTsSizeHeader
Enable or disable the export of SeqTsSize header.
Definition: packet-sink.h:195
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:202
void HandlePeerClose(Ptr< Socket > socket)
Handle an connection close.
Definition: packet-sink.cc:291
Ptr< Socket > m_socket
Listening socket.
Definition: packet-sink.h:187
void PacketReceived(const Ptr< Packet > &p, const Address &from, const Address &localAddress)
Packet received: assemble byte stream to extract SeqTsSizeHeader.
Definition: packet-sink.cc:252
virtual ~PacketSink()
Definition: packet-sink.cc:88
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:50
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hashing for the Address class.
Definition: packet-sink.h:154
size_t operator()(const Address &x) const
operator ()
Definition: packet-sink.h:165