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
ipv4-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/inet-socket-address.h"
34
35
#include "ns3/arp-l3-protocol.h"
36
#include "ns3/ipv4-l3-protocol.h"
37
#include "ns3/icmpv4-l4-protocol.h"
38
#include "ns3/udp-l4-protocol.h"
39
#include "ns3/ipv4-static-routing.h"
40
41
#include <string>
42
#include <limits>
43
44
using namespace
ns3;
45
46
static
void
47
AddInternetStack
(
Ptr<Node>
node)
48
{
49
//ARP
50
Ptr<ArpL3Protocol>
arp = CreateObject<ArpL3Protocol> ();
51
node->
AggregateObject
(arp);
52
//IPV4
53
Ptr<Ipv4L3Protocol>
ipv4 = CreateObject<Ipv4L3Protocol> ();
54
//Routing for Ipv4
55
Ptr<Ipv4StaticRouting>
ipv4Routing = CreateObject<Ipv4StaticRouting> ();
56
ipv4->
SetRoutingProtocol
(ipv4Routing);
57
node->
AggregateObject
(ipv4);
58
node->
AggregateObject
(ipv4Routing);
59
//ICMP
60
Ptr<Icmpv4L4Protocol>
icmp = CreateObject<Icmpv4L4Protocol> ();
61
node->
AggregateObject
(icmp);
62
//UDP
63
Ptr<UdpL4Protocol>
udp = CreateObject<UdpL4Protocol> ();
64
node->
AggregateObject
(udp);
65
}
66
67
68
class
Ipv4ForwardingTest
:
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
Ipv4ForwardingTest
();
77
78
void
ReceivePkt (
Ptr<Socket>
socket);
79
};
80
81
Ipv4ForwardingTest::Ipv4ForwardingTest
()
82
:
TestCase
(
"UDP socket implementation"
)
83
{
84
}
85
86
void
Ipv4ForwardingTest::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
}
93
94
void
95
Ipv4ForwardingTest::DoSendData
(
Ptr<Socket>
socket, std::string to)
96
{
97
Address
realTo =
InetSocketAddress
(
Ipv4Address
(to.c_str ()), 1234);
98
NS_TEST_EXPECT_MSG_EQ
(socket->
SendTo
(Create<Packet> (123), 0, realTo),
99
123,
"100"
);
100
}
101
102
void
103
Ipv4ForwardingTest::SendData
(
Ptr<Socket>
socket, std::string to)
104
{
105
m_receivedPacket
= Create<Packet> ();
106
Simulator::ScheduleWithContext (socket->
GetNode
()->
GetId
(), Seconds (0),
107
&
Ipv4ForwardingTest::DoSendData
,
this
, socket, to);
108
Simulator::Run
();
109
}
110
111
void
112
Ipv4ForwardingTest::DoRun
(
void
)
113
{
114
// Create topology
115
116
// Receiver Node
117
Ptr<Node>
rxNode = CreateObject<Node> ();
118
AddInternetStack
(rxNode);
119
Ptr<SimpleNetDevice>
rxDev;
120
{
// first interface
121
rxDev = CreateObject<SimpleNetDevice> ();
122
rxDev->
SetAddress
(Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
123
rxNode->
AddDevice
(rxDev);
124
Ptr<Ipv4>
ipv4 = rxNode->
GetObject
<
Ipv4
> ();
125
uint32_t netdev_idx = ipv4->
AddInterface
(rxDev);
126
Ipv4InterfaceAddress
ipv4Addr =
Ipv4InterfaceAddress
(
Ipv4Address
(
"10.0.0.2"
),
Ipv4Mask
(0xffff0000U));
127
ipv4->
AddAddress
(netdev_idx, ipv4Addr);
128
ipv4->
SetUp
(netdev_idx);
129
}
130
131
// Forwarding Node
132
Ptr<Node>
fwNode = CreateObject<Node> ();
133
AddInternetStack
(fwNode);
134
Ptr<SimpleNetDevice>
fwDev1, fwDev2;
135
{
// first interface
136
fwDev1 = CreateObject<SimpleNetDevice> ();
137
fwDev1->
SetAddress
(Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
138
fwNode->
AddDevice
(fwDev1);
139
Ptr<Ipv4>
ipv4 = fwNode->
GetObject
<
Ipv4
> ();
140
uint32_t netdev_idx = ipv4->
AddInterface
(fwDev1);
141
Ipv4InterfaceAddress
ipv4Addr =
Ipv4InterfaceAddress
(
Ipv4Address
(
"10.0.0.1"
),
Ipv4Mask
(0xffff0000U));
142
ipv4->
AddAddress
(netdev_idx, ipv4Addr);
143
ipv4->
SetUp
(netdev_idx);
144
}
145
146
{
// second interface
147
fwDev2 = CreateObject<SimpleNetDevice> ();
148
fwDev2->
SetAddress
(Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
149
fwNode->
AddDevice
(fwDev2);
150
Ptr<Ipv4>
ipv4 = fwNode->
GetObject
<
Ipv4
> ();
151
uint32_t netdev_idx = ipv4->
AddInterface
(fwDev2);
152
Ipv4InterfaceAddress
ipv4Addr =
Ipv4InterfaceAddress
(
Ipv4Address
(
"10.1.0.1"
),
Ipv4Mask
(0xffff0000U));
153
ipv4->
AddAddress
(netdev_idx, ipv4Addr);
154
ipv4->
SetUp
(netdev_idx);
155
}
156
157
// Sender Node
158
Ptr<Node>
txNode = CreateObject<Node> ();
159
AddInternetStack
(txNode);
160
Ptr<SimpleNetDevice>
txDev;
161
{
162
txDev = CreateObject<SimpleNetDevice> ();
163
txDev->
SetAddress
(Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
164
txNode->
AddDevice
(txDev);
165
Ptr<Ipv4>
ipv4 = txNode->
GetObject
<
Ipv4
> ();
166
uint32_t netdev_idx = ipv4->
AddInterface
(txDev);
167
Ipv4InterfaceAddress
ipv4Addr =
Ipv4InterfaceAddress
(
Ipv4Address
(
"10.1.0.2"
),
Ipv4Mask
(0xffff0000U));
168
ipv4->
AddAddress
(netdev_idx, ipv4Addr);
169
ipv4->
SetUp
(netdev_idx);
170
Ptr<Ipv4StaticRouting>
ipv4StaticRouting = txNode->
GetObject
<
Ipv4StaticRouting
> ();
171
ipv4StaticRouting->
SetDefaultRoute
(
Ipv4Address
(
"10.1.0.1"
), netdev_idx);
172
}
173
174
// link the two nodes
175
Ptr<SimpleChannel>
channel1 = CreateObject<SimpleChannel> ();
176
rxDev->
SetChannel
(channel1);
177
fwDev1->
SetChannel
(channel1);
178
179
Ptr<SimpleChannel>
channel2 = CreateObject<SimpleChannel> ();
180
fwDev2->
SetChannel
(channel2);
181
txDev->
SetChannel
(channel2);
182
183
// Create the UDP sockets
184
Ptr<SocketFactory>
rxSocketFactory = rxNode->
GetObject
<
UdpSocketFactory
> ();
185
Ptr<Socket>
rxSocket = rxSocketFactory->
CreateSocket
();
186
NS_TEST_EXPECT_MSG_EQ
(rxSocket->Bind (
InetSocketAddress
(
Ipv4Address
(
"10.0.0.2"
), 1234)), 0,
"trivial"
);
187
rxSocket->SetRecvCallback (
MakeCallback
(&
Ipv4ForwardingTest::ReceivePkt
,
this
));
188
189
Ptr<SocketFactory>
txSocketFactory = txNode->
GetObject
<
UdpSocketFactory
> ();
190
Ptr<Socket>
txSocket = txSocketFactory->
CreateSocket
();
191
txSocket->
SetAllowBroadcast
(
true
);
192
193
// ------ Now the tests ------------
194
195
// Unicast test
196
SendData
(txSocket,
"10.0.0.2"
);
197
NS_TEST_EXPECT_MSG_EQ
(
m_receivedPacket
->
GetSize
(), 123,
"IPv4 Forwarding on"
);
198
199
m_receivedPacket
->
RemoveAllByteTags
();
200
m_receivedPacket
= 0;
201
202
Ptr<Ipv4>
ipv4 = fwNode->
GetObject
<
Ipv4
> ();
203
ipv4->
SetAttribute
(
"IpForward"
,
BooleanValue
(
false
));
204
SendData
(txSocket,
"10.0.0.2"
);
205
NS_TEST_EXPECT_MSG_EQ
(
m_receivedPacket
->
GetSize
(), 0,
"IPv4 Forwarding off"
);
206
207
Simulator::Destroy ();
208
209
}
210
211
212
//-----------------------------------------------------------------------------
213
//-----------------------------------------------------------------------------
214
class
Ipv4ForwardingTestSuite
:
public
TestSuite
215
{
216
public
:
217
Ipv4ForwardingTestSuite
() :
TestSuite
(
"ipv4-forwarding"
,
UNIT
)
218
{
219
AddTestCase
(
new
Ipv4ForwardingTest
, TestCase::QUICK);
220
}
221
}
g_ipv4forwardingTestSuite
;
src
internet
test
ipv4-forwarding-test.cc
Generated on Fri Aug 30 2013 01:42:53 for ns-3 by
1.8.1.2