A Discrete-Event Network Simulator
API
nix-simple.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  * Modified By: Ameya Deshpande <ameyanrd@outlook.com>
18  * Tommaso Pecorella <tommaso.pecorella@unifi.it>
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/ipv6-list-routing-helper.h"
27 #include "ns3/ipv6-static-routing-helper.h"
28 #include "ns3/network-module.h"
29 #include "ns3/nix-vector-helper.h"
30 #include "ns3/point-to-point-module.h"
31 
179 using namespace ns3;
180 
181 NS_LOG_COMPONENT_DEFINE("NixSimpleExample");
182 
183 int
184 main(int argc, char* argv[])
185 {
186  bool useIpv6 = false;
187 
188  CommandLine cmd(__FILE__);
189  cmd.AddValue("useIPv6", "Use IPv6 instead of IPv4", useIpv6);
190  cmd.Parse(argc, argv);
191 
192  LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
193  LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
194 
196  nodes.Create(4);
197 
199  pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
200  pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
201 
202  NetDeviceContainer devices01;
203  NetDeviceContainer devices12;
204  NetDeviceContainer devices23;
205  NetDeviceContainer devices02;
206  devices01 = pointToPoint.Install(NodeContainer(nodes.Get(0), nodes.Get(1)));
207  devices12 = pointToPoint.Install(NodeContainer(nodes.Get(1), nodes.Get(2)));
208  devices23 = pointToPoint.Install(NodeContainer(nodes.Get(2), nodes.Get(3)));
209  devices02 = pointToPoint.Install(NodeContainer(nodes.Get(0), nodes.Get(2)));
210 
211  Address udpServerAddress;
212 
213  if (!useIpv6)
214  {
215  // NixHelper to install nix-vector routing
216  // on all nodes
219  stack.SetRoutingHelper(nixRouting); // has effect on the next Install ()
220  stack.Install(nodes);
221 
222  Ipv4AddressHelper address1;
223  address1.SetBase("10.1.1.0", "255.255.255.0");
224  Ipv4AddressHelper address2;
225  address2.SetBase("10.1.2.0", "255.255.255.0");
226  Ipv4AddressHelper address3;
227  address3.SetBase("10.1.3.0", "255.255.255.0");
228  Ipv4AddressHelper address4;
229  address4.SetBase("10.1.4.0", "255.255.255.0");
230 
231  Ipv4InterfaceContainer interfaces01 = address1.Assign(devices01);
232  Ipv4InterfaceContainer interfaces12 = address2.Assign(devices12);
233  Ipv4InterfaceContainer interfaces23 = address3.Assign(devices23);
234  Ipv4InterfaceContainer interfaces02 = address4.Assign(devices02);
235 
236  udpServerAddress = interfaces23.GetAddress(1);
237 
238  // Trace routing paths for different source and destinations.
239  Ptr<OutputStreamWrapper> routingStream =
240  Create<OutputStreamWrapper>("nix-simple-ipv4.routes", std::ios::out);
241  nixRouting.PrintRoutingPathAt(Seconds(3),
242  nodes.Get(0),
243  interfaces23.GetAddress(1),
244  routingStream);
245  nixRouting.PrintRoutingPathAt(Seconds(5),
246  nodes.Get(1),
247  interfaces23.GetAddress(1),
248  routingStream);
249  nixRouting.PrintRoutingPathAt(Seconds(6),
250  nodes.Get(2),
251  interfaces01.GetAddress(0),
252  routingStream);
253  nixRouting.PrintRoutingPathAt(Seconds(7),
254  nodes.Get(1),
255  interfaces01.GetAddress(1),
256  routingStream);
257  // Trace routing tables
258  Ipv4NixVectorHelper::PrintRoutingTableAllAt(Seconds(8), routingStream);
259  }
260  else
261  {
262  // NixHelper to install nix-vector routing
263  // on all nodes
266  stack.SetRoutingHelper(nixRouting); // has effect on the next Install ()
267  stack.Install(nodes);
268 
269  Ipv6AddressHelper address1;
270  address1.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
271  Ipv6AddressHelper address2;
272  address2.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
273  Ipv6AddressHelper address3;
274  address3.SetBase(Ipv6Address("2001:3::"), Ipv6Prefix(64));
275  Ipv6AddressHelper address4;
276  address4.SetBase(Ipv6Address("2001:4::"), Ipv6Prefix(64));
277 
278  Ipv6InterfaceContainer interfaces01 = address1.Assign(devices01);
279  Ipv6InterfaceContainer interfaces12 = address2.Assign(devices12);
280  Ipv6InterfaceContainer interfaces23 = address3.Assign(devices23);
281  Ipv6InterfaceContainer interfaces02 = address4.Assign(devices02);
282 
283  udpServerAddress = interfaces23.GetAddress(1, 1);
284 
285  // Trace routing paths for different source and destinations.
286  Ptr<OutputStreamWrapper> routingStream =
287  Create<OutputStreamWrapper>("nix-simple-ipv6.routes", std::ios::out);
288  nixRouting.PrintRoutingPathAt(Seconds(3),
289  nodes.Get(0),
290  interfaces23.GetAddress(1, 1),
291  routingStream);
292  nixRouting.PrintRoutingPathAt(Seconds(5),
293  nodes.Get(1),
294  interfaces23.GetAddress(1, 1),
295  routingStream);
296  nixRouting.PrintRoutingPathAt(Seconds(6),
297  nodes.Get(2),
298  interfaces01.GetAddress(0, 1),
299  routingStream);
300  nixRouting.PrintRoutingPathAt(Seconds(7),
301  nodes.Get(1),
302  interfaces01.GetAddress(1, 1),
303  routingStream);
304  // Trace routing tables
305  Ipv6NixVectorHelper::PrintRoutingTableAllAt(Seconds(8), routingStream);
306  }
307 
309 
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 
320  clientApps.Start(Seconds(2.0));
321  clientApps.Stop(Seconds(10.0));
322 
323  Simulator::Run();
325  return 0;
326 }
a polymophic address class
Definition: address.h:101
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...
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:49
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:455
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.
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()
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
NodeContainer nodes
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