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 auto it = find(nodeList.begin(), nodeList.end(), destAddress);
130 for (
auto i = it; i != nodeList.end(); ++i)
132 if ((ipv4Address == (*i)) && ((*i) != nodeList.back()))
140 std::vector<Ipv4Address>
144 auto it = find(nodeList.begin(), nodeList.end(), ipv4Address);
145 std::vector<Ipv4Address> cutRoute;
146 for (
auto 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 (
auto i = vec.begin(); i != vec.end(); ++i)
195 if (ipv4Address == (*i))
202 NS_LOG_DEBUG(
"next hop address not found, route corrupted");
219 for (
auto ri = vec.rbegin(); ri != vec.rend(); ++ri)
221 if (ipv4Address == (*ri))
228 NS_LOG_DEBUG(
"next hop address not found, route corrupted");
240 for (
auto ri = vec.rbegin(); ri != vec.rend(); ++ri)
242 if (ipv4Address == (*ri))
244 nextTwoHop = *(ri + 2);
267 for (
auto i = vec.begin(); i != vec.end(); ++i)
278 for (
auto i = vec.begin(); i != vec.end(); ++i)
280 for (
auto j = vec2.begin(); j != vec2.end(); ++j)
295 for (
auto i = vec.begin(); i != vec.end(); ++i)
297 if ((*i) == ipv4Address)
311 std::vector<Ipv4Address> vec2(vec);
314 for (
auto i = vec2.begin(); i != vec2.end(); ++i)
322 for (
auto 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)
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);
467 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
468 << (uint32_t)protocol << isPromisc);
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");
554 uint8_t numberAddress = (buf[1] - 6) / 4;
555 NS_LOG_DEBUG(
"The number of Ip addresses " << (uint32_t)numberAddress);
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 (
auto i = changeRoute.begin(); i != changeRoute.end(); ++i)
711 dsr->ScheduleInitialReply(newPacket, ipv4Address, nextHop,
m_ipv4Route);
721 bool addRoute =
false;
722 if (numberAddress > 0)
727 if (dsr->IsLinkCache())
729 addRoute = dsr->AddRoute_Link(
m_finalRoute, ipv4Address);
733 addRoute = dsr->AddRoute(toSource);
765 if (nextHop ==
"0.0.0.0")
767 dsr->PacketNewRoute(dsrP, ipv4Address, dst, protocol);
774 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
776 dsr->CancelRreqTimer(dst,
true);
800 else if (isRouteInCache && !areThereDuplicates)
806 for (
auto i = saveRoute.begin(); i != saveRoute.end(); ++i)
814 for (
auto j = ip.begin(); j != ip.end(); ++j)
822 bool addRoute =
false;
827 saveRoute.push_back(ipv4Address);
836 NS_ASSERT(saveRoute.front() == ipv4Address);
838 if (dsr->IsLinkCache())
840 addRoute = dsr->AddRoute_Link(saveRoute, ipv4Address);
844 addRoute = dsr->AddRoute(toSource);
849 NS_LOG_LOGIC(
"We have added the route and search send buffer for packet with "
870 if (nextHop ==
"0.0.0.0")
872 dsr->PacketNewRoute(dsrP, ipv4Address, dst, protocol);
879 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
881 dsr->CancelRreqTimer(dst,
true);
912 NS_LOG_DEBUG(
"This is the full route from " << realSource <<
" to "
929 dsr->ScheduleCachedReply(newPacket, ipv4Address, nextHop,
m_ipv4Route, hops);
938 mainVector.push_back(ipv4Address);
949 p->RemoveHeader(rerr);
954 if ((errorSrc == srcAddress) && (unreachNode == ipv4Address))
964 dsr->DeleteAllRoutesIncludeLink(errorSrc, unreachNode, ipv4Address);
974 NS_LOG_DEBUG(
"The RREQ and newUnreach header length " << length);
988 uint8_t ttl = ipv4Header.
GetTtl();
1001 dsr->ScheduleInterRequest(interP);
1017 .SetGroupName(
"Dsr")
1056 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1057 << (uint32_t)protocol << isPromisc);
1064 uint8_t numberAddress = (buf[1] - 2) / 4;
1074 NS_LOG_DEBUG(
"The next header value " << (uint32_t)protocol);
1082 if (targetAddress == ipv4Address)
1086 if (nodeList.empty())
1104 NS_ASSERT(nodeList.front() == ipv4Address);
1105 bool addRoute =
false;
1106 if (dsr->IsLinkCache())
1108 addRoute = dsr->AddRoute_Link(nodeList, ipv4Address);
1112 addRoute = dsr->AddRoute(toDestination);
1118 "We have added the route and search send buffer for packet with destination "
1130 if (nextHop ==
"0.0.0.0")
1132 dsr->PacketNewRoute(dsrP, ipv4Address, dst, protocol);
1138 dsr->CancelRreqTimer(dst,
true);
1142 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
1154 NS_LOG_DEBUG(
"The length of rrep option " << (uint32_t)length);
1156 if (length % 2 != 0)
1167 std::vector<Ipv4Address> routeCopy = nodeList;
1168 std::vector<Ipv4Address> cutRoute =
CutRoute(ipv4Address, nodeList);
1170 if (cutRoute.size() >= 2)
1173 NS_LOG_DEBUG(
"The route destination after cut " << dst);
1177 NS_ASSERT(cutRoute.front() == ipv4Address);
1178 bool addRoute =
false;
1179 if (dsr->IsLinkCache())
1181 addRoute = dsr->AddRoute_Link(nodeList, ipv4Address);
1185 addRoute = dsr->AddRoute(toDestination);
1189 dsr->CancelRreqTimer(dst,
true);
1206 NS_LOG_DEBUG(
"The nextHop address " << nextHop <<
" and the source in the route reply "
1219 NS_LOG_DEBUG(
"The reply header length " << (uint32_t)length);
1227 dsr->SendReply(newPacket, ipv4Address, nextHop,
m_ipv4Route);
1240 .SetGroupName(
"Dsr")
1278 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Address << ipv4Header
1279 << (uint32_t)protocol << isPromisc);
1284 uint8_t numberAddress = (buf[1] - 2) / 4;
1313 NS_LOG_LOGIC(
"We process promiscuous receipt data packet");
1317 dsr->SendGratuitousReply(source, srcAddress, nodeList, protocol);
1323 if (destAddress != destination)
1325 NS_LOG_DEBUG(
"Process the promiscuously received packet");
1326 bool findPassive =
false;
1328 for (int32_t i = 0; i < nNodes; ++i)
1336 findPassive = dsrNode->PassiveEntryCheck(packet,
1351 NS_LOG_DEBUG(
"We find one previously received passive entry");
1361 dsrSrc->CancelPassiveTimer(packet, source, destination, segsLeft);
1366 dsr->PassiveEntryCheck(packet,
1383 uint8_t length = sourceRoute.
GetLength();
1384 uint8_t nextAddressIndex;
1389 auto data =
new uint8_t[size];
1391 uint8_t optionType = 0;
1392 optionType = *(
data);
1395 if (optionType == 160)
1397 NS_LOG_LOGIC(
"Remove the ack request header and add ack header to the packet");
1401 uint16_t ackId = ackReq.
GetAckId();
1408 if (!nodeList.empty())
1410 if (segsLeft > numberAddress)
1418 if (numberAddress - segsLeft < 2)
1424 ackAddress = nodeList[numberAddress - segsLeft - 2];
1427 NS_LOG_DEBUG(
"Send back ACK to the earlier hop " << ackAddress <<
" from us "
1429 dsr->SendAck(ackId, ackAddress, source, destination, protocol,
m_ipv4Route);
1442 if (length % 2 != 0)
1449 if (segsLeft > numberAddress)
1461 nextAddressIndex = numberAddress - segsLeft;
1463 NS_LOG_DEBUG(
"The next address of source route option "
1464 << nextAddress <<
" and the nextAddressIndex: " << (uint32_t)nextAddressIndex
1465 <<
" and the segments left : " << (uint32_t)segsLeft);
1477 if (nextHop ==
"0.0.0.0")
1480 dsr->PacketNewRoute(dsrP, realSource, targetAddress, protocol);
1484 if (ipv4Address == nextHop)
1497 SetRoute(nextAddress, ipv4Address);
1499 dsr->ForwardPacket(dsrP,
1518 .SetGroupName(
"Dsr")
1556 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1557 << (uint32_t)protocol << isPromisc);
1560 auto data =
new uint8_t[size];
1562 uint8_t errorType = *(
data + 2);
1568 NS_LOG_DEBUG(
"The error type value here " << (uint32_t)errorType);
1583 <<
"and the unreachable node is " << unreachAddress);
1592 dsr->DeleteAllRoutesIncludeLink(errorSource, unreachAddress, ipv4Address);
1595 uint32_t serialized =
DoSendError(newP, rerrUnreach, rerrSize, ipv4Address, protocol);
1614 uint32_t serialized = 0;
1629 uint8_t numberAddress = (buf[1] - 2) / 4;
1632 NS_LOG_DEBUG(
"The number of addresses " << (uint32_t)numberAddress);
1646 uint8_t length = sourceRoute.
GetLength();
1647 uint8_t nextAddressIndex;
1659 if (length % 2 != 0)
1666 if (segmentsLeft > numberAddress)
1675 if (segmentsLeft == 0 && targetAddress == ipv4Address)
1677 NS_LOG_INFO(
"This is the destination of the error, send error request");
1678 dsr->SendErrorRequest(rerr, protocol);
1679 return serializedSize;
1685 nextAddressIndex = numberAddress - segmentsLeft;
1695 return serializedSize;
1699 SetRoute(nextAddress, ipv4Address);
1700 dsr->ForwardErrPacket(rerr, newSourceRoute, nextAddress, protocol,
m_ipv4Route);
1701 return serializedSize;
1709 static TypeId tid =
TypeId(
"ns3::dsr::DsrOptionAckReq")
1711 .SetGroupName(
"Dsr")
1749 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1750 << (uint32_t)protocol << isPromisc);
1767 NS_LOG_DEBUG(
"The next header value " << (uint32_t)protocol);
1779 .SetGroupName(
"Dsr")
1817 NS_LOG_FUNCTION(
this << packet << dsrP << ipv4Address << source << ipv4Header
1818 << (uint32_t)protocol << isPromisc);
1836 dsr->UpdateRouteEntry(realDst);
1840 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.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
void AddHeader(const Header &header)
Add header to this packet.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Ptr< Packet > Copy() const
performs a COW copy of the packet.
void AddPacketTag(const Tag &tag) const
Add a packet tag.
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< 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 AttributeAccessor > MakeUintegerAccessor(T1 a1)