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 
181 using namespace ns3;
182 
183 NS_LOG_COMPONENT_DEFINE ("NixSimpleExample");
184 
185 int
186 main (int argc, char *argv[])
187 {
188  bool useIpv6 = false;
189 
190  CommandLine cmd (__FILE__);
191  cmd.AddValue ("useIPv6", "Use IPv6 instead of IPv4", useIpv6);
192  cmd.Parse (argc, argv);
193 
194  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
195  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
196 
198  nodes.Create (4);
199 
201  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
202  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
203 
204  NetDeviceContainer devices01;
205  NetDeviceContainer devices12;
206  NetDeviceContainer devices23;
207  NetDeviceContainer devices02;
208  devices01 = pointToPoint.Install (NodeContainer (nodes.Get (0), nodes.Get (1)));
209  devices12 = pointToPoint.Install (NodeContainer (nodes.Get (1), nodes.Get (2)));
210  devices23 = pointToPoint.Install (NodeContainer (nodes.Get (2), nodes.Get (3)));
211  devices02 = pointToPoint.Install (NodeContainer (nodes.Get (0), nodes.Get (2)));
212 
213  Address udpServerAddress;
214 
215  if (!useIpv6)
216  {
217  // NixHelper to install nix-vector routing
218  // on all nodes
219  Ipv4NixVectorHelper nixRouting;
221  stack.SetRoutingHelper (nixRouting); // has effect on the next Install ()
222  stack.Install (nodes);
223 
224  Ipv4AddressHelper address1;
225  address1.SetBase ("10.1.1.0", "255.255.255.0");
226  Ipv4AddressHelper address2;
227  address2.SetBase ("10.1.2.0", "255.255.255.0");
228  Ipv4AddressHelper address3;
229  address3.SetBase ("10.1.3.0", "255.255.255.0");
230  Ipv4AddressHelper address4;
231  address4.SetBase ("10.1.4.0", "255.255.255.0");
232 
233  Ipv4InterfaceContainer interfaces01 = address1.Assign (devices01);
234  Ipv4InterfaceContainer interfaces12 = address2.Assign (devices12);
235  Ipv4InterfaceContainer interfaces23 = address3.Assign (devices23);
236  Ipv4InterfaceContainer interfaces02 = address4.Assign (devices02);
237 
238  udpServerAddress = interfaces23.GetAddress (1);
239 
240  // Trace routing paths for different source and destinations.
241  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("nix-simple-ipv4.routes", std::ios::out);
242  nixRouting.PrintRoutingPathAt (Seconds (3), nodes.Get (0), interfaces23.GetAddress (1), routingStream);
243  nixRouting.PrintRoutingPathAt (Seconds (5), nodes.Get (1), interfaces23.GetAddress (1), routingStream);
244  nixRouting.PrintRoutingPathAt (Seconds (6), nodes.Get (2), interfaces01.GetAddress (0), routingStream);
245  nixRouting.PrintRoutingPathAt (Seconds (7), nodes.Get (1), interfaces01.GetAddress (1), routingStream);
246  // Trace routing tables
247  nixRouting.PrintRoutingTableAllAt (Seconds (8), routingStream);
248  }
249  else
250  {
251  // NixHelper to install nix-vector routing
252  // on all nodes
253  Ipv6NixVectorHelper nixRouting;
255  stack.SetRoutingHelper (nixRouting); // has effect on the next Install ()
256  stack.Install (nodes);
257 
258  Ipv6AddressHelper address1;
259  address1.SetBase (Ipv6Address ("2001:1::"), Ipv6Prefix (64));
260  Ipv6AddressHelper address2;
261  address2.SetBase (Ipv6Address ("2001:2::"), Ipv6Prefix (64));
262  Ipv6AddressHelper address3;
263  address3.SetBase (Ipv6Address ("2001:3::"), Ipv6Prefix (64));
264  Ipv6AddressHelper address4;
265  address4.SetBase (Ipv6Address ("2001:4::"), Ipv6Prefix (64));
266 
267  Ipv6InterfaceContainer interfaces01 = address1.Assign (devices01);
268  Ipv6InterfaceContainer interfaces12 = address2.Assign (devices12);
269  Ipv6InterfaceContainer interfaces23 = address3.Assign (devices23);
270  Ipv6InterfaceContainer interfaces02 = address4.Assign (devices02);
271 
272  udpServerAddress = interfaces23.GetAddress (1, 1);
273 
274  // Trace routing paths for different source and destinations.
275  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("nix-simple-ipv6.routes", std::ios::out);
276  nixRouting.PrintRoutingPathAt (Seconds (3), nodes.Get (0), interfaces23.GetAddress (1, 1), routingStream);
277  nixRouting.PrintRoutingPathAt (Seconds (5), nodes.Get (1), interfaces23.GetAddress (1, 1), routingStream);
278  nixRouting.PrintRoutingPathAt (Seconds (6), nodes.Get (2), interfaces01.GetAddress (0, 1), routingStream);
279  nixRouting.PrintRoutingPathAt (Seconds (7), nodes.Get (1), interfaces01.GetAddress (1, 1), routingStream);
280  // Trace routing tables
281  nixRouting.PrintRoutingTableAllAt (Seconds (8), routingStream);
282  }
283 
285 
286  ApplicationContainer serverApps = echoServer.Install (nodes.Get (3));
287  serverApps.Start (Seconds (1.0));
288  serverApps.Stop (Seconds (10.0));
289 
290  UdpEchoClientHelper echoClient (udpServerAddress, 9);
291  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
292  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.)));
293  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
294 
295  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
296  clientApps.Start (Seconds (2.0));
297  clientApps.Stop (Seconds (10.0));
298 
299  Simulator::Run ();
301  return 0;
302 }
a polymophic address class
Definition: address.h:91
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...
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class to auto-assign global IPv6 unicast addresses.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Describes an IPv6 address.
Definition: ipv6-address.h:50
Keep track of a set of IPv6 interfaces.
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
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.
Build a set of PointToPointNetDevice objects.
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
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
nodes
Definition: first.py:32
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