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"
35 #include "ns3/inet-socket-address.h"
36 #include "ns3/boolean.h"
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"
46 #include <netinet/in.h>
47 #include <sys/socket.h>
48 #include <sys/types.h>
64 ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
79 void DoSendData (
Ptr<Socket> socket, std::string to);
81 void DoSendData_IpHdr (
Ptr<Socket> socket, std::string to);
82 void SendData_IpHdr (
Ptr<Socket> socket, std::string to);
85 virtual void DoRun (
void);
95 Ipv4RawSocketImplTest::Ipv4RawSocketImplTest ()
96 :
TestCase (
"IPv4 Raw socket implementation")
102 m_receivedPacket = packet;
107 m_receivedPacket2 = packet;
110 void Ipv4RawSocketImplTest::ReceivePkt (
Ptr<Socket> socket)
112 uint32_t availableData;
114 m_receivedPacket = socket->
Recv (2, MSG_PEEK);
116 m_receivedPacket = socket->
Recv (std::numeric_limits<uint32_t>::max (), 0);
117 NS_ASSERT (availableData == m_receivedPacket->GetSize ());
120 void Ipv4RawSocketImplTest::ReceivePkt2 (
Ptr<Socket> socket)
122 uint32_t availableData;
124 m_receivedPacket2 = socket->
Recv (2, MSG_PEEK);
126 m_receivedPacket2 = socket->
Recv (std::numeric_limits<uint32_t>::max (), 0);
127 NS_ASSERT (availableData == m_receivedPacket2->GetSize ());
131 Ipv4RawSocketImplTest::DoSendData (
Ptr<Socket> socket, std::string to)
134 NS_TEST_EXPECT_MSG_EQ (socket->
SendTo (Create<Packet> (123), 0, realTo),
139 Ipv4RawSocketImplTest::SendData (
Ptr<Socket> socket, std::string to)
141 m_receivedPacket = Create<Packet> ();
142 m_receivedPacket2 = Create<Packet> ();
144 &Ipv4RawSocketImplTest::DoSendData,
this, socket, to);
149 Ipv4RawSocketImplTest::DoSendData_IpHdr (
Ptr<Socket> socket, std::string to)
156 ipHeader.SetDestination (
Ipv4Address (to.c_str ()));
157 ipHeader.SetProtocol (0);
158 ipHeader.SetPayloadSize (p->
GetSize ());
159 ipHeader.SetTtl (255);
162 NS_TEST_EXPECT_MSG_EQ (socket->
SendTo (p, 0, realTo),
168 Ipv4RawSocketImplTest::SendData_IpHdr (
Ptr<Socket> socket, std::string to)
170 m_receivedPacket = Create<Packet> ();
171 m_receivedPacket2 = Create<Packet> ();
173 &Ipv4RawSocketImplTest::DoSendData_IpHdr,
this, socket, to);
183 Ptr<Node> rxNode = CreateObject<Node> ();
184 AddInternetStack (rxNode);
187 rxDev1 = CreateObject<SimpleNetDevice> ();
188 rxDev1->
SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
194 ipv4->
SetUp (netdev_idx);
198 rxDev2 = CreateObject<SimpleNetDevice> ();
199 rxDev2->
SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
205 ipv4->
SetUp (netdev_idx);
209 Ptr<Node> txNode = CreateObject<Node> ();
210 AddInternetStack (txNode);
213 txDev1 = CreateObject<SimpleNetDevice> ();
214 txDev1->
SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
220 ipv4->
SetUp (netdev_idx);
224 txDev2 = CreateObject<SimpleNetDevice> ();
225 txDev2->
SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
231 ipv4->
SetUp (netdev_idx);
236 rxDev1->SetChannel (channel1);
237 txDev1->SetChannel (channel1);
240 rxDev2->SetChannel (channel2);
241 txDev2->SetChannel (channel2);
248 rxSocket->SetRecvCallback (
MakeCallback (&Ipv4RawSocketImplTest::ReceivePkt,
this));
260 SendData (txSocket,
"10.0.0.1");
261 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143,
"recv: 10.0.0.1");
262 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0,
"second interface should not receive it");
264 m_receivedPacket->RemoveAllByteTags ();
265 m_receivedPacket2->RemoveAllByteTags ();
268 SendData_IpHdr (txSocket,
"10.0.0.1");
269 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143,
"recv(hdrincl): 10.0.0.1");
270 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0,
"second interface should not receive it");
272 m_receivedPacket->RemoveAllByteTags ();
273 m_receivedPacket2->RemoveAllByteTags ();
278 SendData (txSocket,
"255.255.255.255");
279 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143,
"recv: 255.255.255.255");
280 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");
282 m_receivedPacket->RemoveAllByteTags ();
283 m_receivedPacket2->RemoveAllByteTags ();
289 SendData (txSocket,
"224.0.0.9");
290 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143,
"recv: 224.0.0.9");
291 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");
293 m_receivedPacket->RemoveAllByteTags ();
294 m_receivedPacket2->RemoveAllByteTags ();
307 SendData (txSocket,
"255.255.255.255");
308 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143,
"recv: 255.255.255.255");
309 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 143,
"recv: 255.255.255.255");
312 m_receivedPacket = 0;
313 m_receivedPacket2 = 0;
315 Simulator::Destroy ();
325 } g_ipv4rawTestSuite;
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
a class to represent an Ipv4 address mask
virtual void DoRun(void)
Implementation to actually run this test case.
#define NS_ASSERT(condition)
uint32_t GetSize(void) const
virtual Ptr< Socket > CreateSocket(void)=0
API to create RAW socket instances.
TestSuite(std::string name, Type type=UNIT)
Constuct a new test suite.
a polymophic address class
virtual void SetUp(uint32_t interface)=0
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)
Access to the Ipv4 forwarding table, interfaces, and configuration.
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.
Ipv4 addresses are stored in host order in this class.
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
uint32_t AddDevice(Ptr< NetDevice > device)
uint32_t GetId(void) const
a class to store IPv4 address information on an interface
virtual Ptr< Node > GetNode(void) const =0
virtual void SetAddress(Address address)
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
void SetAttribute(std::string name, const AttributeValue &value)
Ptr< T > GetObject(void) const
void AddHeader(const Header &header)
virtual uint32_t GetRxAvailable(void) const =0
void SetRoutingProtocol(Ptr< Ipv4RoutingProtocol > routingProtocol)
Register a new routing protocol to be used by this Ipv4 stack.