A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ipv4-raw-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: Hajime Tazaki <tazaki@sfc.wide.ad.jp>
19  */
24 #include "ns3/test.h"
25 #include "ns3/socket-factory.h"
26 #include "ns3/ipv4-raw-socket-factory.h"
27 #include "ns3/simulator.h"
28 #include "ns3/simple-channel.h"
29 #include "ns3/simple-net-device.h"
30 #include "ns3/drop-tail-queue.h"
31 #include "ns3/socket.h"
32 
33 #include "ns3/log.h"
34 #include "ns3/node.h"
35 #include "ns3/inet-socket-address.h"
36 #include "ns3/boolean.h"
37 
38 #include "ns3/arp-l3-protocol.h"
39 #include "ns3/ipv4-l3-protocol.h"
40 #include "ns3/icmpv4-l4-protocol.h"
41 #include "ns3/ipv4-list-routing.h"
42 #include "ns3/ipv4-static-routing.h"
43 
44 #include <string>
45 #include <limits>
46 #include <netinet/in.h>
47 #include <sys/socket.h>
48 #include <sys/types.h>
49 namespace ns3 {
50 
51 static void
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 
75 {
78  void DoSendData (Ptr<Socket> socket, std::string to);
79  void SendData (Ptr<Socket> socket, std::string to);
80  void DoSendData_IpHdr (Ptr<Socket> socket, std::string to);
81  void SendData_IpHdr (Ptr<Socket> socket, std::string to);
82 
83 public:
84  virtual void DoRun (void);
86 
87  void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
88  void ReceivePacket2 (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
89  void ReceivePkt (Ptr<Socket> socket);
90  void ReceivePkt2 (Ptr<Socket> socket);
91 };
92 
93 
95  : TestCase ("IPv4 Raw socket implementation")
96 {
97 }
98 
100 {
101  m_receivedPacket = packet;
102 }
103 
105 {
106  m_receivedPacket2 = packet;
107 }
108 
110 {
111  uint32_t availableData;
112  availableData = socket->GetRxAvailable ();
113  m_receivedPacket = socket->Recv (2, MSG_PEEK);
115  m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
116  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
117 }
118 
120 {
121  uint32_t availableData;
122  availableData = socket->GetRxAvailable ();
123  m_receivedPacket2 = socket->Recv (2, MSG_PEEK);
125  m_receivedPacket2 = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
126  NS_ASSERT (availableData == m_receivedPacket2->GetSize ());
127 }
128 
129 void
131 {
132  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 0);
133  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
134  123, to);
135 }
136 
137 void
139 {
140  m_receivedPacket = Create<Packet> ();
141  m_receivedPacket2 = Create<Packet> ();
143  &Ipv4RawSocketImplTest::DoSendData, this, socket, to);
144  Simulator::Run ();
145 }
146 
147 void
149 {
150  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 0);
151  socket->SetAttribute ("IpHeaderInclude", BooleanValue (true));
152  Ptr<Packet> p = Create<Packet> (123);
153  Ipv4Header ipHeader;
154  ipHeader.SetSource (Ipv4Address ("10.0.0.2"));
155  ipHeader.SetDestination (Ipv4Address (to.c_str ()));
156  ipHeader.SetProtocol (0);
157  ipHeader.SetPayloadSize (p->GetSize ());
158  ipHeader.SetTtl (255);
159  p->AddHeader (ipHeader);
160 
161  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (p, 0, realTo),
162  143, to);
163  socket->SetAttribute ("IpHeaderInclude", BooleanValue (false));
164 }
165 
166 void
168 {
169  m_receivedPacket = Create<Packet> ();
170  m_receivedPacket2 = Create<Packet> ();
172  &Ipv4RawSocketImplTest::DoSendData_IpHdr, this, socket, to);
173  Simulator::Run ();
174 }
175 
176 void
178 {
179  // Create topology
180 
181  // Receiver Node
182  Ptr<Node> rxNode = CreateObject<Node> ();
183  AddInternetStack (rxNode);
184  Ptr<SimpleNetDevice> rxDev1, rxDev2;
185  { // first interface
186  rxDev1 = CreateObject<SimpleNetDevice> ();
188  rxNode->AddDevice (rxDev1);
189  Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
190  uint32_t netdev_idx = ipv4->AddInterface (rxDev1);
191  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.1"), Ipv4Mask (0xffff0000U));
192  ipv4->AddAddress (netdev_idx, ipv4Addr);
193  ipv4->SetUp (netdev_idx);
194  }
195 
196  { // second interface
197  rxDev2 = CreateObject<SimpleNetDevice> ();
199  rxNode->AddDevice (rxDev2);
200  Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
201  uint32_t netdev_idx = ipv4->AddInterface (rxDev2);
202  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.1.1"), Ipv4Mask (0xffff0000U));
203  ipv4->AddAddress (netdev_idx, ipv4Addr);
204  ipv4->SetUp (netdev_idx);
205  }
206 
207  // Sender Node
208  Ptr<Node> txNode = CreateObject<Node> ();
209  AddInternetStack (txNode);
210  Ptr<SimpleNetDevice> txDev1;
211  {
212  txDev1 = CreateObject<SimpleNetDevice> ();
214  txNode->AddDevice (txDev1);
215  Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
216  uint32_t netdev_idx = ipv4->AddInterface (txDev1);
217  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.2"), Ipv4Mask (0xffff0000U));
218  ipv4->AddAddress (netdev_idx, ipv4Addr);
219  ipv4->SetUp (netdev_idx);
220  }
221  Ptr<SimpleNetDevice> txDev2;
222  {
223  txDev2 = CreateObject<SimpleNetDevice> ();
225  txNode->AddDevice (txDev2);
226  Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
227  uint32_t netdev_idx = ipv4->AddInterface (txDev2);
228  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.1.2"), Ipv4Mask (0xffff0000U));
229  ipv4->AddAddress (netdev_idx, ipv4Addr);
230  ipv4->SetUp (netdev_idx);
231  }
232 
233  // link the two nodes
234  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
235  rxDev1->SetChannel (channel1);
236  txDev1->SetChannel (channel1);
237 
238  Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel> ();
239  rxDev2->SetChannel (channel2);
240  txDev2->SetChannel (channel2);
241 
242 
243  // Create the IPv4 Raw sockets
244  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory> ();
245  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
246  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
247  rxSocket->SetRecvCallback (MakeCallback (&Ipv4RawSocketImplTest::ReceivePkt, this));
248 
249  Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket ();
251  NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("10.0.1.1"), 0)), 0, "trivial");
252 
253  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory> ();
254  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
255 
256  // ------ Now the tests ------------
257 
258  // Unicast test
259  SendData (txSocket, "10.0.0.1");
260  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 10.0.0.1");
261  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second interface should not receive it");
262 
265 
266  // Unicast w/ header test
267  SendData_IpHdr (txSocket, "10.0.0.1");
268  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv(hdrincl): 10.0.0.1");
269  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second interface should not receive it");
270 
273 
274 #if 0
275  // Simple broadcast test
276 
277  SendData (txSocket, "255.255.255.255");
278  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255");
279  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");
280 
283 #endif
284 
285  // Simple Link-local multicast test
286 
287  txSocket->Bind (InetSocketAddress (Ipv4Address ("10.0.0.2"), 0));
288  SendData (txSocket, "224.0.0.9");
289  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 224.0.0.9");
290  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");
291 
294 
295 #if 0
296  // Broadcast test with multiple receiving sockets
297 
298  // When receiving broadcast packets, all sockets sockets bound to
299  // the address/port should receive a copy of the same packet -- if
300  // the socket address matches.
301  rxSocket2->Dispose ();
302  rxSocket2 = rxSocketFactory->CreateSocket ();
304  NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
305 
306  SendData (txSocket, "255.255.255.255");
307  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255");
308  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 143, "recv: 255.255.255.255");
309 #endif
310 
311  m_receivedPacket = 0;
312  m_receivedPacket2 = 0;
313 
315 }
316 //-----------------------------------------------------------------------------
318 {
319 public:
320  Ipv4RawTestSuite () : TestSuite ("ipv4-raw", UNIT)
321  {
323  }
325 
326 } // namespace ns3