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/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 #include "ns3/traffic-control-layer.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 static void
54 {
55  //ARP
56  Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> ();
57  node->AggregateObject (arp);
58  //IPV4
59  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
60  //Routing for Ipv4
61  Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> ();
62  ipv4->SetRoutingProtocol (ipv4Routing);
63  Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
64  ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
65  node->AggregateObject (ipv4);
66  //ICMP
67  Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
68  node->AggregateObject (icmp);
69  // //Ipv4Raw
70  // Ptr<Ipv4UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
71  // node->AggregateObject(udp);
72  // Traffic Control
73  Ptr<TrafficControlLayer> tc = CreateObject<TrafficControlLayer> ();
74  node->AggregateObject (tc);
75 }
76 
77 
78 class Ipv4HeaderTest : public TestCase
79 {
82  void DoSendData_IpHdr_Dscp (Ptr<Socket> socket, std::string to, Ipv4Header::DscpType dscp,Ipv4Header::EcnType);
83  void SendData_IpHdr_Dscp (Ptr<Socket> socket, std::string to, Ipv4Header::DscpType dscp, Ipv4Header::EcnType);
84 
85 public:
86  virtual void DoRun (void);
87  Ipv4HeaderTest ();
88 
89  void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
90  void ReceivePkt (Ptr<Socket> socket);
91 };
92 
93 
95  : TestCase ("IPv4 Header Test")
96 {
97 }
98 
100 {
101  m_receivedPacket = packet;
102 }
103 
104 
106 {
107  uint32_t availableData;
108  availableData = socket->GetRxAvailable ();
109  m_receivedPacket = socket->Recv (2, MSG_PEEK);
112  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
113  m_receivedPacket->PeekHeader (m_receivedHeader);
114 }
115 
116 
117 
118 void
120 {
121  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 0);
122  socket->SetAttribute ("IpHeaderInclude", BooleanValue (true));
123  Ptr<Packet> p = Create<Packet> (123);
124  Ipv4Header ipHeader;
125  ipHeader.SetSource (Ipv4Address ("10.0.0.2"));
126  ipHeader.SetDestination (Ipv4Address (to.c_str ()));
127  ipHeader.SetProtocol (0);
128  ipHeader.SetPayloadSize (p->GetSize ());
129  ipHeader.SetTtl (255);
130  ipHeader.SetDscp (dscp);
131  ipHeader.SetEcn (ecn);
132  p->AddHeader (ipHeader);
133 
134  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (p, 0, realTo),
135  143, to);
136  socket->SetAttribute ("IpHeaderInclude", BooleanValue (false));
137 }
138 
139 void
141 {
142  m_receivedPacket = Create<Packet> ();
143  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
144  &Ipv4HeaderTest::DoSendData_IpHdr_Dscp, this, socket, to, dscp, ecn);
145  Simulator::Run ();
146 }
147 
148 void
150 {
151  // Create topology
152 
153  // Receiver Node
154  Ptr<Node> rxNode = CreateObject<Node> ();
155  AddInternetStack (rxNode);
156  Ptr<SimpleNetDevice> rxDev1, rxDev2;
157  { // first interface
158  rxDev1 = CreateObject<SimpleNetDevice> ();
159  rxDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
160  rxNode->AddDevice (rxDev1);
161  Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
162  uint32_t netdev_idx = ipv4->AddInterface (rxDev1);
163  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.1"), Ipv4Mask (0xffff0000U));
164  ipv4->AddAddress (netdev_idx, ipv4Addr);
165  ipv4->SetUp (netdev_idx);
166  }
167 
168 
169  // Sender Node
170  Ptr<Node> txNode = CreateObject<Node> ();
171  AddInternetStack (txNode);
172  Ptr<SimpleNetDevice> txDev1;
173  {
174  txDev1 = CreateObject<SimpleNetDevice> ();
175  txDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
176  txNode->AddDevice (txDev1);
177  Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
178  uint32_t netdev_idx = ipv4->AddInterface (txDev1);
179  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.2"), Ipv4Mask (0xffff0000U));
180  ipv4->AddAddress (netdev_idx, ipv4Addr);
181  ipv4->SetUp (netdev_idx);
182  }
183 
184  // link the two nodes
185  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
186  rxDev1->SetChannel (channel1);
187  txDev1->SetChannel (channel1);
188 
189 
190  // Create the IPv4 Raw sockets
191  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory> ();
192  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
193  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
194  rxSocket->SetRecvCallback (MakeCallback (&Ipv4HeaderTest::ReceivePkt, this));
195 
196 
197  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory> ();
198  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
199 
200  // ------ Now the tests ------------
201 
202  // Dscp Tests
203  std::cout << "Dscp Test\n";
204 
205  std::vector <Ipv4Header::DscpType> vDscpTypes;
206  vDscpTypes.push_back (Ipv4Header::DscpDefault);
207  vDscpTypes.push_back (Ipv4Header::DSCP_CS1);
208  vDscpTypes.push_back (Ipv4Header::DSCP_AF11);
209  vDscpTypes.push_back (Ipv4Header::DSCP_AF12);
210  vDscpTypes.push_back (Ipv4Header::DSCP_AF13);
211  vDscpTypes.push_back (Ipv4Header::DSCP_CS2);
212  vDscpTypes.push_back (Ipv4Header::DSCP_AF21);
213  vDscpTypes.push_back (Ipv4Header::DSCP_AF22);
214  vDscpTypes.push_back (Ipv4Header::DSCP_AF23);
215  vDscpTypes.push_back (Ipv4Header::DSCP_CS3);
216  vDscpTypes.push_back (Ipv4Header::DSCP_AF31);
217  vDscpTypes.push_back (Ipv4Header::DSCP_AF32);
218  vDscpTypes.push_back (Ipv4Header::DSCP_AF33);
219  vDscpTypes.push_back (Ipv4Header::DSCP_CS4);
220  vDscpTypes.push_back (Ipv4Header::DSCP_AF41);
221  vDscpTypes.push_back (Ipv4Header::DSCP_AF42);
222  vDscpTypes.push_back (Ipv4Header::DSCP_AF43);
223  vDscpTypes.push_back (Ipv4Header::DSCP_CS5);
224  vDscpTypes.push_back (Ipv4Header::DSCP_EF);
225  vDscpTypes.push_back (Ipv4Header::DSCP_CS6);
226  vDscpTypes.push_back (Ipv4Header::DSCP_CS7);
227 
228  for (uint32_t i = 0; i < vDscpTypes.size (); i++)
229  {
230  SendData_IpHdr_Dscp (txSocket, "10.0.0.1", vDscpTypes [i], Ipv4Header::ECN_ECT1);
231  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv(hdrincl): 10.0.0.1");
232  NS_TEST_EXPECT_MSG_EQ (m_receivedHeader.GetDscp (), vDscpTypes [i], "recv(hdrincl): 10.0.0.1");
233  m_receivedHeader.Print (std::cout);
234  std::cout << std::endl;
236  m_receivedPacket = 0;
237  }
238 
239  // Ecn tests
240  std::cout << "Ecn Test\n";
241  std::vector <Ipv4Header::EcnType> vEcnTypes;
242  vEcnTypes.push_back (Ipv4Header::ECN_NotECT);
243  vEcnTypes.push_back (Ipv4Header::ECN_ECT1);
244  vEcnTypes.push_back (Ipv4Header::ECN_ECT0);
245  vEcnTypes.push_back (Ipv4Header::ECN_CE);
246 
247  for (uint32_t i = 0; i < vEcnTypes.size (); i++)
248  {
249  SendData_IpHdr_Dscp (txSocket, "10.0.0.1", Ipv4Header::DscpDefault, vEcnTypes [i]);
250  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv(hdrincl): 10.0.0.1");
251  NS_TEST_EXPECT_MSG_EQ (m_receivedHeader.GetEcn (), vEcnTypes [i], "recv(hdrincl): 10.0.0.1");
252  m_receivedHeader.Print (std::cout);
253  std::cout << std::endl;
255  m_receivedPacket = 0;
256  }
257 
258 
259 
260  Simulator::Destroy ();
261 }
262 //-----------------------------------------------------------------------------
264 {
265 public:
266  Ipv4HeaderTestSuite () : TestSuite ("ipv4-header", UNIT)
267  {
268  AddTestCase (new Ipv4HeaderTest, TestCase::QUICK);
269  }
void SetSource(Ipv4Address source)
Definition: ipv4-header.cc:285
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
an Inet address class
AttributeValue implementation for Boolean.
Definition: boolean.h:34
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:462
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
A suite of tests to run.
Definition: test.h:1333
void ReceivePkt(Ptr< Socket > socket)
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
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
#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:278
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:792
virtual Ptr< Socket > CreateSocket(void)=0
Ptr< Packet > m_receivedPacket
encapsulates test code
Definition: test.h:1147
void SendData_IpHdr_Dscp(Ptr< Socket > socket, std::string to, Ipv4Header::DscpType dscp, Ipv4Header::EcnType)
This test suite implements a Unit Test.
Definition: test.h:1343
static void AddInternetStack(Ptr< Node > node)
API to create RAW socket instances.
a polymophic address class
Definition: address.h:90
Packet header for IPv4.
Definition: ipv4-header.h:33
Ipv4Header m_receivedHeader
#define max(a, b)
Definition: 80211b.c:45
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
virtual void SetUp(uint32_t interface)=0
DscpType GetDscp(void) const
Definition: ipv4-header.cc:105
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
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.
EcnType
ECN Type defined in RFC 3168
Definition: ipv4-header.h:109
virtual void Print(std::ostream &os) const
Definition: ipv4-header.cc:335
DscpType
DiffServ Code Points Code Points defined in Assured Forwarding (AF) RFC 2597 Expedited Forwarding (EF...
Definition: ipv4-header.h:67
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
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
uint32_t GetId(void) const
Definition: node.cc:107
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.
void DoSendData_IpHdr_Dscp(Ptr< Socket > socket, std::string to, Ipv4Header::DscpType dscp, Ipv4Header::EcnType)
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:895
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:349
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.
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:191
Ipv4HeaderTestSuite g_ipv4HeaderTestSuite
EcnType GetEcn(void) const
Definition: ipv4-header.cc:167
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:257
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.
void SetRoutingProtocol(Ptr< Ipv4RoutingProtocol > routingProtocol)
Register a new routing protocol to be used by this Ipv4 stack.