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
ipv6-forwarding-test.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2013 Universita' di Firenze
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19
*/
20
21
#include "ns3/test.h"
22
#include "ns3/socket-factory.h"
23
#include "ns3/udp-socket-factory.h"
24
#include "ns3/simulator.h"
25
#include "ns3/simple-channel.h"
26
#include "ns3/simple-net-device.h"
27
#include "ns3/drop-tail-queue.h"
28
#include "ns3/socket.h"
29
#include "ns3/boolean.h"
30
31
#include "ns3/log.h"
32
#include "ns3/node.h"
33
#include "ns3/inet6-socket-address.h"
34
35
#include "ns3/ipv6-l3-protocol.h"
36
#include "ns3/icmpv6-l4-protocol.h"
37
#include "ns3/udp-l4-protocol.h"
38
#include "ns3/ipv6-static-routing.h"
39
40
#include <string>
41
#include <limits>
42
43
using namespace
ns3;
44
45
static
void
46
AddInternetStack6
(
Ptr<Node>
node)
47
{
48
//IPV6
49
Ptr<Ipv6L3Protocol>
ipv6 = CreateObject<Ipv6L3Protocol> ();
50
//Routing for Ipv6
51
Ptr<Ipv6StaticRouting>
ipv6Routing = CreateObject<Ipv6StaticRouting> ();
52
ipv6->
SetRoutingProtocol
(ipv6Routing);
53
node->
AggregateObject
(ipv6);
54
node->
AggregateObject
(ipv6Routing);
55
//ICMP
56
Ptr<Icmpv6L4Protocol>
icmp = CreateObject<Icmpv6L4Protocol> ();
57
node->
AggregateObject
(icmp);
58
//Ipv6 Extensions
59
ipv6->
RegisterExtensions
();
60
ipv6->
RegisterOptions
();
61
//UDP
62
Ptr<UdpL4Protocol>
udp = CreateObject<UdpL4Protocol> ();
63
node->
AggregateObject
(udp);
64
}
65
66
67
68
class
Ipv6ForwardingTest
:
public
TestCase
69
{
70
Ptr<Packet>
m_receivedPacket
;
71
void
DoSendData (
Ptr<Socket>
socket, std::string to);
72
void
SendData (
Ptr<Socket>
socket, std::string to);
73
74
public
:
75
virtual
void
DoRun (
void
);
76
Ipv6ForwardingTest
();
77
78
void
ReceivePkt (
Ptr<Socket>
socket);
79
};
80
81
Ipv6ForwardingTest::Ipv6ForwardingTest
()
82
:
TestCase
(
"UDP6 socket implementation"
)
83
{
84
}
85
86
void
Ipv6ForwardingTest::ReceivePkt
(
Ptr<Socket>
socket)
87
{
88
uint32_t availableData;
89
availableData = socket->
GetRxAvailable
();
90
m_receivedPacket
= socket->
Recv
(std::numeric_limits<uint32_t>::max (), 0);
91
NS_ASSERT
(availableData ==
m_receivedPacket
->
GetSize
());
92
//cast availableData to void, to suppress 'availableData' set but not used
93
//compiler warning
94
(void) availableData;
95
}
96
97
void
98
Ipv6ForwardingTest::DoSendData
(
Ptr<Socket>
socket, std::string to)
99
{
100
Address
realTo =
Inet6SocketAddress
(
Ipv6Address
(to.c_str ()), 1234);
101
NS_TEST_EXPECT_MSG_EQ
(socket->
SendTo
(Create<Packet> (123), 0, realTo),
102
123,
"100"
);
103
}
104
105
void
106
Ipv6ForwardingTest::SendData
(
Ptr<Socket>
socket, std::string to)
107
{
108
m_receivedPacket
= Create<Packet> ();
109
Simulator::ScheduleWithContext (socket->
GetNode
()->
GetId
(), Seconds (0),
110
&
Ipv6ForwardingTest::DoSendData
,
this
, socket, to);
111
Simulator::Run
();
112
}
113
114
void
115
Ipv6ForwardingTest::DoRun
(
void
)
116
{
117
// Create topology
118
119
// Receiver Node
120
Ptr<Node>
rxNode = CreateObject<Node> ();
121
AddInternetStack6
(rxNode);
122
Ptr<SimpleNetDevice>
rxDev;
123
{
// first interface
124
rxDev = CreateObject<SimpleNetDevice> ();
125
rxDev->
SetAddress
(Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
126
rxNode->
AddDevice
(rxDev);
127
Ptr<Ipv6>
ipv6 = rxNode->
GetObject
<
Ipv6
> ();
128
uint32_t netdev_idx = ipv6->
AddInterface
(rxDev);
129
Ipv6InterfaceAddress
ipv6Addr =
Ipv6InterfaceAddress
(
Ipv6Address
(
"2001:1::2"
),
Ipv6Prefix
(64));
130
ipv6->
AddAddress
(netdev_idx, ipv6Addr);
131
ipv6->
SetUp
(netdev_idx);
132
}
133
134
// Forwarding Node
135
Ptr<Node>
fwNode = CreateObject<Node> ();
136
AddInternetStack6
(fwNode);
137
Ptr<SimpleNetDevice>
fwDev1, fwDev2;
138
{
// first interface
139
fwDev1 = CreateObject<SimpleNetDevice> ();
140
fwDev1->
SetAddress
(Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
141
fwNode->
AddDevice
(fwDev1);
142
Ptr<Ipv6>
ipv6 = fwNode->
GetObject
<
Ipv6
> ();
143
uint32_t netdev_idx = ipv6->
AddInterface
(fwDev1);
144
Ipv6InterfaceAddress
ipv6Addr =
Ipv6InterfaceAddress
(
Ipv6Address
(
"2001:1::1"
),
Ipv6Prefix
(64));
145
ipv6->
AddAddress
(netdev_idx, ipv6Addr);
146
ipv6->
SetUp
(netdev_idx);
147
}
148
149
Ipv6Address
nextHop;
150
{
// second interface
151
fwDev2 = CreateObject<SimpleNetDevice> ();
152
fwDev2->
SetAddress
(Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
153
fwNode->
AddDevice
(fwDev2);
154
Ptr<Ipv6>
ipv6 = fwNode->
GetObject
<
Ipv6
> ();
155
uint32_t netdev_idx = ipv6->
AddInterface
(fwDev2);
156
Ipv6InterfaceAddress
ipv6Addr =
Ipv6InterfaceAddress
(
Ipv6Address
(
"2001:2::1"
),
Ipv6Prefix
(64));
157
nextHop = ipv6->
GetAddress
(netdev_idx, 0).
GetAddress
();
158
ipv6->
AddAddress
(netdev_idx, ipv6Addr);
159
ipv6->
SetUp
(netdev_idx);
160
}
161
162
// Sender Node
163
Ptr<Node>
txNode = CreateObject<Node> ();
164
AddInternetStack6
(txNode);
165
Ptr<SimpleNetDevice>
txDev;
166
{
167
txDev = CreateObject<SimpleNetDevice> ();
168
txDev->
SetAddress
(Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
169
txNode->
AddDevice
(txDev);
170
Ptr<Ipv6>
ipv6 = txNode->
GetObject
<
Ipv6
> ();
171
uint32_t netdev_idx = ipv6->
AddInterface
(txDev);
172
Ipv6InterfaceAddress
ipv6Addr =
Ipv6InterfaceAddress
(
Ipv6Address
(
"2001:2::2"
),
Ipv6Prefix
(64));
173
ipv6->
AddAddress
(netdev_idx, ipv6Addr);
174
ipv6->
SetUp
(netdev_idx);
175
Ptr<Ipv6StaticRouting>
ipv6StaticRouting = txNode->
GetObject
<
Ipv6StaticRouting
> ();
176
ipv6StaticRouting->
SetDefaultRoute
(nextHop, netdev_idx);
177
}
178
179
// link the two nodes
180
Ptr<SimpleChannel>
channel1 = CreateObject<SimpleChannel> ();
181
rxDev->
SetChannel
(channel1);
182
fwDev1->
SetChannel
(channel1);
183
184
Ptr<SimpleChannel>
channel2 = CreateObject<SimpleChannel> ();
185
fwDev2->
SetChannel
(channel2);
186
txDev->
SetChannel
(channel2);
187
188
// Create the UDP sockets
189
Ptr<SocketFactory>
rxSocketFactory = rxNode->
GetObject
<
UdpSocketFactory
> ();
190
Ptr<Socket>
rxSocket = rxSocketFactory->
CreateSocket
();
191
NS_TEST_EXPECT_MSG_EQ
(rxSocket->Bind (
Inet6SocketAddress
(
Ipv6Address
(
"2001:1::2"
), 1234)), 0,
"trivial"
);
192
rxSocket->SetRecvCallback (
MakeCallback
(&
Ipv6ForwardingTest::ReceivePkt
,
this
));
193
194
Ptr<SocketFactory>
txSocketFactory = txNode->
GetObject
<
UdpSocketFactory
> ();
195
Ptr<Socket>
txSocket = txSocketFactory->
CreateSocket
();
196
txSocket->
SetAllowBroadcast
(
true
);
197
198
// ------ Now the tests ------------
199
200
// Unicast test
201
SendData
(txSocket,
"2001:1::2"
);
202
NS_TEST_EXPECT_MSG_EQ
(
m_receivedPacket
->
GetSize
(), 0,
"IPv6 Forwarding off"
);
203
204
m_receivedPacket
->
RemoveAllByteTags
();
205
m_receivedPacket
= 0;
206
207
Ptr<Ipv6>
ipv6 = fwNode->
GetObject
<
Ipv6
> ();
208
ipv6->
SetAttribute
(
"IpForward"
,
BooleanValue
(
true
));
209
SendData
(txSocket,
"2001:1::2"
);
210
NS_TEST_EXPECT_MSG_EQ
(
m_receivedPacket
->
GetSize
(), 123,
"IPv6 Forwarding on"
);
211
212
m_receivedPacket
->
RemoveAllByteTags
();
213
214
Simulator::Destroy ();
215
216
}
217
218
219
//-----------------------------------------------------------------------------
220
//-----------------------------------------------------------------------------
221
class
Ipv6ForwardingTestSuite
:
public
TestSuite
222
{
223
public
:
224
Ipv6ForwardingTestSuite
() :
TestSuite
(
"ipv6-forwarding"
,
UNIT
)
225
{
226
AddTestCase
(
new
Ipv6ForwardingTest
, TestCase::QUICK);
227
}
228
}
g_ipv6forwardingTestSuite
;
src
internet
test
ipv6-forwarding-test.cc
Generated on Fri Aug 30 2013 01:42:53 for ns-3 by
1.8.1.2