A Discrete-Event Network Simulator
API
simple-routing-ping6.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-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  * Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
19  */
20 
21 // Network topology
22 // //
23 // // n0 r n1
24 // // | _ |
25 // // ====|_|====
26 // // router
27 // //
28 // // - Tracing of queues and packet receptions to file "simple-routing-ping6.tr"
29 
30 #include "ns3/core-module.h"
31 #include "ns3/csma-module.h"
32 #include "ns3/internet-apps-module.h"
33 #include "ns3/internet-module.h"
34 #include "ns3/ipv6-routing-table-entry.h"
35 #include "ns3/ipv6-static-routing-helper.h"
36 
37 #include <fstream>
38 
39 using namespace ns3;
40 
41 NS_LOG_COMPONENT_DEFINE("SimpleRoutingPing6Example");
42 
48 {
49  public:
56  inline void AddAddress(Ptr<Node>& n, uint32_t interface, Ipv6Address address)
57  {
58  Ptr<Ipv6> ipv6 = n->GetObject<Ipv6>();
59  ipv6->AddAddress(interface, address);
60  }
61 
66  inline void PrintRoutingTable(Ptr<Node>& n)
67  {
68  Ptr<Ipv6StaticRouting> routing = nullptr;
69  Ipv6StaticRoutingHelper routingHelper;
70  Ptr<Ipv6> ipv6 = n->GetObject<Ipv6>();
71  uint32_t nbRoutes = 0;
73 
74  routing = routingHelper.GetStaticRouting(ipv6);
75 
76  std::cout << "Routing table of " << n << " : " << std::endl;
77  std::cout << "Destination\t\t\t\t"
78  << "Gateway\t\t\t\t\t"
79  << "Interface\t"
80  << "Prefix to use" << std::endl;
81 
82  nbRoutes = routing->GetNRoutes();
83  for (uint32_t i = 0; i < nbRoutes; i++)
84  {
85  route = routing->GetRoute(i);
86  std::cout << route.GetDest() << "\t" << route.GetGateway() << "\t"
87  << route.GetInterface() << "\t" << route.GetPrefixToUse() << "\t"
88  << std::endl;
89  }
90  }
91 };
92 
93 int
94 main(int argc, char** argv)
95 {
96 #if 0
97  LogComponentEnable ("Ipv6L3Protocol", LOG_LEVEL_ALL);
98  LogComponentEnable ("Icmpv6L4Protocol", LOG_LEVEL_ALL);
99  LogComponentEnable ("Ipv6StaticRouting", LOG_LEVEL_ALL);
100  LogComponentEnable ("Ipv6Interface", LOG_LEVEL_ALL);
102 #endif
103 
104  CommandLine cmd(__FILE__);
105  cmd.Parse(argc, argv);
106 
107  StackHelper stackHelper;
108 
109  NS_LOG_INFO("Create nodes.");
110  Ptr<Node> n0 = CreateObject<Node>();
111  Ptr<Node> r = CreateObject<Node>();
112  Ptr<Node> n1 = CreateObject<Node>();
113 
114  NodeContainer net1(n0, r);
115  NodeContainer net2(r, n1);
116  NodeContainer all(n0, r, n1);
117 
118  NS_LOG_INFO("Create IPv6 Internet Stack");
119  InternetStackHelper internetv6;
120  internetv6.Install(all);
121 
122  NS_LOG_INFO("Create channels.");
124  csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
125  csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
126  NetDeviceContainer d1 = csma.Install(net1);
127  NetDeviceContainer d2 = csma.Install(net2);
128 
129  NS_LOG_INFO("Create networks and assign IPv6 Addresses.");
130  Ipv6AddressHelper ipv6;
131  ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
132  Ipv6InterfaceContainer i1 = ipv6.Assign(d1);
133  i1.SetForwarding(1, true);
135  ipv6.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
136  Ipv6InterfaceContainer i2 = ipv6.Assign(d2);
137  i2.SetForwarding(0, true);
139 
140  stackHelper.PrintRoutingTable(n0);
141 
142  /* Create a Ping application to send ICMPv6 echo request from n0 to n1 via r */
143  uint32_t packetSize = 1024;
144  uint32_t maxPacketCount = 5;
145  PingHelper ping(i2.GetAddress(1, 1));
146 
147  ping.SetAttribute("Count", UintegerValue(maxPacketCount));
148  ping.SetAttribute("Size", UintegerValue(packetSize));
149  ApplicationContainer apps = ping.Install(net1.Get(0));
150  apps.Start(Seconds(2.0));
151  apps.Stop(Seconds(20.0));
152 
153  AsciiTraceHelper ascii;
154  csma.EnableAsciiAll(ascii.CreateFileStream("simple-routing-ping6.tr"));
155  csma.EnablePcapAll("simple-routing-ping6", true);
156 
157  NS_LOG_INFO("Run Simulation.");
158  Simulator::Run();
160  NS_LOG_INFO("Done.");
161 
162  return 0;
163 }
Helper to set or get some IPv6 information about nodes.
void AddAddress(Ptr< Node > &n, uint32_t interface, Ipv6Address address)
Add an address to a IPv6 node.
void PrintRoutingTable(Ptr< Node > &n)
Print the routing table.
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.
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
A record of an IPv6 route.
Ipv6Address GetDest() const
Get the destination.
Ipv6Address GetPrefixToUse() const
Get the prefix to use (for multihomed link).
uint32_t GetInterface() const
Get the interface index.
Ipv6Address GetGateway() const
Get the gateway.
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
address
Definition: first.py:47
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
csma
Definition: second.py:63
cmd
Definition: second.py:40
static const uint32_t packetSize
Packet size generated at the AP.