PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/s2016/ns3-routing/ns-3/src/internet-stack/ipv4-raw-test.cc

https://gitlab.com/pmaddi/cs433
C++ | 326 lines | 234 code | 48 blank | 44 comment | 4 complexity | eac98685939b5a24b7eef57751e07f77 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: Hajime Tazaki <tazaki@sfc.wide.ad.jp>
  19. */
  20. /**
  21. * This is the test code for ipv4-raw-socket-impl.cc.
  22. */
  23. #include "ns3/test.h"
  24. #include "ns3/socket-factory.h"
  25. #include "ns3/ipv4-raw-socket-factory.h"
  26. #include "ns3/simulator.h"
  27. #include "ns3/simple-channel.h"
  28. #include "ns3/simple-net-device.h"
  29. #include "ns3/drop-tail-queue.h"
  30. #include "ns3/socket.h"
  31. #include "ns3/log.h"
  32. #include "ns3/node.h"
  33. #include "ns3/inet-socket-address.h"
  34. #include "ns3/boolean.h"
  35. #include "arp-l3-protocol.h"
  36. #include "ipv4-l3-protocol.h"
  37. #include "icmpv4-l4-protocol.h"
  38. #include "ns3/ipv4-list-routing.h"
  39. #include "ns3/ipv4-static-routing.h"
  40. #include <string>
  41. #include <limits>
  42. #include <netinet/in.h>
  43. namespace ns3 {
  44. static void
  45. AddInternetStack (Ptr<Node> node)
  46. {
  47. //ARP
  48. Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> ();
  49. node->AggregateObject(arp);
  50. //IPV4
  51. Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
  52. //Routing for Ipv4
  53. Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> ();
  54. ipv4->SetRoutingProtocol (ipv4Routing);
  55. Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
  56. ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
  57. node->AggregateObject(ipv4);
  58. //ICMP
  59. Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
  60. node->AggregateObject(icmp);
  61. // //Ipv4Raw
  62. // Ptr<Ipv4UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
  63. // node->AggregateObject(udp);
  64. }
  65. class Ipv4RawSocketImplTest: public TestCase
  66. {
  67. Ptr<Packet> m_receivedPacket;
  68. Ptr<Packet> m_receivedPacket2;
  69. void DoSendData (Ptr<Socket> socket, std::string to);
  70. void SendData (Ptr<Socket> socket, std::string to);
  71. void DoSendData_IpHdr (Ptr<Socket> socket, std::string to);
  72. void SendData_IpHdr (Ptr<Socket> socket, std::string to);
  73. public:
  74. virtual bool DoRun (void);
  75. Ipv4RawSocketImplTest ();
  76. void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
  77. void ReceivePacket2 (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
  78. void ReceivePkt (Ptr<Socket> socket);
  79. void ReceivePkt2 (Ptr<Socket> socket);
  80. };
  81. Ipv4RawSocketImplTest::Ipv4RawSocketImplTest ()
  82. : TestCase ("IPv4 Raw socket implementation")
  83. {
  84. }
  85. void Ipv4RawSocketImplTest::ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from)
  86. {
  87. m_receivedPacket = packet;
  88. }
  89. void Ipv4RawSocketImplTest::ReceivePacket2 (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from)
  90. {
  91. m_receivedPacket2 = packet;
  92. }
  93. void Ipv4RawSocketImplTest::ReceivePkt (Ptr<Socket> socket)
  94. {
  95. uint32_t availableData;
  96. availableData = socket->GetRxAvailable ();
  97. m_receivedPacket = socket->Recv (2, MSG_PEEK);
  98. NS_ASSERT (m_receivedPacket->GetSize () == 2);
  99. m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max(), 0);
  100. NS_ASSERT (availableData == m_receivedPacket->GetSize ());
  101. }
  102. void Ipv4RawSocketImplTest::ReceivePkt2 (Ptr<Socket> socket)
  103. {
  104. uint32_t availableData;
  105. availableData = socket->GetRxAvailable ();
  106. m_receivedPacket2 = socket->Recv (2, MSG_PEEK);
  107. NS_ASSERT (m_receivedPacket2->GetSize () == 2);
  108. m_receivedPacket2 = socket->Recv (std::numeric_limits<uint32_t>::max(), 0);
  109. NS_ASSERT (availableData == m_receivedPacket2->GetSize ());
  110. }
  111. void
  112. Ipv4RawSocketImplTest::DoSendData (Ptr<Socket> socket, std::string to)
  113. {
  114. Address realTo = InetSocketAddress (Ipv4Address(to.c_str()), 0);
  115. NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
  116. 123, to);
  117. }
  118. void
  119. Ipv4RawSocketImplTest::SendData (Ptr<Socket> socket, std::string to)
  120. {
  121. m_receivedPacket = Create<Packet> ();
  122. m_receivedPacket2 = Create<Packet> ();
  123. Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
  124. &Ipv4RawSocketImplTest::DoSendData, this, socket, to);
  125. Simulator::Run ();
  126. }
  127. void
  128. Ipv4RawSocketImplTest::DoSendData_IpHdr (Ptr<Socket> socket, std::string to)
  129. {
  130. Address realTo = InetSocketAddress (Ipv4Address(to.c_str()), 0);
  131. socket->SetAttribute ("IpHeaderInclude", BooleanValue (true));
  132. Ptr<Packet> p = Create<Packet> (123);
  133. Ipv4Header ipHeader;
  134. ipHeader.SetSource (Ipv4Address ("10.0.0.2"));
  135. ipHeader.SetDestination (Ipv4Address (to.c_str ()));
  136. ipHeader.SetProtocol (0);
  137. ipHeader.SetPayloadSize (p->GetSize ());
  138. ipHeader.SetTtl (255);
  139. p->AddHeader (ipHeader);
  140. NS_TEST_EXPECT_MSG_EQ (socket->SendTo (p, 0, realTo),
  141. 143, to);
  142. socket->SetAttribute ("IpHeaderInclude", BooleanValue (false));
  143. }
  144. void
  145. Ipv4RawSocketImplTest::SendData_IpHdr (Ptr<Socket> socket, std::string to)
  146. {
  147. m_receivedPacket = Create<Packet> ();
  148. m_receivedPacket2 = Create<Packet> ();
  149. Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
  150. &Ipv4RawSocketImplTest::DoSendData_IpHdr, this, socket, to);
  151. Simulator::Run ();
  152. }
  153. bool
  154. Ipv4RawSocketImplTest::DoRun (void)
  155. {
  156. // Create topology
  157. // Receiver Node
  158. Ptr<Node> rxNode = CreateObject<Node> ();
  159. AddInternetStack (rxNode);
  160. Ptr<SimpleNetDevice> rxDev1, rxDev2;
  161. { // first interface
  162. rxDev1 = CreateObject<SimpleNetDevice> ();
  163. rxDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
  164. rxNode->AddDevice (rxDev1);
  165. Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
  166. uint32_t netdev_idx = ipv4->AddInterface (rxDev1);
  167. Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.1"), Ipv4Mask (0xffff0000U));
  168. ipv4->AddAddress (netdev_idx, ipv4Addr);
  169. ipv4->SetUp (netdev_idx);
  170. }
  171. { // second interface
  172. rxDev2 = CreateObject<SimpleNetDevice> ();
  173. rxDev2->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
  174. rxNode->AddDevice (rxDev2);
  175. Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
  176. uint32_t netdev_idx = ipv4->AddInterface (rxDev2);
  177. Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.1.1"), Ipv4Mask (0xffff0000U));
  178. ipv4->AddAddress (netdev_idx, ipv4Addr);
  179. ipv4->SetUp (netdev_idx);
  180. }
  181. // Sender Node
  182. Ptr<Node> txNode = CreateObject<Node> ();
  183. AddInternetStack (txNode);
  184. Ptr<SimpleNetDevice> txDev1;
  185. {
  186. txDev1 = CreateObject<SimpleNetDevice> ();
  187. txDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
  188. txNode->AddDevice (txDev1);
  189. Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
  190. uint32_t netdev_idx = ipv4->AddInterface (txDev1);
  191. Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.2"), Ipv4Mask (0xffff0000U));
  192. ipv4->AddAddress (netdev_idx, ipv4Addr);
  193. ipv4->SetUp (netdev_idx);
  194. }
  195. Ptr<SimpleNetDevice> txDev2;
  196. {
  197. txDev2 = CreateObject<SimpleNetDevice> ();
  198. txDev2->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
  199. txNode->AddDevice (txDev2);
  200. Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
  201. uint32_t netdev_idx = ipv4->AddInterface (txDev2);
  202. Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.1.2"), Ipv4Mask (0xffff0000U));
  203. ipv4->AddAddress (netdev_idx, ipv4Addr);
  204. ipv4->SetUp (netdev_idx);
  205. }
  206. // link the two nodes
  207. Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
  208. rxDev1->SetChannel (channel1);
  209. txDev1->SetChannel (channel1);
  210. Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel> ();
  211. rxDev2->SetChannel (channel2);
  212. txDev2->SetChannel (channel2);
  213. // Create the IPv4 Raw sockets
  214. Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory> ();
  215. Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
  216. NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
  217. rxSocket->SetRecvCallback (MakeCallback (&Ipv4RawSocketImplTest::ReceivePkt, this));
  218. Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket ();
  219. rxSocket2->SetRecvCallback (MakeCallback (&Ipv4RawSocketImplTest::ReceivePkt2, this));
  220. NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("10.0.1.1"), 0)), 0, "trivial");
  221. Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory> ();
  222. Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
  223. // ------ Now the tests ------------
  224. // Unicast test
  225. SendData (txSocket, "10.0.0.1");
  226. NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 10.0.0.1");
  227. NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second interface should not receive it");
  228. m_receivedPacket->RemoveAllByteTags ();
  229. m_receivedPacket2->RemoveAllByteTags ();
  230. // Unicast w/ header test
  231. SendData_IpHdr (txSocket, "10.0.0.1");
  232. NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv(hdrincl): 10.0.0.1");
  233. NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second interface should not receive it");
  234. m_receivedPacket->RemoveAllByteTags ();
  235. m_receivedPacket2->RemoveAllByteTags ();
  236. #if 0
  237. // Simple broadcast test
  238. SendData (txSocket, "255.255.255.255");
  239. NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255");
  240. NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second socket should not receive it (it is bound specifically to the second interface's address");
  241. m_receivedPacket->RemoveAllByteTags ();
  242. m_receivedPacket2->RemoveAllByteTags ();
  243. #endif
  244. // Simple Link-local multicast test
  245. txSocket->Bind (InetSocketAddress (Ipv4Address ("10.0.0.2"), 0));
  246. SendData (txSocket, "224.0.0.9");
  247. NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 224.0.0.9");
  248. NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second socket should not receive it (it is bound specifically to the second interface's address");
  249. m_receivedPacket->RemoveAllByteTags ();
  250. m_receivedPacket2->RemoveAllByteTags ();
  251. #if 0
  252. // Broadcast test with multiple receiving sockets
  253. // When receiving broadcast packets, all sockets sockets bound to
  254. // the address/port should receive a copy of the same packet -- if
  255. // the socket address matches.
  256. rxSocket2->Dispose ();
  257. rxSocket2 = rxSocketFactory->CreateSocket ();
  258. rxSocket2->SetRecvCallback (MakeCallback (&Ipv4RawSocketImplTest::ReceivePkt2, this));
  259. NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
  260. SendData (txSocket, "255.255.255.255");
  261. NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255");
  262. NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 143, "recv: 255.255.255.255");
  263. #endif
  264. m_receivedPacket = 0;
  265. m_receivedPacket2 = 0;
  266. Simulator::Destroy ();
  267. return GetErrorStatus ();
  268. }
  269. //-----------------------------------------------------------------------------
  270. class Ipv4RawTestSuite : public TestSuite
  271. {
  272. public:
  273. Ipv4RawTestSuite () : TestSuite ("ipv4-raw", UNIT)
  274. {
  275. AddTestCase (new Ipv4RawSocketImplTest);
  276. }
  277. } g_ipv4rawTestSuite;
  278. }; // namespace ns3