A Discrete-Event Network Simulator
API
nix-simple-multi-address.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 NITK Surathkal
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: Ameya Deshpande <ameyanrd@outlook.com>
18  *
19  */
20 
21 #include "ns3/applications-module.h"
22 #include "ns3/core-module.h"
23 #include "ns3/internet-module.h"
24 #include "ns3/ipv4-list-routing-helper.h"
25 #include "ns3/ipv4-static-routing-helper.h"
26 #include "ns3/network-module.h"
27 #include "ns3/nix-vector-helper.h"
28 #include "ns3/point-to-point-module.h"
29 
132 using namespace ns3;
133 
134 NS_LOG_COMPONENT_DEFINE("NixSimpleMultiAddressExample");
135 
136 int
137 main(int argc, char* argv[])
138 {
139  CommandLine cmd(__FILE__);
140  cmd.Parse(argc, argv);
141 
142  LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
143  LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
144 
145  NodeContainer nodes12;
146  nodes12.Create(2);
147 
148  NodeContainer nodes23;
149  nodes23.Add(nodes12.Get(1));
150  nodes23.Create(1);
151 
152  NodeContainer nodes34;
153  nodes34.Add(nodes23.Get(1));
154  nodes34.Create(1);
155 
157  pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
158  pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
159 
160  NodeContainer allNodes = NodeContainer(nodes12, nodes23.Get(1), nodes34.Get(1));
161 
162  // NixHelper to install nix-vector routing
163  // on all nodes
166  stack.SetRoutingHelper(nixRouting); // has effect on the next Install ()
167  stack.Install(allNodes);
168 
169  NetDeviceContainer devices12;
170  NetDeviceContainer devices23;
171  NetDeviceContainer devices34;
172  devices12 = pointToPoint.Install(nodes12);
173  devices23 = pointToPoint.Install(nodes23);
174  devices34 = pointToPoint.Install(nodes34);
175 
176  Ipv4AddressHelper address1;
177  address1.SetBase("10.1.1.0", "255.255.255.0");
178  Ipv4AddressHelper address2;
179  address2.SetBase("10.1.2.0", "255.255.255.0");
180  Ipv4AddressHelper address3;
181  address3.SetBase("10.1.3.0", "255.255.255.0");
182  Ipv4AddressHelper address4;
183  address4.SetBase("10.2.1.0", "255.255.255.0");
184  Ipv4AddressHelper address5;
185  address5.SetBase("10.2.3.0", "255.255.255.0");
186 
187  address1.Assign(devices12);
188  address2.Assign(devices23);
189  Ipv4InterfaceContainer interfaces34 = address3.Assign(devices34);
190  Ipv4InterfaceContainer interfaces12 = address4.Assign(devices12);
191 
193 
194  ApplicationContainer serverApps = echoServer.Install(nodes34.Get(1));
195  serverApps.Start(Seconds(1.0));
196  serverApps.Stop(Seconds(10.0));
197 
198  // Set the destination address as 10.1.3.2
199  UdpEchoClientHelper echoClient(interfaces34.GetAddress(1), 9);
200  echoClient.SetAttribute("MaxPackets", UintegerValue(1));
201  echoClient.SetAttribute("Interval", TimeValue(Seconds(1.)));
202  echoClient.SetAttribute("PacketSize", UintegerValue(1024));
203 
204  ApplicationContainer clientApps = echoClient.Install(nodes12.Get(0));
205  clientApps.Start(Seconds(2.0));
206  clientApps.Stop(Seconds(10.0));
207 
208  // Trace routing paths for different source and destinations.
209  Ptr<OutputStreamWrapper> routingStream =
210  Create<OutputStreamWrapper>("nix-simple-multi-address.routes", std::ios::out);
211  // Check the path from n0 to n1 (10.1.3.2).
212  nixRouting.PrintRoutingPathAt(Seconds(3),
213  nodes12.Get(0),
214  interfaces12.GetAddress(1, 1),
215  routingStream);
216  // Trace routing tables.
217  Ipv4NixVectorHelper::PrintRoutingTableAllAt(Seconds(4), routingStream);
218  // Assign address5 addresses to n2 and n3.
219  // n2 gets 10.2.3.1 and n3 gets 10.2.3.2 on the same interface.
220  Simulator::Schedule(Seconds(5), &Ipv4AddressHelper::Assign, &address5, devices34);
221 
222  // Trace routing tables.
223  // Notice that NixCache and Ipv4RouteCache becomes empty for each node.
224  // This happens because new addresses are added at t = +5s, due to which
225  // existing caches get flushed.
226  Ipv4NixVectorHelper::PrintRoutingTableAllAt(Seconds(6), routingStream);
227 
228  // Check the path from n0 to n3 (10.2.3.2).
229  nixRouting.PrintRoutingPathAt(Seconds(7),
230  nodes12.Get(0),
231  Ipv4Address("10.2.3.2"),
232  routingStream);
233 
234  Simulator::Run();
236  return 0;
237 }
holds a vector of ns3::Application pointers.
Parse command-line arguments.
Definition: command-line.h:232
aggregate IP/TCP/UDP functionality to existing Nodes.
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...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
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
Helper class that adds Nix-vector routing to nodes.
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.
Build a set of PointToPointNetDevice objects.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
Hold variables of type string.
Definition: string.h:56
Create an application which sends a UDP packet and waits for an echo of this packet.
Create a server application which waits for input UDP packets and sends them back to the original sen...
Hold an unsigned integer type.
Definition: uinteger.h:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
echoClient
Definition: first.py:59
serverApps
Definition: first.py:54
pointToPoint
Definition: first.py:38
echoServer
Definition: first.py:52
clientApps
Definition: first.py:64
stack
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
cmd
Definition: second.py:40