A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
simple-routing-ping6.cc
Go to the documentation of this file.
1
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2008-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
* Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
20
*/
21
22
// Network topology
23
// //
24
// // n0 r n1
25
// // | _ |
26
// // ====|_|====
27
// // router
28
// //
29
// // - Tracing of queues and packet receptions to file "simple-routing-ping6.tr"
30
31
#include <fstream>
32
#include "ns3/core-module.h"
33
#include "ns3/internet-module.h"
34
#include "ns3/csma-module.h"
35
#include "ns3/applications-module.h"
36
#include "ns3/ipv6-static-routing-helper.h"
37
38
#include "ns3/ipv6-routing-table-entry.h"
39
40
using namespace
ns3;
41
42
NS_LOG_COMPONENT_DEFINE
(
"SimpleRoutingPing6Example"
);
43
48
class
StackHelper
49
{
50
public
:
51
58
inline
void
AddAddress (
Ptr<Node>
& n, uint32_t interface,
Ipv6Address
address
)
59
{
60
Ptr<Ipv6>
ipv6 = n->
GetObject
<
Ipv6
> ();
61
ipv6->
AddAddress
(interface, address);
62
}
63
68
inline
void
PrintRoutingTable (
Ptr<Node>
& n)
69
{
70
Ptr<Ipv6StaticRouting>
routing = 0;
71
Ipv6StaticRoutingHelper
routingHelper;
72
Ptr<Ipv6>
ipv6 = n->
GetObject
<
Ipv6
> ();
73
uint32_t nbRoutes = 0;
74
Ipv6RoutingTableEntry
route;
75
76
routing = routingHelper.
GetStaticRouting
(ipv6);
77
78
std::cout <<
"Routing table of "
<< n <<
" : "
<< std::endl;
79
std::cout <<
"Destination\t\t\t\t"
<<
"Gateway\t\t\t\t\t"
<<
"Interface\t"
<<
"Prefix to use"
<< std::endl;
80
81
nbRoutes = routing->
GetNRoutes
();
82
for
(uint32_t i = 0; i < nbRoutes; i++)
83
{
84
route = routing->
GetRoute
(i);
85
std::cout << route.
GetDest
() <<
"\t"
86
<< route.
GetGateway
() <<
"\t"
87
<< route.
GetInterface
() <<
"\t"
88
<< route.
GetPrefixToUse
() <<
"\t"
89
<< std::endl;
90
}
91
}
92
};
93
94
int
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
);
101
LogComponentEnable
(
"Ping6Application"
,
LOG_LEVEL_ALL
);
102
#endif
103
104
CommandLine
cmd;
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."
);
123
CsmaHelper
csma;
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
);
134
i1.
SetDefaultRouteInAllNodes
(1);
135
ipv6.
SetBase
(
Ipv6Address
(
"2001:2::"
),
Ipv6Prefix
(64));
136
Ipv6InterfaceContainer
i2 = ipv6.
Assign
(d2);
137
i2.
SetForwarding
(0,
true
);
138
i2.
SetDefaultRouteInAllNodes
(0);
139
140
stackHelper.
PrintRoutingTable
(n0);
141
142
/* Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via r */
143
uint32_t packetSize = 1024;
144
uint32_t maxPacketCount = 5;
145
Time
interPacketInterval = Seconds (1.);
146
Ping6Helper
ping6;
147
148
ping6.
SetLocal
(i1.
GetAddress
(0, 1));
149
ping6.
SetRemote
(i2.
GetAddress
(1, 1));
150
151
ping6.
SetAttribute
(
"MaxPackets"
,
UintegerValue
(maxPacketCount));
152
ping6.
SetAttribute
(
"Interval"
,
TimeValue
(interPacketInterval));
153
ping6.
SetAttribute
(
"PacketSize"
,
UintegerValue
(packetSize));
154
ApplicationContainer
apps = ping6.
Install
(net1.Get (0));
155
apps.
Start
(Seconds (2.0));
156
apps.
Stop
(Seconds (20.0));
157
158
AsciiTraceHelper
ascii;
159
csma.
EnableAsciiAll
(ascii.
CreateFileStream
(
"simple-routing-ping6.tr"
));
160
csma.
EnablePcapAll
(
"simple-routing-ping6"
,
true
);
161
162
NS_LOG_INFO
(
"Run Simulation."
);
163
Simulator::Run
();
164
Simulator::Destroy
();
165
NS_LOG_INFO
(
"Done."
);
166
}
167
examples
routing
simple-routing-ping6.cc
Generated on Fri Aug 30 2013 01:42:44 for ns-3 by
1.8.1.2