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");
233 for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
247 for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
249 for (std::vector<Ipv4Address>::const_iterator j = vec2.begin (); j != vec2.end (); ++j)
267 for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
269 if ((*i) == ipv4Address)
285 std::vector<Ipv4Address> vec2 (vec);
288 for (std::vector<Ipv4Address>::const_iterator i = vec2.begin (); i != vec2.end (); ++i)
297 for (std::vector<Ipv4Address>::iterator j = vec.begin (); j != vec.end (); ++j)
301 if ((j + 1) != vec.end ())
303 vec.erase (j + 1, vec.end ());
311 else if (j == (vec.end () - 1))
330 for (int32_t i = 0; i < nNodes; ++i)
346 for (int32_t i = 0; i < nNodes; ++i)
365 .AddConstructor<DsrOptionPad1> ()
389 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
405 .AddConstructor<DsrOptionPadn> ()
428 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
445 .AddConstructor<DsrOptionRreq> ()
474 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
481 if (srcAddress == ipv4Address)
500 uint8_t numberAddress = (buf[1] - 6) / 4;
501 NS_LOG_DEBUG (
"The number of Ip addresses " << (uint32_t)numberAddress);
502 if (numberAddress >= 255)
504 NS_LOG_DEBUG (
"Discard the packet, malformed header since two many ip addresses in route");
518 p->RemoveHeader (rreq);
531 std::vector<Ipv4Address> nodeList (mainVector);
548 NS_LOG_DEBUG (
"Our node address is already seen in the route, drop the request");
566 NS_LOG_DEBUG (
"The target address over here " << targetAddress <<
" and the ip address " << ipv4Address <<
" and the source address " << mainVector[0]);
567 if (targetAddress == ipv4Address)
570 if (nodeList.size () == 1)
576 nextHop = srcAddress;
580 std::vector<Ipv4Address> changeRoute (nodeList);
581 changeRoute.push_back (ipv4Address);
583 for (std::vector<Ipv4Address>::iterator i = changeRoute.begin (); i != changeRoute.end (); ++i)
611 dsr->ScheduleInitialReply (newPacket, ipv4Address, nextHop,
m_ipv4Route);
620 bool addRoute =
false;
621 if (numberAddress > 0)
625 if (dsr->IsLinkCache ())
627 addRoute = dsr->AddRoute_Link (
m_finalRoute, ipv4Address);
631 addRoute = dsr->AddRoute (toSource);
648 if (dsr->IsLinkCache ())
658 if (nextHop ==
"0.0.0.0")
660 dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
667 dsr->SendPacketFromBuffer (sourceRoute, nextHop, protocol);
669 dsr->CancelRreqTimer (dst,
true);
691 else if (dsr->LookupRoute (targetAddress, toPrev))
695 std::vector<Ipv4Address> saveRoute (nodeList);
705 for (std::vector<Ipv4Address>::iterator i = saveRoute.begin (); i != saveRoute.end (); ++i)
712 for (std::vector<Ipv4Address>::iterator j = ip.begin (); j != ip.end (); ++j)
719 bool addRoute =
false;
724 saveRoute.push_back (ipv4Address);
731 NS_ASSERT (saveRoute.front () == ipv4Address);
733 if (dsr->IsLinkCache ())
735 addRoute = dsr->AddRoute_Link (saveRoute, ipv4Address);
739 addRoute = dsr->AddRoute (toSource);
744 NS_LOG_DEBUG (
"We have added the route and search send buffer for packet with destination " << dst);
752 if (dsr->IsLinkCache ())
754 dsr->UseExtends (saveRoute);
762 if (nextHop ==
"0.0.0.0")
764 dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
771 dsr->SendPacketFromBuffer (sourceRoute, nextHop, protocol);
773 dsr->CancelRreqTimer (dst,
true);
811 dsrRoutingHeader.SetMessageType (1);
812 dsrRoutingHeader.SetSourceId (
GetIDfromIP (realSource));
813 dsrRoutingHeader.SetDestId (255);
816 dsrRoutingHeader.SetPayloadLength (length + 2);
817 dsrRoutingHeader.AddDsrOption (rrep);
820 dsr->ScheduleCachedReply (newPacket, ipv4Address, nextHop,
m_ipv4Route, hops);
825 NS_LOG_DEBUG (
"There is duplicate ip addresses in the two route parts");
834 mainVector.push_back (ipv4Address);
835 NS_ASSERT (mainVector.front () == source);
845 p->RemoveHeader (rerr);
850 if ((errorSrc == srcAddress) && (unreachNode == ipv4Address))
860 dsr->DeleteAllRoutesIncludeLink (errorSrc, unreachNode, ipv4Address);
869 NS_LOG_DEBUG (
"The RREQ and newUnreach header length " << length);
883 uint8_t ttl = ipv4Header.
GetTtl ();
896 dsr->ScheduleInterRequest (interP);
911 .AddConstructor<DsrOptionRrep> ()
940 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
947 uint8_t numberAddress = (buf[1] - 2) / 4;
956 NS_LOG_DEBUG (
"The next header value " << (uint32_t)protocol);
964 if (targetAddress == ipv4Address)
968 if (nodeList.size () == 0)
983 NS_ASSERT (nodeList.front () == ipv4Address);
984 bool addRoute =
false;
985 if (dsr->IsLinkCache ())
987 addRoute = dsr->AddRoute_Link (nodeList, ipv4Address);
991 addRoute = dsr->AddRoute (toDestination);
996 NS_LOG_DEBUG (
"We have added the route and search send buffer for packet with destination " << dst);
1001 NS_LOG_DEBUG (
"The route length " << nodeList.size ());
1007 if (nextHop ==
"0.0.0.0")
1009 dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
1015 dsr->CancelRreqTimer (dst,
true);
1019 dsr->SendPacketFromBuffer (sourceRoute, nextHop, protocol);
1030 NS_LOG_DEBUG (
"The length of rrep option " << (uint32_t)length);
1032 if (length % 2 != 0)
1042 std::vector<Ipv4Address> routeCopy = nodeList;
1043 std::vector<Ipv4Address> cutRoute =
CutRoute (ipv4Address, nodeList);
1045 if (cutRoute.size () >= 2)
1048 NS_LOG_DEBUG (
"The route destination after cut " << dst);
1050 NS_ASSERT (cutRoute.front () == ipv4Address);
1051 bool addRoute =
false;
1052 if (dsr->IsLinkCache ())
1054 addRoute = dsr->AddRoute_Link (nodeList, ipv4Address);
1058 addRoute = dsr->AddRoute (toDestination);
1062 dsr->CancelRreqTimer (dst,
true);
1077 NS_ASSERT (routeCopy.back () == source);
1079 NS_LOG_DEBUG (
"The nextHop address " << nextHop <<
" and the source in the route reply " << source);
1091 NS_LOG_DEBUG (
"The reply header length " << (uint32_t)length);
1098 newPacket->
AddHeader (dsrRoutingHeader);
1099 dsr->SendReply (newPacket, ipv4Address, nextHop,
m_ipv4Route);
1111 .AddConstructor<DsrOptionSR> ()
1139 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Address << ipv4Header << (uint32_t)protocol << isPromisc);
1144 uint8_t numberAddress = (buf[1] - 2) / 4;
1177 bool findSame = dsr->FindSamePackets (packet, source, destination, segsLeft);
1181 NS_LOG_DEBUG (
"Received one passive acknowledgment for a successfully delivered packet");
1186 dsr->SendGratuitousReply (source, srcAddress, nodeList, protocol);
1198 uint8_t length = sourceRoute.
GetLength ();
1199 uint8_t nextAddressIndex;
1203 uint32_t size = p->
GetSize ();
1204 uint8_t *
data =
new uint8_t[size];
1206 uint8_t optionType = 0;
1207 optionType = *(
data);
1210 NS_LOG_DEBUG (
"The option type over here " << (uint32_t)optionType);
1211 if (optionType == 160)
1213 NS_LOG_DEBUG (
"Here we remove the ack request header and add ack header to the packet");
1217 uint16_t ackId = ackReq.
GetAckId ();
1223 NS_LOG_DEBUG (
"Send back ACK to the earlier hop " << srcAddress <<
" from us " << ipv4Address);
1224 dsr->SendAck (ackId, srcAddress, source, destination, protocol, m_ipv4Route);
1237 if (length % 2 != 0)
1244 if (segsLeft > numberAddress)
1255 nextAddressIndex = numberAddress - segsLeft;
1257 NS_LOG_DEBUG (
"The next address of source route option " << nextAddress <<
" and the nextAddressIndex: " << (uint32_t)nextAddressIndex <<
" and the segments left : " << (uint32_t)segsLeft);
1269 if (nextHop ==
"0.0.0.0")
1272 dsr->PacketNewRoute (dsrP, realSource, targetAddress, protocol);
1276 if (ipv4Address == nextHop)
1289 SetRoute (nextAddress, ipv4Address);
1291 dsr->ForwardPacket (dsrP, newSourceRoute, ipv4Header, realSource, nextAddress, targetAddress, protocol,
m_ipv4Route);
1302 .AddConstructor<DsrOptionRerr> ()
1330 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
1332 uint32_t size = p->
GetSize ();
1333 uint8_t *
data =
new uint8_t[size];
1335 uint8_t errorType = *(data + 2);
1345 NS_LOG_DEBUG (
"The error type value here " << (uint32_t)errorType);
1359 NS_LOG_DEBUG (
"The destination address and the unreachable node " << rerrUnreach.
GetErrorDst () <<
" " << unreachAddress);
1368 dsr->DeleteAllRoutesIncludeLink (errorSource, unreachAddress, ipv4Address);
1371 uint32_t serialized =
DoSendError (newP, rerrUnreach, rerrSize, ipv4Address, protocol);
1390 uint32_t serialized = 0;
1400 uint8_t numberAddress = (buf[1] - 2) / 4;
1403 NS_LOG_DEBUG (
"The number of addresses " << (uint32_t)numberAddress);
1417 uint8_t length = sourceRoute.
GetLength ();
1418 uint8_t nextAddressIndex;
1430 if (length % 2 != 0)
1437 if (segmentsLeft > numberAddress)
1446 if (segmentsLeft == 0 && targetAddress == ipv4Address)
1448 NS_LOG_INFO (
"This is the destination of the error, send error request");
1449 dsr->SendErrorRequest (rerr, protocol);
1450 return serializedSize;
1456 nextAddressIndex = numberAddress - segmentsLeft;
1466 return serializedSize;
1470 SetRoute (nextAddress, ipv4Address);
1471 dsr->ForwardErrPacket (rerr, newSourceRoute, nextAddress, protocol,
m_ipv4Route);
1472 return serializedSize;
1479 static TypeId tid =
TypeId (
"ns3::dsr::DsrOptionAckReq")
1481 .AddConstructor<DsrOptionAckReq> ()
1509 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
1525 NS_LOG_DEBUG (
"The next header value " << (uint32_t)protocol);
1528 SetRoute (srcAddress, ipv4Address);
1539 .AddConstructor<DsrOptionAck> ()
1567 NS_LOG_FUNCTION (
this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
1585 dsr->UpdateRouteEntry (realDst);
1589 dsr->CallCancelPacketTimer (ackId, ipv4Header, realSrc, realDst);