A Discrete-Event Network Simulator
API
icmpv6-redirect.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 Strasbourg University
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  * Author: David Gross <gdavid.devel@gmail.com>
19  */
20 
21 // Network topology
22 //
23 // STA2
24 // |
25 // |
26 // R1 R2
27 // | |
28 // | |
29 // ------------
30 // |
31 // |
32 // STA 1
33 //
34 // - Initial configuration :
35 // - STA1 default route : R1
36 // - R1 static route to STA2 : R2
37 // - STA2 default route : R2
38 // - STA1 send Echo Request to STA2 using its default route to R1
39 // - R1 receive Echo Request from STA1, and forward it to R2
40 // - R1 send an ICMPv6 Redirection to STA1 with Target STA2 and Destination R2
41 // - Next Echo Request from STA1 to STA2 are directly sent to R2
42 
43 #include <fstream>
44 #include "ns3/core-module.h"
45 #include "ns3/internet-module.h"
46 #include "ns3/csma-module.h"
47 #include "ns3/internet-apps-module.h"
48 #include "ns3/ipv6-static-routing-helper.h"
49 
50 #include "ns3/ipv6-routing-table-entry.h"
51 
52 using namespace ns3;
53 
54 NS_LOG_COMPONENT_DEFINE ("Icmpv6RedirectExample");
55 
56 int main (int argc, char **argv)
57 {
58  bool verbose = false;
59 
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);
101  iic1.SetDefaultRouteInAllNodes (1);
102 
103  ipv6.SetBase (Ipv6Address ("2001:2::"), Ipv6Prefix (64));
104  Ipv6InterfaceContainer iic2 = ipv6.Assign (ndc2);
105  iic2.SetForwarding (0, true);
106  iic2.SetDefaultRouteInAllNodes (0);
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), iic1.GetAddress (2, 0), iic1.GetInterfaceIndex (1));
113 
114  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> (&std::cout);
115  routingHelper.PrintRoutingTableAt (Seconds (0.0), r1, routingStream);
116  routingHelper.PrintRoutingTableAt (Seconds (3.0), sta1, routingStream);
117 
118  NS_LOG_INFO ("Create Applications.");
119  uint32_t packetSize = 1024;
120  uint32_t maxPacketCount = 5;
121  Time interPacketInterval = Seconds (1.);
122  Ping6Helper ping6;
123 
124  ping6.SetLocal (iic1.GetAddress (0, 1));
125  ping6.SetRemote (iic2.GetAddress (1, 1));
126  ping6.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
127  ping6.SetAttribute ("Interval", TimeValue (interPacketInterval));
128  ping6.SetAttribute ("PacketSize", UintegerValue (packetSize));
129  ApplicationContainer apps = ping6.Install (sta1);
130  apps.Start (Seconds (2.0));
131  apps.Stop (Seconds (10.0));
132 
133  AsciiTraceHelper ascii;
134  csma.EnableAsciiAll (ascii.CreateFileStream ("icmpv6-redirect.tr"));
135  csma.EnablePcapAll ("icmpv6-redirect", true);
136 
137  /* Now, do the actual simulation. */
138  NS_LOG_INFO ("Run Simulation.");
139  Simulator::Run ();
141  NS_LOG_INFO ("Done.");
142 }
143 
holds a vector of ns3::Application pointers.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
Keep track of a set of IPv6 interfaces.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:81
void SetDefaultRouteInAllNodes(uint32_t router)
Set the default route for all the devices (except the router itself).
ApplicationContainer Install(NodeContainer c)
Install the application in Nodes.
Definition: ping6-helper.cc:50
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1022
aggregate IP/TCP/UDP functionality to existing Nodes.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
void SetAttribute(std::string name, const AttributeValue &value)
Set some attributes.
Definition: ping6-helper.cc:45
cmd
Definition: second.py:35
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
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.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
void AddHostRouteTo(Ipv6Address dest, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse=Ipv6Address("::"), uint32_t metric=0)
Add route to host.
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:369
void SetLocal(Ipv6Address ip)
Set the local IPv6 address.
Definition: ping6-helper.cc:35
AttributeValue implementation for Time.
Definition: nstime.h:1076
Hold an unsigned integer type.
Definition: uinteger.h:44
LOG_INFO and above.
Definition: log.h:104
holds a vector of ns3::NetDevice pointers
uint32_t GetInterfaceIndex(uint32_t i) const
Get the interface index for the specified node index.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
csma
Definition: second.py:63
Parse command-line arguments.
Definition: command-line.h:213
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Ptr< Ipv6StaticRouting > GetStaticRouting(Ptr< Ipv6 > ipv6) const
Get Ipv6StaticRouting pointer from IPv6 stack.
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.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
Helper class that adds ns3::Ipv6StaticRouting objects.
void SetRemote(Ipv6Address ip)
Set the remote IPv6 address.
Definition: ping6-helper.cc:40
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
Helper class to auto-assign global IPv6 unicast addresses.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Describes an IPv6 address.
Definition: ipv6-address.h:49
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
Describes an IPv6 prefix.
Definition: ipv6-address.h:428
Print everything.
Definition: log.h:113
static const uint32_t packetSize
Ping6 application helper.
Definition: ping6-helper.h:38
bool verbose