25 #include "ns3/config.h"
26 #include "ns3/uinteger.h"
27 #include "ns3/socket-factory.h"
28 #include "ns3/ipv4-raw-socket-factory.h"
29 #include "ns3/udp-socket-factory.h"
30 #include "ns3/simulator.h"
31 #include "error-channel.h"
32 #include "error-net-device.h"
33 #include "ns3/drop-tail-queue.h"
34 #include "ns3/socket.h"
35 #include "ns3/udp-socket.h"
39 #include "ns3/inet-socket-address.h"
40 #include "ns3/boolean.h"
42 #include "ns3/arp-l3-protocol.h"
43 #include "ns3/ipv4-l3-protocol.h"
44 #include "ns3/icmpv4-l4-protocol.h"
45 #include "ns3/ipv4-list-routing.h"
46 #include "ns3/ipv4-static-routing.h"
47 #include "ns3/udp-l4-protocol.h"
51 #include <netinet/in.h>
69 ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
95 virtual void DoRun (
void);
106 void HandleReadIcmpClient (
Ipv4Address icmpSource, uint8_t icmpTtl, uint8_t icmpType,
107 uint8_t icmpCode,uint32_t icmpInfo);
109 void SetFill (uint8_t *fill, uint32_t fillSize, uint32_t dataSize);
115 Ipv4FragmentationTest::Ipv4FragmentationTest ()
116 :
TestCase (
"Verify the IPv4 layer 3 protocol fragmentation and reassembly")
123 Ipv4FragmentationTest::~Ipv4FragmentationTest ()
135 Ipv4FragmentationTest::StartServer (
Ptr<Node> ServerNode)
138 if (m_socketServer == 0)
140 TypeId tid = TypeId::LookupByName (
"ns3::UdpSocketFactory");
141 m_socketServer = Socket::CreateSocket (ServerNode, tid);
143 m_socketServer->
Bind (local);
144 Ptr<UdpSocket> udpSocket = DynamicCast<UdpSocket> (m_socketServer);
156 Ipv4FragmentationTest::HandleReadServer (
Ptr<Socket> socket)
160 while ((packet = socket->
RecvFrom (from)))
162 if (InetSocketAddress::IsMatchingType (from))
167 m_receivedPacketServer = packet->
Copy();
173 Ipv4FragmentationTest::StartClient (
Ptr<Node> ClientNode)
176 if (m_socketClient == 0)
178 TypeId tid = TypeId::LookupByName (
"ns3::UdpSocketFactory");
179 m_socketClient = Socket::CreateSocket (ClientNode, tid);
180 m_socketClient->
Bind ();
190 Ipv4FragmentationTest::HandleReadClient (
Ptr<Socket> socket)
194 while ((packet = socket->
RecvFrom (from)))
196 if (InetSocketAddress::IsMatchingType (from))
198 m_receivedPacketClient = packet->
Copy();
204 Ipv4FragmentationTest::HandleReadIcmpClient (
Ipv4Address icmpSource,
205 uint8_t icmpTtl, uint8_t icmpType,
206 uint8_t icmpCode, uint32_t icmpInfo)
208 m_icmpType = icmpType;
212 Ipv4FragmentationTest::SetFill (uint8_t *fill, uint32_t fillSize, uint32_t dataSize)
214 if (dataSize != m_dataSize)
217 m_data =
new uint8_t [dataSize];
218 m_dataSize = dataSize;
221 if (fillSize >= dataSize)
223 memcpy (m_data, fill, dataSize);
228 while (filled + fillSize < dataSize)
230 memcpy (&m_data[filled], fill, fillSize);
234 memcpy(&m_data[filled], fill, dataSize - filled);
239 Ptr<Packet> Ipv4FragmentationTest::SendClient (
void)
244 p = Create<Packet> (m_data, m_dataSize);
248 p = Create<Packet> (m_size);
250 m_socketClient->
Send (p);
260 Config::SetDefault (
"ns3::ArpCache::PendingQueueSize",
UintegerValue (100));
265 Ptr<Node> serverNode = CreateObject<Node> ();
266 AddInternetStack (serverNode);
270 serverDev = CreateObject<ErrorNetDevice> ();
271 serverDev->
SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
274 serverDevErrorModel->
Disable();
280 ipv4->
SetUp (netdev_idx);
282 StartServer(serverNode);
285 Ptr<Node> clientNode = CreateObject<Node> ();
286 AddInternetStack (clientNode);
290 clientDev = CreateObject<ErrorNetDevice> ();
291 clientDev->
SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
294 clientDevErrorModel->
Disable();
300 ipv4->
SetUp (netdev_idx);
302 StartClient(clientNode);
306 serverDev->SetChannel (channel);
307 clientDev->SetChannel (channel);
312 uint32_t packetSizes[5] = {1000, 2000, 5000, 10000, 65000};
315 uint8_t fillData[78];
316 for ( uint32_t k=48; k<=125; k++ )
322 for(
int i= 0; i<5; i++)
324 uint32_t packetSize = packetSizes[i];
326 SetFill (fillData, 78, packetSize);
328 m_receivedPacketServer = Create<Packet> ();
330 &Ipv4FragmentationTest::SendClient,
this);
333 uint8_t recvBuffer[65000];
335 uint16_t recvSize = m_receivedPacketServer->
GetSize ();
337 NS_TEST_EXPECT_MSG_EQ (recvSize, packetSizes[i],
"Packet size not correct");
339 m_receivedPacketServer->
CopyData(recvBuffer, 65000);
340 NS_TEST_EXPECT_MSG_EQ (memcmp(m_data, recvBuffer, m_receivedPacketServer->
GetSize ()),
341 0,
"Packet content differs");
348 for(
int i= 0; i<5; i++)
350 uint32_t packetSize = packetSizes[i];
352 SetFill (fillData, 78, packetSize);
354 m_receivedPacketServer = Create<Packet> ();
356 &Ipv4FragmentationTest::SendClient,
this);
359 uint8_t recvBuffer[65000];
361 uint16_t recvSize = m_receivedPacketServer->
GetSize ();
363 NS_TEST_EXPECT_MSG_EQ (recvSize, packetSizes[i],
"Packet size not correct");
365 m_receivedPacketServer->
CopyData(recvBuffer, 65000);
366 NS_TEST_EXPECT_MSG_EQ (memcmp(m_data, recvBuffer, m_receivedPacketServer->
GetSize ()),
367 0,
"Packet content differs");
378 clientDevErrorModel->
Disable();
379 serverDevErrorModel->
Enable();
380 for(
int i= 1; i<5; i++)
382 uint32_t packetSize = packetSizes[i];
384 SetFill (fillData, 78, packetSize);
387 serverDevErrorModel->Reset();
389 m_receivedPacketServer = Create<Packet> ();
392 &Ipv4FragmentationTest::SendClient,
this);
395 uint16_t recvSize = m_receivedPacketServer->
GetSize ();
397 NS_TEST_EXPECT_MSG_EQ ((recvSize == 0),
true,
"Server got a packet, something wrong");
398 NS_TEST_EXPECT_MSG_EQ ((m_icmpType == 11),
true,
"Client did not receive ICMP::TIME_EXCEEDED");
402 Simulator::Destroy ();
412 } g_ipv4fragmentationTestSuite;
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
a class to represent an Ipv4 address mask
uint32_t GetSize(void) const
virtual bool SetMtu(const uint16_t mtu)
TestSuite(std::string name, Type type=UNIT)
Constuct a new test suite.
A sockets interface to UDP.
a polymophic address class
void RemoveAllPacketTags(void)
virtual int MulticastJoinGroup(uint32_t interface, const Address &groupAddress)=0
Corresponds to socket option MCAST_JOIN_GROUP.
virtual void SetUp(uint32_t interface)=0
Hold an unsigned integer type.
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.
void AggregateObject(Ptr< Object > other)
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
void SetJumpingMode(bool mode)
Set if the odd packets are delayed (even ones are not delayed ever)
Access to the Ipv4 forwarding table, interfaces, and configuration.
Ptr< Packet > Copy(void) const
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
void SetReceiveErrorModel(Ptr< ErrorModel > em)
virtual void SetAddress(Address address)
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
void RemoveAllByteTags(void)
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
virtual void DoRun(void)
Implementation to actually run this test case.
void SetJumpingTime(Time delay)
Set the delay for the odd packets (even ones are not delayed)
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.
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
void SetAttribute(std::string name, const AttributeValue &value)
Ptr< T > GetObject(void) const
a unique identifier for an interface.
void SetRoutingProtocol(Ptr< Ipv4RoutingProtocol > routingProtocol)
Register a new routing protocol to be used by this Ipv4 stack.