28#include "ns3/assert.h"
29#include "ns3/fatal-error.h"
30#include "ns3/iana-internet-protocol-numbers.h"
31#include "ns3/icmpv4-l4-protocol.h"
32#include "ns3/ip-l4-protocol.h"
33#include "ns3/ipv4-address.h"
34#include "ns3/ipv4-header.h"
35#include "ns3/ipv4-interface.h"
36#include "ns3/ipv4-l3-protocol.h"
37#include "ns3/ipv4-route.h"
39#include "ns3/node-list.h"
41#include "ns3/object-vector.h"
42#include "ns3/pointer.h"
44#include "ns3/trace-source-accessor.h"
45#include "ns3/udp-header.h"
46#include "ns3/uinteger.h"
69 .AddAttribute(
"OptionNumber",
70 "The Dsr option number.",
74 .AddTraceSource(
"Drop",
77 "ns3::Packet::TracedCallback")
79 "Receive DSR packet.",
81 "ns3::dsr::DsrOptionSRHeader::TracedCallback");
112 std::vector<Ipv4Address>& nodeList)
115 auto it = find(nodeList.begin(), nodeList.end(), destAddress);
117 for (
auto i = it; i != nodeList.end(); ++i)
119 if ((ipv4Address == (*i)) && ((*i) != nodeList.back()))
127std::vector<Ipv4Address>
131 auto it = find(nodeList.begin(), nodeList.end(), ipv4Address);
132 std::vector<Ipv4Address> cutRoute;
133 for (
auto i = it; i != nodeList.end(); ++i)
135 cutRoute.push_back(*i);
156 std::reverse(vec.begin(), vec.end());
174 if (ipv4Address == vec.back())
176 NS_LOG_DEBUG(
"We have reached to the final destination " << ipv4Address <<
" "
180 for (
auto i = vec.begin(); i != vec.end(); ++i)
182 if (ipv4Address == (*i))
189 NS_LOG_DEBUG(
"next hop address not found, route corrupted");
206 for (
auto ri = vec.rbegin(); ri != vec.rend(); ++ri)
208 if (ipv4Address == (*ri))
215 NS_LOG_DEBUG(
"next hop address not found, route corrupted");
227 for (
auto ri = vec.rbegin(); ri != vec.rend(); ++ri)
229 if (ipv4Address == (*ri))
231 nextTwoHop = *(ri + 2);
254 for (
auto i = vec.begin(); i != vec.end(); ++i)
265 for (
auto i = vec.begin(); i != vec.end(); ++i)
267 for (
auto j = vec2.begin(); j != vec2.end(); ++j)
282 for (
auto i = vec.begin(); i != vec.end(); ++i)
284 if ((*i) == ipv4Address)
298 std::vector<Ipv4Address> vec2(vec);
301 for (
auto i = vec2.begin(); i != vec2.end(); ++i)
309 for (
auto j = vec.begin(); j != vec.end(); ++j)
313 if ((j + 1) != vec.end())
315 vec.erase(j + 1, vec.end());
320 else if (j == (vec.end() - 1))
334 for (
int32_t i = 0; i < nNodes; ++i)
338 int32_t ifIndex = ipv4->GetInterfaceForAddress(ipv4Address);
387 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
388 << (
uint32_t)protocol << isPromisc);
391 p->RemoveHeader(pad1Header);
437 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
438 << (
uint32_t)protocol << isPromisc);
442 p->RemoveHeader(padnHeader);
489 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
490 << (
uint32_t)protocol << isPromisc);
496 if (source == ipv4Address)
498 NS_LOG_DEBUG(
"Discard the packet since it was originated from same source address");
516 p->CopyData(buf,
sizeof(buf));
517 uint8_t numberAddress = (buf[1] - 6) / 4;
519 if (numberAddress >= 255)
521 NS_LOG_DEBUG(
"Discard the packet, malformed header since two many ip addresses in route");
535 p->RemoveHeader(rreq);
544 uint16_t requestId = rreq.
GetId();
549 std::vector<Ipv4Address> nodeList(mainVector);
559 uint8_t ttl = ipv4Header.
GetTtl();
561 bool dupRequest =
false;
566 dupRequest =
dsr->FindSourceEntry(source, targetAddress, requestId);
587 NS_LOG_DEBUG(
"Our node address is already seen in the route, drop the request");
592 bool isRouteInCache =
dsr->LookupRoute(targetAddress, toPrev);
596 std::vector<Ipv4Address> saveRoute(nodeList);
609 NS_LOG_DEBUG(
"The target address over here " << targetAddress <<
" and the ip address "
610 << ipv4Address <<
" and the source address "
612 if (targetAddress == ipv4Address)
615 if (nodeList.empty())
649 newPacket->AddHeader(dsrRoutingHeader);
650 dsr->ScheduleInitialReply(newPacket, ipv4Address, nextHop,
m_ipv4Route);
660 bool addRoute =
false;
664 if (
dsr->IsLinkCache())
670 addRoute =
dsr->AddRoute(toSource);
692 if (nextHop ==
"0.0.0.0")
694 dsr->PacketNewRoute(dsrP, source, dst, protocol);
701 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
703 dsr->CancelRreqTimer(dst,
true);
727 else if (isRouteInCache && !areThereDuplicates)
741 bool addRoute =
false;
747 saveRoute.insert(saveRoute.begin(), source);
748 saveRoute.push_back(ipv4Address);
758 if (
dsr->IsLinkCache())
760 addRoute =
dsr->AddRoute_Link(saveRoute, ipv4Address);
764 addRoute =
dsr->AddRoute(toSource);
769 NS_LOG_LOGIC(
"We have added the route and search send buffer for packet with "
786 if (nextHop ==
"0.0.0.0")
788 dsr->PacketNewRoute(dsrP, source, dst, protocol);
795 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
797 dsr->CancelRreqTimer(dst,
true);
835 newPacket->AddHeader(dsrRoutingHeader);
836 dsr->ScheduleCachedReply(newPacket, ipv4Address, nextHop,
m_ipv4Route, hops);
844 mainVector.push_back(ipv4Address);
861 p->RemoveHeader(rerr);
866 if ((errorSrc == source) && (unreachNode == ipv4Address))
874 dsr->DeleteAllRoutesIncludeLink(errorSrc, unreachNode, ipv4Address);
895 ttl = ipv4Header.
GetTtl();
906 interP->AddPacketTag(tag);
907 interP->AddHeader(dsrRoutingHeader);
908 dsr->ScheduleInterRequest(interP, source);
954 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
955 << (
uint32_t)protocol << isPromisc);
963 p->CopyData(buf,
sizeof(buf));
964 uint8_t numberAddress = (buf[1] - 2) / 4;
969 p->RemoveHeader(rrep);
982 if (targetAddress == ipv4Address)
986 if (nodeList.empty())
1004 bool addRoute =
false;
1005 if (
dsr->IsLinkCache())
1007 addRoute =
dsr->AddRoute_Link(nodeList, ipv4Address);
1011 addRoute =
dsr->AddRoute(toDestination);
1017 "We have added the route and search send buffer for packet with destination "
1029 if (nextHop ==
"0.0.0.0")
1031 dsr->PacketNewRoute(dsrP, srcAddress, dst, protocol);
1037 dsr->CancelRreqTimer(dst,
true);
1041 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
1065 std::vector<Ipv4Address> routeCopy = nodeList;
1066 std::vector<Ipv4Address> cutRoute =
CutRoute(ipv4Address, nodeList);
1068 if (cutRoute.size() >= 2)
1071 NS_LOG_DEBUG(
"The route destination after cut " << dst);
1075 bool addRoute =
false;
1076 if (
dsr->IsLinkCache())
1078 addRoute =
dsr->AddRoute_Link(nodeList, ipv4Address);
1082 addRoute =
dsr->AddRoute(toDestination);
1086 dsr->CancelRreqTimer(dst,
true);
1102 NS_LOG_DEBUG(
"The nextHop address " << nextHop <<
" and the source in the route reply "
1118 newPacket->AddHeader(dsrRoutingHeader);
1132 .SetGroupName(
"Dsr")
1164 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Address << ipv4Header
1165 << (
uint32_t)protocol << isPromisc);
1169 p->CopyData(buf,
sizeof(buf));
1170 uint8_t numberAddress = (buf[1] - 2) / 4;
1173 p->RemoveHeader(sourceRoute);
1199 NS_LOG_LOGIC(
"We process promiscuous receipt data packet");
1203 dsr->SendGratuitousReply(source, srcAddress, nodeList, protocol);
1209 if (destAddress != destination)
1211 NS_LOG_DEBUG(
"Process the promiscuously received packet");
1212 bool findPassive =
false;
1214 for (
int32_t i = 0; i < nNodes; ++i)
1222 findPassive = dsrNode->PassiveEntryCheck(packet,
1237 NS_LOG_DEBUG(
"We find one previously received passive entry");
1247 dsrSrc->CancelPassiveTimer(packet, source, destination, segsLeft);
1252 dsr->PassiveEntryCheck(packet,
1269 uint8_t nextAddressIndex;
1274 auto data =
new uint8_t[size];
1275 p->CopyData(
data, size);
1276 uint8_t optionType = 0;
1277 optionType = *(
data);
1280 if (optionType == 160)
1282 NS_LOG_LOGIC(
"Remove the ack request header and add ack header to the packet");
1285 p->RemoveHeader(ackReq);
1286 uint16_t ackId = ackReq.
GetAckId();
1293 if (!nodeList.empty())
1295 if (segsLeft > numberAddress)
1303 if (numberAddress - segsLeft < 2)
1309 ackAddress = nodeList[numberAddress - segsLeft - 2];
1312 NS_LOG_DEBUG(
"Send back ACK to the earlier hop " << ackAddress <<
" from us "
1314 dsr->SendAck(ackId, ackAddress, source, destination,
m_ipv4Route);
1334 if (segsLeft > numberAddress)
1346 nextAddressIndex = numberAddress - segsLeft;
1348 NS_LOG_DEBUG(
"The next address of source route option "
1349 << nextAddress <<
" and the nextAddressIndex: " << (
uint32_t)nextAddressIndex
1350 <<
" and the segments left : " << (
uint32_t)segsLeft);
1362 if (nextHop ==
"0.0.0.0")
1365 dsr->PacketNewRoute(dsrP, realSource, targetAddress, protocol);
1369 if (ipv4Address == nextHop)
1382 SetRoute(nextAddress, ipv4Address);
1384 dsr->ForwardPacket(dsrP,
1403 .SetGroupName(
"Dsr")
1435 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1436 << (
uint32_t)protocol << isPromisc);
1439 auto data =
new uint8_t[size];
1440 p->CopyData(
data, size);
1441 uint8_t errorType = *(
data + 2);
1454 p->RemoveHeader(rerrUnreach);
1462 <<
"and the unreachable node is " << unreachAddress);
1471 dsr->DeleteAllRoutesIncludeLink(errorSource, unreachAddress, ipv4Address);
1487 p->RemoveHeader(rerrUnsupported);
1506 p->CopyData(buf,
sizeof(buf));
1507 uint8_t numberAddress = (buf[1] - 2) / 4;
1513 p->RemoveHeader(sourceRoute);
1524 uint8_t nextAddressIndex;
1543 if (segmentsLeft > numberAddress)
1552 if (segmentsLeft == 0 && targetAddress == ipv4Address)
1554 NS_LOG_INFO(
"This is the destination of the error, send error request");
1555 dsr->SendErrorRequest(rerr, protocol);
1556 return serializedSize;
1562 nextAddressIndex = numberAddress - segmentsLeft;
1572 return serializedSize;
1576 SetRoute(nextAddress, ipv4Address);
1577 dsr->ForwardErrPacket(rerr, newSourceRoute, nextAddress,
m_ipv4Route);
1578 return serializedSize;
1586 static TypeId tid =
TypeId(
"ns3::dsr::DsrOptionAckReq")
1588 .SetGroupName(
"Dsr")
1620 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1621 << (
uint32_t)protocol << isPromisc);
1630 p->RemoveHeader(ackReq);
1649 .SetGroupName(
"Dsr")
1681 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1682 << (
uint32_t)protocol << isPromisc);
1688 p->RemoveHeader(ack);
1700 dsr->UpdateRouteEntry(realDst);
1704 dsr->CallCancelPacketTimer(ackId, ipv4Header, realSrc, realDst);
Ipv4 addresses are stored in host order in this class.
Access to the IPv4 forwarding table, interfaces, and configuration.
static uint32_t GetNNodes()
static Ptr< Node > GetNode(uint32_t n)
Object()
Caller graph was not generated because of its size.
Smart pointer class similar to boost::intrusive_ptr.
This class implements a tag that carries the socket-specific TTL of a packet to the IP layer.
void SetTtl(uint8_t ttl)
Set the tag's TTL.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Hold an unsigned integer type.
uint8_t Process(Ptr< Packet > packet, Ptr< Packet > dsrP, Ipv4Address ipv4Address, Ipv4Address source, const Ipv4Header &ipv4Header, uint8_t protocol, bool &isPromisc, Ipv4Address promiscSource) override
Process method.
static const uint8_t OPT_NUMBER
The Dsr Ack option number.
static TypeId GetTypeId()
Get the type ID.
uint8_t GetOptionNumber() const override
Get the option number.
static TypeId GetTypeId()
Get the type ID.
uint8_t GetOptionNumber() const override
Get the option number.
uint8_t Process(Ptr< Packet > packet, Ptr< Packet > dsrP, Ipv4Address ipv4Address, Ipv4Address source, const Ipv4Header &ipv4Header, uint8_t protocol, bool &isPromisc, Ipv4Address promiscSource) override
Process method.
~DsrOptionAckReq() override
static const uint8_t OPT_NUMBER
Dsr ack request option number.
uint8_t GetOptionNumber() const override
Get the option number.
static TypeId GetTypeId()
Get the type ID.
~DsrOptionPad1() override
uint8_t Process(Ptr< Packet > packet, Ptr< Packet > dsrP, Ipv4Address ipv4Address, Ipv4Address source, const Ipv4Header &ipv4Header, uint8_t protocol, bool &isPromisc, Ipv4Address promiscSource) override
Process method.
static const uint8_t OPT_NUMBER
Pad1 option number.
static const uint8_t OPT_NUMBER
PadN option number.
uint8_t Process(Ptr< Packet > packet, Ptr< Packet > dsrP, Ipv4Address ipv4Address, Ipv4Address source, const Ipv4Header &ipv4Header, uint8_t protocol, bool &isPromisc, Ipv4Address promiscSource) override
Process method.
static TypeId GetTypeId()
Get the type ID.
uint8_t GetOptionNumber() const override
Get the option number.
~DsrOptionPadn() override
uint8_t DoSendError(Ptr< Packet > p, DsrOptionRerrUnreachHeader &rerr, uint32_t rerrSize, Ipv4Address ipv4Address, uint8_t protocol)
Do Send error message.
static const uint8_t OPT_NUMBER
Dsr Route Error option number.
static TypeId GetTypeId()
Get the type ID.
~DsrOptionRerr() override
uint8_t Process(Ptr< Packet > packet, Ptr< Packet > dsrP, Ipv4Address ipv4Address, Ipv4Address source, const Ipv4Header &ipv4Header, uint8_t protocol, bool &isPromisc, Ipv4Address promiscSource) override
Process method.
uint8_t GetOptionNumber() const override
Get the option number.
static const uint8_t OPT_NUMBER
Router alert option number.
static TypeId GetTypeId()
Get the type ID.
uint8_t GetOptionNumber() const override
Get the option number.
~DsrOptionRrep() override
uint8_t Process(Ptr< Packet > packet, Ptr< Packet > dsrP, Ipv4Address ipv4Address, Ipv4Address source, const Ipv4Header &ipv4Header, uint8_t protocol, bool &isPromisc, Ipv4Address promiscSource) override
Process method.
static TypeId GetTypeId()
Get the type ID.
static const uint8_t OPT_NUMBER
Rreq option number.
uint8_t Process(Ptr< Packet > packet, Ptr< Packet > dsrP, Ipv4Address ipv4Address, Ipv4Address source, const Ipv4Header &ipv4Header, uint8_t protocol, bool &isPromisc, Ipv4Address promiscSource) override
Process method.
uint8_t GetOptionNumber() const override
Get the option number.
~DsrOptionRreq() override
Destructor.
DsrOptionRreq()
Constructor.
static const uint8_t OPT_NUMBER
Source Route option number.
uint8_t Process(Ptr< Packet > packet, Ptr< Packet > dsrP, Ipv4Address ipv4Address, Ipv4Address source, const Ipv4Header &ipv4Header, uint8_t protocol, bool &isPromisc, Ipv4Address promiscSource) override
Process method.
uint8_t GetOptionNumber() const override
Get the option number.
static TypeId GetTypeId()
Get the type ID.
Ipv4Address SearchNextHop(Ipv4Address ipv4Address, std::vector< Ipv4Address > &vec)
Search for the next hop in the route.
Ipv4Address ReverseSearchNextTwoHop(Ipv4Address ipv4Address, std::vector< Ipv4Address > &vec)
Reverse search for the next two hop in the route.
TracedCallback< Ptr< const Packet > > m_dropTrace
Drop trace callback.
Ptr< Node > GetNodeWithAddress(Ipv4Address ipv4Address)
Get the node object with Ipv4Address.
Ptr< Node > GetNode() const
Get the node.
Time ActiveRouteTimeout
The active route timeout value.
bool CheckDuplicates(Ipv4Address ipv4Address, std::vector< Ipv4Address > &vec)
Check if the route already contains the node ip address.
void SetNode(Ptr< Node > node)
Set the node.
virtual uint8_t GetOptionNumber() const =0
Get the option number.
~DsrOptions() override
Destructor.
std::vector< Ipv4Address > m_finalRoute
The vector of final Ipv4 address.
void PrintVector(std::vector< Ipv4Address > &vec)
Print out the elements in the route vector.
bool IfDuplicates(std::vector< Ipv4Address > &vec, std::vector< Ipv4Address > &vec2)
Check if the two vectors contain duplicate or not.
bool ReverseRoutes(std::vector< Ipv4Address > &vec)
Reverse the routes.
void RemoveDuplicates(std::vector< Ipv4Address > &vec)
Remove the duplicates from the route.
static TypeId GetTypeId()
Get the type identificator.
std::vector< Ipv4Address > CutRoute(Ipv4Address ipv4Address, std::vector< Ipv4Address > &nodeList)
Cut the route from ipv4Address to the end of the route vector.
bool ContainAddressAfter(Ipv4Address ipv4Address, Ipv4Address destAddress, std::vector< Ipv4Address > &nodeList)
Search for the ipv4 address in the node list.
TracedCallback< const DsrOptionSRHeader & > m_rxPacketTrace
The receive trace back, only triggered when final destination receive data packet.
Ipv4Address ReverseSearchNextHop(Ipv4Address ipv4Address, std::vector< Ipv4Address > &vec)
Reverse search for the next hop in the route.
Ptr< Ipv4Route > m_ipv4Route
The ipv4 route.
virtual Ptr< Ipv4Route > SetRoute(Ipv4Address nextHop, Ipv4Address srcAddress)
Set the route to use for data packets, used by the option headers when sending data/control packets.
Ptr< Node > m_node
the node
DsrRouteCacheEntry class for entries in the route cache.
IP_VECTOR GetVector() const
Get the IP vector.
std::vector< Ipv4Address > IP_VECTOR
Define the vector to hold Ip address.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
constexpr uint8_t NO_NEXT_HEADER
No Next Header for IPV4 and IPv6 [RFC8200].
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeUintegerChecker()
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)