PageRenderTime 94ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/internet/test/ipv4-header-test.cc

https://gitlab.com/msepahkar/location_in_wireless_ndn
C++ | 266 lines | 192 code | 38 blank | 36 comment | 4 complexity | 33fe4324e22ba35f3756c54b76763104 MD5 | raw 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. #include "ns3/test.h"
  22. #include "ns3/socket-factory.h"
  23. #include "ns3/ipv4-raw-socket-factory.h"
  24. #include "ns3/simulator.h"
  25. #include "ns3/simple-channel.h"
  26. #include "ns3/simple-net-device.h"
  27. #include "ns3/drop-tail-queue.h"
  28. #include "ns3/socket.h"
  29. #include "ns3/log.h"
  30. #include "ns3/node.h"
  31. #include "ns3/inet-socket-address.h"
  32. #include "ns3/boolean.h"
  33. #include "ns3/arp-l3-protocol.h"
  34. #include "ns3/ipv4-l3-protocol.h"
  35. #include "ns3/icmpv4-l4-protocol.h"
  36. #include "ns3/ipv4-list-routing.h"
  37. #include "ns3/ipv4-static-routing.h"
  38. #include <string>
  39. #include <sstream>
  40. #include <limits>
  41. #include <netinet/in.h>
  42. #include <sys/socket.h>
  43. #include <sys/types.h>
  44. using namespace ns3;
  45. static void
  46. AddInternetStack (Ptr<Node> node)
  47. {
  48. //ARP
  49. Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> ();
  50. node->AggregateObject (arp);
  51. //IPV4
  52. Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
  53. //Routing for Ipv4
  54. Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> ();
  55. ipv4->SetRoutingProtocol (ipv4Routing);
  56. Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
  57. ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
  58. node->AggregateObject (ipv4);
  59. //ICMP
  60. Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
  61. node->AggregateObject (icmp);
  62. // //Ipv4Raw
  63. // Ptr<Ipv4UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
  64. // node->AggregateObject(udp);
  65. }
  66. class Ipv4HeaderTest : public TestCase
  67. {
  68. Ptr<Packet> m_receivedPacket;
  69. Ipv4Header m_receivedHeader;
  70. void DoSendData_IpHdr_Dscp (Ptr<Socket> socket, std::string to, Ipv4Header::DscpType dscp,Ipv4Header::EcnType);
  71. void SendData_IpHdr_Dscp (Ptr<Socket> socket, std::string to, Ipv4Header::DscpType dscp, Ipv4Header::EcnType);
  72. public:
  73. virtual void DoRun (void);
  74. Ipv4HeaderTest ();
  75. void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
  76. void ReceivePkt (Ptr<Socket> socket);
  77. };
  78. Ipv4HeaderTest::Ipv4HeaderTest ()
  79. : TestCase ("IPv4 Header Test")
  80. {
  81. }
  82. void Ipv4HeaderTest::ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from)
  83. {
  84. m_receivedPacket = packet;
  85. }
  86. void Ipv4HeaderTest::ReceivePkt (Ptr<Socket> socket)
  87. {
  88. uint32_t availableData;
  89. availableData = socket->GetRxAvailable ();
  90. m_receivedPacket = socket->Recv (2, MSG_PEEK);
  91. NS_ASSERT (m_receivedPacket->GetSize () == 2);
  92. m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
  93. NS_ASSERT (availableData == m_receivedPacket->GetSize ());
  94. m_receivedPacket->PeekHeader (m_receivedHeader);
  95. }
  96. void
  97. Ipv4HeaderTest::DoSendData_IpHdr_Dscp (Ptr<Socket> socket, std::string to, Ipv4Header::DscpType dscp, Ipv4Header::EcnType ecn)
  98. {
  99. Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 0);
  100. socket->SetAttribute ("IpHeaderInclude", BooleanValue (true));
  101. Ptr<Packet> p = Create<Packet> (123);
  102. Ipv4Header ipHeader;
  103. ipHeader.SetSource (Ipv4Address ("10.0.0.2"));
  104. ipHeader.SetDestination (Ipv4Address (to.c_str ()));
  105. ipHeader.SetProtocol (0);
  106. ipHeader.SetPayloadSize (p->GetSize ());
  107. ipHeader.SetTtl (255);
  108. ipHeader.SetDscp (dscp);
  109. ipHeader.SetEcn (ecn);
  110. p->AddHeader (ipHeader);
  111. NS_TEST_EXPECT_MSG_EQ (socket->SendTo (p, 0, realTo),
  112. 143, to);
  113. socket->SetAttribute ("IpHeaderInclude", BooleanValue (false));
  114. }
  115. void
  116. Ipv4HeaderTest::SendData_IpHdr_Dscp (Ptr<Socket> socket, std::string to, Ipv4Header::DscpType dscp, Ipv4Header::EcnType ecn)
  117. {
  118. m_receivedPacket = Create<Packet> ();
  119. Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
  120. &Ipv4HeaderTest::DoSendData_IpHdr_Dscp, this, socket, to, dscp, ecn);
  121. Simulator::Run ();
  122. }
  123. void
  124. Ipv4HeaderTest::DoRun (void)
  125. {
  126. // Create topology
  127. // Receiver Node
  128. Ptr<Node> rxNode = CreateObject<Node> ();
  129. AddInternetStack (rxNode);
  130. Ptr<SimpleNetDevice> rxDev1, rxDev2;
  131. { // first interface
  132. rxDev1 = CreateObject<SimpleNetDevice> ();
  133. rxDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
  134. rxNode->AddDevice (rxDev1);
  135. Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
  136. uint32_t netdev_idx = ipv4->AddInterface (rxDev1);
  137. Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.1"), Ipv4Mask (0xffff0000U));
  138. ipv4->AddAddress (netdev_idx, ipv4Addr);
  139. ipv4->SetUp (netdev_idx);
  140. }
  141. // Sender Node
  142. Ptr<Node> txNode = CreateObject<Node> ();
  143. AddInternetStack (txNode);
  144. Ptr<SimpleNetDevice> txDev1;
  145. {
  146. txDev1 = CreateObject<SimpleNetDevice> ();
  147. txDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
  148. txNode->AddDevice (txDev1);
  149. Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
  150. uint32_t netdev_idx = ipv4->AddInterface (txDev1);
  151. Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.2"), Ipv4Mask (0xffff0000U));
  152. ipv4->AddAddress (netdev_idx, ipv4Addr);
  153. ipv4->SetUp (netdev_idx);
  154. }
  155. // link the two nodes
  156. Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
  157. rxDev1->SetChannel (channel1);
  158. txDev1->SetChannel (channel1);
  159. // Create the IPv4 Raw sockets
  160. Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory> ();
  161. Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
  162. NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
  163. rxSocket->SetRecvCallback (MakeCallback (&Ipv4HeaderTest::ReceivePkt, this));
  164. Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory> ();
  165. Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
  166. // ------ Now the tests ------------
  167. // Dscp Tests
  168. std::cout << "Dscp Test\n";
  169. std::vector <Ipv4Header::DscpType> vDscpTypes;
  170. vDscpTypes.push_back (Ipv4Header::DscpDefault);
  171. vDscpTypes.push_back (Ipv4Header::DSCP_CS1);
  172. vDscpTypes.push_back (Ipv4Header::DSCP_AF11);
  173. vDscpTypes.push_back (Ipv4Header::DSCP_AF12);
  174. vDscpTypes.push_back (Ipv4Header::DSCP_AF13);
  175. vDscpTypes.push_back (Ipv4Header::DSCP_CS2);
  176. vDscpTypes.push_back (Ipv4Header::DSCP_AF21);
  177. vDscpTypes.push_back (Ipv4Header::DSCP_AF22);
  178. vDscpTypes.push_back (Ipv4Header::DSCP_AF23);
  179. vDscpTypes.push_back (Ipv4Header::DSCP_CS3);
  180. vDscpTypes.push_back (Ipv4Header::DSCP_AF31);
  181. vDscpTypes.push_back (Ipv4Header::DSCP_AF32);
  182. vDscpTypes.push_back (Ipv4Header::DSCP_AF33);
  183. vDscpTypes.push_back (Ipv4Header::DSCP_CS4);
  184. vDscpTypes.push_back (Ipv4Header::DSCP_AF41);
  185. vDscpTypes.push_back (Ipv4Header::DSCP_AF42);
  186. vDscpTypes.push_back (Ipv4Header::DSCP_AF43);
  187. vDscpTypes.push_back (Ipv4Header::DSCP_CS5);
  188. vDscpTypes.push_back (Ipv4Header::DSCP_EF);
  189. vDscpTypes.push_back (Ipv4Header::DSCP_CS6);
  190. vDscpTypes.push_back (Ipv4Header::DSCP_CS7);
  191. for (uint32_t i = 0; i < vDscpTypes.size (); i++)
  192. {
  193. SendData_IpHdr_Dscp (txSocket, "10.0.0.1", vDscpTypes [i], Ipv4Header::ECN_ECT1);
  194. NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv(hdrincl): 10.0.0.1");
  195. NS_TEST_EXPECT_MSG_EQ (m_receivedHeader.GetDscp (), vDscpTypes [i], "recv(hdrincl): 10.0.0.1");
  196. m_receivedHeader.Print (std::cout);
  197. std::cout << std::endl;
  198. m_receivedPacket->RemoveAllByteTags ();
  199. m_receivedPacket = 0;
  200. }
  201. // Ecn tests
  202. std::cout << "Ecn Test\n";
  203. std::vector <Ipv4Header::EcnType> vEcnTypes;
  204. vEcnTypes.push_back (Ipv4Header::ECN_NotECT);
  205. vEcnTypes.push_back (Ipv4Header::ECN_ECT1);
  206. vEcnTypes.push_back (Ipv4Header::ECN_ECT0);
  207. vEcnTypes.push_back (Ipv4Header::ECN_CE);
  208. for (uint32_t i = 0; i < vEcnTypes.size (); i++)
  209. {
  210. SendData_IpHdr_Dscp (txSocket, "10.0.0.1", Ipv4Header::DscpDefault, vEcnTypes [i]);
  211. NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv(hdrincl): 10.0.0.1");
  212. NS_TEST_EXPECT_MSG_EQ (m_receivedHeader.GetEcn (), vEcnTypes [i], "recv(hdrincl): 10.0.0.1");
  213. m_receivedHeader.Print (std::cout);
  214. std::cout << std::endl;
  215. m_receivedPacket->RemoveAllByteTags ();
  216. m_receivedPacket = 0;
  217. }
  218. Simulator::Destroy ();
  219. }
  220. //-----------------------------------------------------------------------------
  221. class Ipv4HeaderTestSuite : public TestSuite
  222. {
  223. public:
  224. Ipv4HeaderTestSuite () : TestSuite ("ipv4-header", UNIT)
  225. {
  226. AddTestCase (new Ipv4HeaderTest, TestCase::QUICK);
  227. }
  228. } g_ipv4HeaderTestSuite;