A Discrete-Event Network Simulator
API
ipv4-header-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Hajime Tazaki
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: John Abraham <john.abraham@gatech.edu>
18 * Adapted from: ipv4-raw-test.cc
19 */
20
21#include "ns3/arp-l3-protocol.h"
22#include "ns3/boolean.h"
23#include "ns3/icmpv4-l4-protocol.h"
24#include "ns3/inet-socket-address.h"
25#include "ns3/internet-stack-helper.h"
26#include "ns3/ipv4-l3-protocol.h"
27#include "ns3/ipv4-list-routing.h"
28#include "ns3/ipv4-raw-socket-factory.h"
29#include "ns3/ipv4-static-routing.h"
30#include "ns3/log.h"
31#include "ns3/node.h"
32#include "ns3/simple-channel.h"
33#include "ns3/simple-net-device.h"
34#include "ns3/simulator.h"
35#include "ns3/socket-factory.h"
36#include "ns3/socket.h"
37#include "ns3/test.h"
38#include "ns3/traffic-control-layer.h"
39
40#ifdef __WIN32__
41#include "ns3/win32-internet.h"
42#else
43#include <netinet/in.h>
44#include <sys/socket.h>
45#endif
46
47#include <limits>
48#include <sstream>
49#include <string>
50#include <sys/types.h>
51
52using namespace ns3;
53
61{
64
73 std::string to,
76
85 std::string to,
88
89 public:
90 void DoRun() override;
92
99 void ReceivePacket(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
104 void ReceivePkt(Ptr<Socket> socket);
105};
106
108 : TestCase("IPv4 Header Test")
109{
110}
111
112void
114{
115 m_receivedPacket = packet;
116}
117
118void
120{
121 uint32_t availableData;
122 availableData = socket->GetRxAvailable();
123 m_receivedPacket = socket->Recv(2, MSG_PEEK);
125 2,
126 "Received packet size is not equal to 2");
128 NS_TEST_ASSERT_MSG_EQ(availableData,
130 "Received packet size is not equal to Rx buffer size");
132}
133
134void
136 std::string to,
139{
140 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 0);
141 socket->SetAttribute("IpHeaderInclude", BooleanValue(true));
142 Ptr<Packet> p = Create<Packet>(123);
143 Ipv4Header ipHeader;
144 ipHeader.SetSource(Ipv4Address("10.0.0.2"));
145 ipHeader.SetDestination(Ipv4Address(to.c_str()));
146 ipHeader.SetProtocol(0);
147 ipHeader.SetPayloadSize(p->GetSize());
148 ipHeader.SetTtl(255);
149 ipHeader.SetDscp(dscp);
150 ipHeader.SetEcn(ecn);
151 p->AddHeader(ipHeader);
152
153 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(p, 0, realTo), 143, to);
154 socket->SetAttribute("IpHeaderInclude", BooleanValue(false));
155}
156
157void
159 std::string to,
162{
163 m_receivedPacket = Create<Packet>();
164 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
165 Seconds(0),
167 this,
168 socket,
169 to,
170 dscp,
171 ecn);
172 Simulator::Run();
173}
174
175void
177{
178 // Create topology
179
180 InternetStackHelper internet;
181 internet.SetIpv6StackInstall(false);
182
183 // Receiver Node
184 Ptr<Node> rxNode = CreateObject<Node>();
185 internet.Install(rxNode);
186
189 { // first interface
190 rxDev1 = CreateObject<SimpleNetDevice>();
191 rxDev1->SetAddress(Mac48Address::ConvertFrom(Mac48Address::Allocate()));
192 rxNode->AddDevice(rxDev1);
193 Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4>();
194 uint32_t netdev_idx = ipv4->AddInterface(rxDev1);
195 Ipv4InterfaceAddress ipv4Addr =
196 Ipv4InterfaceAddress(Ipv4Address("10.0.0.1"), Ipv4Mask(0xffff0000U));
197 ipv4->AddAddress(netdev_idx, ipv4Addr);
198 ipv4->SetUp(netdev_idx);
199 }
200
201 // Sender Node
202 Ptr<Node> txNode = CreateObject<Node>();
203 internet.Install(txNode);
205 {
206 txDev1 = CreateObject<SimpleNetDevice>();
207 txDev1->SetAddress(Mac48Address::ConvertFrom(Mac48Address::Allocate()));
208 txNode->AddDevice(txDev1);
209 Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4>();
210 uint32_t netdev_idx = ipv4->AddInterface(txDev1);
211 Ipv4InterfaceAddress ipv4Addr =
212 Ipv4InterfaceAddress(Ipv4Address("10.0.0.2"), Ipv4Mask(0xffff0000U));
213 ipv4->AddAddress(netdev_idx, ipv4Addr);
214 ipv4->SetUp(netdev_idx);
215 }
216
217 // link the two nodes
218 Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel>();
219 rxDev1->SetChannel(channel1);
220 txDev1->SetChannel(channel1);
221
222 // Create the IPv4 Raw sockets
223 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory>();
224 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
225 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("0.0.0.0"), 0)),
226 0,
227 "trivial");
228 rxSocket->SetRecvCallback(MakeCallback(&Ipv4HeaderTest::ReceivePkt, this));
229
230 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory>();
231 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
232
233 // ------ Now the tests ------------
234
235 // Dscp Tests
236 std::cout << "Dscp Test\n";
237
238 std::vector<Ipv4Header::DscpType> vDscpTypes;
239 vDscpTypes.push_back(Ipv4Header::DscpDefault);
240 vDscpTypes.push_back(Ipv4Header::DSCP_CS1);
241 vDscpTypes.push_back(Ipv4Header::DSCP_AF11);
242 vDscpTypes.push_back(Ipv4Header::DSCP_AF12);
243 vDscpTypes.push_back(Ipv4Header::DSCP_AF13);
244 vDscpTypes.push_back(Ipv4Header::DSCP_CS2);
245 vDscpTypes.push_back(Ipv4Header::DSCP_AF21);
246 vDscpTypes.push_back(Ipv4Header::DSCP_AF22);
247 vDscpTypes.push_back(Ipv4Header::DSCP_AF23);
248 vDscpTypes.push_back(Ipv4Header::DSCP_CS3);
249 vDscpTypes.push_back(Ipv4Header::DSCP_AF31);
250 vDscpTypes.push_back(Ipv4Header::DSCP_AF32);
251 vDscpTypes.push_back(Ipv4Header::DSCP_AF33);
252 vDscpTypes.push_back(Ipv4Header::DSCP_CS4);
253 vDscpTypes.push_back(Ipv4Header::DSCP_AF41);
254 vDscpTypes.push_back(Ipv4Header::DSCP_AF42);
255 vDscpTypes.push_back(Ipv4Header::DSCP_AF43);
256 vDscpTypes.push_back(Ipv4Header::DSCP_CS5);
257 vDscpTypes.push_back(Ipv4Header::DSCP_EF);
258 vDscpTypes.push_back(Ipv4Header::DSCP_CS6);
259 vDscpTypes.push_back(Ipv4Header::DSCP_CS7);
260
261 for (uint32_t i = 0; i < vDscpTypes.size(); i++)
262 {
263 SendData_IpHdr_Dscp(txSocket, "10.0.0.1", vDscpTypes[i], Ipv4Header::ECN_ECT1);
264 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 143, "recv(hdrincl): 10.0.0.1");
265 NS_TEST_EXPECT_MSG_EQ(m_receivedHeader.GetDscp(), vDscpTypes[i], "recv(hdrincl): 10.0.0.1");
266 m_receivedHeader.Print(std::cout);
267 std::cout << std::endl;
269 m_receivedPacket = nullptr;
270 }
271
272 // Ecn tests
273 std::cout << "Ecn Test\n";
274 std::vector<Ipv4Header::EcnType> vEcnTypes;
275 vEcnTypes.push_back(Ipv4Header::ECN_NotECT);
276 vEcnTypes.push_back(Ipv4Header::ECN_ECT1);
277 vEcnTypes.push_back(Ipv4Header::ECN_ECT0);
278 vEcnTypes.push_back(Ipv4Header::ECN_CE);
279
280 for (uint32_t i = 0; i < vEcnTypes.size(); i++)
281 {
282 SendData_IpHdr_Dscp(txSocket, "10.0.0.1", Ipv4Header::DscpDefault, vEcnTypes[i]);
283 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 143, "recv(hdrincl): 10.0.0.1");
284 NS_TEST_EXPECT_MSG_EQ(m_receivedHeader.GetEcn(), vEcnTypes[i], "recv(hdrincl): 10.0.0.1");
285 m_receivedHeader.Print(std::cout);
286 std::cout << std::endl;
288 m_receivedPacket = nullptr;
289 }
290
291 Simulator::Destroy();
292}
293
301{
302 public:
304 : TestSuite("ipv4-header", UNIT)
305 {
306 AddTestCase(new Ipv4HeaderTest, TestCase::QUICK);
307 }
308};
309
#define max(a, b)
Definition: 80211b.c:43
IPv4 Header Test.
void DoRun() override
Implementation to actually run this TestCase.
Ptr< Packet > m_receivedPacket
Received packet.
Ipv4Header m_receivedHeader
Received header.
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Receives a packet.
void DoSendData_IpHdr_Dscp(Ptr< Socket > socket, std::string to, Ipv4Header::DscpType dscp, Ipv4Header::EcnType ecn)
Send a packet with speciic DSCP and ECN fields.
void ReceivePkt(Ptr< Socket > socket)
Receives a packet.
void SendData_IpHdr_Dscp(Ptr< Socket > socket, std::string to, Ipv4Header::DscpType dscp, Ipv4Header::EcnType ecn)
Send a packet with speciic DSCP and ECN fields.
IPv4 Header TestSuite.
a polymophic address class
Definition: address.h:92
AttributeValue implementation for Boolean.
Definition: boolean.h:37
an Inet address class
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...
void SetIpv6StackInstall(bool enable)
Enable/disable IPv6 stack install.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
Packet header for IPv4.
Definition: ipv4-header.h:34
void Print(std::ostream &os) const override
Definition: ipv4-header.cc:347
void SetDestination(Ipv4Address destination)
Definition: ipv4-header.cc:309
void SetPayloadSize(uint16_t size)
Definition: ipv4-header.cc:57
EcnType GetEcn() const
Definition: ipv4-header.cc:169
void SetTtl(uint8_t ttl)
Definition: ipv4-header.cc:267
void SetDscp(DscpType dscp)
Set DSCP Field.
Definition: ipv4-header.cc:92
EcnType
ECN Type defined in RFC 3168
Definition: ipv4-header.h:114
DscpType GetDscp() const
Definition: ipv4-header.cc:108
void SetEcn(EcnType ecn)
Set ECN Field.
Definition: ipv4-header.cc:100
DscpType
DiffServ codepoints.
Definition: ipv4-header.h:72
void SetProtocol(uint8_t num)
Definition: ipv4-header.cc:288
void SetSource(Ipv4Address source)
Definition: ipv4-header.cc:295
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:79
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
API to create RAW socket instances.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
uint32_t GetId() const
Definition: node.cc:117
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:258
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:268
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition: packet.cc:393
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:305
virtual uint32_t GetRxAvailable() const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual Ptr< Node > GetNode() const =0
Return the node this socket is associated with.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:305
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:144
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:251
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
static Ipv4HeaderTestSuite g_ipv4HeaderTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:691