A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
csma-ping.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5// Network topology
6//
7// n0 n1 n2 n3
8// | | | |
9// =====================
10//
11// node n0,n1,n3 pings to node n2
12// node n0 generates protocol 2 (IGMP) to node n3
13
14#include "ns3/applications-module.h"
15#include "ns3/core-module.h"
16#include "ns3/csma-module.h"
17#include "ns3/internet-apps-module.h"
18#include "ns3/internet-module.h"
19#include "ns3/network-module.h"
20
21#include <cassert>
22#include <fstream>
23#include <iostream>
24#include <string>
25
26using namespace ns3;
27
28NS_LOG_COMPONENT_DEFINE("CsmaPingExample");
29
30/**
31 * Rx sink
32 *
33 * \param p The packer.
34 * \param ad The sender address.
35 */
36static void
38{
39 std::cout << *p << std::endl;
40}
41
42/**
43 * Ping RTT trace sink
44 *
45 * \param context The context.
46 * \param seqNo The Sequence Number.
47 * \param rtt The RTT.
48 */
49static void
50PingRtt(std::string context, uint16_t seqNo, Time rtt)
51{
52 std::cout << context << " " << seqNo << " " << rtt << std::endl;
53}
54
55int
56main(int argc, char* argv[])
57{
58 bool verbose{false};
59
60 CommandLine cmd(__FILE__);
61 cmd.AddValue("verbose",
62 "print received background traffic packets and ping RTT values",
63 verbose);
64 cmd.Parse(argc, argv);
65
66 // Here, we will explicitly create four nodes.
67 NS_LOG_INFO("Create nodes.");
69 c.Create(4);
70
71 // connect all our nodes to a shared channel.
72 NS_LOG_INFO("Build Topology.");
74 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
75 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
76 csma.SetDeviceAttribute("EncapsulationMode", StringValue("Llc"));
77 NetDeviceContainer devs = csma.Install(c);
78
79 // add an ip stack to all nodes.
80 NS_LOG_INFO("Add ip stack.");
81 InternetStackHelper ipStack;
82 ipStack.Install(c);
83
84 // assign ip addresses
85 NS_LOG_INFO("Assign ip addresses.");
87 ip.SetBase("192.168.1.0", "255.255.255.0");
88 Ipv4InterfaceContainer addresses = ip.Assign(devs);
89
90 NS_LOG_INFO("Create Source");
91 Config::SetDefault("ns3::Ipv4RawSocketImpl::Protocol", StringValue("2"));
92 InetSocketAddress dst(addresses.GetAddress(3));
93 OnOffHelper onoff = OnOffHelper("ns3::Ipv4RawSocketFactory", dst);
94 onoff.SetConstantRate(DataRate(15000));
95 onoff.SetAttribute("PacketSize", UintegerValue(1200));
96
97 ApplicationContainer apps = onoff.Install(c.Get(0));
98 apps.Start(Seconds(1.0));
99 apps.Stop(Seconds(10.0));
100
101 NS_LOG_INFO("Create Sink.");
102 PacketSinkHelper sink = PacketSinkHelper("ns3::Ipv4RawSocketFactory", dst);
103 apps = sink.Install(c.Get(3));
104 apps.Start(Seconds(0.0));
105 apps.Stop(Seconds(11.0));
106
107 NS_LOG_INFO("Create pinger");
108 PingHelper ping(addresses.GetAddress(2));
109 NodeContainer pingers;
110 pingers.Add(c.Get(0));
111 pingers.Add(c.Get(1));
112 pingers.Add(c.Get(3));
113 apps = ping.Install(pingers);
114 apps.Start(Seconds(2.0));
115 apps.Stop(Seconds(5.0));
116
117 NS_LOG_INFO("Configure Tracing.");
118 // first, pcap tracing in non-promiscuous mode
119 csma.EnablePcapAll("csma-ping", false);
120
121 if (verbose)
122 {
123 // then, print what the packet sink receives.
124 Config::ConnectWithoutContext("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx",
126 // finally, print the ping rtts.
127 Config::Connect("/NodeList/*/ApplicationList/*/$ns3::Ping/Rtt", MakeCallback(&PingRtt));
128 }
129
131
132 NS_LOG_INFO("Run Simulation.");
135 NS_LOG_INFO("Done.");
136
137 return 0;
138}
a polymophic address class
Definition address.h:90
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.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
Class for representing data rates.
Definition data-rate.h:78
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
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.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
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::OnOffApplication on a set of nodes.
static void EnablePrinting()
Enable printing packets metadata.
Definition packet.cc:585
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Create a ping application and associate it to a node.
Definition ping-helper.h:31
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
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
Hold an unsigned integer type.
Definition uinteger.h:34
static void SinkRx(Ptr< const Packet > p, const Address &ad)
Rx sink.
Definition csma-ping.cc:37
static void PingRtt(std::string context, uint16_t seqNo, Time rtt)
Ping RTT trace sink.
Definition csma-ping.cc:50
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
void Connect(std::string path, const CallbackBase &cb)
Definition config.cc:967
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition config.cc:943
#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
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
bool verbose
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44