20#define NS_LOG_APPEND_CONTEXT \
21 if (GetObject<Node>()) \
23 std::clog << "[node " << GetObject<Node>()->GetId() << "] "; \
31#include "ns3/assert.h"
32#include "ns3/fatal-error.h"
33#include "ns3/icmpv4-l4-protocol.h"
34#include "ns3/ip-l4-protocol.h"
35#include "ns3/ipv4-address.h"
36#include "ns3/ipv4-header.h"
37#include "ns3/ipv4-interface.h"
38#include "ns3/ipv4-l3-protocol.h"
39#include "ns3/ipv4-route.h"
41#include "ns3/node-list.h"
43#include "ns3/object-vector.h"
44#include "ns3/pointer.h"
46#include "ns3/trace-source-accessor.h"
47#include "ns3/udp-header.h"
48#include "ns3/uinteger.h"
71 .AddAttribute(
"OptionNumber",
72 "The Dsr option number.",
76 .AddTraceSource(
"Drop",
79 "ns3::Packet::TracedCallback")
81 "Receive DSR packet.",
83 "ns3::dsr::DsrOptionSRHeader::TracedCallback");
114 std::vector<Ipv4Address>& nodeList)
117 auto it = find(nodeList.begin(), nodeList.end(), destAddress);
119 for (
auto i = it; i != nodeList.end(); ++i)
121 if ((ipv4Address == (*i)) && ((*i) != nodeList.back()))
129std::vector<Ipv4Address>
133 auto it = find(nodeList.begin(), nodeList.end(), ipv4Address);
134 std::vector<Ipv4Address> cutRoute;
135 for (
auto i = it; i != nodeList.end(); ++i)
137 cutRoute.push_back(*i);
158 std::reverse(vec.begin(), vec.end());
176 if (ipv4Address == vec.back())
178 NS_LOG_DEBUG(
"We have reached to the final destination " << ipv4Address <<
" "
182 for (
auto i = vec.begin(); i != vec.end(); ++i)
184 if (ipv4Address == (*i))
191 NS_LOG_DEBUG(
"next hop address not found, route corrupted");
208 for (
auto ri = vec.rbegin(); ri != vec.rend(); ++ri)
210 if (ipv4Address == (*ri))
217 NS_LOG_DEBUG(
"next hop address not found, route corrupted");
229 for (
auto ri = vec.rbegin(); ri != vec.rend(); ++ri)
231 if (ipv4Address == (*ri))
233 nextTwoHop = *(ri + 2);
256 for (
auto i = vec.begin(); i != vec.end(); ++i)
267 for (
auto i = vec.begin(); i != vec.end(); ++i)
269 for (
auto j = vec2.begin(); j != vec2.end(); ++j)
284 for (
auto i = vec.begin(); i != vec.end(); ++i)
286 if ((*i) == ipv4Address)
300 std::vector<Ipv4Address> vec2(vec);
303 for (
auto i = vec2.begin(); i != vec2.end(); ++i)
311 for (
auto j = vec.begin(); j != vec.end(); ++j)
315 if ((j + 1) != vec.end())
317 vec.erase(j + 1, vec.end());
322 else if (j == (vec.end() - 1))
336 for (
int32_t i = 0; i < nNodes; ++i)
340 if (ipv4->GetAddress(1, 0).GetLocal() == address)
353 for (
int32_t i = 0; i < nNodes; ++i)
357 int32_t ifIndex = ipv4->GetInterfaceForAddress(ipv4Address);
406 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
407 << (
uint32_t)protocol << isPromisc);
410 p->RemoveHeader(pad1Header);
456 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
457 << (
uint32_t)protocol << isPromisc);
461 p->RemoveHeader(padnHeader);
514 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
515 << (
uint32_t)protocol << isPromisc);
522 if (source == ipv4Address)
524 NS_LOG_DEBUG(
"Discard the packet since it was originated from same source address");
542 p->CopyData(buf,
sizeof(buf));
543 uint8_t numberAddress = (buf[1] - 6) / 4;
545 if (numberAddress >= 255)
547 NS_LOG_DEBUG(
"Discard the packet, malformed header since two many ip addresses in route");
561 p->RemoveHeader(rreq);
571 uint16_t requestId = rreq.
GetId();
576 std::vector<Ipv4Address> nodeList(mainVector);
595 uint8_t ttl = ipv4Header.
GetTtl();
596 bool dupRequest =
false;
601 dupRequest = dsr->FindSourceEntry(sourceAddress, targetAddress, requestId);
624 NS_LOG_DEBUG(
"Our node address is already seen in the route, drop the request");
631 bool isRouteInCache = dsr->LookupRoute(targetAddress, toPrev);
635 std::vector<Ipv4Address> saveRoute(nodeList);
650 NS_LOG_DEBUG(
"The target address over here " << targetAddress <<
" and the ip address "
651 << ipv4Address <<
" and the source address "
653 if (targetAddress == ipv4Address)
656 if (nodeList.size() == 1)
664 nextHop = srcAddress;
668 std::vector<Ipv4Address> changeRoute(nodeList);
669 changeRoute.push_back(ipv4Address);
671 for (
auto i = changeRoute.begin(); i != changeRoute.end(); ++i)
699 newPacket->AddHeader(dsrRoutingHeader);
700 dsr->ScheduleInitialReply(newPacket, ipv4Address, nextHop,
m_ipv4Route);
710 bool addRoute =
false;
711 if (numberAddress > 0)
716 if (dsr->IsLinkCache())
718 addRoute = dsr->AddRoute_Link(
m_finalRoute, ipv4Address);
722 addRoute = dsr->AddRoute(toSource);
754 if (nextHop ==
"0.0.0.0")
756 dsr->PacketNewRoute(dsrP, ipv4Address, dst, protocol);
763 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
765 dsr->CancelRreqTimer(dst,
true);
789 else if (isRouteInCache && !areThereDuplicates)
795 for (
auto i = saveRoute.begin(); i != saveRoute.end(); ++i)
803 for (
auto j = ip.begin(); j != ip.end(); ++j)
811 bool addRoute =
false;
816 saveRoute.push_back(ipv4Address);
825 NS_ASSERT(saveRoute.front() == ipv4Address);
827 if (dsr->IsLinkCache())
829 addRoute = dsr->AddRoute_Link(saveRoute, ipv4Address);
833 addRoute = dsr->AddRoute(toSource);
838 NS_LOG_LOGIC(
"We have added the route and search send buffer for packet with "
859 if (nextHop ==
"0.0.0.0")
861 dsr->PacketNewRoute(dsrP, ipv4Address, dst, protocol);
868 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
870 dsr->CancelRreqTimer(dst,
true);
901 NS_LOG_DEBUG(
"This is the full route from " << realSource <<
" to "
917 newPacket->AddHeader(dsrRoutingHeader);
918 dsr->ScheduleCachedReply(newPacket, ipv4Address, nextHop,
m_ipv4Route, hops);
927 mainVector.push_back(ipv4Address);
938 p->RemoveHeader(rerr);
943 if ((errorSrc == srcAddress) && (unreachNode == ipv4Address))
953 dsr->DeleteAllRoutesIncludeLink(errorSrc, unreachNode, ipv4Address);
963 NS_LOG_DEBUG(
"The RREQ and newUnreach header length " << length);
977 uint8_t ttl = ipv4Header.
GetTtl();
988 interP->AddPacketTag(tag);
989 interP->AddHeader(dsrRoutingHeader);
990 dsr->ScheduleInterRequest(interP);
1006 .SetGroupName(
"Dsr")
1045 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1046 << (
uint32_t)protocol << isPromisc);
1052 p->CopyData(buf,
sizeof(buf));
1053 uint8_t numberAddress = (buf[1] - 2) / 4;
1058 p->RemoveHeader(rrep);
1071 if (targetAddress == ipv4Address)
1075 if (nodeList.empty())
1093 NS_ASSERT(nodeList.front() == ipv4Address);
1094 bool addRoute =
false;
1095 if (dsr->IsLinkCache())
1097 addRoute = dsr->AddRoute_Link(nodeList, ipv4Address);
1101 addRoute = dsr->AddRoute(toDestination);
1107 "We have added the route and search send buffer for packet with destination "
1119 if (nextHop ==
"0.0.0.0")
1121 dsr->PacketNewRoute(dsrP, ipv4Address, dst, protocol);
1127 dsr->CancelRreqTimer(dst,
true);
1131 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
1145 if (length % 2 != 0)
1156 std::vector<Ipv4Address> routeCopy = nodeList;
1157 std::vector<Ipv4Address> cutRoute =
CutRoute(ipv4Address, nodeList);
1159 if (cutRoute.size() >= 2)
1162 NS_LOG_DEBUG(
"The route destination after cut " << dst);
1166 NS_ASSERT(cutRoute.front() == ipv4Address);
1167 bool addRoute =
false;
1168 if (dsr->IsLinkCache())
1170 addRoute = dsr->AddRoute_Link(nodeList, ipv4Address);
1174 addRoute = dsr->AddRoute(toDestination);
1178 dsr->CancelRreqTimer(dst,
true);
1195 NS_LOG_DEBUG(
"The nextHop address " << nextHop <<
" and the source in the route reply "
1215 newPacket->AddHeader(dsrRoutingHeader);
1216 dsr->SendReply(newPacket, ipv4Address, nextHop,
m_ipv4Route);
1229 .SetGroupName(
"Dsr")
1267 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Address << ipv4Header
1268 << (
uint32_t)protocol << isPromisc);
1272 p->CopyData(buf,
sizeof(buf));
1273 uint8_t numberAddress = (buf[1] - 2) / 4;
1276 p->RemoveHeader(sourceRoute);
1302 NS_LOG_LOGIC(
"We process promiscuous receipt data packet");
1306 dsr->SendGratuitousReply(source, srcAddress, nodeList, protocol);
1312 if (destAddress != destination)
1314 NS_LOG_DEBUG(
"Process the promiscuously received packet");
1315 bool findPassive =
false;
1317 for (
int32_t i = 0; i < nNodes; ++i)
1325 findPassive = dsrNode->PassiveEntryCheck(packet,
1340 NS_LOG_DEBUG(
"We find one previously received passive entry");
1350 dsrSrc->CancelPassiveTimer(packet, source, destination, segsLeft);
1355 dsr->PassiveEntryCheck(packet,
1372 uint8_t length = sourceRoute.
GetLength();
1373 uint8_t nextAddressIndex;
1378 auto data =
new uint8_t[size];
1379 p->CopyData(
data, size);
1380 uint8_t optionType = 0;
1381 optionType = *(
data);
1384 if (optionType == 160)
1386 NS_LOG_LOGIC(
"Remove the ack request header and add ack header to the packet");
1389 p->RemoveHeader(ackReq);
1390 uint16_t ackId = ackReq.
GetAckId();
1397 if (!nodeList.empty())
1399 if (segsLeft > numberAddress)
1407 if (numberAddress - segsLeft < 2)
1413 ackAddress = nodeList[numberAddress - segsLeft - 2];
1416 NS_LOG_DEBUG(
"Send back ACK to the earlier hop " << ackAddress <<
" from us "
1418 dsr->SendAck(ackId, ackAddress, source, destination, protocol,
m_ipv4Route);
1431 if (length % 2 != 0)
1438 if (segsLeft > numberAddress)
1450 nextAddressIndex = numberAddress - segsLeft;
1452 NS_LOG_DEBUG(
"The next address of source route option "
1453 << nextAddress <<
" and the nextAddressIndex: " << (
uint32_t)nextAddressIndex
1454 <<
" and the segments left : " << (
uint32_t)segsLeft);
1466 if (nextHop ==
"0.0.0.0")
1469 dsr->PacketNewRoute(dsrP, realSource, targetAddress, protocol);
1473 if (ipv4Address == nextHop)
1486 SetRoute(nextAddress, ipv4Address);
1488 dsr->ForwardPacket(dsrP,
1507 .SetGroupName(
"Dsr")
1545 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1546 << (
uint32_t)protocol << isPromisc);
1549 auto data =
new uint8_t[size];
1550 p->CopyData(
data, size);
1551 uint8_t errorType = *(
data + 2);
1564 p->RemoveHeader(rerrUnreach);
1572 <<
"and the unreachable node is " << unreachAddress);
1581 dsr->DeleteAllRoutesIncludeLink(errorSource, unreachAddress, ipv4Address);
1598 p->RemoveHeader(rerrUnsupported);
1617 p->CopyData(buf,
sizeof(buf));
1618 uint8_t numberAddress = (buf[1] - 2) / 4;
1624 p->RemoveHeader(sourceRoute);
1635 uint8_t length = sourceRoute.
GetLength();
1636 uint8_t nextAddressIndex;
1648 if (length % 2 != 0)
1655 if (segmentsLeft > numberAddress)
1664 if (segmentsLeft == 0 && targetAddress == ipv4Address)
1666 NS_LOG_INFO(
"This is the destination of the error, send error request");
1667 dsr->SendErrorRequest(rerr, protocol);
1668 return serializedSize;
1674 nextAddressIndex = numberAddress - segmentsLeft;
1684 return serializedSize;
1688 SetRoute(nextAddress, ipv4Address);
1689 dsr->ForwardErrPacket(rerr, newSourceRoute, nextAddress, protocol,
m_ipv4Route);
1690 return serializedSize;
1698 static TypeId tid =
TypeId(
"ns3::dsr::DsrOptionAckReq")
1700 .SetGroupName(
"Dsr")
1738 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1739 << (
uint32_t)protocol << isPromisc);
1749 p->RemoveHeader(ackReq);
1768 .SetGroupName(
"Dsr")
1806 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1807 << (
uint32_t)protocol << isPromisc);
1813 p->RemoveHeader(ack);
1825 dsr->UpdateRouteEntry(realDst);
1829 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)
A base class which provides memory management and object aggregation.
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.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
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.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
void AddDsrOption(const DsrOptionHeader &option)
Serialize the option, prepending pad1 or padn option as necessary.
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.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
static const uint8_t OPT_NUMBER
Router alert option number.
static TypeId GetTypeId()
Get the type ID.
TypeId GetInstanceTypeId() const override
Get the instance 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.
TypeId GetInstanceTypeId() const override
Get the instance 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.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
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.
uint32_t GetIDfromIP(Ipv4Address address)
Get the node id with Ipv4Address.
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.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeUintegerChecker()
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)