A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ipv4-header-test.cc
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 
42 #include <string>
43 #include <sstream>
44 #include <limits>
45 #include <netinet/in.h>
46 #include <sys/socket.h>
47 #include <sys/types.h>
48 
49 using namespace ns3;
50 
51 static void
52 AddInternetStack (Ptr<Node> node)
53 {
54  //ARP
55  Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> ();
56  node->AggregateObject (arp);
57  //IPV4
58  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
59  //Routing for Ipv4
60  Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> ();
61  ipv4->SetRoutingProtocol (ipv4Routing);
62  Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
63  ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
64  node->AggregateObject (ipv4);
65  //ICMP
66  Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
67  node->AggregateObject (icmp);
68  // //Ipv4Raw
69  // Ptr<Ipv4UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
70  // node->AggregateObject(udp);
71 }
72 
73 
74 class Ipv4HeaderTest : public TestCase
75 {
76  Ptr<Packet> m_receivedPacket;
77  Ipv4Header m_receivedHeader;
78  void DoSendData_IpHdr_Dscp (Ptr<Socket> socket, std::string to, Ipv4Header::DscpType dscp,Ipv4Header::EcnType);
79  void SendData_IpHdr_Dscp (Ptr<Socket> socket, std::string to, Ipv4Header::DscpType dscp, Ipv4Header::EcnType);
80 
81 public:
82  virtual void DoRun (void);
83  Ipv4HeaderTest ();
84 
85  void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
86  void ReceivePkt (Ptr<Socket> socket);
87 };
88 
89 
90 Ipv4HeaderTest::Ipv4HeaderTest ()
91  : TestCase ("IPv4 Header Test")
92 {
93 }
94 
95 void Ipv4HeaderTest::ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from)
96 {
97  m_receivedPacket = packet;
98 }
99 
100 
101 void Ipv4HeaderTest::ReceivePkt (Ptr<Socket> socket)
102 {
103  uint32_t availableData;
104  availableData = socket->GetRxAvailable ();
105  m_receivedPacket = socket->Recv (2, MSG_PEEK);
106  NS_ASSERT (m_receivedPacket->GetSize () == 2);
107  m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
108  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
109  m_receivedPacket->PeekHeader (m_receivedHeader);
110 }
111 
112 
113 
114 void
115 Ipv4HeaderTest::DoSendData_IpHdr_Dscp (Ptr<Socket> socket, std::string to, Ipv4Header::DscpType dscp, Ipv4Header::EcnType ecn)
116 {
117  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 0);
118  socket->SetAttribute ("IpHeaderInclude", BooleanValue (true));
119  Ptr<Packet> p = Create<Packet> (123);
120  Ipv4Header ipHeader;
121  ipHeader.SetSource (Ipv4Address ("10.0.0.2"));
122  ipHeader.SetDestination (Ipv4Address (to.c_str ()));
123  ipHeader.SetProtocol (0);
124  ipHeader.SetPayloadSize (p->GetSize ());
125  ipHeader.SetTtl (255);
126  ipHeader.SetDscp (dscp);
127  ipHeader.SetEcn (ecn);
128  p->AddHeader (ipHeader);
129 
130  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (p, 0, realTo),
131  143, to);
132  socket->SetAttribute ("IpHeaderInclude", BooleanValue (false));
133 }
134 
135 void
136 Ipv4HeaderTest::SendData_IpHdr_Dscp (Ptr<Socket> socket, std::string to, Ipv4Header::DscpType dscp, Ipv4Header::EcnType ecn)
137 {
138  m_receivedPacket = Create<Packet> ();
139  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
140  &Ipv4HeaderTest::DoSendData_IpHdr_Dscp, this, socket, to, dscp, ecn);
141  Simulator::Run ();
142 }
143 
144 void
146 {
147  // Create topology
148 
149  // Receiver Node
150  Ptr<Node> rxNode = CreateObject<Node> ();
151  AddInternetStack (rxNode);
152  Ptr<SimpleNetDevice> rxDev1, rxDev2;
153  { // first interface
154  rxDev1 = CreateObject<SimpleNetDevice> ();
155  rxDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
156  rxNode->AddDevice (rxDev1);
157  Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
158  uint32_t netdev_idx = ipv4->AddInterface (rxDev1);
159  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.1"), Ipv4Mask (0xffff0000U));
160  ipv4->AddAddress (netdev_idx, ipv4Addr);
161  ipv4->SetUp (netdev_idx);
162  }
163 
164 
165  // Sender Node
166  Ptr<Node> txNode = CreateObject<Node> ();
167  AddInternetStack (txNode);
168  Ptr<SimpleNetDevice> txDev1;
169  {
170  txDev1 = CreateObject<SimpleNetDevice> ();
171  txDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
172  txNode->AddDevice (txDev1);
173  Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
174  uint32_t netdev_idx = ipv4->AddInterface (txDev1);
175  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.2"), Ipv4Mask (0xffff0000U));
176  ipv4->AddAddress (netdev_idx, ipv4Addr);
177  ipv4->SetUp (netdev_idx);
178  }
179 
180  // link the two nodes
181  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
182  rxDev1->SetChannel (channel1);
183  txDev1->SetChannel (channel1);
184 
185 
186  // Create the IPv4 Raw sockets
187  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory> ();
188  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
189  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
190  rxSocket->SetRecvCallback (MakeCallback (&Ipv4HeaderTest::ReceivePkt, this));
191 
192 
193  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory> ();
194  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
195 
196  // ------ Now the tests ------------
197 
198  // Dscp Tests
199  std::cout << "Dscp Test\n";
200 
201  std::vector <Ipv4Header::DscpType> vDscpTypes;
202  vDscpTypes.push_back (Ipv4Header::DscpDefault);
203  vDscpTypes.push_back (Ipv4Header::CS1);
204  vDscpTypes.push_back (Ipv4Header::AF11);
205  vDscpTypes.push_back (Ipv4Header::AF12);
206  vDscpTypes.push_back (Ipv4Header::AF13);
207  vDscpTypes.push_back (Ipv4Header::CS2);
208  vDscpTypes.push_back (Ipv4Header::AF21);
209  vDscpTypes.push_back (Ipv4Header::AF22);
210  vDscpTypes.push_back (Ipv4Header::AF23);
211  vDscpTypes.push_back (Ipv4Header::CS3);
212  vDscpTypes.push_back (Ipv4Header::AF31);
213  vDscpTypes.push_back (Ipv4Header::AF32);
214  vDscpTypes.push_back (Ipv4Header::AF33);
215  vDscpTypes.push_back (Ipv4Header::CS4);
216  vDscpTypes.push_back (Ipv4Header::AF41);
217  vDscpTypes.push_back (Ipv4Header::AF42);
218  vDscpTypes.push_back (Ipv4Header::AF43);
219  vDscpTypes.push_back (Ipv4Header::CS5);
220  vDscpTypes.push_back (Ipv4Header::EF);
221  vDscpTypes.push_back (Ipv4Header::CS6);
222  vDscpTypes.push_back (Ipv4Header::CS7);
223 
224  for (uint32_t i = 0; i < vDscpTypes.size (); i++)
225  {
226  SendData_IpHdr_Dscp (txSocket, "10.0.0.1", vDscpTypes [i], Ipv4Header::ECT1);
227  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv(hdrincl): 10.0.0.1");
228  NS_TEST_EXPECT_MSG_EQ (m_receivedHeader.GetDscp (), vDscpTypes [i], "recv(hdrincl): 10.0.0.1");
229  m_receivedHeader.Print (std::cout);
230  std::cout << std::endl;
231  m_receivedPacket->RemoveAllByteTags ();
232  m_receivedPacket = 0;
233  }
234 
235  // Ecn tests
236  std::cout << "Ecn Test\n";
237  std::vector <Ipv4Header::EcnType> vEcnTypes;
238  vEcnTypes.push_back (Ipv4Header::NotECT);
239  vEcnTypes.push_back (Ipv4Header::ECT1);
240  vEcnTypes.push_back (Ipv4Header::ECT0);
241  vEcnTypes.push_back (Ipv4Header::CE);
242 
243  for (uint32_t i = 0; i < vEcnTypes.size (); i++)
244  {
245  SendData_IpHdr_Dscp (txSocket, "10.0.0.1", Ipv4Header::DscpDefault, vEcnTypes [i]);
246  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv(hdrincl): 10.0.0.1");
247  NS_TEST_EXPECT_MSG_EQ (m_receivedHeader.GetEcn (), vEcnTypes [i], "recv(hdrincl): 10.0.0.1");
248  m_receivedHeader.Print (std::cout);
249  std::cout << std::endl;
250  m_receivedPacket->RemoveAllByteTags ();
251  m_receivedPacket = 0;
252  }
253 
254 
255 
256  Simulator::Destroy ();
257 }
258 //-----------------------------------------------------------------------------
260 {
261 public:
262  Ipv4HeaderTestSuite () : TestSuite ("ipv4-header", UNIT)
263  {
264  AddTestCase (new Ipv4HeaderTest, TestCase::QUICK);
265  }
266 } g_ipv4HeaderTestSuite;
void SetSource(Ipv4Address source)
Definition: ipv4-header.cc:284
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
an Inet address class
Hold a bool native type.
Definition: boolean.h:38
DscpType
DiffServ Code Points Code Points defined in Assured Forwarding (AF) RFC 2597 Expedited Forwarding (EF...
Definition: ipv4-header.h:64
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:210
A suite of tests to run.
Definition: test.h:962
#define NS_ASSERT(condition)
Definition: assert.h:64
uint32_t GetSize(void) const
Definition: packet.h:620
virtual Ptr< Socket > CreateSocket(void)=0
encapsulates test code
Definition: test.h:834
API to create RAW socket instances.
TestSuite(std::string name, Type type=UNIT)
Constuct a new test suite.
Definition: test.cc:354
a polymophic address class
Definition: address.h:86
Packet header for IPv4.
Definition: ipv4-header.h:31
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:502
void AggregateObject(Ptr< Object > other)
Definition: object.cc:242
Access to the Ipv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:75
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual void Print(std::ostream &os) const
Definition: ipv4-header.cc:333
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:119
uint32_t GetId(void) const
Definition: node.cc:105
a class to store IPv4 address information on an interface
virtual Ptr< Node > GetNode(void) const =0
virtual void SetAddress(Address address)
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 SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:160
Ptr< T > GetObject(void) const
Definition: object.h:332
EcnType GetEcn(void) const
Definition: ipv4-header.cc:167
void AddHeader(const Header &header)
Definition: packet.cc:270
EcnType
ECN Type defined in RFC 3168.
Definition: ipv4-header.h:105
virtual uint32_t GetRxAvailable(void) const =0
virtual void DoRun(void)
Implementation to actually run this test case.
void SetRoutingProtocol(Ptr< Ipv4RoutingProtocol > routingProtocol)
Register a new routing protocol to be used by this Ipv4 stack.