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
52using namespace ns3;
53
54NS_LOG_COMPONENT_DEFINE ("Icmpv6RedirectExample");
55
56int 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.");
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), 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 ();
140 Simulator::Destroy ();
141 NS_LOG_INFO ("Done.");
142}
143
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
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:163
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:229
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
AttributeValue implementation for DataRate.
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:50
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:456
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(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
Ping6 application helper.
Definition: ping6-helper.h:39
void SetRemote(Ipv6Address ip)
Set the remote IPv6 address.
Definition: ping6-helper.cc:40
ApplicationContainer Install(NodeContainer c)
Install the application in Nodes.
Definition: ping6-helper.cc:50
void SetAttribute(std::string name, const AttributeValue &value)
Set some attributes.
Definition: ping6-helper.cc:45
void SetLocal(Ipv6Address ip)
Set the local IPv6 address.
Definition: ping6-helper.cc:35
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
AttributeValue implementation for Time.
Definition: nstime.h:1308
Hold an unsigned integer type.
Definition: uinteger.h:44
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1252
Every class exported by the ns3 library is enclosed in the ns3 namespace.
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
csma
Definition: second.py:63
cmd
Definition: second.py:35
bool verbose
static const uint32_t packetSize
Packet size generated at the AP.