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"
35 #include "ns3/inet6-socket-address.h"
36 #include "ns3/boolean.h"
37 #include "ns3/uinteger.h"
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"
46 #include <netinet/in.h>
47 #include <sys/socket.h>
48 #include <sys/types.h>
75 void DoSendData (
Ptr<Socket> socket, std::string to);
79 virtual void DoRun (
void);
89 Ipv6RawSocketImplTest::Ipv6RawSocketImplTest ()
90 :
TestCase (
"Ipv6 Raw socket implementation")
96 m_receivedPacket = packet;
101 m_receivedPacket2 = packet;
104 void Ipv6RawSocketImplTest::ReceivePkt (
Ptr<Socket> socket)
106 uint32_t availableData;
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 ());
114 void Ipv6RawSocketImplTest::ReceivePkt2 (
Ptr<Socket> socket)
116 uint32_t availableData;
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 ());
124 NS_TEST_EXPECT_MSG_EQ (v6addr.GetIpv6 (),
Ipv6Address (
"fe80:0000:0000:0000:0200:00ff:fe00:0003"),
"recvfrom");
128 Ipv6RawSocketImplTest::DoSendData (
Ptr<Socket> socket, std::string to)
131 NS_TEST_EXPECT_MSG_EQ (socket->
SendTo (Create<Packet> (123), 0, realTo),
136 Ipv6RawSocketImplTest::SendData (
Ptr<Socket> socket, std::string to)
138 m_receivedPacket = Create<Packet> ();
139 m_receivedPacket2 = Create<Packet> ();
141 &Ipv6RawSocketImplTest::DoSendData,
this, socket, to);
151 Ptr<Node> rxNode = CreateObject<Node> ();
152 AddInternetStack (rxNode);
155 rxDev1 = CreateObject<SimpleNetDevice> ();
156 rxDev1->
SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
162 ipv6->
SetUp (netdev_idx);
166 rxDev2 = CreateObject<SimpleNetDevice> ();
167 rxDev2->
SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
173 ipv6->
SetUp (netdev_idx);
177 Ptr<Node> txNode = CreateObject<Node> ();
178 AddInternetStack (txNode);
181 txDev1 = CreateObject<SimpleNetDevice> ();
182 txDev1->
SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
188 ipv6->
SetUp (netdev_idx);
192 txDev2 = CreateObject<SimpleNetDevice> ();
193 txDev2->
SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
199 ipv6->
SetUp (netdev_idx);
204 rxDev1->SetChannel (channel1);
205 txDev1->SetChannel (channel1);
208 rxDev2->SetChannel (channel2);
209 txDev2->SetChannel (channel2);
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));
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");
235 m_receivedPacket->RemoveAllByteTags ();
236 m_receivedPacket2->RemoveAllByteTags ();
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");
244 m_receivedPacket->RemoveAllByteTags ();
245 m_receivedPacket2->RemoveAllByteTags ();
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");
262 m_receivedPacket = 0;
263 m_receivedPacket2 = 0;
265 Simulator::Destroy ();
275 } g_ipv6rawTestSuite;
Access to the IPv6 forwarding table, interfaces, and configuration.
#define NS_ASSERT(condition)
IPv6 address associated with an interface.
uint32_t GetSize(void) const
virtual Ptr< Socket > CreateSocket(void)=0
TestSuite(std::string name, Type type=UNIT)
Constuct a new test suite.
virtual void AddRoutingProtocol(Ptr< Ipv6RoutingProtocol > routingProtocol, int16_t priority)
Register a new routing protocol to be used in this IPv4 stack.
a polymophic address class
API to create IPv6 RAW socket instances.
Hold an unsigned integer type.
virtual void DoRun(void)
Implementation to actually run this test case.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
void AggregateObject(Ptr< Object > other)
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 AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
Add a NetDevice interface.
virtual void RegisterExtensions()
Register the IPv6 Extensions.
Describes an IPv6 address.
void SetRoutingProtocol(Ptr< Ipv6RoutingProtocol > routingProtocol)
Set routing protocol for this stack.
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
uint32_t AddDevice(Ptr< NetDevice > device)
uint32_t GetId(void) const
virtual Ptr< Node > GetNode(void) const =0
void Insert(Ptr< IpL4Protocol > protocol)
Add an L4 protocol.
virtual void SetAddress(Address address)
virtual void RegisterOptions()
Register the IPv6 Options.
Describes an IPv6 prefix. It is just a bitmask like Ipv4Mask.
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.
void SetAttribute(std::string name, const AttributeValue &value)
Ptr< T > GetObject(void) const
virtual void SetUp(uint32_t interface)=0
Set the interface into the "up" state.
virtual uint32_t GetRxAvailable(void) const =0