32 #define NS_LOG_APPEND_CONTEXT \
33 if (GetObject<Node> ()) { std::clog << "[node " << GetObject<Node> ()->GetId () << "] "; }
40 #include "ns3/assert.h"
41 #include "ns3/fatal-error.h"
43 #include "ns3/uinteger.h"
44 #include "ns3/trace-source-accessor.h"
45 #include "ns3/udp-header.h"
46 #include "ns3/pointer.h"
47 #include "ns3/node-list.h"
48 #include "ns3/object-vector.h"
49 #include "ns3/ipv4-l3-protocol.h"
50 #include "ns3/ipv4-interface.h"
51 #include "ns3/ipv4-header.h"
52 #include "ns3/ipv4-address.h"
53 #include "ns3/ipv4-route.h"
54 #include "ns3/icmpv4-l4-protocol.h"
55 #include "ns3/ip-l4-protocol.h"
73 .AddAttribute (
"OptionNumber",
"The Dsr option number.",
76 MakeUintegerChecker<uint8_t> ())
77 .AddTraceSource (
"Drop",
80 "ns3::Packet::TracedCallback")
81 .AddTraceSource (
"Rx",
82 "Receive DSR packet.",
84 "ns3::dsr::DsrOptionSRHeader::TracedCallback")
114 std::vector<Ipv4Address>::iterator it = find (nodeList.begin (), nodeList.end (), destAddress);
116 for (std::vector<Ipv4Address>::iterator i = it; i != nodeList.end (); ++i)
118 if ((ipv4Address == (*i)) && ((*i) != nodeList.back ()))
126 std::vector<Ipv4Address>
130 std::vector<Ipv4Address>::iterator it = find (nodeList.begin (), nodeList.end (), ipv4Address);
131 std::vector<Ipv4Address> cutRoute;
132 for (std::vector<Ipv4Address>::iterator i = it; i != nodeList.end (); ++i)
134 cutRoute.push_back (*i);
152 std::vector<Ipv4Address> vec2 (vec);
154 for (std::vector<Ipv4Address>::reverse_iterator ri = vec2.rbegin (); ri
155 != vec2.rend (); ++ri)
160 if ((vec.size () == vec2.size ()) && (vec.front () == vec2.back ()))
172 if (vec.size () == 2)
180 if (ipv4Address == vec.back ())
182 NS_LOG_DEBUG (
"We have reached to the final destination " << ipv4Address <<
" " << vec.back ());
185 for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
187 if (ipv4Address == (*i))
194 NS_LOG_DEBUG (
"next hop address not found, route corrupted");
203 if (vec.size () == 2)
211 for (std::vector<Ipv4Address>::reverse_iterator ri = vec.rbegin (); ri != vec.rend (); ++ri)
213 if (ipv4Address == (*ri))
220 NS_LOG_DEBUG (
"next hop address not found, route corrupted");
231 for (std::vector<Ipv4Address>::reverse_iterator ri = vec.rbegin (); ri != vec.rend (); ++ri)
233 if (ipv4Address == (*ri))
235 nextTwoHop = *(ri + 2);
257 for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
267 for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
269 for (std::vector<Ipv4Address>::const_iterator j = vec2.begin (); j != vec2.end (); ++j)
287 for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
289 if ((*i) == ipv4Address)
305 std::vector<Ipv4Address> vec2 (vec);
308 for (std::vector<Ipv4Address>::const_iterator i = vec2.begin (); i != vec2.end (); ++i)
317 for (std::vector<Ipv4Address>::iterator j = vec.begin (); j != vec.end (); ++j)
321 if ((j + 1) != vec.end ())
323 vec.erase (j + 1, vec.end ());
331 else if (j == (vec.end () - 1))
350 for (int32_t i = 0; i < nNodes; ++i)
354 if (ipv4->GetAddress (1, 0).GetLocal () ==
address)
366 for (int32_t i = 0; i < nNodes; ++i)
370 int32_t ifIndex = ipv4->GetInterfaceForAddress (ipv4Address);
385 .SetGroupName (
"Dsr")
410 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
426 .SetGroupName (
"Dsr")
450 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
467 .SetGroupName (
"Dsr")
497 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
504 if (source == ipv4Address)
506 NS_LOG_DEBUG (
"Discard the packet since it was originated from same source address");
523 uint8_t numberAddress = (buf[1] - 6) / 4;
524 NS_LOG_DEBUG (
"The number of Ip addresses " << (uint32_t)numberAddress);
525 if (numberAddress >= 255)
527 NS_LOG_DEBUG (
"Discard the packet, malformed header since two many ip addresses in route");
541 p->RemoveHeader (rreq);
551 uint16_t requestId = rreq.
GetId ();
556 std::vector<Ipv4Address> nodeList (mainVector);
574 uint8_t ttl = ipv4Header.
GetTtl ();
575 bool dupRequest =
false;
580 dupRequest = dsr->FindSourceEntry (sourceAddress, targetAddress, requestId);
603 NS_LOG_DEBUG (
"Our node address is already seen in the route, drop the request");
610 bool isRouteInCache = dsr->LookupRoute (targetAddress,
614 std::vector<Ipv4Address> saveRoute (nodeList);
629 NS_LOG_DEBUG (
"The target address over here " << targetAddress <<
" and the ip address " << ipv4Address <<
" and the source address " << mainVector[0]);
630 if (targetAddress == ipv4Address)
633 if (nodeList.size () == 1)
640 nextHop = srcAddress;
644 std::vector<Ipv4Address> changeRoute (nodeList);
645 changeRoute.push_back (ipv4Address);
647 for (std::vector<Ipv4Address>::iterator i = changeRoute.begin (); i != changeRoute.end (); ++i)
675 dsr->ScheduleInitialReply (newPacket, ipv4Address, nextHop,
m_ipv4Route);
684 bool addRoute =
false;
685 if (numberAddress > 0)
689 if (dsr->IsLinkCache ())
691 addRoute = dsr->AddRoute_Link (
m_finalRoute, ipv4Address);
695 addRoute = dsr->AddRoute (toSource);
726 if (nextHop ==
"0.0.0.0")
728 dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
735 dsr->SendPacketFromBuffer (sourceRoute, nextHop, protocol);
737 dsr->CancelRreqTimer (dst,
true);
761 else if (isRouteInCache && !areThereDuplicates)
767 for (std::vector<Ipv4Address>::iterator i = saveRoute.begin (); i != saveRoute.end (); ++i)
774 for (std::vector<Ipv4Address>::iterator j = ip.begin (); j != ip.end (); ++j)
781 bool addRoute =
false;
786 saveRoute.push_back (ipv4Address);
793 NS_ASSERT (saveRoute.front () == ipv4Address);
795 if (dsr->IsLinkCache ())
797 addRoute = dsr->AddRoute_Link (saveRoute, ipv4Address);
801 addRoute = dsr->AddRoute (toSource);
806 NS_LOG_LOGIC (
"We have added the route and search send buffer for packet with destination " << dst);
824 if (nextHop ==
"0.0.0.0")
826 dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
833 dsr->SendPacketFromBuffer (sourceRoute, nextHop, protocol);
835 dsr->CancelRreqTimer (dst,
true);
881 dsr->ScheduleCachedReply (newPacket, ipv4Address, nextHop,
m_ipv4Route, hops);
890 mainVector.push_back (ipv4Address);
891 NS_ASSERT (mainVector.front () == source);
901 p->RemoveHeader (rerr);
906 if ((errorSrc == srcAddress) && (unreachNode == ipv4Address))
916 dsr->DeleteAllRoutesIncludeLink (errorSrc, unreachNode, ipv4Address);
925 NS_LOG_DEBUG (
"The RREQ and newUnreach header length " << length);
939 uint8_t ttl = ipv4Header.
GetTtl ();
952 dsr->ScheduleInterRequest (interP);
967 .SetGroupName (
"Dsr")
997 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
1004 uint8_t numberAddress = (buf[1] - 2) / 4;
1013 NS_LOG_DEBUG (
"The next header value " << (uint32_t)protocol);
1021 if (targetAddress == ipv4Address)
1025 if (nodeList.size () == 0)
1040 NS_ASSERT (nodeList.front () == ipv4Address);
1041 bool addRoute =
false;
1042 if (dsr->IsLinkCache ())
1044 addRoute = dsr->AddRoute_Link (nodeList, ipv4Address);
1048 addRoute = dsr->AddRoute (toDestination);
1053 NS_LOG_DEBUG (
"We have added the route and search send buffer for packet with destination " << dst);
1058 NS_LOG_DEBUG (
"The route length " << nodeList.size ());
1064 if (nextHop ==
"0.0.0.0")
1066 dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
1072 dsr->CancelRreqTimer (dst,
true);
1076 dsr->SendPacketFromBuffer (sourceRoute, nextHop, protocol);
1087 NS_LOG_DEBUG (
"The length of rrep option " << (uint32_t)length);
1089 if (length % 2 != 0)
1099 std::vector<Ipv4Address> routeCopy = nodeList;
1100 std::vector<Ipv4Address> cutRoute =
CutRoute (ipv4Address, nodeList);
1102 if (cutRoute.size () >= 2)
1105 NS_LOG_DEBUG (
"The route destination after cut " << dst);
1107 NS_ASSERT (cutRoute.front () == ipv4Address);
1108 bool addRoute =
false;
1109 if (dsr->IsLinkCache ())
1111 addRoute = dsr->AddRoute_Link (nodeList, ipv4Address);
1115 addRoute = dsr->AddRoute (toDestination);
1119 dsr->CancelRreqTimer (dst,
true);
1134 NS_ASSERT (routeCopy.back () == source);
1136 NS_LOG_DEBUG (
"The nextHop address " << nextHop <<
" and the source in the route reply " << source);
1148 NS_LOG_DEBUG (
"The reply header length " << (uint32_t)length);
1155 newPacket->
AddHeader (dsrRoutingHeader);
1156 dsr->SendReply (newPacket, ipv4Address, nextHop,
m_ipv4Route);
1168 .SetGroupName (
"Dsr")
1197 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Address << ipv4Header << (uint32_t)protocol << isPromisc);
1202 uint8_t numberAddress = (buf[1] - 2) / 4;
1231 NS_LOG_LOGIC (
"We process promiscuous receipt data packet");
1235 dsr->SendGratuitousReply (source, srcAddress, nodeList, protocol);
1241 if (destAddress != destination)
1243 NS_LOG_DEBUG (
"Process the promiscuously received packet");
1244 bool findPassive =
false;
1246 for (int32_t i = 0; i < nNodes; ++i)
1253 findPassive = dsrNode->PassiveEntryCheck (packet, source, destination, segsLeft, fragmentOffset, identification,
false);
1262 NS_LOG_DEBUG (
"We find one previously received passive entry");
1272 dsrSrc->CancelPassiveTimer (packet, source, destination, segsLeft);
1277 dsr->PassiveEntryCheck (packet, source, destination, segsLeft, fragmentOffset, identification,
true);
1288 uint8_t length = sourceRoute.
GetLength ();
1289 uint8_t nextAddressIndex;
1293 uint32_t size = p->
GetSize ();
1294 uint8_t *
data =
new uint8_t[size];
1296 uint8_t optionType = 0;
1297 optionType = *(
data);
1300 if (optionType == 160)
1302 NS_LOG_LOGIC (
"Remove the ack request header and add ack header to the packet");
1306 uint16_t ackId = ackReq.
GetAckId ();
1313 if (!nodeList.empty ())
1315 if (segsLeft > numberAddress)
1322 if (numberAddress - segsLeft < 2)
1328 ackAddress = nodeList[numberAddress - segsLeft - 2];
1331 NS_LOG_DEBUG (
"Send back ACK to the earlier hop " << ackAddress <<
" from us " << ipv4Address);
1332 dsr->SendAck (ackId, ackAddress, source, destination, protocol,
m_ipv4Route);
1345 if (length % 2 != 0)
1352 if (segsLeft > numberAddress)
1363 nextAddressIndex = numberAddress - segsLeft;
1365 NS_LOG_DEBUG (
"The next address of source route option " << nextAddress <<
" and the nextAddressIndex: " << (uint32_t)nextAddressIndex <<
" and the segments left : " << (uint32_t)segsLeft);
1377 if (nextHop ==
"0.0.0.0")
1380 dsr->PacketNewRoute (dsrP, realSource, targetAddress, protocol);
1384 if (ipv4Address == nextHop)
1397 SetRoute (nextAddress, ipv4Address);
1399 dsr->ForwardPacket (dsrP, newSourceRoute, ipv4Header, realSource, nextAddress, targetAddress, protocol,
m_ipv4Route);
1410 .SetGroupName (
"Dsr")
1439 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
1441 uint32_t size = p->
GetSize ();
1442 uint8_t *
data =
new uint8_t[size];
1444 uint8_t errorType = *(
data + 2);
1454 NS_LOG_DEBUG (
"The error type value here " << (uint32_t)errorType);
1468 NS_LOG_DEBUG (
"The error source is " << rerrUnreach.
GetErrorDst () <<
"and the unreachable node is " << unreachAddress);
1477 dsr->DeleteAllRoutesIncludeLink (errorSource, unreachAddress, ipv4Address);
1480 uint32_t serialized =
DoSendError (newP, rerrUnreach, rerrSize, ipv4Address, protocol);
1500 uint32_t serialized = 0;
1510 uint8_t numberAddress = (buf[1] - 2) / 4;
1513 NS_LOG_DEBUG (
"The number of addresses " << (uint32_t)numberAddress);
1527 uint8_t length = sourceRoute.
GetLength ();
1528 uint8_t nextAddressIndex;
1540 if (length % 2 != 0)
1547 if (segmentsLeft > numberAddress)
1556 if (segmentsLeft == 0 && targetAddress == ipv4Address)
1558 NS_LOG_INFO (
"This is the destination of the error, send error request");
1559 dsr->SendErrorRequest (rerr, protocol);
1560 return serializedSize;
1566 nextAddressIndex = numberAddress - segmentsLeft;
1576 return serializedSize;
1580 SetRoute (nextAddress, ipv4Address);
1581 dsr->ForwardErrPacket (rerr, newSourceRoute, nextAddress, protocol,
m_ipv4Route);
1582 return serializedSize;
1589 static TypeId tid =
TypeId (
"ns3::dsr::DsrOptionAckReq")
1591 .SetGroupName (
"Dsr")
1620 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
1636 NS_LOG_DEBUG (
"The next header value " << (uint32_t)protocol);
1647 .SetGroupName (
"Dsr")
1676 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
1694 dsr->UpdateRouteEntry (realDst);
1698 dsr->CallCancelPacketTimer (ackId, ipv4Header, realSrc, realDst);