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 
52 using namespace ns3;
53 
59 class Ipv4HeaderTest : public TestCase
60 {
63 
71  void DoSendData_IpHdr_Dscp(Ptr<Socket> socket,
72  std::string to,
75 
83  void SendData_IpHdr_Dscp(Ptr<Socket> socket,
84  std::string to,
87 
88  public:
89  void DoRun() override;
91 
98  void ReceivePacket(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
103  void ReceivePkt(Ptr<Socket> socket);
104 };
105 
107  : TestCase("IPv4 Header Test")
108 {
109 }
110 
111 void
113 {
114  m_receivedPacket = packet;
115 }
116 
117 void
119 {
120  uint32_t availableData;
121  availableData = socket->GetRxAvailable();
122  m_receivedPacket = socket->Recv(2, MSG_PEEK);
124  2,
125  "Received packet size is not equal to 2");
127  NS_TEST_ASSERT_MSG_EQ(availableData,
129  "Received packet size is not equal to Rx buffer size");
131 }
132 
133 void
135  std::string to,
138 {
139  Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 0);
140  socket->SetAttribute("IpHeaderInclude", BooleanValue(true));
141  Ptr<Packet> p = Create<Packet>(123);
142  Ipv4Header ipHeader;
143  ipHeader.SetSource(Ipv4Address("10.0.0.2"));
144  ipHeader.SetDestination(Ipv4Address(to.c_str()));
145  ipHeader.SetProtocol(0);
146  ipHeader.SetPayloadSize(p->GetSize());
147  ipHeader.SetTtl(255);
148  ipHeader.SetDscp(dscp);
149  ipHeader.SetEcn(ecn);
150  p->AddHeader(ipHeader);
151 
152  NS_TEST_EXPECT_MSG_EQ(socket->SendTo(p, 0, realTo), 143, to);
153  socket->SetAttribute("IpHeaderInclude", BooleanValue(false));
154 }
155 
156 void
158  std::string to,
161 {
162  m_receivedPacket = Create<Packet>();
163  Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
164  Seconds(0),
166  this,
167  socket,
168  to,
169  dscp,
170  ecn);
171  Simulator::Run();
172 }
173 
174 void
176 {
177  // Create topology
178 
180  internet.SetIpv6StackInstall(false);
181 
182  // Receiver Node
183  Ptr<Node> rxNode = CreateObject<Node>();
184  internet.Install(rxNode);
185 
186  Ptr<SimpleNetDevice> rxDev1;
187  Ptr<SimpleNetDevice> rxDev2;
188  { // first interface
189  rxDev1 = CreateObject<SimpleNetDevice>();
190  rxDev1->SetAddress(Mac48Address::ConvertFrom(Mac48Address::Allocate()));
191  rxNode->AddDevice(rxDev1);
192  Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4>();
193  uint32_t netdev_idx = ipv4->AddInterface(rxDev1);
194  Ipv4InterfaceAddress ipv4Addr =
195  Ipv4InterfaceAddress(Ipv4Address("10.0.0.1"), Ipv4Mask(0xffff0000U));
196  ipv4->AddAddress(netdev_idx, ipv4Addr);
197  ipv4->SetUp(netdev_idx);
198  }
199 
200  // Sender Node
201  Ptr<Node> txNode = CreateObject<Node>();
202  internet.Install(txNode);
203  Ptr<SimpleNetDevice> txDev1;
204  {
205  txDev1 = CreateObject<SimpleNetDevice>();
206  txDev1->SetAddress(Mac48Address::ConvertFrom(Mac48Address::Allocate()));
207  txNode->AddDevice(txDev1);
208  Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4>();
209  uint32_t netdev_idx = ipv4->AddInterface(txDev1);
210  Ipv4InterfaceAddress ipv4Addr =
211  Ipv4InterfaceAddress(Ipv4Address("10.0.0.2"), Ipv4Mask(0xffff0000U));
212  ipv4->AddAddress(netdev_idx, ipv4Addr);
213  ipv4->SetUp(netdev_idx);
214  }
215 
216  // link the two nodes
217  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel>();
218  rxDev1->SetChannel(channel1);
219  txDev1->SetChannel(channel1);
220 
221  // Create the IPv4 Raw sockets
222  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory>();
223  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
224  NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("0.0.0.0"), 0)),
225  0,
226  "trivial");
227  rxSocket->SetRecvCallback(MakeCallback(&Ipv4HeaderTest::ReceivePkt, this));
228 
229  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory>();
230  Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
231 
232  // ------ Now the tests ------------
233 
234  // Dscp Tests
235  std::cout << "Dscp Test\n";
236 
237  std::vector<Ipv4Header::DscpType> vDscpTypes;
238  vDscpTypes.push_back(Ipv4Header::DscpDefault);
239  vDscpTypes.push_back(Ipv4Header::DSCP_CS1);
240  vDscpTypes.push_back(Ipv4Header::DSCP_AF11);
241  vDscpTypes.push_back(Ipv4Header::DSCP_AF12);
242  vDscpTypes.push_back(Ipv4Header::DSCP_AF13);
243  vDscpTypes.push_back(Ipv4Header::DSCP_CS2);
244  vDscpTypes.push_back(Ipv4Header::DSCP_AF21);
245  vDscpTypes.push_back(Ipv4Header::DSCP_AF22);
246  vDscpTypes.push_back(Ipv4Header::DSCP_AF23);
247  vDscpTypes.push_back(Ipv4Header::DSCP_CS3);
248  vDscpTypes.push_back(Ipv4Header::DSCP_AF31);
249  vDscpTypes.push_back(Ipv4Header::DSCP_AF32);
250  vDscpTypes.push_back(Ipv4Header::DSCP_AF33);
251  vDscpTypes.push_back(Ipv4Header::DSCP_CS4);
252  vDscpTypes.push_back(Ipv4Header::DSCP_AF41);
253  vDscpTypes.push_back(Ipv4Header::DSCP_AF42);
254  vDscpTypes.push_back(Ipv4Header::DSCP_AF43);
255  vDscpTypes.push_back(Ipv4Header::DSCP_CS5);
256  vDscpTypes.push_back(Ipv4Header::DSCP_EF);
257  vDscpTypes.push_back(Ipv4Header::DSCP_CS6);
258  vDscpTypes.push_back(Ipv4Header::DSCP_CS7);
259 
260  for (uint32_t i = 0; i < vDscpTypes.size(); i++)
261  {
262  SendData_IpHdr_Dscp(txSocket, "10.0.0.1", vDscpTypes[i], Ipv4Header::ECN_ECT1);
263  NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 143, "recv(hdrincl): 10.0.0.1");
264  NS_TEST_EXPECT_MSG_EQ(m_receivedHeader.GetDscp(), vDscpTypes[i], "recv(hdrincl): 10.0.0.1");
265  m_receivedHeader.Print(std::cout);
266  std::cout << std::endl;
268  m_receivedPacket = nullptr;
269  }
270 
271  // Ecn tests
272  std::cout << "Ecn Test\n";
273  std::vector<Ipv4Header::EcnType> vEcnTypes;
274  vEcnTypes.push_back(Ipv4Header::ECN_NotECT);
275  vEcnTypes.push_back(Ipv4Header::ECN_ECT1);
276  vEcnTypes.push_back(Ipv4Header::ECN_ECT0);
277  vEcnTypes.push_back(Ipv4Header::ECN_CE);
278 
279  for (uint32_t i = 0; i < vEcnTypes.size(); i++)
280  {
281  SendData_IpHdr_Dscp(txSocket, "10.0.0.1", Ipv4Header::DscpDefault, vEcnTypes[i]);
282  NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 143, "recv(hdrincl): 10.0.0.1");
283  NS_TEST_EXPECT_MSG_EQ(m_receivedHeader.GetEcn(), vEcnTypes[i], "recv(hdrincl): 10.0.0.1");
284  m_receivedHeader.Print(std::cout);
285  std::cout << std::endl;
287  m_receivedPacket = nullptr;
288  }
289 
290  Simulator::Destroy();
291 }
292 
299 {
300  public:
302  : TestSuite("ipv4-header", UNIT)
303  {
304  AddTestCase(new Ipv4HeaderTest, TestCase::QUICK);
305  }
306 };
307 
#define max(a, b)
Definition: 80211b.c:42
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:101
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
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:80
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
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:204
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:861
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:301
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
void ReceivePacket(Ptr< Socket > socket)
#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:1326
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:704