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);
508 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
509 << (
uint32_t)protocol << isPromisc);
516 if (source == ipv4Address)
518 NS_LOG_DEBUG(
"Discard the packet since it was originated from same source address");
536 p->CopyData(buf,
sizeof(buf));
537 uint8_t numberAddress = (buf[1] - 6) / 4;
539 if (numberAddress >= 255)
541 NS_LOG_DEBUG(
"Discard the packet, malformed header since two many ip addresses in route");
555 p->RemoveHeader(rreq);
565 uint16_t requestId = rreq.
GetId();
570 std::vector<Ipv4Address> nodeList(mainVector);
589 uint8_t ttl = ipv4Header.
GetTtl();
590 bool dupRequest =
false;
595 dupRequest = dsr->FindSourceEntry(sourceAddress, targetAddress, requestId);
618 NS_LOG_DEBUG(
"Our node address is already seen in the route, drop the request");
625 bool isRouteInCache = dsr->LookupRoute(targetAddress, toPrev);
629 std::vector<Ipv4Address> saveRoute(nodeList);
644 NS_LOG_DEBUG(
"The target address over here " << targetAddress <<
" and the ip address "
645 << ipv4Address <<
" and the source address "
647 if (targetAddress == ipv4Address)
650 if (nodeList.size() == 1)
658 nextHop = srcAddress;
662 std::vector<Ipv4Address> changeRoute(nodeList);
663 changeRoute.push_back(ipv4Address);
665 for (
auto i = changeRoute.begin(); i != changeRoute.end(); ++i)
693 newPacket->AddHeader(dsrRoutingHeader);
694 dsr->ScheduleInitialReply(newPacket, ipv4Address, nextHop,
m_ipv4Route);
704 bool addRoute =
false;
705 if (numberAddress > 0)
710 if (dsr->IsLinkCache())
712 addRoute = dsr->AddRoute_Link(
m_finalRoute, ipv4Address);
716 addRoute = dsr->AddRoute(toSource);
748 if (nextHop ==
"0.0.0.0")
750 dsr->PacketNewRoute(dsrP, ipv4Address, dst, protocol);
757 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
759 dsr->CancelRreqTimer(dst,
true);
783 else if (isRouteInCache && !areThereDuplicates)
789 for (
auto i = saveRoute.begin(); i != saveRoute.end(); ++i)
797 for (
auto j = ip.begin(); j != ip.end(); ++j)
805 bool addRoute =
false;
810 saveRoute.push_back(ipv4Address);
819 NS_ASSERT(saveRoute.front() == ipv4Address);
821 if (dsr->IsLinkCache())
823 addRoute = dsr->AddRoute_Link(saveRoute, ipv4Address);
827 addRoute = dsr->AddRoute(toSource);
832 NS_LOG_LOGIC(
"We have added the route and search send buffer for packet with "
853 if (nextHop ==
"0.0.0.0")
855 dsr->PacketNewRoute(dsrP, ipv4Address, dst, protocol);
862 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
864 dsr->CancelRreqTimer(dst,
true);
895 NS_LOG_DEBUG(
"This is the full route from " << realSource <<
" to "
911 newPacket->AddHeader(dsrRoutingHeader);
912 dsr->ScheduleCachedReply(newPacket, ipv4Address, nextHop,
m_ipv4Route, hops);
921 mainVector.push_back(ipv4Address);
932 p->RemoveHeader(rerr);
937 if ((errorSrc == srcAddress) && (unreachNode == ipv4Address))
947 dsr->DeleteAllRoutesIncludeLink(errorSrc, unreachNode, ipv4Address);
957 NS_LOG_DEBUG(
"The RREQ and newUnreach header length " << length);
971 uint8_t ttl = ipv4Header.
GetTtl();
982 interP->AddPacketTag(tag);
983 interP->AddHeader(dsrRoutingHeader);
984 dsr->ScheduleInterRequest(interP);
1000 .SetGroupName(
"Dsr")
1033 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1034 << (
uint32_t)protocol << isPromisc);
1040 p->CopyData(buf,
sizeof(buf));
1041 uint8_t numberAddress = (buf[1] - 2) / 4;
1046 p->RemoveHeader(rrep);
1059 if (targetAddress == ipv4Address)
1063 if (nodeList.empty())
1081 NS_ASSERT(nodeList.front() == ipv4Address);
1082 bool addRoute =
false;
1083 if (dsr->IsLinkCache())
1085 addRoute = dsr->AddRoute_Link(nodeList, ipv4Address);
1089 addRoute = dsr->AddRoute(toDestination);
1095 "We have added the route and search send buffer for packet with destination "
1107 if (nextHop ==
"0.0.0.0")
1109 dsr->PacketNewRoute(dsrP, ipv4Address, dst, protocol);
1115 dsr->CancelRreqTimer(dst,
true);
1119 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
1133 if (length % 2 != 0)
1144 std::vector<Ipv4Address> routeCopy = nodeList;
1145 std::vector<Ipv4Address> cutRoute =
CutRoute(ipv4Address, nodeList);
1147 if (cutRoute.size() >= 2)
1150 NS_LOG_DEBUG(
"The route destination after cut " << dst);
1154 NS_ASSERT(cutRoute.front() == ipv4Address);
1155 bool addRoute =
false;
1156 if (dsr->IsLinkCache())
1158 addRoute = dsr->AddRoute_Link(nodeList, ipv4Address);
1162 addRoute = dsr->AddRoute(toDestination);
1166 dsr->CancelRreqTimer(dst,
true);
1183 NS_LOG_DEBUG(
"The nextHop address " << nextHop <<
" and the source in the route reply "
1203 newPacket->AddHeader(dsrRoutingHeader);
1204 dsr->SendReply(newPacket, ipv4Address, nextHop,
m_ipv4Route);
1217 .SetGroupName(
"Dsr")
1249 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Address << ipv4Header
1250 << (
uint32_t)protocol << isPromisc);
1254 p->CopyData(buf,
sizeof(buf));
1255 uint8_t numberAddress = (buf[1] - 2) / 4;
1258 p->RemoveHeader(sourceRoute);
1284 NS_LOG_LOGIC(
"We process promiscuous receipt data packet");
1288 dsr->SendGratuitousReply(source, srcAddress, nodeList, protocol);
1294 if (destAddress != destination)
1296 NS_LOG_DEBUG(
"Process the promiscuously received packet");
1297 bool findPassive =
false;
1299 for (
int32_t i = 0; i < nNodes; ++i)
1307 findPassive = dsrNode->PassiveEntryCheck(packet,
1322 NS_LOG_DEBUG(
"We find one previously received passive entry");
1332 dsrSrc->CancelPassiveTimer(packet, source, destination, segsLeft);
1337 dsr->PassiveEntryCheck(packet,
1354 uint8_t length = sourceRoute.
GetLength();
1355 uint8_t nextAddressIndex;
1360 auto data =
new uint8_t[size];
1361 p->CopyData(
data, size);
1362 uint8_t optionType = 0;
1363 optionType = *(
data);
1366 if (optionType == 160)
1368 NS_LOG_LOGIC(
"Remove the ack request header and add ack header to the packet");
1371 p->RemoveHeader(ackReq);
1372 uint16_t ackId = ackReq.
GetAckId();
1379 if (!nodeList.empty())
1381 if (segsLeft > numberAddress)
1389 if (numberAddress - segsLeft < 2)
1395 ackAddress = nodeList[numberAddress - segsLeft - 2];
1398 NS_LOG_DEBUG(
"Send back ACK to the earlier hop " << ackAddress <<
" from us "
1400 dsr->SendAck(ackId, ackAddress, source, destination, protocol,
m_ipv4Route);
1413 if (length % 2 != 0)
1420 if (segsLeft > numberAddress)
1432 nextAddressIndex = numberAddress - segsLeft;
1434 NS_LOG_DEBUG(
"The next address of source route option "
1435 << nextAddress <<
" and the nextAddressIndex: " << (
uint32_t)nextAddressIndex
1436 <<
" and the segments left : " << (
uint32_t)segsLeft);
1448 if (nextHop ==
"0.0.0.0")
1451 dsr->PacketNewRoute(dsrP, realSource, targetAddress, protocol);
1455 if (ipv4Address == nextHop)
1468 SetRoute(nextAddress, ipv4Address);
1470 dsr->ForwardPacket(dsrP,
1489 .SetGroupName(
"Dsr")
1521 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1522 << (
uint32_t)protocol << isPromisc);
1525 auto data =
new uint8_t[size];
1526 p->CopyData(
data, size);
1527 uint8_t errorType = *(
data + 2);
1540 p->RemoveHeader(rerrUnreach);
1548 <<
"and the unreachable node is " << unreachAddress);
1557 dsr->DeleteAllRoutesIncludeLink(errorSource, unreachAddress, ipv4Address);
1574 p->RemoveHeader(rerrUnsupported);
1593 p->CopyData(buf,
sizeof(buf));
1594 uint8_t numberAddress = (buf[1] - 2) / 4;
1600 p->RemoveHeader(sourceRoute);
1611 uint8_t length = sourceRoute.
GetLength();
1612 uint8_t nextAddressIndex;
1624 if (length % 2 != 0)
1631 if (segmentsLeft > numberAddress)
1640 if (segmentsLeft == 0 && targetAddress == ipv4Address)
1642 NS_LOG_INFO(
"This is the destination of the error, send error request");
1643 dsr->SendErrorRequest(rerr, protocol);
1644 return serializedSize;
1650 nextAddressIndex = numberAddress - segmentsLeft;
1660 return serializedSize;
1664 SetRoute(nextAddress, ipv4Address);
1665 dsr->ForwardErrPacket(rerr, newSourceRoute, nextAddress, protocol,
m_ipv4Route);
1666 return serializedSize;
1674 static TypeId tid =
TypeId(
"ns3::dsr::DsrOptionAckReq")
1676 .SetGroupName(
"Dsr")
1708 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1709 << (
uint32_t)protocol << isPromisc);
1719 p->RemoveHeader(ackReq);
1738 .SetGroupName(
"Dsr")
1770 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1771 << (
uint32_t)protocol << isPromisc);
1777 p->RemoveHeader(ack);
1789 dsr->UpdateRouteEntry(realDst);
1793 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.
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.
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.
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.
Introspection did not find any typical Config paths.
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,...
Ptr< const AttributeChecker > MakeUintegerChecker()
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
#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.