31#define NS_LOG_APPEND_CONTEXT \
32 if (GetObject<Node>()) \
34 std::clog << "[node " << GetObject<Node>()->GetId() << "] "; \
42#include "ns3/assert.h"
43#include "ns3/fatal-error.h"
44#include "ns3/icmpv4-l4-protocol.h"
45#include "ns3/ip-l4-protocol.h"
46#include "ns3/ipv4-address.h"
47#include "ns3/ipv4-header.h"
48#include "ns3/ipv4-interface.h"
49#include "ns3/ipv4-l3-protocol.h"
50#include "ns3/ipv4-route.h"
52#include "ns3/node-list.h"
54#include "ns3/object-vector.h"
55#include "ns3/pointer.h"
57#include "ns3/trace-source-accessor.h"
58#include "ns3/udp-header.h"
59#include "ns3/uinteger.h"
82 .AddAttribute(
"OptionNumber",
83 "The Dsr option number.",
86 MakeUintegerChecker<uint8_t>())
87 .AddTraceSource(
"Drop",
90 "ns3::Packet::TracedCallback")
92 "Receive DSR packet.",
94 "ns3::dsr::DsrOptionSRHeader::TracedCallback");
125 std::vector<Ipv4Address>& nodeList)
128 std::vector<Ipv4Address>::iterator it = find(nodeList.begin(), nodeList.end(), destAddress);
130 for (std::vector<Ipv4Address>::iterator i = it; i != nodeList.end(); ++i)
132 if ((ipv4Address == (*i)) && ((*i) != nodeList.back()))
140std::vector<Ipv4Address>
144 std::vector<Ipv4Address>::iterator it = find(nodeList.begin(), nodeList.end(), ipv4Address);
145 std::vector<Ipv4Address> cutRoute;
146 for (std::vector<Ipv4Address>::iterator i = it; i != nodeList.end(); ++i)
148 cutRoute.push_back(*i);
169 std::reverse(vec.begin(), vec.end());
187 if (ipv4Address == vec.back())
189 NS_LOG_DEBUG(
"We have reached to the final destination " << ipv4Address <<
" "
193 for (std::vector<Ipv4Address>::const_iterator i = vec.begin(); i != vec.end(); ++i)
195 if (ipv4Address == (*i))
202 NS_LOG_DEBUG(
"next hop address not found, route corrupted");
219 for (std::vector<Ipv4Address>::reverse_iterator ri = vec.rbegin(); ri != vec.rend(); ++ri)
221 if (ipv4Address == (*ri))
228 NS_LOG_DEBUG(
"next hop address not found, route corrupted");
240 for (std::vector<Ipv4Address>::reverse_iterator ri = vec.rbegin(); ri != vec.rend(); ++ri)
242 if (ipv4Address == (*ri))
244 nextTwoHop = *(ri + 2);
267 for (std::vector<Ipv4Address>::const_iterator i = vec.begin(); i != vec.end(); ++i)
278 for (std::vector<Ipv4Address>::const_iterator i = vec.begin(); i != vec.end(); ++i)
280 for (std::vector<Ipv4Address>::const_iterator j = vec2.begin(); j != vec2.end(); ++j)
295 for (std::vector<Ipv4Address>::const_iterator i = vec.begin(); i != vec.end(); ++i)
297 if ((*i) == ipv4Address)
311 std::vector<Ipv4Address> vec2(vec);
314 for (std::vector<Ipv4Address>::const_iterator i = vec2.begin(); i != vec2.end(); ++i)
322 for (std::vector<Ipv4Address>::iterator j = vec.begin(); j != vec.end(); ++j)
326 if ((j + 1) != vec.end())
328 vec.erase(j + 1, vec.end());
333 else if (j == (vec.end() - 1))
347 for (
int32_t i = 0; i < nNodes; ++i)
351 if (ipv4->GetAddress(1, 0).GetLocal() == address)
364 for (
int32_t i = 0; i < nNodes; ++i)
368 int32_t ifIndex = ipv4->GetInterfaceForAddress(ipv4Address);
417 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
418 << (
uint32_t)protocol << isPromisc);
421 p->RemoveHeader(pad1Header);
467 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
468 << (
uint32_t)protocol << isPromisc);
472 p->RemoveHeader(padnHeader);
525 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
526 << (
uint32_t)protocol << isPromisc);
533 if (source == ipv4Address)
535 NS_LOG_DEBUG(
"Discard the packet since it was originated from same source address");
553 p->CopyData(buf,
sizeof(buf));
554 uint8_t numberAddress = (buf[1] - 6) / 4;
556 if (numberAddress >= 255)
558 NS_LOG_DEBUG(
"Discard the packet, malformed header since two many ip addresses in route");
572 p->RemoveHeader(rreq);
582 uint16_t requestId = rreq.
GetId();
587 std::vector<Ipv4Address> nodeList(mainVector);
606 uint8_t ttl = ipv4Header.
GetTtl();
607 bool dupRequest =
false;
612 dupRequest = dsr->FindSourceEntry(sourceAddress, targetAddress, requestId);
635 NS_LOG_DEBUG(
"Our node address is already seen in the route, drop the request");
642 bool isRouteInCache = dsr->LookupRoute(targetAddress, toPrev);
646 std::vector<Ipv4Address> saveRoute(nodeList);
661 NS_LOG_DEBUG(
"The target address over here " << targetAddress <<
" and the ip address "
662 << ipv4Address <<
" and the source address "
664 if (targetAddress == ipv4Address)
667 if (nodeList.size() == 1)
675 nextHop = srcAddress;
679 std::vector<Ipv4Address> changeRoute(nodeList);
680 changeRoute.push_back(ipv4Address);
682 for (std::vector<Ipv4Address>::iterator i = changeRoute.begin();
683 i != changeRoute.end();
712 newPacket->AddHeader(dsrRoutingHeader);
713 dsr->ScheduleInitialReply(newPacket, ipv4Address, nextHop,
m_ipv4Route);
723 bool addRoute =
false;
724 if (numberAddress > 0)
729 if (dsr->IsLinkCache())
731 addRoute = dsr->AddRoute_Link(
m_finalRoute, ipv4Address);
735 addRoute = dsr->AddRoute(toSource);
767 if (nextHop ==
"0.0.0.0")
769 dsr->PacketNewRoute(dsrP, ipv4Address, dst, protocol);
776 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
778 dsr->CancelRreqTimer(dst,
true);
802 else if (isRouteInCache && !areThereDuplicates)
808 for (std::vector<Ipv4Address>::iterator i = saveRoute.begin(); i != saveRoute.end();
817 for (std::vector<Ipv4Address>::iterator j = ip.begin(); j != ip.end(); ++j)
825 bool addRoute =
false;
830 saveRoute.push_back(ipv4Address);
839 NS_ASSERT(saveRoute.front() == ipv4Address);
841 if (dsr->IsLinkCache())
843 addRoute = dsr->AddRoute_Link(saveRoute, ipv4Address);
847 addRoute = dsr->AddRoute(toSource);
852 NS_LOG_LOGIC(
"We have added the route and search send buffer for packet with "
873 if (nextHop ==
"0.0.0.0")
875 dsr->PacketNewRoute(dsrP, ipv4Address, dst, protocol);
882 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
884 dsr->CancelRreqTimer(dst,
true);
915 NS_LOG_DEBUG(
"This is the full route from " << realSource <<
" to "
931 newPacket->AddHeader(dsrRoutingHeader);
932 dsr->ScheduleCachedReply(newPacket, ipv4Address, nextHop,
m_ipv4Route, hops);
941 mainVector.push_back(ipv4Address);
952 p->RemoveHeader(rerr);
957 if ((errorSrc == srcAddress) && (unreachNode == ipv4Address))
967 dsr->DeleteAllRoutesIncludeLink(errorSrc, unreachNode, ipv4Address);
977 NS_LOG_DEBUG(
"The RREQ and newUnreach header length " << length);
991 uint8_t ttl = ipv4Header.
GetTtl();
1002 interP->AddPacketTag(tag);
1003 interP->AddHeader(dsrRoutingHeader);
1004 dsr->ScheduleInterRequest(interP);
1020 .SetGroupName(
"Dsr")
1059 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1060 << (
uint32_t)protocol << isPromisc);
1066 p->CopyData(buf,
sizeof(buf));
1067 uint8_t numberAddress = (buf[1] - 2) / 4;
1072 p->RemoveHeader(rrep);
1085 if (targetAddress == ipv4Address)
1089 if (nodeList.empty())
1107 NS_ASSERT(nodeList.front() == ipv4Address);
1108 bool addRoute =
false;
1109 if (dsr->IsLinkCache())
1111 addRoute = dsr->AddRoute_Link(nodeList, ipv4Address);
1115 addRoute = dsr->AddRoute(toDestination);
1121 "We have added the route and search send buffer for packet with destination "
1133 if (nextHop ==
"0.0.0.0")
1135 dsr->PacketNewRoute(dsrP, ipv4Address, dst, protocol);
1141 dsr->CancelRreqTimer(dst,
true);
1145 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
1159 if (length % 2 != 0)
1170 std::vector<Ipv4Address> routeCopy = nodeList;
1171 std::vector<Ipv4Address> cutRoute =
CutRoute(ipv4Address, nodeList);
1173 if (cutRoute.size() >= 2)
1176 NS_LOG_DEBUG(
"The route destination after cut " << dst);
1180 NS_ASSERT(cutRoute.front() == ipv4Address);
1181 bool addRoute =
false;
1182 if (dsr->IsLinkCache())
1184 addRoute = dsr->AddRoute_Link(nodeList, ipv4Address);
1188 addRoute = dsr->AddRoute(toDestination);
1192 dsr->CancelRreqTimer(dst,
true);
1209 NS_LOG_DEBUG(
"The nextHop address " << nextHop <<
" and the source in the route reply "
1229 newPacket->AddHeader(dsrRoutingHeader);
1230 dsr->SendReply(newPacket, ipv4Address, nextHop,
m_ipv4Route);
1243 .SetGroupName(
"Dsr")
1281 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Address << ipv4Header
1282 << (
uint32_t)protocol << isPromisc);
1286 p->CopyData(buf,
sizeof(buf));
1287 uint8_t numberAddress = (buf[1] - 2) / 4;
1290 p->RemoveHeader(sourceRoute);
1316 NS_LOG_LOGIC(
"We process promiscuous receipt data packet");
1320 dsr->SendGratuitousReply(source, srcAddress, nodeList, protocol);
1326 if (destAddress != destination)
1328 NS_LOG_DEBUG(
"Process the promiscuously received packet");
1329 bool findPassive =
false;
1331 for (
int32_t i = 0; i < nNodes; ++i)
1339 findPassive = dsrNode->PassiveEntryCheck(packet,
1354 NS_LOG_DEBUG(
"We find one previously received passive entry");
1364 dsrSrc->CancelPassiveTimer(packet, source, destination, segsLeft);
1369 dsr->PassiveEntryCheck(packet,
1386 uint8_t length = sourceRoute.
GetLength();
1387 uint8_t nextAddressIndex;
1392 uint8_t*
data =
new uint8_t[size];
1393 p->CopyData(
data, size);
1394 uint8_t optionType = 0;
1395 optionType = *(
data);
1398 if (optionType == 160)
1400 NS_LOG_LOGIC(
"Remove the ack request header and add ack header to the packet");
1403 p->RemoveHeader(ackReq);
1404 uint16_t ackId = ackReq.
GetAckId();
1411 if (!nodeList.empty())
1413 if (segsLeft > numberAddress)
1421 if (numberAddress - segsLeft < 2)
1427 ackAddress = nodeList[numberAddress - segsLeft - 2];
1430 NS_LOG_DEBUG(
"Send back ACK to the earlier hop " << ackAddress <<
" from us "
1432 dsr->SendAck(ackId, ackAddress, source, destination, protocol,
m_ipv4Route);
1445 if (length % 2 != 0)
1452 if (segsLeft > numberAddress)
1464 nextAddressIndex = numberAddress - segsLeft;
1466 NS_LOG_DEBUG(
"The next address of source route option "
1467 << nextAddress <<
" and the nextAddressIndex: " << (
uint32_t)nextAddressIndex
1468 <<
" and the segments left : " << (
uint32_t)segsLeft);
1480 if (nextHop ==
"0.0.0.0")
1483 dsr->PacketNewRoute(dsrP, realSource, targetAddress, protocol);
1487 if (ipv4Address == nextHop)
1500 SetRoute(nextAddress, ipv4Address);
1502 dsr->ForwardPacket(dsrP,
1521 .SetGroupName(
"Dsr")
1559 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1560 << (
uint32_t)protocol << isPromisc);
1563 uint8_t*
data =
new uint8_t[size];
1564 p->CopyData(
data, size);
1565 uint8_t errorType = *(
data + 2);
1578 p->RemoveHeader(rerrUnreach);
1586 <<
"and the unreachable node is " << unreachAddress);
1595 dsr->DeleteAllRoutesIncludeLink(errorSource, unreachAddress, ipv4Address);
1612 p->RemoveHeader(rerrUnsupported);
1631 p->CopyData(buf,
sizeof(buf));
1632 uint8_t numberAddress = (buf[1] - 2) / 4;
1638 p->RemoveHeader(sourceRoute);
1649 uint8_t length = sourceRoute.
GetLength();
1650 uint8_t nextAddressIndex;
1662 if (length % 2 != 0)
1669 if (segmentsLeft > numberAddress)
1678 if (segmentsLeft == 0 && targetAddress == ipv4Address)
1680 NS_LOG_INFO(
"This is the destination of the error, send error request");
1681 dsr->SendErrorRequest(rerr, protocol);
1682 return serializedSize;
1688 nextAddressIndex = numberAddress - segmentsLeft;
1698 return serializedSize;
1702 SetRoute(nextAddress, ipv4Address);
1703 dsr->ForwardErrPacket(rerr, newSourceRoute, nextAddress, protocol,
m_ipv4Route);
1704 return serializedSize;
1712 static TypeId tid =
TypeId(
"ns3::dsr::DsrOptionAckReq")
1714 .SetGroupName(
"Dsr")
1752 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1753 << (
uint32_t)protocol << isPromisc);
1763 p->RemoveHeader(ackReq);
1782 .SetGroupName(
"Dsr")
1820 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1821 << (
uint32_t)protocol << isPromisc);
1827 p->RemoveHeader(ack);
1839 dsr->UpdateRouteEntry(realDst);
1843 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.
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 AttributeAccessor > MakeUintegerAccessor(T1 a1)
#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< 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.