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-header-test.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2010 Hajime Tazaki
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: John Abraham <john.abraham@gatech.edu>
19
* Adapted from: ipv4-raw-test.cc
20
*/
21
22
#include "ns3/test.h"
23
#include "ns3/socket-factory.h"
24
#include "ns3/ipv4-raw-socket-factory.h"
25
#include "ns3/simulator.h"
26
#include "ns3/simple-channel.h"
27
#include "ns3/simple-net-device.h"
28
#include "ns3/drop-tail-queue.h"
29
#include "ns3/socket.h"
30
31
#include "ns3/log.h"
32
#include "ns3/node.h"
33
#include "ns3/inet-socket-address.h"
34
#include "ns3/boolean.h"
35
36
#include "ns3/arp-l3-protocol.h"
37
#include "ns3/ipv4-l3-protocol.h"
38
#include "ns3/icmpv4-l4-protocol.h"
39
#include "ns3/ipv4-list-routing.h"
40
#include "ns3/ipv4-static-routing.h"
41
42
#include <string>
43
#include <sstream>
44
#include <limits>
45
#include <netinet/in.h>
46
#include <sys/socket.h>
47
#include <sys/types.h>
48
namespace
ns3 {
49
50
static
void
51
AddInternetStack
(
Ptr<Node>
node)
52
{
53
//ARP
54
Ptr<ArpL3Protocol>
arp = CreateObject<ArpL3Protocol> ();
55
node->
AggregateObject
(arp);
56
//IPV4
57
Ptr<Ipv4L3Protocol>
ipv4 = CreateObject<Ipv4L3Protocol> ();
58
//Routing for Ipv4
59
Ptr<Ipv4ListRouting>
ipv4Routing = CreateObject<Ipv4ListRouting> ();
60
ipv4->
SetRoutingProtocol
(ipv4Routing);
61
Ptr<Ipv4StaticRouting>
ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
62
ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
63
node->
AggregateObject
(ipv4);
64
//ICMP
65
Ptr<Icmpv4L4Protocol>
icmp = CreateObject<Icmpv4L4Protocol> ();
66
node->
AggregateObject
(icmp);
67
// //Ipv4Raw
68
// Ptr<Ipv4UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
69
// node->AggregateObject(udp);
70
}
71
72
73
class
Ipv4HeaderTest
:
public
TestCase
74
{
75
Ptr<Packet>
m_receivedPacket
;
76
Ipv4Header
m_receivedHeader
;
77
void
DoSendData_IpHdr_Dscp
(
Ptr<Socket>
socket, std::string to,
Ipv4Header::DscpType
dscp,
Ipv4Header::EcnType
);
78
void
SendData_IpHdr_Dscp
(
Ptr<Socket>
socket, std::string to,
Ipv4Header::DscpType
dscp,
Ipv4Header::EcnType
);
79
80
public
:
81
virtual
void
DoRun
(
void
);
82
Ipv4HeaderTest
();
83
84
void
ReceivePacket
(
Ptr<Socket>
socket,
Ptr<Packet>
packet,
const
Address
&from);
85
void
ReceivePkt
(
Ptr<Socket>
socket);
86
};
87
88
89
Ipv4HeaderTest::Ipv4HeaderTest
()
90
:
TestCase
(
"IPv4 Header Test"
)
91
{
92
}
93
94
void
Ipv4HeaderTest::ReceivePacket
(
Ptr<Socket>
socket,
Ptr<Packet>
packet,
const
Address
&from)
95
{
96
m_receivedPacket
= packet;
97
}
98
99
100
void
Ipv4HeaderTest::ReceivePkt
(
Ptr<Socket>
socket)
101
{
102
uint32_t availableData;
103
availableData = socket->
GetRxAvailable
();
104
m_receivedPacket
= socket->
Recv
(2, MSG_PEEK);
105
NS_ASSERT
(
m_receivedPacket
->
GetSize
() == 2);
106
m_receivedPacket
= socket->
Recv
(std::numeric_limits<uint32_t>::max (), 0);
107
NS_ASSERT
(availableData == m_receivedPacket->GetSize ());
108
m_receivedPacket->PeekHeader (
m_receivedHeader
);
109
}
110
111
112
113
void
114
Ipv4HeaderTest::DoSendData_IpHdr_Dscp
(
Ptr<Socket>
socket, std::string to,
Ipv4Header::DscpType
dscp,
Ipv4Header::EcnType
ecn)
115
{
116
Address
realTo =
InetSocketAddress
(
Ipv4Address
(to.c_str ()), 0);
117
socket->
SetAttribute
(
"IpHeaderInclude"
,
BooleanValue
(
true
));
118
Ptr<Packet>
p = Create<Packet> (123);
119
Ipv4Header
ipHeader;
120
ipHeader.
SetSource
(
Ipv4Address
(
"10.0.0.2"
));
121
ipHeader.SetDestination (
Ipv4Address
(to.c_str ()));
122
ipHeader.SetProtocol (0);
123
ipHeader.SetPayloadSize (p->
GetSize
());
124
ipHeader.SetTtl (255);
125
ipHeader.SetDscp (dscp);
126
ipHeader.SetEcn (ecn);
127
p->
AddHeader
(ipHeader);
128
129
NS_TEST_EXPECT_MSG_EQ
(socket->
SendTo
(p, 0, realTo),
130
143, to);
131
socket->
SetAttribute
(
"IpHeaderInclude"
,
BooleanValue
(
false
));
132
}
133
134
void
135
Ipv4HeaderTest::SendData_IpHdr_Dscp
(
Ptr<Socket>
socket, std::string to,
Ipv4Header::DscpType
dscp,
Ipv4Header::EcnType
ecn)
136
{
137
m_receivedPacket
= Create<Packet> ();
138
Simulator::ScheduleWithContext
(socket->
GetNode
()->
GetId
(),
Seconds
(0),
139
&
Ipv4HeaderTest::DoSendData_IpHdr_Dscp
,
this
, socket, to, dscp, ecn);
140
Simulator::Run
();
141
}
142
143
void
144
Ipv4HeaderTest::DoRun
(
void
)
145
{
146
// Create topology
147
148
// Receiver Node
149
Ptr<Node>
rxNode = CreateObject<Node> ();
150
AddInternetStack
(rxNode);
151
Ptr<SimpleNetDevice>
rxDev1, rxDev2;
152
{
// first interface
153
rxDev1 = CreateObject<SimpleNetDevice> ();
154
rxDev1->
SetAddress
(
Mac48Address::ConvertFrom
(
Mac48Address::Allocate
()));
155
rxNode->
AddDevice
(rxDev1);
156
Ptr<Ipv4>
ipv4 = rxNode->
GetObject
<
Ipv4
> ();
157
uint32_t netdev_idx = ipv4->
AddInterface
(rxDev1);
158
Ipv4InterfaceAddress
ipv4Addr =
Ipv4InterfaceAddress
(
Ipv4Address
(
"10.0.0.1"
),
Ipv4Mask
(0xffff0000U));
159
ipv4->
AddAddress
(netdev_idx, ipv4Addr);
160
ipv4->
SetUp
(netdev_idx);
161
}
162
163
164
// Sender Node
165
Ptr<Node>
txNode = CreateObject<Node> ();
166
AddInternetStack
(txNode);
167
Ptr<SimpleNetDevice>
txDev1;
168
{
169
txDev1 = CreateObject<SimpleNetDevice> ();
170
txDev1->
SetAddress
(
Mac48Address::ConvertFrom
(
Mac48Address::Allocate
()));
171
txNode->
AddDevice
(txDev1);
172
Ptr<Ipv4>
ipv4 = txNode->
GetObject
<
Ipv4
> ();
173
uint32_t netdev_idx = ipv4->
AddInterface
(txDev1);
174
Ipv4InterfaceAddress
ipv4Addr =
Ipv4InterfaceAddress
(
Ipv4Address
(
"10.0.0.2"
),
Ipv4Mask
(0xffff0000U));
175
ipv4->
AddAddress
(netdev_idx, ipv4Addr);
176
ipv4->
SetUp
(netdev_idx);
177
}
178
179
// link the two nodes
180
Ptr<SimpleChannel>
channel1 = CreateObject<SimpleChannel> ();
181
rxDev1->
SetChannel
(channel1);
182
txDev1->
SetChannel
(channel1);
183
184
185
// Create the IPv4 Raw sockets
186
Ptr<SocketFactory>
rxSocketFactory = rxNode->
GetObject
<
Ipv4RawSocketFactory
> ();
187
Ptr<Socket>
rxSocket = rxSocketFactory->CreateSocket ();
188
NS_TEST_EXPECT_MSG_EQ
(rxSocket->Bind (
InetSocketAddress
(
Ipv4Address
(
"0.0.0.0"
), 0)), 0,
"trivial"
);
189
rxSocket->SetRecvCallback (
MakeCallback
(&
Ipv4HeaderTest::ReceivePkt
,
this
));
190
191
192
Ptr<SocketFactory>
txSocketFactory = txNode->
GetObject
<
Ipv4RawSocketFactory
> ();
193
Ptr<Socket>
txSocket = txSocketFactory->CreateSocket ();
194
195
// ------ Now the tests ------------
196
197
// Dscp Tests
198
std::cout <<
"Dscp Test\n"
;
199
200
std::vector <Ipv4Header::DscpType> vDscpTypes;
201
vDscpTypes.push_back (
Ipv4Header::DscpDefault
);
202
vDscpTypes.push_back (
Ipv4Header::CS1
);
203
vDscpTypes.push_back (
Ipv4Header::AF11
);
204
vDscpTypes.push_back (
Ipv4Header::AF12
);
205
vDscpTypes.push_back (
Ipv4Header::AF13
);
206
vDscpTypes.push_back (
Ipv4Header::CS2
);
207
vDscpTypes.push_back (
Ipv4Header::AF21
);
208
vDscpTypes.push_back (
Ipv4Header::AF22
);
209
vDscpTypes.push_back (
Ipv4Header::AF23
);
210
vDscpTypes.push_back (
Ipv4Header::CS3
);
211
vDscpTypes.push_back (
Ipv4Header::AF31
);
212
vDscpTypes.push_back (
Ipv4Header::AF32
);
213
vDscpTypes.push_back (
Ipv4Header::AF33
);
214
vDscpTypes.push_back (
Ipv4Header::CS4
);
215
vDscpTypes.push_back (
Ipv4Header::AF41
);
216
vDscpTypes.push_back (
Ipv4Header::AF42
);
217
vDscpTypes.push_back (
Ipv4Header::AF43
);
218
vDscpTypes.push_back (
Ipv4Header::CS5
);
219
vDscpTypes.push_back (
Ipv4Header::EF
);
220
vDscpTypes.push_back (
Ipv4Header::CS6
);
221
vDscpTypes.push_back (
Ipv4Header::CS7
);
222
223
for
(uint32_t i = 0; i < vDscpTypes.size (); i++)
224
{
225
SendData_IpHdr_Dscp
(txSocket,
"10.0.0.1"
, vDscpTypes [i],
Ipv4Header::ECT1
);
226
NS_TEST_EXPECT_MSG_EQ
(
m_receivedPacket
->
GetSize
(), 143,
"recv(hdrincl): 10.0.0.1"
);
227
NS_TEST_EXPECT_MSG_EQ
(
m_receivedHeader
.
GetDscp
(), vDscpTypes [i],
"recv(hdrincl): 10.0.0.1"
);
228
m_receivedHeader
.
Print
(std::cout);
229
std::cout << std::endl;
230
m_receivedPacket
->
RemoveAllByteTags
();
231
m_receivedPacket
= 0;
232
}
233
234
// Ecn tests
235
std::cout <<
"Ecn Test\n"
;
236
std::vector <Ipv4Header::EcnType> vEcnTypes;
237
vEcnTypes.push_back (
Ipv4Header::NotECT
);
238
vEcnTypes.push_back (
Ipv4Header::ECT1
);
239
vEcnTypes.push_back (
Ipv4Header::ECT0
);
240
vEcnTypes.push_back (
Ipv4Header::CE
);
241
242
for
(uint32_t i = 0; i < vEcnTypes.size (); i++)
243
{
244
SendData_IpHdr_Dscp
(txSocket,
"10.0.0.1"
,
Ipv4Header::DscpDefault
, vEcnTypes [i]);
245
NS_TEST_EXPECT_MSG_EQ
(
m_receivedPacket
->
GetSize
(), 143,
"recv(hdrincl): 10.0.0.1"
);
246
NS_TEST_EXPECT_MSG_EQ
(
m_receivedHeader
.
GetEcn
(), vEcnTypes [i],
"recv(hdrincl): 10.0.0.1"
);
247
m_receivedHeader
.
Print
(std::cout);
248
std::cout << std::endl;
249
m_receivedPacket
->
RemoveAllByteTags
();
250
m_receivedPacket
= 0;
251
}
252
253
254
255
Simulator::Destroy
();
256
}
257
//-----------------------------------------------------------------------------
258
class
Ipv4HeaderTestSuite
:
public
TestSuite
259
{
260
public
:
261
Ipv4HeaderTestSuite
() :
TestSuite
(
"ipv4-header"
,
UNIT
)
262
{
263
AddTestCase
(
new
Ipv4HeaderTest
);
264
}
265
}
g_ipv4HeaderTestSuite
;
266
267
}
// namespace ns3
src
internet
test
ipv4-header-test.cc
Generated on Tue Oct 9 2012 16:45:39 for ns-3 by
1.8.1.2