A Discrete-Event Network Simulator
API
icmpv6-redirect.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 Strasbourg University
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  * Author: David Gross <gdavid.devel@gmail.com>
18  */
19 
20 // Network topology
21 //
22 // STA2
23 // |
24 // |
25 // R1 R2
26 // | |
27 // | |
28 // ------------
29 // |
30 // |
31 // STA 1
32 //
33 // - Initial configuration :
34 // - STA1 default route : R1
35 // - R1 static route to STA2 : R2
36 // - STA2 default route : R2
37 // - STA1 send Echo Request to STA2 using its default route to R1
38 // - R1 receive Echo Request from STA1, and forward it to R2
39 // - R1 send an ICMPv6 Redirection to STA1 with Target STA2 and Destination R2
40 // - Next Echo Request from STA1 to STA2 are directly sent to R2
41 
42 #include "ns3/core-module.h"
43 #include "ns3/csma-module.h"
44 #include "ns3/internet-apps-module.h"
45 #include "ns3/internet-module.h"
46 #include "ns3/ipv6-routing-table-entry.h"
47 #include "ns3/ipv6-static-routing-helper.h"
48 
49 #include <fstream>
50 
51 using namespace ns3;
52 
53 NS_LOG_COMPONENT_DEFINE("Icmpv6RedirectExample");
54 
55 int
56 main(int argc, char** argv)
57 {
58  bool verbose = false;
59 
60  CommandLine cmd(__FILE__);
61  cmd.AddValue("verbose", "turn on log components", verbose);
62  cmd.Parse(argc, argv);
63 
64  if (verbose)
65  {
66  LogComponentEnable("Icmpv6RedirectExample", LOG_LEVEL_INFO);
67  LogComponentEnable("Icmpv6L4Protocol", LOG_LEVEL_INFO);
68  LogComponentEnable("Ipv6L3Protocol", LOG_LEVEL_ALL);
69  LogComponentEnable("Ipv6StaticRouting", LOG_LEVEL_ALL);
70  LogComponentEnable("Ipv6Interface", LOG_LEVEL_ALL);
71  LogComponentEnable("Icmpv6L4Protocol", LOG_LEVEL_ALL);
72  LogComponentEnable("NdiscCache", LOG_LEVEL_ALL);
73  }
74 
75  NS_LOG_INFO("Create nodes.");
76  Ptr<Node> sta1 = CreateObject<Node>();
77  Ptr<Node> r1 = CreateObject<Node>();
78  Ptr<Node> r2 = CreateObject<Node>();
79  Ptr<Node> sta2 = CreateObject<Node>();
80  NodeContainer net1(sta1, r1, r2);
81  NodeContainer net2(r2, sta2);
82  NodeContainer all(sta1, r1, r2, sta2);
83 
84  InternetStackHelper internetv6;
85  internetv6.Install(all);
86 
87  NS_LOG_INFO("Create channels.");
89  csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
90  csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
91  NetDeviceContainer ndc1 = csma.Install(net1);
92  NetDeviceContainer ndc2 = csma.Install(net2);
93 
94  NS_LOG_INFO("Assign IPv6 Addresses.");
95  Ipv6AddressHelper ipv6;
96 
97  ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
98  Ipv6InterfaceContainer iic1 = ipv6.Assign(ndc1);
99  iic1.SetForwarding(2, true);
100  iic1.SetForwarding(1, true);
102 
103  ipv6.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
104  Ipv6InterfaceContainer iic2 = ipv6.Assign(ndc2);
105  iic2.SetForwarding(0, true);
107 
108  Ipv6StaticRoutingHelper routingHelper;
109 
110  // manually inject a static route to the second router.
111  Ptr<Ipv6StaticRouting> routing = routingHelper.GetStaticRouting(r1->GetObject<Ipv6>());
112  routing->AddHostRouteTo(iic2.GetAddress(1, 1),
113  iic1.GetAddress(2, 0),
114  iic1.GetInterfaceIndex(1));
115 
116  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper>(&std::cout);
117  Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(0.0), r1, routingStream);
118  Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(3.0), sta1, routingStream);
119 
120  NS_LOG_INFO("Create Applications.");
121  uint32_t packetSize = 1024;
122  uint32_t maxPacketCount = 5;
123  PingHelper ping(iic2.GetAddress(1, 1));
124 
125  ping.SetAttribute("Count", UintegerValue(maxPacketCount));
126  ping.SetAttribute("Size", UintegerValue(packetSize));
127  ApplicationContainer apps = ping.Install(sta1);
128  apps.Start(Seconds(2.0));
129  apps.Stop(Seconds(10.0));
130 
131  AsciiTraceHelper ascii;
132  csma.EnableAsciiAll(ascii.CreateFileStream("icmpv6-redirect.tr"));
133  csma.EnablePcapAll("icmpv6-redirect", true);
134 
135  /* Now, do the actual simulation. */
136  NS_LOG_INFO("Run Simulation.");
137  Simulator::Run();
139  NS_LOG_INFO("Done.");
140 
141  return 0;
142 }
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.
Definition: trace-helper.h:174
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.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
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.
Definition: ipv6-address.h:49
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:82
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.
Definition: ipv6-address.h:455
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.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
Create a ping application and associate it to a node.
Definition: ping-helper.h:48
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
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
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
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_ALL
Print everything.
Definition: log.h:116
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
csma
Definition: second.py:63
cmd
Definition: second.py:40
bool verbose
static const uint32_t packetSize
Packet size generated at the AP.