A Discrete-Event Network Simulator
API
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/socket.h"
29 
30 #include "ns3/log.h"
31 #include "ns3/node.h"
32 #include "ns3/inet-socket-address.h"
33 #include "ns3/boolean.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/ipv4-list-routing.h"
39 #include "ns3/ipv4-static-routing.h"
40 #include "ns3/traffic-control-layer.h"
41 #include "ns3/internet-stack-helper.h"
42 
43 #include <string>
44 #include <sstream>
45 #include <limits>
46 #include <netinet/in.h>
47 #include <sys/socket.h>
48 #include <sys/types.h>
49 
50 using namespace ns3;
51 
52 
59 class Ipv4HeaderTest : public TestCase
60 {
63 
71  void DoSendData_IpHdr_Dscp (Ptr<Socket> socket, std::string to, Ipv4Header::DscpType dscp, Ipv4Header::EcnType ecn);
72 
80  void SendData_IpHdr_Dscp (Ptr<Socket> socket, std::string to, Ipv4Header::DscpType dscp, Ipv4Header::EcnType ecn);
81 
82 public:
83  virtual void DoRun (void);
84  Ipv4HeaderTest ();
85 
92  void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
97  void ReceivePkt (Ptr<Socket> socket);
98 };
99 
100 
102  : TestCase ("IPv4 Header Test")
103 {
104 }
105 
107 {
108  m_receivedPacket = packet;
109 }
110 
112 {
113  uint32_t availableData;
114  availableData = socket->GetRxAvailable ();
115  m_receivedPacket = socket->Recv (2, MSG_PEEK);
118  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
120 }
121 
122 void
124 {
125  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 0);
126  socket->SetAttribute ("IpHeaderInclude", BooleanValue (true));
127  Ptr<Packet> p = Create<Packet> (123);
128  Ipv4Header ipHeader;
129  ipHeader.SetSource (Ipv4Address ("10.0.0.2"));
130  ipHeader.SetDestination (Ipv4Address (to.c_str ()));
131  ipHeader.SetProtocol (0);
132  ipHeader.SetPayloadSize (p->GetSize ());
133  ipHeader.SetTtl (255);
134  ipHeader.SetDscp (dscp);
135  ipHeader.SetEcn (ecn);
136  p->AddHeader (ipHeader);
137 
138  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (p, 0, realTo),
139  143, to);
140  socket->SetAttribute ("IpHeaderInclude", BooleanValue (false));
141 }
142 
143 void
145 {
146  m_receivedPacket = Create<Packet> ();
147  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
148  &Ipv4HeaderTest::DoSendData_IpHdr_Dscp, this, socket, to, dscp, ecn);
149  Simulator::Run ();
150 }
151 
152 void
154 {
155  // Create topology
156 
157  InternetStackHelper internet;
158  internet.SetIpv6StackInstall (false);
159 
160  // Receiver Node
161  Ptr<Node> rxNode = CreateObject<Node> ();
162  internet.Install (rxNode);
163 
164  Ptr<SimpleNetDevice> rxDev1, rxDev2;
165  { // first interface
166  rxDev1 = CreateObject<SimpleNetDevice> ();
167  rxDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
168  rxNode->AddDevice (rxDev1);
169  Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
170  uint32_t netdev_idx = ipv4->AddInterface (rxDev1);
171  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.1"), Ipv4Mask (0xffff0000U));
172  ipv4->AddAddress (netdev_idx, ipv4Addr);
173  ipv4->SetUp (netdev_idx);
174  }
175 
176  // Sender Node
177  Ptr<Node> txNode = CreateObject<Node> ();
178  internet.Install (txNode);
179  Ptr<SimpleNetDevice> txDev1;
180  {
181  txDev1 = CreateObject<SimpleNetDevice> ();
182  txDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
183  txNode->AddDevice (txDev1);
184  Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
185  uint32_t netdev_idx = ipv4->AddInterface (txDev1);
186  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.2"), Ipv4Mask (0xffff0000U));
187  ipv4->AddAddress (netdev_idx, ipv4Addr);
188  ipv4->SetUp (netdev_idx);
189  }
190 
191  // link the two nodes
192  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
193  rxDev1->SetChannel (channel1);
194  txDev1->SetChannel (channel1);
195 
196 
197  // Create the IPv4 Raw sockets
198  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory> ();
199  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
200  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
201  rxSocket->SetRecvCallback (MakeCallback (&Ipv4HeaderTest::ReceivePkt, this));
202 
203 
204  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory> ();
205  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
206 
207  // ------ Now the tests ------------
208 
209  // Dscp Tests
210  std::cout << "Dscp Test\n";
211 
212  std::vector <Ipv4Header::DscpType> vDscpTypes;
213  vDscpTypes.push_back (Ipv4Header::DscpDefault);
214  vDscpTypes.push_back (Ipv4Header::DSCP_CS1);
215  vDscpTypes.push_back (Ipv4Header::DSCP_AF11);
216  vDscpTypes.push_back (Ipv4Header::DSCP_AF12);
217  vDscpTypes.push_back (Ipv4Header::DSCP_AF13);
218  vDscpTypes.push_back (Ipv4Header::DSCP_CS2);
219  vDscpTypes.push_back (Ipv4Header::DSCP_AF21);
220  vDscpTypes.push_back (Ipv4Header::DSCP_AF22);
221  vDscpTypes.push_back (Ipv4Header::DSCP_AF23);
222  vDscpTypes.push_back (Ipv4Header::DSCP_CS3);
223  vDscpTypes.push_back (Ipv4Header::DSCP_AF31);
224  vDscpTypes.push_back (Ipv4Header::DSCP_AF32);
225  vDscpTypes.push_back (Ipv4Header::DSCP_AF33);
226  vDscpTypes.push_back (Ipv4Header::DSCP_CS4);
227  vDscpTypes.push_back (Ipv4Header::DSCP_AF41);
228  vDscpTypes.push_back (Ipv4Header::DSCP_AF42);
229  vDscpTypes.push_back (Ipv4Header::DSCP_AF43);
230  vDscpTypes.push_back (Ipv4Header::DSCP_CS5);
231  vDscpTypes.push_back (Ipv4Header::DSCP_EF);
232  vDscpTypes.push_back (Ipv4Header::DSCP_CS6);
233  vDscpTypes.push_back (Ipv4Header::DSCP_CS7);
234 
235  for (uint32_t i = 0; i < vDscpTypes.size (); i++)
236  {
237  SendData_IpHdr_Dscp (txSocket, "10.0.0.1", vDscpTypes [i], Ipv4Header::ECN_ECT1);
238  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv(hdrincl): 10.0.0.1");
239  NS_TEST_EXPECT_MSG_EQ (m_receivedHeader.GetDscp (), vDscpTypes [i], "recv(hdrincl): 10.0.0.1");
240  m_receivedHeader.Print (std::cout);
241  std::cout << std::endl;
243  m_receivedPacket = 0;
244  }
245 
246  // Ecn tests
247  std::cout << "Ecn Test\n";
248  std::vector <Ipv4Header::EcnType> vEcnTypes;
249  vEcnTypes.push_back (Ipv4Header::ECN_NotECT);
250  vEcnTypes.push_back (Ipv4Header::ECN_ECT1);
251  vEcnTypes.push_back (Ipv4Header::ECN_ECT0);
252  vEcnTypes.push_back (Ipv4Header::ECN_CE);
253 
254  for (uint32_t i = 0; i < vEcnTypes.size (); i++)
255  {
256  SendData_IpHdr_Dscp (txSocket, "10.0.0.1", Ipv4Header::DscpDefault, vEcnTypes [i]);
257  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv(hdrincl): 10.0.0.1");
258  NS_TEST_EXPECT_MSG_EQ (m_receivedHeader.GetEcn (), vEcnTypes [i], "recv(hdrincl): 10.0.0.1");
259  m_receivedHeader.Print (std::cout);
260  std::cout << std::endl;
262  m_receivedPacket = 0;
263  }
264 
265  Simulator::Destroy ();
266 }
267 
275 {
276 public:
277  Ipv4HeaderTestSuite () : TestSuite ("ipv4-header", UNIT)
278  {
279  AddTestCase (new Ipv4HeaderTest, TestCase::QUICK);
280  }
281 };
282 
void SetSource(Ipv4Address source)
Definition: ipv4-header.cc:285
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.
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
an Inet address class
AttributeValue implementation for Boolean.
Definition: boolean.h:36
uint32_t GetId(void) const
Definition: node.cc:107
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
DscpType
DiffServ codepoints.
Definition: ipv4-header.h:70
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
A suite of tests to run.
Definition: test.h:1342
void ReceivePkt(Ptr< Socket > socket)
Receives a packet.
void ReceivePacket(Ptr< Socket > socket)
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
aggregate IP/TCP/UDP functionality to existing Nodes.
#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:285
virtual Ptr< Socket > CreateSocket(void)=0
Ptr< Packet > m_receivedPacket
Received packet.
encapsulates test code
Definition: test.h:1155
API to create RAW socket instances.
static Ipv4HeaderTestSuite g_ipv4HeaderTestSuite
Static variable for test initialization.
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.
a polymophic address class
Definition: address.h:90
Packet header for IPv4.
Definition: ipv4-header.h:33
IPv4 Header TestSuite.
Ipv4Header m_receivedHeader
Received header.
IPv4 Header Test.
#define max(a, b)
Definition: 80211b.c:43
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
virtual void SetUp(uint32_t interface)=0
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:290
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
DscpType GetDscp(void) const
Definition: ipv4-header.cc:105
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Receives a packet.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
a class to store IPv4 address information on an interface
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
virtual void SetAddress(Address address)
Set the address of this interface.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1062
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:371
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
This test suite implements a Unit Test.
Definition: test.h:1351
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
EcnType GetEcn(void) const
Definition: ipv4-header.cc:167
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
EcnType
ECN Type defined in RFC 3168
Definition: ipv4-header.h:112
void SetIpv6StackInstall(bool enable)
Enable/disable IPv6 stack install.
virtual void Print(std::ostream &os) const
Definition: ipv4-header.cc:335
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
virtual void DoRun(void)
Implementation to actually run this TestCase.