A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
icmpv6-redirect.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 Strasbourg University
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: David Gross <gdavid.devel@gmail.com>
7 */
8
9// Network topology
10//
11// STA2
12// |
13// |
14// R1 R2
15// | |
16// | |
17// ------------
18// |
19// |
20// STA 1
21//
22// - Initial configuration :
23// - STA1 default route : R1
24// - R1 static route to STA2 : R2
25// - STA2 default route : R2
26// - STA1 send Echo Request to STA2 using its default route to R1
27// - R1 receive Echo Request from STA1, and forward it to R2
28// - R1 send an ICMPv6 Redirection to STA1 with Target STA2 and Destination R2
29// - Next Echo Request from STA1 to STA2 are directly sent to R2
30
31#include "ns3/core-module.h"
32#include "ns3/csma-module.h"
33#include "ns3/internet-apps-module.h"
34#include "ns3/internet-module.h"
35#include "ns3/ipv6-routing-table-entry.h"
36#include "ns3/ipv6-static-routing-helper.h"
37
38#include <fstream>
39
40using namespace ns3;
41
42NS_LOG_COMPONENT_DEFINE("Icmpv6RedirectExample");
43
44int
45main(int argc, char** argv)
46{
47 bool verbose = false;
48
49 CommandLine cmd(__FILE__);
50 cmd.AddValue("verbose", "turn on log components", verbose);
51 cmd.Parse(argc, argv);
52
53 if (verbose)
54 {
55 LogComponentEnable("Icmpv6RedirectExample", LOG_LEVEL_INFO);
56 LogComponentEnable("Icmpv6L4Protocol", LOG_LEVEL_INFO);
57 LogComponentEnable("Ipv6L3Protocol", LOG_LEVEL_ALL);
58 LogComponentEnable("Ipv6StaticRouting", LOG_LEVEL_ALL);
59 LogComponentEnable("Ipv6Interface", LOG_LEVEL_ALL);
60 LogComponentEnable("Icmpv6L4Protocol", LOG_LEVEL_ALL);
61 LogComponentEnable("NdiscCache", LOG_LEVEL_ALL);
62 }
63
64 NS_LOG_INFO("Create nodes.");
69 NodeContainer net1(sta1, r1, r2);
70 NodeContainer net2(r2, sta2);
71 NodeContainer all(sta1, r1, r2, sta2);
72
73 InternetStackHelper internetv6;
74 internetv6.Install(all);
75
76 NS_LOG_INFO("Create channels.");
78 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
79 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
80 NetDeviceContainer ndc1 = csma.Install(net1);
81 NetDeviceContainer ndc2 = csma.Install(net2);
82
83 NS_LOG_INFO("Assign IPv6 Addresses.");
85
86 ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
87 Ipv6InterfaceContainer iic1 = ipv6.Assign(ndc1);
88 iic1.SetForwarding(2, true);
89 iic1.SetForwarding(1, true);
91
92 ipv6.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
93 Ipv6InterfaceContainer iic2 = ipv6.Assign(ndc2);
94 iic2.SetForwarding(0, true);
96
97 Ipv6StaticRoutingHelper routingHelper;
98
99 // manually inject a static route to the second router.
100 Ptr<Ipv6StaticRouting> routing = routingHelper.GetStaticRouting(r1->GetObject<Ipv6>());
101 routing->AddHostRouteTo(iic2.GetAddress(1, 1),
102 iic1.GetAddress(2, 0),
103 iic1.GetInterfaceIndex(1));
104
105 Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper>(&std::cout);
106 Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(0.0), r1, routingStream);
107 Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(3.0), sta1, routingStream);
108
109 NS_LOG_INFO("Create Applications.");
110 uint32_t packetSize = 1024;
111 uint32_t maxPacketCount = 5;
112 PingHelper ping(iic2.GetAddress(1, 1));
113
114 ping.SetAttribute("Count", UintegerValue(maxPacketCount));
115 ping.SetAttribute("Size", UintegerValue(packetSize));
116 ApplicationContainer apps = ping.Install(sta1);
117 apps.Start(Seconds(2.0));
118 apps.Stop(Seconds(10.0));
119
120 AsciiTraceHelper ascii;
121 csma.EnableAsciiAll(ascii.CreateFileStream("icmpv6-redirect.tr"));
122 csma.EnablePcapAll("icmpv6-redirect", true);
123
124 /* Now, do the actual simulation. */
125 NS_LOG_INFO("Run Simulation.");
128 NS_LOG_INFO("Done.");
129
130 return 0;
131}
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Manage ASCII trace files for device models.
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
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.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition ipv6.h:71
Keep track of a set of IPv6 interfaces.
void SetForwarding(uint32_t i, bool state)
Set the state of the stack (act as a router or as an host) for the specified index.
uint32_t GetInterfaceIndex(uint32_t i) const
Get the interface index for the specified node index.
void SetDefaultRouteInAllNodes(uint32_t router)
Set the default route for all the devices (except the router itself).
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
Describes an IPv6 prefix.
static void PrintRoutingTableAt(Time printTime, Ptr< Node > node, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the routing tables of a node at a particular time.
Helper class that adds ns3::Ipv6StaticRouting objects.
Ptr< Ipv6StaticRouting > GetStaticRouting(Ptr< Ipv6 > ipv6) const
Get Ipv6StaticRouting pointer from IPv6 stack.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Create a ping application and associate it to a node.
Definition ping-helper.h:31
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
Hold an unsigned integer type.
Definition uinteger.h:34
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
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:291
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
bool verbose
static const uint32_t packetSize
Packet size generated at the AP.