A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ipv6-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) 2012 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/ipv6-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/inet6-socket-address.h"
36 #include "ns3/boolean.h"
37 #include "ns3/uinteger.h"
38 
39 #include "ns3/ipv6-l3-protocol.h"
40 #include "ns3/icmpv6-l4-protocol.h"
41 #include "ns3/ipv6-list-routing.h"
42 #include "ns3/ipv6-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 
50 using namespace ns3;
51 
52 static void
54 {
55  Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> ();
56  Ptr<Icmpv6L4Protocol> icmpv6 = CreateObject<Icmpv6L4Protocol> ();
57  node->AggregateObject (ipv6);
58  node->AggregateObject (icmpv6);
59  ipv6->Insert (icmpv6);
60  icmpv6->SetAttribute ("DAD", BooleanValue (false));
61 
62  //Routing for Ipv6
63  Ptr<Ipv6ListRouting> ipv6Routing = CreateObject<Ipv6ListRouting> ();
64  ipv6->SetRoutingProtocol (ipv6Routing);
65  Ptr<Ipv6StaticRouting> ipv6staticRouting = CreateObject<Ipv6StaticRouting> (); ipv6Routing->AddRoutingProtocol (ipv6staticRouting, 0);
66  /* register IPv6 extensions and options */
67  ipv6->RegisterExtensions (); ipv6->RegisterOptions ();
68 }
69 
70 
72 {
75  void DoSendData (Ptr<Socket> socket, std::string to);
76  void SendData (Ptr<Socket> socket, std::string to);
77 
78 public:
79  virtual void DoRun (void);
81 
82  void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
83  void ReceivePacket2 (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
84  void ReceivePkt (Ptr<Socket> socket);
85  void ReceivePkt2 (Ptr<Socket> socket);
86 };
87 
88 
90  : TestCase ("Ipv6 Raw socket implementation")
91 {
92 }
93 
95 {
96  m_receivedPacket = packet;
97 }
98 
100 {
101  m_receivedPacket2 = packet;
102 }
103 
105 {
106  uint32_t availableData;
107  availableData = socket->GetRxAvailable ();
108  m_receivedPacket = socket->Recv (2, MSG_PEEK);
110  m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
111  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
112 }
113 
115 {
116  uint32_t availableData;
117  Address addr;
118  availableData = socket->GetRxAvailable ();
119  m_receivedPacket2 = socket->Recv (2, MSG_PEEK);
121  m_receivedPacket2 = socket->RecvFrom (std::numeric_limits<uint32_t>::max (), 0, addr);
122  NS_ASSERT (availableData == m_receivedPacket2->GetSize ());
123  Inet6SocketAddress v6addr = Inet6SocketAddress::ConvertFrom (addr);
124  NS_TEST_EXPECT_MSG_EQ (v6addr.GetIpv6 (),Ipv6Address ("2001:0db8:0000:0000:0000:0000:0000:0002"), "recvfrom");
125 }
126 
127 void
129 {
130  Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str ()), 0);
131  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
132  123, to);
133 }
134 
135 void
137 {
138  m_receivedPacket = Create<Packet> ();
139  m_receivedPacket2 = Create<Packet> ();
140  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
141  &Ipv6RawSocketImplTest::DoSendData, this, socket, to);
142  Simulator::Run ();
143 }
144 
145 void
147 {
148  // Create topology
149 
150  // Receiver Node
151  Ptr<Node> rxNode = CreateObject<Node> ();
152  AddInternetStack (rxNode);
153  Ptr<SimpleNetDevice> rxDev1, rxDev2;
154  { // first interface
155  rxDev1 = CreateObject<SimpleNetDevice> ();
156  rxDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
157  rxNode->AddDevice (rxDev1);
158  Ptr<Ipv6> ipv6 = rxNode->GetObject<Ipv6> ();
159  uint32_t netdev_idx = ipv6->AddInterface (rxDev1);
160  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:db8:0::1"), Ipv6Prefix (64));
161  ipv6->AddAddress (netdev_idx, ipv6Addr);
162  ipv6->SetUp (netdev_idx);
163  }
164 
165  { // second interface
166  rxDev2 = CreateObject<SimpleNetDevice> ();
167  rxDev2->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
168  rxNode->AddDevice (rxDev2);
169  Ptr<Ipv6> ipv6 = rxNode->GetObject<Ipv6> ();
170  uint32_t netdev_idx = ipv6->AddInterface (rxDev2);
171  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:db8:1::1"), Ipv6Prefix (64));
172  ipv6->AddAddress (netdev_idx, ipv6Addr);
173  ipv6->SetUp (netdev_idx);
174  }
175 
176  // Sender Node
177  Ptr<Node> txNode = CreateObject<Node> ();
178  AddInternetStack (txNode);
179  Ptr<SimpleNetDevice> txDev1;
180  {
181  txDev1 = CreateObject<SimpleNetDevice> ();
182  txDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
183  txNode->AddDevice (txDev1);
184  Ptr<Ipv6> ipv6 = txNode->GetObject<Ipv6> ();
185  uint32_t netdev_idx = ipv6->AddInterface (txDev1);
186  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:db8:0::2"), Ipv6Prefix (64));
187  ipv6->AddAddress (netdev_idx, ipv6Addr);
188  ipv6->SetUp (netdev_idx);
189  }
190  Ptr<SimpleNetDevice> txDev2;
191  {
192  txDev2 = CreateObject<SimpleNetDevice> ();
193  txDev2->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
194  txNode->AddDevice (txDev2);
195  Ptr<Ipv6> ipv6 = txNode->GetObject<Ipv6> ();
196  uint32_t netdev_idx = ipv6->AddInterface (txDev2);
197  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:db8:1::2"), Ipv6Prefix (64));
198  ipv6->AddAddress (netdev_idx, ipv6Addr);
199  ipv6->SetUp (netdev_idx);
200  }
201 
202  // link the two nodes
203  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
204  rxDev1->SetChannel (channel1);
205  txDev1->SetChannel (channel1);
206 
207  Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel> ();
208  rxDev2->SetChannel (channel2);
209  txDev2->SetChannel (channel2);
210 
211 
212  // Create the Ipv6 Raw sockets
213  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv6RawSocketFactory> ();
214  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
215  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (Inet6SocketAddress (Ipv6Address::GetAny (), 0)), 0, "trivial");
216  rxSocket->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
217  rxSocket->SetRecvCallback (MakeCallback (&Ipv6RawSocketImplTest::ReceivePkt, this));
218 
219  Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket ();
221  rxSocket2->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
222  NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (Inet6SocketAddress (Ipv6Address ("2001:db8:1::1"), 0)), 0, "trivial");
223 
224  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv6RawSocketFactory> ();
225  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
226  txSocket->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
227 
228  // ------ Now the tests ------------
229 
230  // Unicast test
231  SendData (txSocket, "2001:db8:0::1");
232  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 163, "recv: 2001:db8:0::1");
233  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second interface should not receive it");
234 
237 
238  // Simple Link-local multicast test
239  txSocket->Bind (Inet6SocketAddress (Ipv6Address ("2001:db8:0::2"), 0));
240  SendData (txSocket, "ff02::1");
241  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 163, "recv: ff02::1");
242  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");
243 
246 
247  // Broadcast test with multiple receiving sockets
248 
249  // When receiving broadcast packets, all sockets sockets bound to
250  // the address/port should receive a copy of the same packet -- if
251  // the socket address matches.
252  rxSocket2->Dispose ();
253  rxSocket2 = rxSocketFactory->CreateSocket ();
255  rxSocket2->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
256  NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (Inet6SocketAddress (Ipv6Address::GetAny (), 0)), 0, "trivial");
257 
258  SendData (txSocket, "ff02::1");
259  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 163, "recv: ff02::1");
260  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 163, "recv: ff02::1");
261 
262  m_receivedPacket = 0;
263  m_receivedPacket2 = 0;
264 
265  Simulator::Destroy ();
266 }
267 //-----------------------------------------------------------------------------
269 {
270 public:
271  Ipv6RawTestSuite () : TestSuite ("ipv6-raw", UNIT)
272  {
273  AddTestCase (new Ipv6RawSocketImplTest, TestCase::QUICK);
274  }
void ReceivePkt(Ptr< Socket > socket)
Hold a bool native type.
Definition: boolean.h:38
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:79
A suite of tests to run.
Definition: test.h:1025
void ReceivePacket(Ptr< Socket > socket)
#define NS_ASSERT(condition)
Definition: assert.h:64
IPv6 address associated with an interface.
uint32_t GetSize(void) const
Definition: packet.h:650
virtual Ptr< Socket > CreateSocket(void)=0
encapsulates test code
Definition: test.h:849
void ReceivePkt2(Ptr< Socket > socket)
virtual void AddRoutingProtocol(Ptr< Ipv6RoutingProtocol > routingProtocol, int16_t priority)
Register a new routing protocol to be used in this IPv4 stack.
Ipv6RawTestSuite g_ipv6rawTestSuite
a polymophic address class
Definition: address.h:86
void ReceivePacket2(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
API to create IPv6 RAW socket instances.
static void AddInternetStack(Ptr< Node > node)
Hold an unsigned integer type.
Definition: uinteger.h:46
#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:244
void SendData(Ptr< Socket > socket, std::string to)
virtual void DoRun(void)
Implementation to actually run this TestCase.
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:71
void AggregateObject(Ptr< Object > other)
Definition: object.cc:243
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
void DoSendData(Ptr< Socket > socket, std::string to)
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:173
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
Add a NetDevice interface.
virtual void RegisterExtensions()
Register the IPv6 Extensions.
Describes an IPv6 address.
Definition: ipv6-address.h:46
void SetRoutingProtocol(Ptr< Ipv6RoutingProtocol > routingProtocol)
Set routing protocol for this stack.
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:118
uint32_t GetId(void) const
Definition: node.cc:104
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
void Insert(Ptr< IpL4Protocol > protocol)
Add an L4 protocol.
virtual void SetAddress(Address address)
Set the address of this interface.
Ptr< Packet > m_receivedPacket
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:361
virtual void RegisterOptions()
Register the IPv6 Options.
Describes an IPv6 prefix.
Definition: ipv6-address.h:387
virtual bool AddAddress(uint32_t interface, Ipv6InterfaceAddress address)=0
Add an address on the specified IPv6 interface.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
This test suite implements a Unit Test.
Definition: test.h:1035
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:161
Ptr< T > GetObject(void) const
Definition: object.h:361
void Dispose(void)
Run the DoDispose methods of this object and all the objects aggregated to it.
Definition: object.cc:205
Ptr< Packet > m_receivedPacket2
virtual void SetUp(uint32_t interface)=0
Set the interface into the "up" state.
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.