A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
simple-routing-ping6.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008-2009 Strasbourg University
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: David Gross <gdavid.devel@gmail.com>
7 * Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
8 */
9
10// Network topology
11// //
12// // n0 r n1
13// // | _ |
14// // ====|_|====
15// // router
16// //
17// // - Tracing of queues and packet receptions to file "simple-routing-ping6.tr"
18
19#include "ns3/core-module.h"
20#include "ns3/csma-module.h"
21#include "ns3/internet-apps-module.h"
22#include "ns3/internet-module.h"
23#include "ns3/ipv6-routing-table-entry.h"
24#include "ns3/ipv6-static-routing-helper.h"
25
26#include <fstream>
27
28using namespace ns3;
29
30NS_LOG_COMPONENT_DEFINE("SimpleRoutingPing6Example");
31
32/**
33 * \class StackHelper
34 * \brief Helper to set or get some IPv6 information about nodes.
35 */
37{
38 public:
39 /**
40 * \brief Add an address to a IPv6 node.
41 * \param n node
42 * \param interface interface index
43 * \param address IPv6 address to add
44 */
45 inline void AddAddress(Ptr<Node>& n, uint32_t interface, Ipv6Address address)
46 {
47 Ptr<Ipv6> ipv6 = n->GetObject<Ipv6>();
48 ipv6->AddAddress(interface, address);
49 }
50
51 /**
52 * \brief Print the routing table.
53 * \param n the node
54 */
56 {
57 Ptr<Ipv6StaticRouting> routing = nullptr;
58 Ipv6StaticRoutingHelper routingHelper;
59 Ptr<Ipv6> ipv6 = n->GetObject<Ipv6>();
60 uint32_t nbRoutes = 0;
62
63 routing = routingHelper.GetStaticRouting(ipv6);
64
65 std::cout << "Routing table of " << n << " : " << std::endl;
66 std::cout << "Destination\t\t\t\t"
67 << "Gateway\t\t\t\t\t"
68 << "Interface\t"
69 << "Prefix to use" << std::endl;
70
71 nbRoutes = routing->GetNRoutes();
72 for (uint32_t i = 0; i < nbRoutes; i++)
73 {
74 route = routing->GetRoute(i);
75 std::cout << route.GetDest() << "\t" << route.GetGateway() << "\t"
76 << route.GetInterface() << "\t" << route.GetPrefixToUse() << "\t"
77 << std::endl;
78 }
79 }
80};
81
82int
83main(int argc, char** argv)
84{
85#if 0
86 LogComponentEnable ("Ipv6L3Protocol", LOG_LEVEL_ALL);
87 LogComponentEnable ("Icmpv6L4Protocol", LOG_LEVEL_ALL);
88 LogComponentEnable ("Ipv6StaticRouting", LOG_LEVEL_ALL);
89 LogComponentEnable ("Ipv6Interface", LOG_LEVEL_ALL);
91#endif
92
93 CommandLine cmd(__FILE__);
94 cmd.Parse(argc, argv);
95
96 StackHelper stackHelper;
97
98 NS_LOG_INFO("Create nodes.");
102
103 NodeContainer net1(n0, r);
104 NodeContainer net2(r, n1);
105 NodeContainer all(n0, r, n1);
106
107 NS_LOG_INFO("Create IPv6 Internet Stack");
108 InternetStackHelper internetv6;
109 internetv6.Install(all);
110
111 NS_LOG_INFO("Create channels.");
113 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
114 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
115 NetDeviceContainer d1 = csma.Install(net1);
116 NetDeviceContainer d2 = csma.Install(net2);
117
118 NS_LOG_INFO("Create networks and assign IPv6 Addresses.");
120 ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
121 Ipv6InterfaceContainer i1 = ipv6.Assign(d1);
122 i1.SetForwarding(1, true);
124 ipv6.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
125 Ipv6InterfaceContainer i2 = ipv6.Assign(d2);
126 i2.SetForwarding(0, true);
128
129 stackHelper.PrintRoutingTable(n0);
130
131 /* Create a Ping application to send ICMPv6 echo request from n0 to n1 via r */
132 uint32_t packetSize = 1024;
133 uint32_t maxPacketCount = 5;
134 PingHelper ping(i2.GetAddress(1, 1));
135
136 ping.SetAttribute("Count", UintegerValue(maxPacketCount));
137 ping.SetAttribute("Size", UintegerValue(packetSize));
138 ApplicationContainer apps = ping.Install(net1.Get(0));
139 apps.Start(Seconds(2.0));
140 apps.Stop(Seconds(20.0));
141
142 AsciiTraceHelper ascii;
143 csma.EnableAsciiAll(ascii.CreateFileStream("simple-routing-ping6.tr"));
144 csma.EnablePcapAll("simple-routing-ping6", true);
145
146 NS_LOG_INFO("Run Simulation.");
149 NS_LOG_INFO("Done.");
150
151 return 0;
152}
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.
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.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
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.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition ipv6.h:71
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.
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.
Create a ping application and associate it to a node.
Definition ping-helper.h:31
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
Hold an unsigned integer type.
Definition uinteger.h:34
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
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:291
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
static const uint32_t packetSize
Packet size generated at the AP.