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