A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-header-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Hajime Tazaki
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: John Abraham <john.abraham@gatech.edu>
7 * Adapted from: ipv4-raw-test.cc
8 */
9
10#include "ns3/arp-l3-protocol.h"
11#include "ns3/boolean.h"
12#include "ns3/icmpv4-l4-protocol.h"
13#include "ns3/inet-socket-address.h"
14#include "ns3/internet-stack-helper.h"
15#include "ns3/ipv4-l3-protocol.h"
16#include "ns3/ipv4-list-routing.h"
17#include "ns3/ipv4-raw-socket-factory.h"
18#include "ns3/ipv4-static-routing.h"
19#include "ns3/log.h"
20#include "ns3/node.h"
21#include "ns3/simple-channel.h"
22#include "ns3/simple-net-device.h"
23#include "ns3/simulator.h"
24#include "ns3/socket-factory.h"
25#include "ns3/socket.h"
26#include "ns3/test.h"
27#include "ns3/traffic-control-layer.h"
28
29#ifdef __WIN32__
30#include "ns3/win32-internet.h"
31#else
32#include <netinet/in.h>
33#include <sys/socket.h>
34#endif
35
36#include <limits>
37#include <sstream>
38#include <string>
39#include <sys/types.h>
40
41using namespace ns3;
42
43/**
44 * @ingroup internet-test
45 *
46 * @brief IPv4 Header Test
47 */
49{
50 Ptr<Packet> m_receivedPacket; //!< Received packet.
51 Ipv4Header m_receivedHeader; //!< Received header.
52
53 /**
54 * @brief Send a packet with specific DSCP and ECN fields.
55 * @param socket The source socket.
56 * @param to The destination address.
57 * @param dscp The DSCP field.
58 * @param ecn The ECN field.
59 */
61 std::string to,
64
65 /**
66 * @brief Send a packet with specific DSCP and ECN fields.
67 * @param socket The source socket.
68 * @param to The destination address.
69 * @param dscp The DSCP field.
70 * @param ecn The ECN field.
71 */
73 std::string to,
76
77 public:
78 void DoRun() override;
80
81 /**
82 * @brief Receives a packet.
83 * @param socket The receiving socket.
84 * @param packet The packet.
85 * @param from The source address.
86 */
87 void ReceivePacket(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
88 /**
89 * @brief Receives a packet.
90 * @param socket The receiving socket.
91 */
92 void ReceivePkt(Ptr<Socket> socket);
93};
94
96 : TestCase("IPv4 Header Test")
97{
98}
99
100void
102{
103 m_receivedPacket = packet;
104}
105
106void
108{
109 uint32_t availableData;
110 availableData = socket->GetRxAvailable();
111 m_receivedPacket = socket->Recv(2, MSG_PEEK);
113 2,
114 "Received packet size is not equal to 2");
115 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
116 NS_TEST_ASSERT_MSG_EQ(availableData,
118 "Received packet size is not equal to Rx buffer size");
120}
121
122void
124 std::string to,
127{
128 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 0);
129 socket->SetAttribute("IpHeaderInclude", BooleanValue(true));
131 Ipv4Header ipHeader;
132 ipHeader.SetSource(Ipv4Address("10.0.0.2"));
133 ipHeader.SetDestination(Ipv4Address(to.c_str()));
134 ipHeader.SetProtocol(0);
135 ipHeader.SetPayloadSize(p->GetSize());
136 ipHeader.SetTtl(255);
137 ipHeader.SetDscp(dscp);
138 ipHeader.SetEcn(ecn);
139 p->AddHeader(ipHeader);
140
141 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(p, 0, realTo), 143, to);
142 socket->SetAttribute("IpHeaderInclude", BooleanValue(false));
143}
144
145void
147 std::string to,
150{
152 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
153 Seconds(0),
155 this,
156 socket,
157 to,
158 dscp,
159 ecn);
161}
162
163void
165{
166 // Create topology
167
168 InternetStackHelper internet;
169 internet.SetIpv6StackInstall(false);
170
171 // Receiver Node
172 Ptr<Node> rxNode = CreateObject<Node>();
173 internet.Install(rxNode);
174
177 { // first interface
180 rxNode->AddDevice(rxDev1);
181 Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4>();
182 uint32_t netdev_idx = ipv4->AddInterface(rxDev1);
183 Ipv4InterfaceAddress ipv4Addr =
184 Ipv4InterfaceAddress(Ipv4Address("10.0.0.1"), Ipv4Mask(0xffff0000U));
185 ipv4->AddAddress(netdev_idx, ipv4Addr);
186 ipv4->SetUp(netdev_idx);
187 }
188
189 // Sender Node
190 Ptr<Node> txNode = CreateObject<Node>();
191 internet.Install(txNode);
193 {
196 txNode->AddDevice(txDev1);
197 Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4>();
198 uint32_t netdev_idx = ipv4->AddInterface(txDev1);
199 Ipv4InterfaceAddress ipv4Addr =
200 Ipv4InterfaceAddress(Ipv4Address("10.0.0.2"), Ipv4Mask(0xffff0000U));
201 ipv4->AddAddress(netdev_idx, ipv4Addr);
202 ipv4->SetUp(netdev_idx);
203 }
204
205 // link the two nodes
207 rxDev1->SetChannel(channel1);
208 txDev1->SetChannel(channel1);
209
210 // Create the IPv4 Raw sockets
211 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory>();
212 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
213 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("0.0.0.0"), 0)),
214 0,
215 "trivial");
216 rxSocket->SetRecvCallback(MakeCallback(&Ipv4HeaderTest::ReceivePkt, this));
217
218 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory>();
219 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
220
221 // ------ Now the tests ------------
222
223 // Dscp Tests
224 std::cout << "Dscp Test\n";
225
226 std::vector<Ipv4Header::DscpType> vDscpTypes;
227 vDscpTypes.push_back(Ipv4Header::DscpDefault);
228 vDscpTypes.push_back(Ipv4Header::DSCP_CS1);
229 vDscpTypes.push_back(Ipv4Header::DSCP_AF11);
230 vDscpTypes.push_back(Ipv4Header::DSCP_AF12);
231 vDscpTypes.push_back(Ipv4Header::DSCP_AF13);
232 vDscpTypes.push_back(Ipv4Header::DSCP_CS2);
233 vDscpTypes.push_back(Ipv4Header::DSCP_AF21);
234 vDscpTypes.push_back(Ipv4Header::DSCP_AF22);
235 vDscpTypes.push_back(Ipv4Header::DSCP_AF23);
236 vDscpTypes.push_back(Ipv4Header::DSCP_CS3);
237 vDscpTypes.push_back(Ipv4Header::DSCP_AF31);
238 vDscpTypes.push_back(Ipv4Header::DSCP_AF32);
239 vDscpTypes.push_back(Ipv4Header::DSCP_AF33);
240 vDscpTypes.push_back(Ipv4Header::DSCP_CS4);
241 vDscpTypes.push_back(Ipv4Header::DSCP_AF41);
242 vDscpTypes.push_back(Ipv4Header::DSCP_AF42);
243 vDscpTypes.push_back(Ipv4Header::DSCP_AF43);
244 vDscpTypes.push_back(Ipv4Header::DSCP_CS5);
245 vDscpTypes.push_back(Ipv4Header::DSCP_EF);
246 vDscpTypes.push_back(Ipv4Header::DSCP_CS6);
247 vDscpTypes.push_back(Ipv4Header::DSCP_CS7);
248
249 for (uint32_t i = 0; i < vDscpTypes.size(); i++)
250 {
251 SendData_IpHdr_Dscp(txSocket, "10.0.0.1", vDscpTypes[i], Ipv4Header::ECN_ECT1);
252 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 143, "recv(hdrincl): 10.0.0.1");
253 NS_TEST_EXPECT_MSG_EQ(m_receivedHeader.GetDscp(), vDscpTypes[i], "recv(hdrincl): 10.0.0.1");
254 m_receivedHeader.Print(std::cout);
255 std::cout << std::endl;
257 m_receivedPacket = nullptr;
258 }
259
260 // Ecn tests
261 std::cout << "Ecn Test\n";
262 std::vector<Ipv4Header::EcnType> vEcnTypes;
263 vEcnTypes.push_back(Ipv4Header::ECN_NotECT);
264 vEcnTypes.push_back(Ipv4Header::ECN_ECT1);
265 vEcnTypes.push_back(Ipv4Header::ECN_ECT0);
266 vEcnTypes.push_back(Ipv4Header::ECN_CE);
267
268 for (uint32_t i = 0; i < vEcnTypes.size(); i++)
269 {
270 SendData_IpHdr_Dscp(txSocket, "10.0.0.1", Ipv4Header::DscpDefault, vEcnTypes[i]);
271 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 143, "recv(hdrincl): 10.0.0.1");
272 NS_TEST_EXPECT_MSG_EQ(m_receivedHeader.GetEcn(), vEcnTypes[i], "recv(hdrincl): 10.0.0.1");
273 m_receivedHeader.Print(std::cout);
274 std::cout << std::endl;
276 m_receivedPacket = nullptr;
277 }
278
280}
281
282/**
283 * @ingroup internet-test
284 *
285 * @brief IPv4 Header TestSuite
286 */
288{
289 public:
291 : TestSuite("ipv4-header", Type::UNIT)
292 {
293 AddTestCase(new Ipv4HeaderTest, TestCase::Duration::QUICK);
294 }
295};
296
297static Ipv4HeaderTestSuite g_ipv4HeaderTestSuite; //!< Static variable for test initialization
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 specific 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 specific DSCP and ECN fields.
IPv4 Header TestSuite.
a polymophic address class
Definition address.h:90
AttributeValue implementation for Boolean.
Definition boolean.h:26
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
Ipv4 addresses are stored in host order in this class.
Packet header for IPv4.
Definition ipv4-header.h:23
void Print(std::ostream &os) const override
void SetDestination(Ipv4Address destination)
void SetPayloadSize(uint16_t size)
EcnType GetEcn() const
void SetTtl(uint8_t ttl)
void SetDscp(DscpType dscp)
Set DSCP Field.
EcnType
ECN Type defined in RFC 3168
DscpType GetDscp() const
void SetEcn(EcnType ecn)
Set ECN Field.
DscpType
DiffServ codepoints.
Definition ipv4-header.h:61
void SetProtocol(uint8_t num)
void SetSource(Ipv4Address source)
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
API to create RAW socket instances.
static Mac48Address ConvertFrom(const Address &address)
static Mac48Address Allocate()
Allocate a new Mac48Address.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition packet.h:850
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition packet.cc:382
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition packet.cc:294
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static void Run()
Run the simulation.
Definition simulator.cc:167
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
static constexpr auto UNIT
Definition test.h:1291
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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:134
#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:241
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
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:684