A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tcp-pcap-nanosec-example.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5// ============================================================ //
6// BASED ON tcp-bulk-send.cc //
7// ============================================================ //
8
9// Network topology
10//
11// n0 ----------- n1
12// 500 Kbps
13// 5 ms
14//
15// - Flow from n0 to n1 using BulkSendApplication.
16// - Tracing of queues and packet receptions to file "tcp-pcap-nanosec-example.pcap"
17// when tracing is turned on.
18// - Trace file timestamps are recorded in nanoseconds, when requested
19//
20
21// ============================================================ //
22// NOTE: You can check the "magic" number of a pcap file with //
23// the following command: //
24// //
25// od -N4 -tx1 filename.pcap //
26// //
27// ============================================================ //
28
29#include "ns3/applications-module.h"
30#include "ns3/core-module.h"
31#include "ns3/internet-module.h"
32#include "ns3/network-module.h"
33#include "ns3/point-to-point-module.h"
34
35#include <fstream>
36#include <string>
37
38using namespace ns3;
39
40NS_LOG_COMPONENT_DEFINE("TcpPcapNanosecExample");
41
42int
43main(int argc, char* argv[])
44{
45 bool tracing = false;
46 bool nanosec = false;
47 uint32_t maxBytes = 0;
48
49 //
50 // Allow the user to override any of the defaults at
51 // run-time, via command-line arguments
52 //
53 CommandLine cmd(__FILE__);
54 cmd.AddValue("tracing", "Flag to enable tracing", tracing);
55 cmd.AddValue("nanosec", "Flag to use nanosecond timestamps for pcap as default", nanosec);
56 cmd.AddValue("maxBytes", "Total number of bytes for application to send", maxBytes);
57 cmd.Parse(argc, argv);
58
59 //
60 // If requested via the --nanosec cmdline flag, generate nanosecond timestamp for pcap traces
61 //
62 if (nanosec)
63 {
64 Config::SetDefault("ns3::PcapFileWrapper::NanosecMode", BooleanValue(true));
65 }
66
67 //
68 // Explicitly create the nodes required by the topology (shown above).
69 //
70 NS_LOG_INFO("Create nodes.");
72 nodes.Create(2);
73
74 NS_LOG_INFO("Create channels.");
75
76 //
77 // Explicitly create the point-to-point link required by the topology (shown above).
78 //
80 pointToPoint.SetDeviceAttribute("DataRate", StringValue("500Kbps"));
81 pointToPoint.SetChannelAttribute("Delay", StringValue("5ms"));
82
84 devices = pointToPoint.Install(nodes);
85
86 //
87 // Install the internet stack on the nodes
88 //
90 internet.Install(nodes);
91
92 //
93 // We've got the "hardware" in place. Now we need to add IP addresses.
94 //
95 NS_LOG_INFO("Assign IP Addresses.");
97 ipv4.SetBase("10.1.1.0", "255.255.255.0");
98 Ipv4InterfaceContainer i = ipv4.Assign(devices);
99
100 NS_LOG_INFO("Create Applications.");
101
102 //
103 // Create a BulkSendApplication and install it on node 0
104 //
105 uint16_t port = 9; // well-known echo port number
106
107 BulkSendHelper source("ns3::TcpSocketFactory", InetSocketAddress(i.GetAddress(1), port));
108 // Set the amount of data to send in bytes. Zero is unlimited.
109 source.SetAttribute("MaxBytes", UintegerValue(maxBytes));
110 ApplicationContainer sourceApps = source.Install(nodes.Get(0));
111 sourceApps.Start(Seconds(0.0));
112 sourceApps.Stop(Seconds(10.0));
113
114 //
115 // Create a PacketSinkApplication and install it on node 1
116 //
117 PacketSinkHelper sink("ns3::TcpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), port));
118 ApplicationContainer sinkApps = sink.Install(nodes.Get(1));
119 sinkApps.Start(Seconds(0.0));
120 sinkApps.Stop(Seconds(10.0));
121
122 //
123 // Set up tracing if enabled
124 //
125 if (tracing)
126 {
127 AsciiTraceHelper ascii;
128 pointToPoint.EnablePcapAll("tcp-pcap-nanosec-example", false);
129 }
130
131 //
132 // Now, do the actual simulation.
133 //
134 NS_LOG_INFO("Run Simulation.");
138 NS_LOG_INFO("Done.");
139
140 Ptr<PacketSink> sink1 = DynamicCast<PacketSink>(sinkApps.Get(0));
141 std::cout << "Total Bytes Received: " << sink1->GetTotalRx() << std::endl;
142
143 return 0;
144}
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Manage ASCII trace files for device models.
A helper to make it easier to instantiate an ns3::BulkSendApplication on a set of nodes.
Parse command-line arguments.
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
Hold an unsigned integer type.
Definition uinteger.h:34
uint16_t port
Definition dsdv-manet.cc:33
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
NodeContainer nodes
pointToPoint
Definition first.py:27
devices
Definition first.py:31
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
bool tracing
Flag to enable/disable generation of tracing files.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44