A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-global-routing-print-route.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 Shashwat Patni
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Shashwat Patni <shashwatpatni25@gmail.com>
7 */
8
9#include "ns3/core-module.h"
10#include "ns3/internet-module.h"
11#include "ns3/network-module.h"
12#include "ns3/point-to-point-module.h"
13
14/**
15 * @file This example demonstrates how to use Ipv4GlobalRoutingHelper::PrintRoute()
16 * and PrintRoutePathAt() and its overloads to print the route path as calculated
17 * by the global routing protocol. The Print Format loosely follows that of traceroute.
18 * Similar to traceroute, there is an option to disable the Reverse Node ID lookup.
19 * We have two functions PrintRoute() and PrintRouteAt(). Each of these has 4 overloads.
20 * Please refer to the doxygen documentation for more details on each of the functions and its
21 * overloads.
22 * The default behaviour is to also print the node id. This can be disabled by setting the
23 * nodeIdLookup argument to false. This is similar to linux traceroute -n option.
24 * We explain the use of each of the overloads for PrintRoute() using the below topology
25 * The PrintRouteAt() variant behaves similarly with the added time argument.
26 * @verbatim Simple point to point links:
27 ________
28 / \
29 n0 -- n1 -- n2 -- n3 n4----n5
30
31 n0 IP: 10.1.1.1, 10.1.4.1
32 n1 IP: 10.1.1.2, 10.1.2.1
33 n2 IP: 10.1.2.2, 10.1.3.1, 10.1.4.2
34 n3 IP: 10.1.3.2
35 n4 IP: 10.1.5.1
36 n5 IP: 10:1:5:2
37
38 Test 1:Route from n1 to 10.1.2.2
39 expected Output:
40 PrintRoute at Time: +0s from Node 1 to address 10.1.2.2, 64 hops Max.
41 1 10.1.2.2 (Node 2)
42
43 Test 2: Route from n1 to n3
44 expected Output:
45 PrintRoute at Time: +0s from Node 1 to Node 3, 64 hops Max.
46 1 10.1.2.2 (Node 2)
47 2 10.1.3.2 (Node 3)
48
49 Test 3: Route from n0 to 10.1.5.2
50 expected Output:
51 PrintRoute at Time: +0s from Node 0 to address 10.1.5.2, 64 hops Max.
52 There is no path from Node 0 to Node 5.
53
54 Test 4: Route from n0 to n3
55 expected Output:
56 PrintRoute at Time: +0s from Node 0 to Node 3, 64 hops Max.
57 1 10.1.4.2 (Node 2)
58 2 10.1.3.2 (Node 3)
59
60 Test 5: Route from n0 to 10.1.2.2
61 expected Output:
62 PrintRoute at Time: +0s from Node 0 to address 10.1.2.2, 64 hops Max.
63 1 10.1.2.2
64
65 Test 6: Route from n0 to n3
66 expected Output:
67 PrintRoute at Time: +0s from Node 0 to Node 3, 64 hops Max.
68 1 10.1.4.2
69 2 10.1.3.2
70
71 Test 7: Route from n1 to 10.1.2.2 at time +2s
72 expected Output:
73 PrintRoute at Time: +2s from Node 1 to address 10.1.2.2, 64 hops Max.
74 1 10.1.2.2 (Node 2)
75
76 Test 8: Route from n1 to n3 at time +4s
77 expected Output:
78 PrintRoute at Time: +4s from Node 1 to Node 3, 64 hops Max.
79 1 10.1.2.2 (Node 2)
80 2 10.1.3.2 (Node 3)
81 @endverbatim
82*/
83
84using namespace ns3;
85
86NS_LOG_COMPONENT_DEFINE("Ipv4GlobalRoutingPrintRoute");
87
88int
89main(int argc, char* argv[])
90{
92 n.Create(6);
94 // globalroutinghelper to install global routing on all nodes
95 Ipv4GlobalRoutingHelper globalRouting;
97 stack.SetRoutingHelper(globalRouting); // has effect on the next Install ()
98 stack.Install(n);
99
100 // NetDeviceContainers are the return type
101 auto devices01 = pointToPoint.Install(n.Get(0), n.Get(1));
102 auto devices12 = pointToPoint.Install(n.Get(1), n.Get(2));
103 auto devices23 = pointToPoint.Install(n.Get(2), n.Get(3));
104 auto devices02 = pointToPoint.Install(n.Get(0), n.Get(2));
105 auto devices45 = pointToPoint.Install(n.Get(4), n.Get(5));
106
107 // Assign IP Addresses
109 address.SetBase("10.1.1.0", "255.255.255.0");
110 Ipv4InterfaceContainer interfaces01 = address.Assign(devices01);
111 address.SetBase("10.1.2.0", "255.255.255.0");
112 Ipv4InterfaceContainer interfaces12 = address.Assign(devices12);
113 address.SetBase("10.1.3.0", "255.255.255.0");
114 Ipv4InterfaceContainer interfaces23 = address.Assign(devices23);
115 address.SetBase("10.1.4.0", "255.255.255.0");
116 Ipv4InterfaceContainer interfaces02 = address.Assign(devices02);
117 address.SetBase("10.1.5.0", "255.255.255.0");
118 Ipv4InterfaceContainer interfaces45 = address.Assign(devices45);
119
121
122 // create an output stream to be used in the examples
123 Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper>(&std::cout);
124
125 //----------------------Now the examples-----------------------
126
127 // PrintRoute(): Route from: Source node to destination IP Address given an output stream
128 Ipv4GlobalRoutingHelper::PrintRoute(n.Get(1), interfaces12.GetAddress(1), routingStream);
129
130 // PrintRoute(): Route from : Source node to destination node given an output stream
131 Ipv4GlobalRoutingHelper::PrintRoute(n.Get(1), n.Get(3), routingStream);
132
133 // Variant with output stream as std::cout by default
135
137
138 // PrintRoute() with nodeIdLookup disabled
139 Ipv4GlobalRoutingHelper::PrintRoute(n.Get(0), interfaces12.GetAddress(1), false);
140 // also provide the output stream
141 Ipv4GlobalRoutingHelper::PrintRoute(n.Get(0), n.Get(3), routingStream, false);
142
143 // The At Variants of PrintRoute() behave similarly with the added time argument.
146
147 // uncomment to see all routing tables
148 // Ipv4GlobalRoutingHelper::PrintRoutingTableAllAt(Seconds(3),routingStream);
149
153 return 0;
154}
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Helper class that adds ns3::Ipv4GlobalRouting objects.
static void PrintRouteAt(Ptr< Node > sourceNode, Ipv4Address dest, Time printTime, bool nodeIdLookup=true, Time::Unit unit=Time::S)
prints the routing path for the source and destination at a particular time to the standard cout outp...
static void PrintRoute(Ptr< Node > sourceNode, Ipv4Address dest, bool nodeIdLookup=true, Time::Unit unit=Time::S)
prints the routing path for a source and destination to the standard cout output stream.
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
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.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void Run()
Run the simulation.
Definition simulator.cc:161
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:169
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:454
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1381
address
Definition first.py:36
pointToPoint
Definition first.py:27
stack
Definition first.py:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.