32 #define NS_LOG_APPEND_CONTEXT \
33 if (GetObject<Node> ()) { std::clog << "[node " << GetObject<Node> ()->GetId () << "] "; }
41 #include "ns3/assert.h"
42 #include "ns3/fatal-error.h"
44 #include "ns3/uinteger.h"
45 #include "ns3/trace-source-accessor.h"
46 #include "ns3/udp-header.h"
47 #include "ns3/pointer.h"
48 #include "ns3/node-list.h"
49 #include "ns3/uinteger.h"
50 #include "ns3/object-vector.h"
51 #include "ns3/ipv4-l3-protocol.h"
52 #include "ns3/ipv4-interface.h"
53 #include "ns3/ipv4-header.h"
54 #include "ns3/ipv4-address.h"
55 #include "ns3/ipv4-route.h"
56 #include "ns3/icmpv4-l4-protocol.h"
57 #include "ns3/ip-l4-protocol.h"
74 .AddAttribute (
"OptionNumber",
"The Dsr option number.",
77 MakeUintegerChecker<uint8_t> ())
78 .AddTraceSource (
"Rx",
"Receive DSR packet.",
109 std::vector<Ipv4Address>::iterator it = find (nodeList.begin (), nodeList.end (), destAddress);
111 for (std::vector<Ipv4Address>::iterator i = it; i != nodeList.end (); ++i)
113 if ((ipv4Address == (*i)) && ((*i) != nodeList.back ()))
121 std::vector<Ipv4Address>
125 std::vector<Ipv4Address>::iterator it = find (nodeList.begin (), nodeList.end (), ipv4Address);
126 std::vector<Ipv4Address> cutRoute;
127 for (std::vector<Ipv4Address>::iterator i = it; i != nodeList.end (); ++i)
129 cutRoute.push_back (*i);
147 std::vector<Ipv4Address> vec2 (vec);
149 for (std::vector<Ipv4Address>::reverse_iterator ri = vec2.rbegin (); ri
150 != vec2.rend (); ++ri)
155 if ((vec.size () == vec2.size ()) && (vec.front () == vec2.back ()))
167 if (vec.size () == 2)
175 if (ipv4Address == vec.back ())
177 NS_LOG_DEBUG (
"We have reached to the final destination " << ipv4Address <<
" " << vec.back ());
180 for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
182 if (ipv4Address == (*i))
189 NS_LOG_DEBUG (
"next hop address not found, route corrupted");
198 if (vec.size () == 2)
206 for (std::vector<Ipv4Address>::reverse_iterator ri = vec.rbegin (); ri != vec.rend (); ++ri)
208 if (ipv4Address == (*ri))
215 NS_LOG_DEBUG (
"next hop address not found, route corrupted");
226 for (std::vector<Ipv4Address>::reverse_iterator ri = vec.rbegin (); ri != vec.rend (); ++ri)
228 if (ipv4Address == (*ri))
230 nextTwoHop = *(ri + 2);
252 for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
262 for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
264 for (std::vector<Ipv4Address>::const_iterator j = vec2.begin (); j != vec2.end (); ++j)
282 for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
284 if ((*i) == ipv4Address)
300 std::vector<Ipv4Address> vec2 (vec);
303 for (std::vector<Ipv4Address>::const_iterator i = vec2.begin (); i != vec2.end (); ++i)
312 for (std::vector<Ipv4Address>::iterator j = vec.begin (); j != vec.end (); ++j)
316 if ((j + 1) != vec.end ())
318 vec.erase (j + 1, vec.end ());
326 else if (j == (vec.end () - 1))
345 for (int32_t i = 0; i < nNodes; ++i)
361 for (int32_t i = 0; i < nNodes; ++i)
380 .AddConstructor<DsrOptionPad1> ()
404 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
420 .AddConstructor<DsrOptionPadn> ()
443 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
460 .AddConstructor<DsrOptionRreq> ()
489 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
496 if (srcAddress == ipv4Address)
515 uint8_t numberAddress = (buf[1] - 6) / 4;
516 NS_LOG_DEBUG (
"The number of Ip addresses " << (uint32_t)numberAddress);
517 if (numberAddress >= 255)
519 NS_LOG_DEBUG (
"Discard the packet, malformed header since two many ip addresses in route");
533 p->RemoveHeader (rreq);
543 uint16_t requestId = rreq.
GetId ();
548 std::vector<Ipv4Address> nodeList (mainVector);
566 uint8_t ttl = ipv4Header.
GetTtl ();
567 bool dupRequest =
false;
572 dupRequest = dsr->FindSourceEntry (sourceAddress, targetAddress, requestId);
595 NS_LOG_DEBUG (
"Our node address is already seen in the route, drop the request");
613 NS_LOG_DEBUG (
"The target address over here " << targetAddress <<
" and the ip address " << ipv4Address <<
" and the source address " << mainVector[0]);
614 if (targetAddress == ipv4Address)
617 if (nodeList.size () == 1)
623 nextHop = srcAddress;
627 std::vector<Ipv4Address> changeRoute (nodeList);
628 changeRoute.push_back (ipv4Address);
630 for (std::vector<Ipv4Address>::iterator i = changeRoute.begin (); i != changeRoute.end (); ++i)
658 dsr->ScheduleInitialReply (newPacket, ipv4Address, nextHop,
m_ipv4Route);
667 bool addRoute =
false;
668 if (numberAddress > 0)
672 if (dsr->IsLinkCache ())
674 addRoute = dsr->AddRoute_Link (
m_finalRoute, ipv4Address);
678 addRoute = dsr->AddRoute (toSource);
695 if (dsr->IsLinkCache ())
705 if (nextHop ==
"0.0.0.0")
707 dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
714 dsr->SendPacketFromBuffer (sourceRoute, nextHop, protocol);
716 dsr->CancelRreqTimer (dst,
true);
738 else if (dsr->LookupRoute (targetAddress, toPrev))
742 std::vector<Ipv4Address> saveRoute (nodeList);
752 for (std::vector<Ipv4Address>::iterator i = saveRoute.begin (); i != saveRoute.end (); ++i)
759 for (std::vector<Ipv4Address>::iterator j = ip.begin (); j != ip.end (); ++j)
766 bool addRoute =
false;
771 saveRoute.push_back (ipv4Address);
778 NS_ASSERT (saveRoute.front () == ipv4Address);
780 if (dsr->IsLinkCache ())
782 addRoute = dsr->AddRoute_Link (saveRoute, ipv4Address);
786 addRoute = dsr->AddRoute (toSource);
791 NS_LOG_LOGIC (
"We have added the route and search send buffer for packet with destination " << dst);
799 if (dsr->IsLinkCache ())
801 dsr->UseExtends (saveRoute);
809 if (nextHop ==
"0.0.0.0")
811 dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
818 dsr->SendPacketFromBuffer (sourceRoute, nextHop, protocol);
820 dsr->CancelRreqTimer (dst,
true);
858 dsrRoutingHeader.SetMessageType (1);
859 dsrRoutingHeader.SetSourceId (
GetIDfromIP (realSource));
860 dsrRoutingHeader.SetDestId (255);
863 dsrRoutingHeader.SetPayloadLength (length + 2);
864 dsrRoutingHeader.AddDsrOption (rrep);
867 dsr->ScheduleCachedReply (newPacket, ipv4Address, nextHop,
m_ipv4Route, hops);
872 NS_LOG_DEBUG (
"There is duplicate ip addresses in the two route parts");
881 mainVector.push_back (ipv4Address);
882 NS_ASSERT (mainVector.front () == source);
892 p->RemoveHeader (rerr);
897 if ((errorSrc == srcAddress) && (unreachNode == ipv4Address))
907 dsr->DeleteAllRoutesIncludeLink (errorSrc, unreachNode, ipv4Address);
916 NS_LOG_DEBUG (
"The RREQ and newUnreach header length " << length);
930 uint8_t ttl = ipv4Header.
GetTtl ();
943 dsr->ScheduleInterRequest (interP);
958 .AddConstructor<DsrOptionRrep> ()
987 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
994 uint8_t numberAddress = (buf[1] - 2) / 4;
1003 NS_LOG_DEBUG (
"The next header value " << (uint32_t)protocol);
1011 if (targetAddress == ipv4Address)
1015 if (nodeList.size () == 0)
1030 NS_ASSERT (nodeList.front () == ipv4Address);
1031 bool addRoute =
false;
1032 if (dsr->IsLinkCache ())
1034 addRoute = dsr->AddRoute_Link (nodeList, ipv4Address);
1038 addRoute = dsr->AddRoute (toDestination);
1043 NS_LOG_DEBUG (
"We have added the route and search send buffer for packet with destination " << dst);
1048 NS_LOG_DEBUG (
"The route length " << nodeList.size ());
1054 if (nextHop ==
"0.0.0.0")
1056 dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
1062 dsr->CancelRreqTimer (dst,
true);
1066 dsr->SendPacketFromBuffer (sourceRoute, nextHop, protocol);
1077 NS_LOG_DEBUG (
"The length of rrep option " << (uint32_t)length);
1079 if (length % 2 != 0)
1089 std::vector<Ipv4Address> routeCopy = nodeList;
1090 std::vector<Ipv4Address> cutRoute =
CutRoute (ipv4Address, nodeList);
1092 if (cutRoute.size () >= 2)
1095 NS_LOG_DEBUG (
"The route destination after cut " << dst);
1097 NS_ASSERT (cutRoute.front () == ipv4Address);
1098 bool addRoute =
false;
1099 if (dsr->IsLinkCache ())
1101 addRoute = dsr->AddRoute_Link (nodeList, ipv4Address);
1105 addRoute = dsr->AddRoute (toDestination);
1109 dsr->CancelRreqTimer (dst,
true);
1124 NS_ASSERT (routeCopy.back () == source);
1126 NS_LOG_DEBUG (
"The nextHop address " << nextHop <<
" and the source in the route reply " << source);
1138 NS_LOG_DEBUG (
"The reply header length " << (uint32_t)length);
1145 newPacket->
AddHeader (dsrRoutingHeader);
1146 dsr->SendReply (newPacket, ipv4Address, nextHop,
m_ipv4Route);
1158 .AddConstructor<DsrOptionSR> ()
1186 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Address << ipv4Header << (uint32_t)protocol << isPromisc);
1191 uint8_t numberAddress = (buf[1] - 2) / 4;
1220 NS_LOG_LOGIC (
"We process promiscuous receipt data packet");
1224 dsr->SendGratuitousReply (source, srcAddress, nodeList, protocol);
1230 if (destAddress != destination)
1232 NS_LOG_DEBUG (
"Process the promiscuously received packet");
1233 bool findPassive =
false;
1235 for (int32_t i = 0; i < nNodes; ++i)
1242 findPassive = dsrNode->PassiveEntryCheck (packet, source, destination, segsLeft, fragmentOffset, identification,
false);
1251 NS_LOG_DEBUG (
"We find one previously received passive entry");
1261 dsrSrc->CancelPassiveTimer (packet, source, destination, segsLeft);
1266 dsr->PassiveEntryCheck (packet, source, destination, segsLeft, fragmentOffset, identification,
true);
1277 uint8_t length = sourceRoute.
GetLength ();
1278 uint8_t nextAddressIndex;
1282 uint32_t size = p->
GetSize ();
1283 uint8_t *
data =
new uint8_t[size];
1285 uint8_t optionType = 0;
1286 optionType = *(
data);
1289 if (optionType == 160)
1291 NS_LOG_LOGIC (
"Remove the ack request header and add ack header to the packet");
1295 uint16_t ackId = ackReq.
GetAckId ();
1302 if (!nodeList.empty ())
1304 if (segsLeft > numberAddress)
1310 if (numberAddress - segsLeft - 2 < 0)
1316 ackAddress = nodeList[numberAddress - segsLeft - 2];
1319 NS_LOG_DEBUG (
"Send back ACK to the earlier hop " << ackAddress <<
" from us " << ipv4Address);
1320 dsr->SendAck (ackId, ackAddress, source, destination, protocol,
m_ipv4Route);
1333 if (length % 2 != 0)
1340 if (segsLeft > numberAddress)
1351 nextAddressIndex = numberAddress - segsLeft;
1353 NS_LOG_DEBUG (
"The next address of source route option " << nextAddress <<
" and the nextAddressIndex: " << (uint32_t)nextAddressIndex <<
" and the segments left : " << (uint32_t)segsLeft);
1365 if (nextHop ==
"0.0.0.0")
1368 dsr->PacketNewRoute (dsrP, realSource, targetAddress, protocol);
1372 if (ipv4Address == nextHop)
1385 SetRoute (nextAddress, ipv4Address);
1387 dsr->ForwardPacket (dsrP, newSourceRoute, ipv4Header, realSource, nextAddress, targetAddress, protocol,
m_ipv4Route);
1398 .AddConstructor<DsrOptionRerr> ()
1426 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
1428 uint32_t size = p->
GetSize ();
1429 uint8_t *
data =
new uint8_t[size];
1431 uint8_t errorType = *(data + 2);
1441 NS_LOG_DEBUG (
"The error type value here " << (uint32_t)errorType);
1455 NS_LOG_DEBUG (
"The error source is " << rerrUnreach.
GetErrorDst () <<
"and the unreachable node is " << unreachAddress);
1464 dsr->DeleteAllRoutesIncludeLink (errorSource, unreachAddress, ipv4Address);
1467 uint32_t serialized =
DoSendError (newP, rerrUnreach, rerrSize, ipv4Address, protocol);
1486 uint32_t serialized = 0;
1496 uint8_t numberAddress = (buf[1] - 2) / 4;
1499 NS_LOG_DEBUG (
"The number of addresses " << (uint32_t)numberAddress);
1513 uint8_t length = sourceRoute.
GetLength ();
1514 uint8_t nextAddressIndex;
1526 if (length % 2 != 0)
1533 if (segmentsLeft > numberAddress)
1542 if (segmentsLeft == 0 && targetAddress == ipv4Address)
1544 NS_LOG_INFO (
"This is the destination of the error, send error request");
1545 dsr->SendErrorRequest (rerr, protocol);
1546 return serializedSize;
1552 nextAddressIndex = numberAddress - segmentsLeft;
1562 return serializedSize;
1566 SetRoute (nextAddress, ipv4Address);
1567 dsr->ForwardErrPacket (rerr, newSourceRoute, nextAddress, protocol,
m_ipv4Route);
1568 return serializedSize;
1575 static TypeId tid =
TypeId (
"ns3::dsr::DsrOptionAckReq")
1577 .AddConstructor<DsrOptionAckReq> ()
1605 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
1621 NS_LOG_DEBUG (
"The next header value " << (uint32_t)protocol);
1632 .AddConstructor<DsrOptionAck> ()
1660 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
1678 dsr->UpdateRouteEntry (realDst);
1682 dsr->CallCancelPacketTimer (ackId, ipv4Header, realSrc, realDst);