A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/applications-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 
60  CommandLine cmd;
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.");
88  CsmaHelper csma;
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