A Discrete-Event Network Simulator
API
nix-simple.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  * Modified By: Ameya Deshpande <ameyanrd@outlook.com>
19  * Tommaso Pecorella <tommaso.pecorella@unifi.it>
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/ipv6-static-routing-helper.h"
30 #include "ns3/ipv6-list-routing-helper.h"
31 #include "ns3/nix-vector-helper.h"
32 
191 using namespace ns3;
192 
193 NS_LOG_COMPONENT_DEFINE ("NixSimpleExample");
194 
195 int
196 main (int argc, char *argv[])
197 {
198  bool useIpv6 = false;
199 
200  CommandLine cmd (__FILE__);
201  cmd.AddValue ("useIPv6", "Use IPv6 instead of IPv4", useIpv6);
202  cmd.Parse (argc, argv);
203 
204  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
205  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
206 
207  NodeContainer nodes12;
208  nodes12.Create (2);
209 
210  NodeContainer nodes23;
211  nodes23.Add (nodes12.Get (1));
212  nodes23.Create (1);
213 
214  NodeContainer nodes34;
215  nodes34.Add (nodes23.Get (1));
216  nodes34.Create (1);
217 
218  NodeContainer nodes13;
219  nodes13.Add (nodes12.Get (0));
220  nodes13.Add (nodes34.Get (0));
221 
223  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
224  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
225 
226  NodeContainer allNodes = NodeContainer (nodes12, nodes23.Get (1), nodes34.Get (1));
227 
228  NetDeviceContainer devices12;
229  NetDeviceContainer devices23;
230  NetDeviceContainer devices34;
231  NetDeviceContainer devices13;
232  devices12 = pointToPoint.Install (nodes12);
233  devices23 = pointToPoint.Install (nodes23);
234  devices34 = pointToPoint.Install (nodes34);
235  devices13 = pointToPoint.Install (nodes13);
236 
237  Address udpServerAddress;
238 
239  if (!useIpv6)
240  {
241  // NixHelper to install nix-vector routing
242  // on all nodes
243  Ipv4NixVectorHelper nixRouting;
245  stack.SetRoutingHelper (nixRouting); // has effect on the next Install ()
246  stack.Install (allNodes);
247 
248  Ipv4AddressHelper address1;
249  address1.SetBase ("10.1.1.0", "255.255.255.0");
250  Ipv4AddressHelper address2;
251  address2.SetBase ("10.1.2.0", "255.255.255.0");
252  Ipv4AddressHelper address3;
253  address3.SetBase ("10.1.3.0", "255.255.255.0");
254  Ipv4AddressHelper address4;
255  address4.SetBase ("10.1.4.0", "255.255.255.0");
256 
257  Ipv4InterfaceContainer interfaces12 = address1.Assign (devices12);
258  Ipv4InterfaceContainer interfaces23 = address2.Assign (devices23);
259  Ipv4InterfaceContainer interfaces34 = address3.Assign (devices34);
260  Ipv4InterfaceContainer interfaces13 = address4.Assign (devices13);
261 
262  udpServerAddress = interfaces34.GetAddress (1);
263 
264  // Trace routing paths for different source and destinations.
265  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("nix-simple-ipv4.routes", std::ios::out);
266  nixRouting.PrintRoutingPathAt (Seconds (3), nodes12.Get (0), interfaces34.GetAddress (1), routingStream);
267  nixRouting.PrintRoutingPathAt (Seconds (5), nodes12.Get (1), interfaces34.GetAddress (1), routingStream);
268  nixRouting.PrintRoutingPathAt (Seconds (6), nodes23.Get (1), interfaces12.GetAddress (0), routingStream);
269  nixRouting.PrintRoutingPathAt (Seconds (7), nodes12.Get (1), interfaces12.GetAddress (1), routingStream);
270  // Trace routing tables
271  nixRouting.PrintRoutingTableAllAt (Seconds (8), routingStream);
272  }
273  else
274  {
275  // NixHelper to install nix-vector routing
276  // on all nodes
277  Ipv6NixVectorHelper nixRouting;
279  stack.SetRoutingHelper (nixRouting); // has effect on the next Install ()
280  stack.Install (allNodes);
281 
282  Ipv6AddressHelper address1;
283  address1.SetBase (Ipv6Address ("2001:1::"), Ipv6Prefix (64));
284  Ipv6AddressHelper address2;
285  address2.SetBase (Ipv6Address ("2001:2::"), Ipv6Prefix (64));
286  Ipv6AddressHelper address3;
287  address3.SetBase (Ipv6Address ("2001:3::"), Ipv6Prefix (64));
288  Ipv6AddressHelper address4;
289  address4.SetBase (Ipv6Address ("2001:4::"), Ipv6Prefix (64));
290 
291  Ipv6InterfaceContainer interfaces12 = address1.Assign (devices12);
292  Ipv6InterfaceContainer interfaces23 = address2.Assign (devices23);
293  Ipv6InterfaceContainer interfaces34 = address3.Assign (devices34);
294  Ipv6InterfaceContainer interfaces13 = address4.Assign (devices13);
295 
296  udpServerAddress = interfaces34.GetAddress (1, 1);
297 
298  // Trace routing paths for different source and destinations.
299  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("nix-simple-ipv6.routes", std::ios::out);
300  nixRouting.PrintRoutingPathAt (Seconds (3), nodes12.Get (0), interfaces34.GetAddress (1, 1), routingStream);
301  nixRouting.PrintRoutingPathAt (Seconds (5), nodes12.Get (1), interfaces34.GetAddress (1, 1), routingStream);
302  nixRouting.PrintRoutingPathAt (Seconds (6), nodes23.Get (1), interfaces12.GetAddress (0, 1), routingStream);
303  nixRouting.PrintRoutingPathAt (Seconds (7), nodes12.Get (1), interfaces12.GetAddress (1, 1), routingStream);
304  // Trace routing tables
305  nixRouting.PrintRoutingTableAllAt (Seconds (8), routingStream);
306  }
307 
309 
310  ApplicationContainer serverApps = echoServer.Install (nodes34.Get (1));
311  serverApps.Start (Seconds (1.0));
312  serverApps.Stop (Seconds (10.0));
313 
314  UdpEchoClientHelper echoClient (udpServerAddress, 9);
315  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
316  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.)));
317  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
318 
319  ApplicationContainer clientApps = echoClient.Install (nodes12.Get (0));
320  clientApps.Start (Seconds (2.0));
321  clientApps.Stop (Seconds (10.0));
322 
323  Simulator::Run ();
325  return 0;
326 }
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::LOG_LEVEL_INFO
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
ns3::UdpEchoClientHelper
Create an application which sends a UDP packet and waits for an echo of this packet.
Definition: udp-echo-helper.h:107
ns3::LogComponentEnable
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
ns3::Ipv6Address
Describes an IPv6 address.
Definition: ipv6-address.h:50
ns3::Ipv6AddressHelper::SetBase
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Definition: ipv6-address-helper.cc:69
ns3::Ipv6AddressHelper
Helper class to auto-assign global IPv6 unicast addresses.
Definition: ipv6-address-helper.h:83
ns3::NodeContainer::Create
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Definition: node-container.cc:98
ns3::NodeContainer::Add
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
Definition: node-container.cc:114
ns3::Ipv4AddressHelper::SetBase
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Definition: ipv4-address-helper.cc:64
ns3::Ipv6AddressHelper::Assign
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Definition: ipv6-address-helper.cc:210
ns3::Ptr< OutputStreamWrapper >
ns3::NixVectorHelper
Helper class that adds Nix-vector routing to nodes.
Definition: nix-vector-helper.h:51
first.stack
stack
Definition: first.py:41
ns3::Address
a polymophic address class
Definition: address.h:91
ns3::Ipv6InterfaceContainer::GetAddress
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
Definition: ipv6-interface-container.cc:57
ns3::Ipv4InterfaceContainer
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Definition: ipv4-interface-container.h:55
ns3::Ipv6InterfaceContainer
Keep track of a set of IPv6 interfaces.
Definition: ipv6-interface-container.h:42
ns3::NodeContainer::Get
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Definition: node-container.cc:93
second.cmd
cmd
Definition: second.py:35
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
first.echoClient
echoClient
Definition: first.py:56
first.clientApps
clientApps
Definition: first.py:61
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Ipv4AddressHelper::Assign
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Definition: ipv4-address-helper.cc:135
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition: application-container.h:43
first.serverApps
serverApps
Definition: first.py:52
first.pointToPoint
pointToPoint
Definition: first.py:35
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::PointToPointHelper
Build a set of PointToPointNetDevice objects.
Definition: point-to-point-helper.h:45
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::Ipv4InterfaceContainer::GetAddress
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Definition: ipv4-interface-container.cc:59
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::UdpEchoServerHelper
Create a server application which waits for input UDP packets and sends them back to the original sen...
Definition: udp-echo-helper.h:38
ns3::Ipv6Prefix
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::NixVectorHelper::PrintRoutingPathAt
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.
Definition: nix-vector-helper.cc:67
first.echoServer
echoServer
Definition: first.py:50