src/dsr/model/dsr-options.cc
changeset 8752 2da1fab73114
parent 8751 efad81f3cb47
child 8753 8d84b96e26bf
equal deleted inserted replaced
8751:efad81f3cb47 8752:2da1fab73114
   104   return m_node;
   104   return m_node;
   105 }
   105 }
   106 
   106 
   107 bool DsrOptions::ContainAddressAfter (Ipv4Address ipv4Address, Ipv4Address destAddress, std::vector<Ipv4Address> &nodeList)
   107 bool DsrOptions::ContainAddressAfter (Ipv4Address ipv4Address, Ipv4Address destAddress, std::vector<Ipv4Address> &nodeList)
   108 {
   108 {
       
   109   NS_LOG_FUNCTION (this << ipv4Address << destAddress);
   109   std::vector<Ipv4Address>::iterator it = find (nodeList.begin (), nodeList.end (), destAddress);
   110   std::vector<Ipv4Address>::iterator it = find (nodeList.begin (), nodeList.end (), destAddress);
   110 
   111 
   111   for (std::vector<Ipv4Address>::iterator i = it; i != nodeList.end (); ++i)
   112   for (std::vector<Ipv4Address>::iterator i = it; i != nodeList.end (); ++i)
   112     {
   113     {
   113       if ((ipv4Address == (*i)) && ((*i) != nodeList.back ()))
   114       if ((ipv4Address == (*i)) && ((*i) != nodeList.back ()))
   119 }
   120 }
   120 
   121 
   121 std::vector<Ipv4Address>
   122 std::vector<Ipv4Address>
   122 DsrOptions::CutRoute (Ipv4Address ipv4Address, std::vector<Ipv4Address> &nodeList)
   123 DsrOptions::CutRoute (Ipv4Address ipv4Address, std::vector<Ipv4Address> &nodeList)
   123 {
   124 {
       
   125   NS_LOG_FUNCTION (this << ipv4Address);
   124   std::vector<Ipv4Address>::iterator it = find (nodeList.begin (), nodeList.end (), ipv4Address);
   126   std::vector<Ipv4Address>::iterator it = find (nodeList.begin (), nodeList.end (), ipv4Address);
   125   std::vector<Ipv4Address> cutRoute;
   127   std::vector<Ipv4Address> cutRoute;
   126   for (std::vector<Ipv4Address>::iterator i = it; i != nodeList.end (); ++i)
   128   for (std::vector<Ipv4Address>::iterator i = it; i != nodeList.end (); ++i)
   127     {
   129     {
   128       cutRoute.push_back (*i);
   130       cutRoute.push_back (*i);
   140   return m_ipv4Route;
   142   return m_ipv4Route;
   141 }
   143 }
   142 
   144 
   143 bool DsrOptions::ReverseRoutes (std::vector<Ipv4Address> & vec)
   145 bool DsrOptions::ReverseRoutes (std::vector<Ipv4Address> & vec)
   144 {
   146 {
       
   147   NS_LOG_FUNCTION (this);
   145   std::vector<Ipv4Address> vec2 (vec);
   148   std::vector<Ipv4Address> vec2 (vec);
   146   vec.clear ();    // To ensure vec is empty before start
   149   vec.clear ();    // To ensure vec is empty before start
   147   for (std::vector<Ipv4Address>::reverse_iterator ri = vec2.rbegin (); ri
   150   for (std::vector<Ipv4Address>::reverse_iterator ri = vec2.rbegin (); ri
   148        != vec2.rend (); ++ri)
   151        != vec2.rend (); ++ri)
   149     {
   152     {
   157   return false;
   160   return false;
   158 }
   161 }
   159 
   162 
   160 Ipv4Address DsrOptions::SearchNextHop (Ipv4Address ipv4Address, std::vector<Ipv4Address>& vec)
   163 Ipv4Address DsrOptions::SearchNextHop (Ipv4Address ipv4Address, std::vector<Ipv4Address>& vec)
   161 {
   164 {
       
   165   NS_LOG_FUNCTION (this << ipv4Address);
   162   Ipv4Address nextHop;
   166   Ipv4Address nextHop;
       
   167   NS_LOG_DEBUG ("the vector size " << vec.size ());
   163   if (vec.size () == 2)
   168   if (vec.size () == 2)
   164     {
   169     {
   165       NS_LOG_DEBUG ("The two nodes are neighbors");
   170       NS_LOG_DEBUG ("The two nodes are neighbors");
   166       nextHop = vec[1];
   171       nextHop = vec[1];
   167       return nextHop;
   172       return nextHop;
   171       if (ipv4Address == vec.back ())
   176       if (ipv4Address == vec.back ())
   172         {
   177         {
   173           NS_LOG_DEBUG ("We have reached to the final destination " << ipv4Address << " " << vec.back ());
   178           NS_LOG_DEBUG ("We have reached to the final destination " << ipv4Address << " " << vec.back ());
   174           return ipv4Address;
   179           return ipv4Address;
   175         }
   180         }
   176       for (std::vector<Ipv4Address>::iterator i = vec.begin (); i != vec.end (); ++i)
   181       for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
   177         {
   182         {
   178           if (ipv4Address == (*i))
   183           if (ipv4Address == (*i))
   179             {
   184             {
   180               nextHop = *(++i);
   185               nextHop = *(++i);
   181               return nextHop;
   186               return nextHop;
   187   return none;
   192   return none;
   188 }
   193 }
   189 
   194 
   190 Ipv4Address DsrOptions::ReverseSearchNextHop (Ipv4Address ipv4Address, std::vector<Ipv4Address>& vec)
   195 Ipv4Address DsrOptions::ReverseSearchNextHop (Ipv4Address ipv4Address, std::vector<Ipv4Address>& vec)
   191 {
   196 {
       
   197   NS_LOG_FUNCTION (this << ipv4Address);
   192   Ipv4Address nextHop;
   198   Ipv4Address nextHop;
   193   if (vec.size () == 2)
   199   if (vec.size () == 2)
   194     {
   200     {
   195       NS_LOG_DEBUG ("The two nodes are neighbors");
   201       NS_LOG_DEBUG ("The two nodes are neighbors");
   196       nextHop = vec[0];
   202       nextHop = vec[0];
   212   return none;
   218   return none;
   213 }
   219 }
   214 
   220 
   215 void DsrOptions::PrintVector (std::vector<Ipv4Address>& vec)
   221 void DsrOptions::PrintVector (std::vector<Ipv4Address>& vec)
   216 {
   222 {
       
   223   NS_LOG_FUNCTION (this);
   217   /*
   224   /*
   218    * Check elements in a route vector
   225    * Check elements in a route vector
   219    */
   226    */
   220   if (!vec.size ())
   227   if (!vec.size ())
   221     {
   228     {
   231     }
   238     }
   232 }
   239 }
   233 
   240 
   234 bool DsrOptions::IfDuplicates (std::vector<Ipv4Address>& vec, std::vector<Ipv4Address>& vec2)
   241 bool DsrOptions::IfDuplicates (std::vector<Ipv4Address>& vec, std::vector<Ipv4Address>& vec2)
   235 {
   242 {
       
   243   NS_LOG_FUNCTION (this);
       
   244   NS_LOG_DEBUG ("The first vector ");
       
   245   PrintVector (vec);
       
   246   NS_LOG_DEBUG ("The second vector ");
       
   247   PrintVector (vec2);
   236   for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
   248   for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
   237     {
   249     {
   238       for (std::vector<Ipv4Address>::const_iterator j = vec2.begin (); j != vec2.end (); ++j)
   250       for (std::vector<Ipv4Address>::const_iterator j = vec2.begin (); j != vec2.end (); ++j)
   239         {
   251         {
   240           if ((*i) == (*j))
   252           if ((*i) == (*j))
   250   return false;
   262   return false;
   251 }
   263 }
   252 
   264 
   253 bool DsrOptions::CheckDuplicates (Ipv4Address ipv4Address, std::vector<Ipv4Address>& vec)
   265 bool DsrOptions::CheckDuplicates (Ipv4Address ipv4Address, std::vector<Ipv4Address>& vec)
   254 {
   266 {
       
   267   NS_LOG_FUNCTION (this << ipv4Address);
   255   for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
   268   for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
   256     {
   269     {
   257       if ((*i) == ipv4Address)
   270       if ((*i) == ipv4Address)
   258         {
   271         {
   259           return true;
   272           return true;
   266   return false;
   279   return false;
   267 }
   280 }
   268 
   281 
   269 void DsrOptions::RemoveDuplicates (std::vector<Ipv4Address>& vec)
   282 void DsrOptions::RemoveDuplicates (std::vector<Ipv4Address>& vec)
   270 {
   283 {
       
   284   NS_LOG_FUNCTION (this);
   271   //Remove duplicate ip address from the route if any, should not happen with normal behavior nodes
   285   //Remove duplicate ip address from the route if any, should not happen with normal behavior nodes
   272   std::vector<Ipv4Address> vec2 (vec); // declare vec2 as a copy of the vec
   286   std::vector<Ipv4Address> vec2 (vec); // declare vec2 as a copy of the vec
   273   PrintVector (vec2); // Print all the ip address in the route
   287   PrintVector (vec2); // Print all the ip address in the route
   274   vec.clear (); // clear vec
   288   vec.clear (); // clear vec
   275   for (std::vector<Ipv4Address>::const_iterator i = vec2.begin (); i != vec2.end (); ++i)
   289   for (std::vector<Ipv4Address>::const_iterator i = vec2.begin (); i != vec2.end (); ++i)
   310 }
   324 }
   311 
   325 
   312 uint32_t
   326 uint32_t
   313 DsrOptions::GetIDfromIP (Ipv4Address address)
   327 DsrOptions::GetIDfromIP (Ipv4Address address)
   314 {
   328 {
       
   329   NS_LOG_FUNCTION (this << address);
   315   int32_t nNodes = NodeList::GetNNodes ();
   330   int32_t nNodes = NodeList::GetNNodes ();
   316   for (int32_t i = 0; i < nNodes; ++i)
   331   for (int32_t i = 0; i < nNodes; ++i)
   317     {
   332     {
   318       Ptr<Node> node = NodeList::GetNode (i);
   333       Ptr<Node> node = NodeList::GetNode (i);
   319       Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
   334       Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
   325   return 255;
   340   return 255;
   326 }
   341 }
   327 
   342 
   328 Ptr<Node> DsrOptions::GetNodeWithAddress (Ipv4Address ipv4Address)
   343 Ptr<Node> DsrOptions::GetNodeWithAddress (Ipv4Address ipv4Address)
   329 {
   344 {
       
   345   NS_LOG_FUNCTION (this << ipv4Address);
   330   int32_t nNodes = NodeList::GetNNodes ();
   346   int32_t nNodes = NodeList::GetNNodes ();
   331   for (int32_t i = 0; i < nNodes; ++i)
   347   for (int32_t i = 0; i < nNodes; ++i)
   332     {
   348     {
   333       Ptr<Node> node = NodeList::GetNode (i);
   349       Ptr<Node> node = NodeList::GetNode (i);
   334       Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
   350       Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
   370 }
   386 }
   371 
   387 
   372 uint8_t DsrOptionPad1::Process (Ptr<Packet> packet, Ptr<Packet> dsrP, Ipv4Address ipv4Address, Ipv4Address source, Ipv4Header const& ipv4Header, uint8_t protocol, bool& isPromisc)
   388 uint8_t DsrOptionPad1::Process (Ptr<Packet> packet, Ptr<Packet> dsrP, Ipv4Address ipv4Address, Ipv4Address source, Ipv4Header const& ipv4Header, uint8_t protocol, bool& isPromisc)
   373 {
   389 {
   374   NS_LOG_FUNCTION (this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
   390   NS_LOG_FUNCTION (this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
   375 
       
   376   Ptr<Packet> p = packet->Copy ();
   391   Ptr<Packet> p = packet->Copy ();
   377   DsrOptionPad1Header pad1Header;
   392   DsrOptionPad1Header pad1Header;
   378   p->RemoveHeader (pad1Header);
   393   p->RemoveHeader (pad1Header);
   379 
   394 
   380   isPromisc = false;
   395   isPromisc = false;
   412 uint8_t DsrOptionPadn::Process (Ptr<Packet> packet, Ptr<Packet> dsrP, Ipv4Address ipv4Address, Ipv4Address source, Ipv4Header const& ipv4Header, uint8_t protocol, bool& isPromisc)
   427 uint8_t DsrOptionPadn::Process (Ptr<Packet> packet, Ptr<Packet> dsrP, Ipv4Address ipv4Address, Ipv4Address source, Ipv4Header const& ipv4Header, uint8_t protocol, bool& isPromisc)
   413 {
   428 {
   414   NS_LOG_FUNCTION (this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
   429   NS_LOG_FUNCTION (this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
   415 
   430 
   416   Ptr<Packet> p = packet->Copy ();
   431   Ptr<Packet> p = packet->Copy ();
   417 
       
   418   DsrOptionPadnHeader padnHeader;
   432   DsrOptionPadnHeader padnHeader;
   419   p->RemoveHeader (padnHeader);
   433   p->RemoveHeader (padnHeader);
   420 
   434 
   421   isPromisc = false;
   435   isPromisc = false;
   422 
   436 
   457 }
   471 }
   458 
   472 
   459 uint8_t DsrOptionRreq::Process (Ptr<Packet> packet, Ptr<Packet> dsrP, Ipv4Address ipv4Address, Ipv4Address source, Ipv4Header const& ipv4Header, uint8_t protocol, bool& isPromisc)
   473 uint8_t DsrOptionRreq::Process (Ptr<Packet> packet, Ptr<Packet> dsrP, Ipv4Address ipv4Address, Ipv4Address source, Ipv4Header const& ipv4Header, uint8_t protocol, bool& isPromisc)
   460 {
   474 {
   461   NS_LOG_FUNCTION (this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
   475   NS_LOG_FUNCTION (this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
       
   476   // Fields from IP header
       
   477   Ipv4Address srcAddress = ipv4Header.GetSource ();
       
   478   Ipv4Address destAddress = ipv4Header.GetDestination ();
       
   479   /*
       
   480    * \ when the ip source address is equal to the address of our own, this is request packet originated
       
   481    * \ by the node itself, discard it
       
   482    */
       
   483   if (srcAddress == ipv4Address)
       
   484     {
       
   485       NS_LOG_DEBUG ("Discard the packet");
       
   486       m_dropTrace (packet); // call the drop trace to show in the tracing
       
   487       return 0;
       
   488     }
   462   /*
   489   /*
   463    * Get the node associated with the ipv4 address and get several objects from the node and leave for further use
   490    * Get the node associated with the ipv4 address and get several objects from the node and leave for further use
   464    */
   491    */
   465   Ptr<Node> node = GetNodeWithAddress (ipv4Address);
   492   Ptr<Node> node = GetNodeWithAddress (ipv4Address);
   466   Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
   493   Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
   467   Ptr<dsr::RouteCache> m_routeCache = node->GetObject<dsr::RouteCache> ();
       
   468   Ptr<dsr::RreqTable>  m_rreqTable = node->GetObject<dsr::RreqTable> ();
       
   469   ActiveRouteTimeout = m_routeCache->GetCacheTimeout ();
       
   470 
   494 
   471   // Set the isError boolean value as false
   495   // Set the isError boolean value as false
   472   bool isError = false;
   496   bool isError = false;
   473   Ptr<Packet> p = packet->Copy (); // The packet here doesn't contain the fixed size dsr header
   497   Ptr<Packet> p = packet->Copy (); // The packet here doesn't contain the fixed size dsr header
   474   /*
   498   /*
   476    * \peek the packet and get the value
   500    * \peek the packet and get the value
   477    */
   501    */
   478   uint8_t buf[2];
   502   uint8_t buf[2];
   479   p->CopyData (buf, sizeof(buf));
   503   p->CopyData (buf, sizeof(buf));
   480   uint8_t numberAddress = (buf[1] - 6) / 4;
   504   uint8_t numberAddress = (buf[1] - 6) / 4;
   481 
   505   NS_LOG_DEBUG ("The number of Ip addresses " << (uint32_t)numberAddress);
   482   if (numberAddress >= 255)
   506   if (numberAddress >= 255)
   483     {
   507     {
   484       NS_LOG_DEBUG ("Discard the packet, malformed header since two many ip addresses in route");
   508       NS_LOG_DEBUG ("Discard the packet, malformed header since two many ip addresses in route");
   485       m_dropTrace (packet); // call the drop trace to show in the tracing
   509       m_dropTrace (packet); // call the drop trace to show in the tracing
   486       return 0;
   510       return 0;
   487     }
   511     }
   488 
   512 
   489   NS_LOG_DEBUG ("The number of Ip addresses " << (uint32_t)numberAddress);
       
   490   /*
   513   /*
   491    * Create the dsr rreq header
   514    * Create the dsr rreq header
   492    */
   515    */
   493   DsrOptionRreqHeader rreq;
   516   DsrOptionRreqHeader rreq;
   494 
       
   495   // Fields from IP header
       
   496   Ipv4Address srcAddress = ipv4Header.GetSource ();
       
   497   Ipv4Address destAddress = ipv4Header.GetDestination ();
       
   498   /*
   517   /*
   499    * Set the number of addresses with the value from peek data and remove the rreq header
   518    * Set the number of addresses with the value from peek data and remove the rreq header
   500    */
   519    */
   501   rreq.SetNumberAddress (numberAddress);
   520   rreq.SetNumberAddress (numberAddress);
   502 
   521   // Remove the route request header
   503   /*
   522   p->RemoveHeader (rreq);
   504    * \ when the ip source address is equal to the address of our own, this is request packet originated
   523   // Verify the option length
   505    * \ by the node itself, discard it
   524   uint8_t length = rreq.GetLength ();
   506    */
   525   if (length % 2 != 0)
   507   if (srcAddress == ipv4Address)
   526     {
   508     {
   527       NS_LOG_LOGIC ("Malformed header. Drop!");
   509       NS_LOG_DEBUG ("Discard the packet");
   528       m_dropTrace (packet); // call drop trace
   510       m_dropTrace (packet); // call the drop trace to show in the tracing
       
   511       return 0;
   529       return 0;
   512     }
   530     }
   513 
       
   514   p->RemoveHeader (rreq);
       
   515   Ptr<Packet> errP = p->Copy ();
       
   516 
       
   517   // The target address is where we want to send the data packets
   531   // The target address is where we want to send the data packets
   518   Ipv4Address targetAddress = rreq.GetTarget ();
   532   Ipv4Address targetAddress = rreq.GetTarget ();
   519   uint16_t id = rreq.GetId ();
       
   520   // Get the node list and source address from the route request header
   533   // Get the node list and source address from the route request header
   521   std::vector<Ipv4Address> nodeList = rreq.GetNodesAddresses ();
   534   std::vector<Ipv4Address> mainVector = rreq.GetNodesAddresses ();
       
   535   std::vector<Ipv4Address> nodeList (mainVector);
   522   PrintVector (nodeList);
   536   PrintVector (nodeList);
   523 
   537   /*
   524   // Get the TTL value
   538    * Construct the dsr routing header for future use
   525   uint8_t ttl = ipv4Header.GetTtl ();
   539    */
   526   NS_LOG_DEBUG ("The TTL " << (uint32_t) ttl);
   540   DsrRoutingHeader dsrRoutingHeader;
   527   /*
   541   dsrRoutingHeader.SetNextHeader (protocol);
   528    *  Node checks to determine whether it has received a RREQ with the same Originator IP Address and RREQ ID.
   542   dsrRoutingHeader.SetMessageType (1);
   529    *  If such a RREQ has been received, the node silently discards the newly received RREQ.
   543   dsrRoutingHeader.SetSourceId (GetIDfromIP (source));
   530    */
   544   dsrRoutingHeader.SetDestId (255);
   531   if (ttl)
       
   532     {
       
   533       // if the TTL value is not 0, save the route request entry
       
   534       if (m_rreqTable->FindSrc (source, targetAddress, id))
       
   535         {
       
   536           NS_LOG_DEBUG ("Received the same route request recently, not processing it further");
       
   537           return 0;
       
   538         }
       
   539     }
       
   540 
   545 
   541   if (CheckDuplicates (ipv4Address, nodeList))
   546   if (CheckDuplicates (ipv4Address, nodeList))
   542     {
   547     {
   543       /*
   548       /*
   544        * if the route contains the node address already, drop the request packet
   549        * if the route contains the node address already, drop the request packet
   547       NS_LOG_DEBUG ("Our node address is already seen in the route, drop the request");
   552       NS_LOG_DEBUG ("Our node address is already seen in the route, drop the request");
   548       return 0;
   553       return 0;
   549     }
   554     }
   550   else
   555   else
   551     {
   556     {
   552       // Verify the option length
       
   553       uint8_t length = rreq.GetLength ();
       
   554       if (length % 2 != 0)
       
   555         {
       
   556           NS_LOG_LOGIC ("Malformed header. Drop!");
       
   557           m_dropTrace (packet); // call drop trace
       
   558           return 0;
       
   559         }
       
   560 
       
   561       // A node ignores all RREQs received from any node in its blacklist
   557       // A node ignores all RREQs received from any node in its blacklist
   562       RouteCacheEntry toPrev;
   558       RouteCacheEntry toPrev;
   563       /*
   559       /*
   564        *  When the reverse route is created or updated, the following actions on the route are also carried out:
   560        *  When the reverse route is created or updated, the following actions on the route are also carried out:
   565        *  3. the next hop in the routing table becomes the node from which the  RREQ was received
   561        *  3. the next hop in the routing table becomes the node from which the  RREQ was received
   569       //  A node generates a RREP if either:
   565       //  A node generates a RREP if either:
   570       //  (i)  it is itself the destination,
   566       //  (i)  it is itself the destination,
   571       /*
   567       /*
   572        * The target address equal to our own ip address
   568        * The target address equal to our own ip address
   573        */
   569        */
       
   570       NS_LOG_DEBUG ("The target address over here " << targetAddress << " and the ip address " << ipv4Address << " and the source address " << mainVector[0]);
   574       if (targetAddress == ipv4Address)
   571       if (targetAddress == ipv4Address)
   575         {
   572         {
   576           Ipv4Address nextHop; // Declare the next hop address to use
   573           Ipv4Address nextHop; // Declare the next hop address to use
   577           if (nodeList.size () == 1)
   574           if (nodeList.size () == 1)
   578             {
   575             {
   582               m_finalRoute.push_back (ipv4Address);    // push back our own address
   579               m_finalRoute.push_back (ipv4Address);    // push back our own address
   583               nextHop = srcAddress;
   580               nextHop = srcAddress;
   584             }
   581             }
   585           else
   582           else
   586             {
   583             {
   587               nodeList.push_back (ipv4Address);    // push back our own address
   584               std::vector<Ipv4Address> changeRoute (nodeList);
       
   585               changeRoute.push_back (ipv4Address);    // push back our own address
   588               m_finalRoute.clear ();              // get a clear route vector
   586               m_finalRoute.clear ();              // get a clear route vector
   589               for (std::vector<Ipv4Address>::iterator i = nodeList.begin (); i != nodeList.end (); ++i)
   587               for (std::vector<Ipv4Address>::iterator i = changeRoute.begin (); i != changeRoute.end (); ++i)
   590                 {
   588                 {
   591                   m_finalRoute.push_back (*i);  // Get the full route from source to destination
   589                   m_finalRoute.push_back (*i);  // Get the full route from source to destination
   592                 }
   590                 }
   593               PrintVector (m_finalRoute);
   591               PrintVector (m_finalRoute);
   594               nextHop = ReverseSearchNextHop (ipv4Address, m_finalRoute); // get the next hop
   592               nextHop = ReverseSearchNextHop (ipv4Address, m_finalRoute); // get the next hop
   597           DsrOptionRrepHeader rrep;
   595           DsrOptionRrepHeader rrep;
   598           rrep.SetNodesAddress (m_finalRoute);     // Set the node addresses in the route reply header
   596           rrep.SetNodesAddress (m_finalRoute);     // Set the node addresses in the route reply header
   599           NS_LOG_DEBUG ("The nextHop address " << nextHop);
   597           NS_LOG_DEBUG ("The nextHop address " << nextHop);
   600           Ipv4Address replyDst = m_finalRoute.front ();
   598           Ipv4Address replyDst = m_finalRoute.front ();
   601           /*
   599           /*
   602            * This part add dsr header to the packet and send reply
   600            * This part add dsr header to the packet and send route reply packet
   603            */
   601            */
   604           DsrRoutingHeader dsrRoutingHeader;
   602           DsrRoutingHeader dsrRoutingHeader;
   605           dsrRoutingHeader.SetNextHeader (protocol);
   603           dsrRoutingHeader.SetNextHeader (protocol);
   606           dsrRoutingHeader.SetMessageType (1);
   604           dsrRoutingHeader.SetMessageType (1);
   607           dsrRoutingHeader.SetSourceId (GetIDfromIP (ipv4Address));
   605           dsrRoutingHeader.SetSourceId (GetIDfromIP (ipv4Address));
   624               PrintVector (m_finalRoute);
   622               PrintVector (m_finalRoute);
   625               Ipv4Address dst = m_finalRoute.back ();
   623               Ipv4Address dst = m_finalRoute.back ();
   626               bool addRoute = false;
   624               bool addRoute = false;
   627               if (numberAddress > 0)
   625               if (numberAddress > 0)
   628                 {
   626                 {
   629                   RouteCacheEntry toSource (/*IP_VECTOR=*/ m_finalRoute, /*dst=*/
   627                   RouteCacheEntry toSource (/*IP_VECTOR=*/m_finalRoute, /*dst=*/
   630                                                            dst, /*expire time=*/ ActiveRouteTimeout);
   628                                   dst, /*expire time=*/ActiveRouteTimeout);
   631                   if (m_routeCache->IsLinkCache ())
   629                   if (dsr->IsLinkCache ())
   632                     {
   630                     {
   633                       addRoute = m_routeCache->AddRoute_Link (m_finalRoute, ipv4Address);
   631                       addRoute = dsr->AddRoute_Link (m_finalRoute, ipv4Address);
   634                     }
   632                     }
   635                   else
   633                   else
   636                     {
   634                     {
   637                       addRoute = m_routeCache->AddRoute (toSource);
   635                       addRoute = dsr->AddRoute (toSource);
   638                     }
   636                     }
   639                 }
   637                 }
   640               else
   638               else
   641                 {
   639                 {
   642                   NS_LOG_DEBUG ("Abnormal RouteRequest");
   640                   NS_LOG_DEBUG ("Abnormal RouteRequest");
   643                   return 0;
   641                   return 0;
   644                 }
   642                 }
   645 
   643 
   646               if (addRoute)
   644               if (addRoute)
   647                 {
   645                 {
   648                   NS_LOG_DEBUG ("The route is failed to add in cache");
       
   649                   return 0;
       
   650                 }
       
   651               else
       
   652                 {
       
   653                   /*
   646                   /*
   654                    * Found a route to the dst, construct the source route option header
   647                    * Found a route to the dst, construct the source route option header
   655                    */
   648                    */
   656                   DsrOptionSRHeader sourceRoute;
   649                   DsrOptionSRHeader sourceRoute;
   657                   NS_LOG_DEBUG ("The route length " << m_finalRoute.size ());
   650                   NS_LOG_DEBUG ("The route length " << m_finalRoute.size ());
   658                   sourceRoute.SetNodesAddress (m_finalRoute);
   651                   sourceRoute.SetNodesAddress (m_finalRoute);
   659                   if (m_routeCache->IsLinkCache ())
   652                   if (dsr->IsLinkCache ())
   660                     {
   653                     {
   661                       m_routeCache->UseExtends (m_finalRoute);
   654                       dsr->UseExtends (m_finalRoute);
   662                     }
   655                     }
   663                   sourceRoute.SetSegmentsLeft ((m_finalRoute.size () - 2));
   656                   sourceRoute.SetSegmentsLeft ((m_finalRoute.size () - 2));
   664                   uint8_t salvage = 0;
   657                   // The salvage value here is 0
   665                   sourceRoute.SetSalvage (salvage);
   658                   sourceRoute.SetSalvage (0);
   666                   Ipv4Address nextHop = SearchNextHop (ipv4Address, m_finalRoute); // Get the next hop address
   659                   Ipv4Address nextHop = SearchNextHop (ipv4Address, m_finalRoute); // Get the next hop address
   667                   NS_LOG_DEBUG ("The nextHop address " << nextHop);
   660                   NS_LOG_DEBUG ("The nextHop address " << nextHop);
   668 
   661 
   669                   if (nextHop == "0.0.0.0")
   662                   if (nextHop == "0.0.0.0")
   670                     {
   663                     {
   671                       dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
   664                       dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
   672                       return 0;
   665                       return 0;
   673                     }
   666                     }
   674                   SetRoute (nextHop, ipv4Address);
   667                   SetRoute (nextHop, ipv4Address);
   675                   /*
   668                   /*
   676                    * Schedule the packet retry
   669                    * Send the data packet from the send buffer
   677                    */
   670                    */
   678                   dsr->SendPacket (sourceRoute, nextHop, protocol);
   671                   dsr->SendPacketFromBuffer (sourceRoute, nextHop, protocol);
   679                   // Cancel the route request timer for destination
   672                   // Cancel the route request timer for destination after sending the data packet
   680                   dsr->CancelRreqTimer (dst);
   673                   dsr->CancelRreqTimer (dst, true);
       
   674                 }
       
   675               else
       
   676                 {
       
   677                   NS_LOG_DEBUG ("The route is failed to add in cache");
       
   678                   return 0;
   681                 }
   679                 }
   682             }
   680             }
   683           else
   681           else
   684             {
   682             {
   685               NS_LOG_DEBUG ("Unable to reverse route");
   683               NS_LOG_DEBUG ("Unable to reverse route");
   692       /*
   690       /*
   693        * (ii) or it has an active route to the destination, send reply based on request header and route cache,
   691        * (ii) or it has an active route to the destination, send reply based on request header and route cache,
   694        *      need to delay based on a random value from d = H * (h - 1 + r), which can avoid possible route
   692        *      need to delay based on a random value from d = H * (h - 1 + r), which can avoid possible route
   695        *      reply storm.
   693        *      reply storm.
   696        */
   694        */
   697       else if (m_routeCache->LookupRoute (targetAddress, toPrev))
   695       else if (dsr->LookupRoute (targetAddress, toPrev))
   698         {
   696         {
   699 
       
   700           RouteCacheEntry::IP_VECTOR ip = toPrev.GetVector (); // The route from our own route cache to dst
   697           RouteCacheEntry::IP_VECTOR ip = toPrev.GetVector (); // The route from our own route cache to dst
   701 
       
   702           PrintVector (ip);
   698           PrintVector (ip);
   703           std::vector<Ipv4Address> nodeList = rreq.GetNodesAddresses (); // Route from rreq header
       
   704           std::vector<Ipv4Address> saveRoute (nodeList);
   699           std::vector<Ipv4Address> saveRoute (nodeList);
   705           PrintVector (saveRoute);
   700           PrintVector (saveRoute);
   706           // Verify if the two vector contains duplicates, if so, do not use
   701           // Verify if the two vector contains duplicates, if so, do not use
   707           // the route found and forward the route request
   702           // the route found and forward the route request
   708           if (!(IfDuplicates (ip, nodeList)))
   703           if (!(IfDuplicates (ip, saveRoute)))
   709             {
   704             {
   710               m_finalRoute.clear (); // Clear the final route vector
   705               m_finalRoute.clear ();            // Clear the final route vector
   711               /**
   706               /**
   712                * push back the intermediate node address from the source to this node
   707                * push back the intermediate node address from the source to this node
   713                */
   708                */
   714               for (std::vector<Ipv4Address>::iterator i = nodeList.begin (); i != nodeList.end (); ++i)
   709               for (std::vector<Ipv4Address>::iterator i = saveRoute.begin (); i != saveRoute.end (); ++i)
   715                 {
   710                 {
   716                   m_finalRoute.push_back (*i);
   711                   m_finalRoute.push_back (*i);
   717                 }
   712                 }
   718               /**
   713               /**
   719                * push back the route vector we found in our route cache to destination, including this node's address
   714                * push back the route vector we found in our route cache to destination, including this node's address
   728               bool addRoute = false;
   723               bool addRoute = false;
   729               std::vector<Ipv4Address> reverseRoute (m_finalRoute);
   724               std::vector<Ipv4Address> reverseRoute (m_finalRoute);
   730 
   725 
   731               if (ReverseRoutes (reverseRoute))
   726               if (ReverseRoutes (reverseRoute))
   732                 {
   727                 {
   733                   Ipv4Address dst = reverseRoute.back ();
       
   734                   saveRoute.push_back (ipv4Address);
   728                   saveRoute.push_back (ipv4Address);
   735                   ReverseRoutes (saveRoute);
   729                   ReverseRoutes (saveRoute);
       
   730                   Ipv4Address dst = saveRoute.back ();
       
   731                   NS_LOG_DEBUG ("This is the route save in route cache");
   736                   PrintVector (saveRoute);
   732                   PrintVector (saveRoute);
       
   733 
   737                   RouteCacheEntry toSource (/*IP_VECTOR=*/ saveRoute, /*dst=*/ dst, /*expire time=*/ ActiveRouteTimeout);
   734                   RouteCacheEntry toSource (/*IP_VECTOR=*/ saveRoute, /*dst=*/ dst, /*expire time=*/ ActiveRouteTimeout);
   738                   NS_ASSERT (saveRoute.front () == ipv4Address);
   735                   NS_ASSERT (saveRoute.front () == ipv4Address);
   739                   // Add the route entry in the route cache
   736                   // Add the route entry in the route cache
   740                   if (m_routeCache->IsLinkCache ())
   737                   if (dsr->IsLinkCache ())
   741                     {
   738                     {
   742                       addRoute = m_routeCache->AddRoute_Link (saveRoute, ipv4Address);
   739                       addRoute = dsr->AddRoute_Link (saveRoute, ipv4Address);
   743                     }
   740                     }
   744                   else
   741                   else
   745                     {
   742                     {
   746                       addRoute = m_routeCache->AddRoute (toSource);
   743                       addRoute = dsr->AddRoute (toSource);
   747                     }
   744                     }
       
   745 
   748                   if (addRoute)
   746                   if (addRoute)
   749                     {
       
   750                       NS_LOG_DEBUG ("The route is failed to add in cache");
       
   751                       return 0;
       
   752                     }
       
   753                   else
       
   754                     {
   747                     {
   755                       NS_LOG_DEBUG ("We have added the route and search send buffer for packet with destination " << dst);
   748                       NS_LOG_DEBUG ("We have added the route and search send buffer for packet with destination " << dst);
   756                       /*
   749                       /*
   757                        * Found a route the dst, construct the source route option header
   750                        * Found a route the dst, construct the source route option header
   758                        */
   751                        */
   759                       DsrOptionSRHeader sourceRoute;
   752                       DsrOptionSRHeader sourceRoute;
   760                       NS_LOG_DEBUG ("The route length " << reverseRoute.size ());
   753                       PrintVector (saveRoute);
   761                       sourceRoute.SetNodesAddress (reverseRoute);
   754 
   762                       if (m_routeCache->IsLinkCache ())
   755                       sourceRoute.SetNodesAddress (saveRoute);
       
   756                       if (dsr->IsLinkCache ())
   763                         {
   757                         {
   764                           m_routeCache->UseExtends (reverseRoute);
   758                           dsr->UseExtends (saveRoute);
   765                         }
   759                         }
   766                       sourceRoute.SetSegmentsLeft ((saveRoute.size () - 2));
   760                       sourceRoute.SetSegmentsLeft ((saveRoute.size () - 2));
   767                       uint8_t salvage = 0;
   761                       uint8_t salvage = 0;
   768                       sourceRoute.SetSalvage (salvage);
   762                       sourceRoute.SetSalvage (salvage);
   769                       Ipv4Address nextHop = SearchNextHop (ipv4Address, reverseRoute); // Get the next hop address
   763                       Ipv4Address nextHop = SearchNextHop (ipv4Address, saveRoute); // Get the next hop address
   770                       NS_LOG_DEBUG ("The nextHop address " << nextHop);
   764                       NS_LOG_DEBUG ("The nextHop address " << nextHop);
   771 
   765 
   772                       if (nextHop == "0.0.0.0")
   766                       if (nextHop == "0.0.0.0")
   773                         {
   767                         {
   774                           dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
   768                           dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
   776                         }
   770                         }
   777                       SetRoute (nextHop, ipv4Address);
   771                       SetRoute (nextHop, ipv4Address);
   778                       /*
   772                       /*
   779                        * Schedule the packet retry
   773                        * Schedule the packet retry
   780                        */
   774                        */
   781                       dsr->SendPacket (sourceRoute, nextHop, protocol);
   775                       dsr->SendPacketFromBuffer (sourceRoute, nextHop, protocol);
   782                       // Cancel the route request timer for destination
   776                       // Cancel the route request timer for destination
   783                       dsr->CancelRreqTimer (dst);
   777                       dsr->CancelRreqTimer (dst, true);
       
   778                     }
       
   779                   else
       
   780                     {
       
   781                       NS_LOG_DEBUG ("The route is failed to add in cache");
       
   782                       return 0;
   784                     }
   783                     }
   785                 }
   784                 }
   786               else
   785               else
   787                 {
   786                 {
   788                   NS_LOG_DEBUG ("Unable to reverse the route");
   787                   NS_LOG_DEBUG ("Unable to reverse the route");
       
   788                   return 0;
   789                 }
   789                 }
   790 
   790 
   791               /*
   791               /*
   792                * Need to first pin down the next hop address before removing duplicates
   792                * Need to first pin down the next hop address before removing duplicates
   793                */
   793                */
   794               Ipv4Address nextHop = ReverseSearchNextHop (ipv4Address, m_finalRoute);
   794               Ipv4Address nextHop = ReverseSearchNextHop (ipv4Address, m_finalRoute);
       
   795               NS_LOG_DEBUG ("The nextHop address " << nextHop);
   795               /*
   796               /*
   796                * First remove the duplicate ip address to automatically shorten the route, and then reversely
   797                * First remove the duplicate ip address to automatically shorten the route, and then reversely
   797                * search the next hop address
   798                * search the next hop address
   798                */
   799                */
   799               PrintVector (m_finalRoute);
       
   800               // Set the route
   800               // Set the route
   801               SetRoute (nextHop, ipv4Address);
   801               SetRoute (nextHop, ipv4Address);
   802 
   802 
   803               uint16_t hops = m_finalRoute.size ();
   803               uint16_t hops = m_finalRoute.size ();
   804               DsrOptionRrepHeader rrep;
   804               DsrOptionRrepHeader rrep;
   805               rrep.SetNodesAddress (m_finalRoute);     // Set the node addresses in the route reply header
   805               rrep.SetNodesAddress (m_finalRoute);     // Set the node addresses in the route reply header
   806               NS_LOG_DEBUG ("The nextHop address " << nextHop);
       
   807               // Get the real source of the reply
   806               // Get the real source of the reply
   808               Ipv4Address realSource = m_finalRoute.back ();
   807               Ipv4Address realSource = m_finalRoute.back ();
       
   808               Ipv4Address realDst = m_finalRoute.front ();
       
   809               PrintVector (m_finalRoute);
       
   810               NS_LOG_DEBUG ("This is the full route from " << realSource << " to " << realDst);
   809               /*
   811               /*
   810                * This part add dsr header to the packet and send reply
   812                * This part add dsr header to the packet and send route reply packet
   811                */
   813                */
   812               DsrRoutingHeader dsrRoutingHeader;
   814               DsrRoutingHeader dsrRoutingHeader;
   813               dsrRoutingHeader.SetNextHeader (protocol);
   815               dsrRoutingHeader.SetNextHeader (protocol);
   814               dsrRoutingHeader.SetMessageType (1);
   816               dsrRoutingHeader.SetMessageType (1);
   815               dsrRoutingHeader.SetSourceId (GetIDfromIP (realSource));
   817               dsrRoutingHeader.SetSourceId (GetIDfromIP (realSource));
   821               Ptr<Packet> newPacket = Create<Packet> ();
   823               Ptr<Packet> newPacket = Create<Packet> ();
   822               newPacket->AddHeader (dsrRoutingHeader);
   824               newPacket->AddHeader (dsrRoutingHeader);
   823               dsr->ScheduleCachedReply (newPacket, ipv4Address, nextHop, m_ipv4Route, hops);
   825               dsr->ScheduleCachedReply (newPacket, ipv4Address, nextHop, m_ipv4Route, hops);
   824               isPromisc = false;
   826               isPromisc = false;
   825             }
   827             }
       
   828           else
       
   829             {
       
   830               NS_LOG_DEBUG ("There is duplicate ip addresses in the two route parts");
       
   831             }
       
   832           return rreq.GetSerializedSize ();
   826         }
   833         }
   827       /*
   834       /*
   828        * (iii) no route in any type has been found
   835        * (iii) no route in any type has been found
   829        */
   836        */
   830       else
   837       else
   831         {
   838         {
   832           std::vector<Ipv4Address> nodeList = rreq.GetNodesAddresses (); // route from rreq header
   839           mainVector.push_back (ipv4Address);
   833           PrintVector (nodeList);
   840           NS_ASSERT (mainVector.front () == source);
   834           /*
   841           NS_LOG_DEBUG ("Print out the main vector");
   835            * Otherwise, forward the route request packet
   842           PrintVector (mainVector);
   836            */
   843           rreq.SetNodesAddress (mainVector);
   837           PrintVector (nodeList);
   844 
   838           nodeList.push_back (ipv4Address);
   845           Ptr<Packet> errP = p->Copy ();
   839           PrintVector (nodeList);
   846           if (errP->GetSize ())
   840           NS_ASSERT (nodeList.front () == source);
   847             {
   841           rreq.SetNodesAddress (nodeList);
   848               NS_LOG_DEBUG ("Error header included");
       
   849               DsrOptionRerrUnreachHeader rerr;
       
   850               p->RemoveHeader (rerr);
       
   851               Ipv4Address errorSrc = rerr.GetErrorSrc ();
       
   852               Ipv4Address unreachNode = rerr.GetUnreachNode ();
       
   853               Ipv4Address errorDst = rerr.GetErrorDst ();
       
   854 
       
   855               if ((errorSrc == srcAddress) && (unreachNode == ipv4Address))
       
   856                 {
       
   857                   NS_LOG_DEBUG ("The error link back to work again");
       
   858                   uint16_t length = rreq.GetLength ();
       
   859                   NS_LOG_DEBUG ("The RREQ header length " <<  length);
       
   860                   dsrRoutingHeader.AddDsrOption (rreq);
       
   861                   dsrRoutingHeader.SetPayloadLength (length + 2);
       
   862                 }
       
   863               else
       
   864                 {
       
   865                   isError = true;
       
   866                   dsr->DeleteAllRoutesIncludeLink (errorSrc, unreachNode, ipv4Address);
       
   867 
       
   868                   DsrOptionRerrUnreachHeader newUnreach;
       
   869                   newUnreach.SetErrorType (1);
       
   870                   newUnreach.SetErrorSrc (errorSrc);
       
   871                   newUnreach.SetUnreachNode (unreachNode);
       
   872                   newUnreach.SetErrorDst (errorDst);
       
   873                   newUnreach.SetSalvage (rerr.GetSalvage ()); // Set the value about whether to salvage a packet or not
       
   874                   uint16_t length = rreq.GetLength () + newUnreach.GetLength ();
       
   875                   NS_LOG_DEBUG ("The RREQ and newUnreach header length " <<  length);
       
   876                   dsrRoutingHeader.SetPayloadLength (length + 4);
       
   877                   dsrRoutingHeader.AddDsrOption (rreq);
       
   878                   dsrRoutingHeader.AddDsrOption (newUnreach);
       
   879                 }
       
   880             }
       
   881           else
       
   882             {
       
   883               uint16_t length = rreq.GetLength ();
       
   884               NS_LOG_DEBUG ("The RREQ header length " <<  length);
       
   885               dsrRoutingHeader.AddDsrOption (rreq);
       
   886               dsrRoutingHeader.SetPayloadLength (length + 2);
       
   887             }
       
   888           // Get the TTL value
       
   889           uint8_t ttl = ipv4Header.GetTtl ();
   842           /*
   890           /*
   843           * Decrease the TTL value in the packet tag by one, this tag will go to ip layer 3 send function
   891           * Decrease the TTL value in the packet tag by one, this tag will go to ip layer 3 send function
   844           * and drop packet when TTL value equals to 0
   892           * and drop packet when TTL value equals to 0
   845           */
   893           */
       
   894           NS_LOG_DEBUG ("The ttl value here " << (uint32_t)ttl);
   846           if (ttl)
   895           if (ttl)
   847             {
   896             {
   848               Ptr<Packet> interP = Create<Packet> ();
   897               Ptr<Packet> interP = Create<Packet> ();
   849               SocketIpTtlTag tag;
   898               SocketIpTtlTag tag;
   850               tag.SetTtl (ttl - 1);
   899               tag.SetTtl (ttl - 1);
   851               interP->AddPacketTag (tag);
   900               interP->AddPacketTag (tag);
   852               /*
   901               interP->AddHeader (dsrRoutingHeader);
   853                * Construct the route request header to forward the route requests
       
   854                */
       
   855               DsrRoutingHeader dsrRoutingHeader;
       
   856               dsrRoutingHeader.SetNextHeader (protocol);
       
   857               dsrRoutingHeader.SetMessageType (1);
       
   858               dsrRoutingHeader.SetSourceId (GetIDfromIP (source));
       
   859               dsrRoutingHeader.SetDestId (255);
       
   860               dsrRoutingHeader.AddDsrOption (rreq);
       
   861 
       
   862               // if the errP packet is not 0, then there is an error header after it
       
   863               if (errP->GetSize ())
       
   864                 {
       
   865                   NS_LOG_DEBUG ("Error header included");
       
   866                   DsrOptionRerrUnreachHeader rerr;
       
   867                   p->RemoveHeader (rerr);
       
   868                   Ipv4Address errorSrc = rerr.GetErrorSrc ();
       
   869                   Ipv4Address unreachNode = rerr.GetUnreachNode ();
       
   870                   isError = true;
       
   871                   m_routeCache->DeleteAllRoutesIncludeLink (errorSrc, unreachNode, ipv4Address);
       
   872 
       
   873                   DsrOptionRerrUnreachHeader newUnreach;
       
   874                   newUnreach.SetErrorType (1);
       
   875                   newUnreach.SetErrorSrc (rerr.GetErrorSrc ());
       
   876                   newUnreach.SetUnreachNode (rerr.GetUnreachNode ());
       
   877                   newUnreach.SetErrorDst (rerr.GetErrorDst ());
       
   878                   newUnreach.SetSalvage (rerr.GetSalvage ()); // Set the value about whether to salvage a packet or not
       
   879                   dsrRoutingHeader.AddDsrOption (newUnreach);
       
   880                   uint16_t length = rreq.GetLength () + newUnreach.GetLength ();
       
   881                   NS_LOG_DEBUG ("The RREQ and newUnreach header length " <<  length);
       
   882                   dsrRoutingHeader.SetPayloadLength (length + 4);
       
   883                   interP->AddHeader (dsrRoutingHeader);
       
   884                 }
       
   885               else
       
   886                 {
       
   887                   uint16_t length = rreq.GetLength ();
       
   888                   NS_LOG_DEBUG ("The RREQ header length " <<  length);
       
   889                   dsrRoutingHeader.SetPayloadLength (length + 2);
       
   890                   interP->AddHeader (dsrRoutingHeader);
       
   891                 }
       
   892 
       
   893               dsr->ScheduleInterRequest (interP);
   902               dsr->ScheduleInterRequest (interP);
   894               isPromisc = false;
   903               isPromisc = false;
   895             }
   904             }
   896         }
   905           return rreq.GetSerializedSize ();
   897     }
   906         }
   898 
   907     }
   899   return rreq.GetSerializedSize ();
   908   return rreq.GetSerializedSize ();
   900 }
   909 }
   901 
   910 
   902 NS_OBJECT_ENSURE_REGISTERED (DsrOptionRrep);
   911 NS_OBJECT_ENSURE_REGISTERED (DsrOptionRrep);
   903 
   912 
   947   rrep.SetNumberAddress (numberAddress);  // Set the number of ip address in the header to reserver space for deserialize header
   956   rrep.SetNumberAddress (numberAddress);  // Set the number of ip address in the header to reserver space for deserialize header
   948   p->RemoveHeader (rrep);
   957   p->RemoveHeader (rrep);
   949 
   958 
   950   Ptr<Node> node = GetNodeWithAddress (ipv4Address);
   959   Ptr<Node> node = GetNodeWithAddress (ipv4Address);
   951   Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
   960   Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
   952   Ptr<dsr::RouteCache> m_routeCache = node->GetObject<dsr::RouteCache> ();
       
   953   Ptr<dsr::RreqTable>  m_rreqTable = node->GetObject<dsr::RreqTable> ();
       
   954   ActiveRouteTimeout = m_routeCache->GetCacheTimeout ();
       
   955 
   961 
   956   NS_LOG_DEBUG ("The next header value " << (uint32_t)protocol);
   962   NS_LOG_DEBUG ("The next header value " << (uint32_t)protocol);
   957 
   963 
   958   std::vector<Ipv4Address> nodeList = rrep.GetNodesAddress ();
   964   std::vector<Ipv4Address> nodeList = rrep.GetNodesAddress ();
   959   /*
   965   /**
   960    * Get the destination address, which is the last element in the nodeList
   966    * Get the destination address, which is the last element in the nodeList
   961    */
   967    */
   962   Ipv4Address targetAddress = nodeList.front ();
   968   Ipv4Address targetAddress = nodeList.front ();
   963   // If the RREP option has reached to the destination
   969   // If the RREP option has reached to the destination
   964   if (targetAddress == ipv4Address)
   970   if (targetAddress == ipv4Address)
   968       if (nodeList.size () == 0)
   974       if (nodeList.size () == 0)
   969         {
   975         {
   970           NS_LOG_DEBUG ("The route we have contains 0 entries");
   976           NS_LOG_DEBUG ("The route we have contains 0 entries");
   971           return 0;
   977           return 0;
   972         }
   978         }
   973       /*
   979       /**
   974        * Get the destination address for the data packet, which is the last element in the nodeList
   980        * Get the destination address for the data packet, which is the last element in the nodeList
   975        */
   981        */
   976       Ipv4Address dst = nodeList.back ();
   982       Ipv4Address dst = nodeList.back ();
   977       /*
   983       /**
   978        * Add the newly found route to the route cache
   984        * Add the newly found route to the route cache
   979        * The route looks like:
   985        * The route looks like:
   980        * \\ "srcAddress" + "intermediate node address" + "targetAddress"
   986        * \\ "srcAddress" + "intermediate node address" + "targetAddress"
   981        */
   987        */
   982       RouteCacheEntry toDestination (/*IP_VECTOR=*/ nodeList, /*dst=*/ dst, /*expire time=*/ ActiveRouteTimeout);
   988       RouteCacheEntry toDestination (/*IP_VECTOR=*/ nodeList, /*dst=*/ dst, /*expire time=*/ ActiveRouteTimeout);
   983       NS_ASSERT (nodeList.front () == ipv4Address);
   989       NS_ASSERT (nodeList.front () == ipv4Address);
   984       bool addRoute = false;
   990       bool addRoute = false;
   985       if (m_routeCache->IsLinkCache ())
   991       if(dsr->IsLinkCache())
   986         {
   992         {
   987           m_routeCache->AddRoute_Link (nodeList, ipv4Address);
   993           addRoute = dsr->AddRoute_Link (nodeList, ipv4Address);
   988         }
   994         }
   989       else
   995       else
   990         {
   996         {
   991           m_routeCache->AddRoute (toDestination);
   997           addRoute = dsr->AddRoute (toDestination);
   992         }
   998         }
       
   999 
   993       if (addRoute)
  1000       if (addRoute)
   994         {
  1001         {
   995           NS_LOG_DEBUG ("Failed to add the route");
       
   996           return 0;
       
   997         }
       
   998       else
       
   999         {
       
  1000           NS_LOG_DEBUG ("We have added the route and search send buffer for packet with destination " << dst);
  1002           NS_LOG_DEBUG ("We have added the route and search send buffer for packet with destination " << dst);
  1001           /*
  1003           /**
  1002            * Found a route the dst, construct the source route option header
  1004            * Found a route the dst, construct the source route option header
  1003            */
  1005            */
  1004           DsrOptionSRHeader sourceRoute;
  1006           DsrOptionSRHeader sourceRoute;
  1005           NS_LOG_DEBUG ("The route length " << nodeList.size ());
  1007           NS_LOG_DEBUG ("The route length " << nodeList.size ());
  1006           sourceRoute.SetNodesAddress (nodeList);
  1008           sourceRoute.SetNodesAddress (nodeList);
  1007           sourceRoute.SetSegmentsLeft ((nodeList.size () - 2));
  1009           sourceRoute.SetSegmentsLeft ((nodeList.size () - 2));
  1008           uint8_t salvage = 0;
  1010           sourceRoute.SetSalvage (0);
  1009           sourceRoute.SetSalvage (salvage);
       
  1010           Ipv4Address nextHop = SearchNextHop (ipv4Address, nodeList); // Get the next hop address
  1011           Ipv4Address nextHop = SearchNextHop (ipv4Address, nodeList); // Get the next hop address
  1011           NS_LOG_DEBUG ("The nextHop address " << nextHop);
  1012           NS_LOG_DEBUG ("The nextHop address " << nextHop);
  1012           if (nextHop == "0.0.0.0")
  1013           if (nextHop == "0.0.0.0")
  1013             {
  1014             {
  1014               dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
  1015               dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
  1015               return 0;
  1016               return 0;
  1016             }
  1017             }
  1017           PrintVector (nodeList);
  1018           PrintVector (nodeList);
  1018           SetRoute (nextHop, ipv4Address);
  1019           SetRoute (nextHop, ipv4Address);
  1019           /*
  1020           // Cancel the route request timer for destination
       
  1021           dsr->CancelRreqTimer (dst, true);
       
  1022           /**
  1020            * Schedule the packet retry
  1023            * Schedule the packet retry
  1021            */
  1024            */
  1022           dsr->SendPacket (sourceRoute, nextHop, protocol);
  1025           dsr->SendPacketFromBuffer (sourceRoute, nextHop, protocol);
  1023           // Cancel the route request timer for destination
  1026         }
  1024           dsr->CancelRreqTimer (dst);
  1027       else
       
  1028         {
       
  1029           NS_LOG_DEBUG ("Failed to add the route");
       
  1030           return 0;
  1025         }
  1031         }
  1026     }
  1032     }
  1027   else
  1033   else
  1028     {
  1034     {
  1029       uint8_t length = rrep.GetLength () - 2; // The get length - 2 is to get aligned for the malformed header check
  1035       uint8_t length = rrep.GetLength () - 2; // The get length - 2 is to get aligned for the malformed header check
  1047           Ipv4Address dst = cutRoute.back ();
  1053           Ipv4Address dst = cutRoute.back ();
  1048           NS_LOG_DEBUG ("The route destination after cut " << dst);
  1054           NS_LOG_DEBUG ("The route destination after cut " << dst);
  1049           RouteCacheEntry toDestination (/*IP_VECTOR=*/ cutRoute, /*dst=*/ dst, /*expire time=*/ ActiveRouteTimeout);
  1055           RouteCacheEntry toDestination (/*IP_VECTOR=*/ cutRoute, /*dst=*/ dst, /*expire time=*/ ActiveRouteTimeout);
  1050           NS_ASSERT (cutRoute.front () == ipv4Address);
  1056           NS_ASSERT (cutRoute.front () == ipv4Address);
  1051           bool addRoute = false;
  1057           bool addRoute = false;
  1052           if (m_routeCache->IsLinkCache ())
  1058           if(dsr->IsLinkCache())
  1053             {
  1059             {
  1054               m_routeCache->AddRoute_Link (nodeList, ipv4Address);
  1060               addRoute = dsr->AddRoute_Link (nodeList, ipv4Address);
  1055             }
  1061             }
  1056           else
  1062           else
  1057             {
  1063             {
  1058               m_routeCache->AddRoute (toDestination);
  1064               addRoute = dsr->AddRoute (toDestination);
  1059             }
  1065             }
  1060           if (addRoute)
  1066           if (addRoute)
  1061             {
  1067             {
  1062               dsr->CancelRreqTimer (dst);
  1068               dsr->CancelRreqTimer (dst, true);
  1063             }
  1069             }
  1064           else
  1070           else
  1065             {
  1071             {
  1066               NS_LOG_DEBUG ("The route not added");
  1072               NS_LOG_DEBUG ("The route not added");
  1067             }
  1073             }
  1146   sourceRoute.SetNumberAddress (numberAddress);
  1152   sourceRoute.SetNumberAddress (numberAddress);
  1147   p->RemoveHeader (sourceRoute);
  1153   p->RemoveHeader (sourceRoute);
  1148 
  1154 
  1149   // The route size saved in the source route
  1155   // The route size saved in the source route
  1150   std::vector<Ipv4Address> nodeList = sourceRoute.GetNodesAddress ();
  1156   std::vector<Ipv4Address> nodeList = sourceRoute.GetNodesAddress ();
  1151 
       
  1152   uint8_t segsLeft = sourceRoute.GetSegmentsLeft ();
  1157   uint8_t segsLeft = sourceRoute.GetSegmentsLeft ();
  1153   uint8_t salvage = sourceRoute.GetSalvage ();
  1158   uint8_t salvage = sourceRoute.GetSalvage ();
  1154 
       
  1155   // Here we remove the ack packet to the previous hop
       
  1156   DsrOptionAckReqHeader ackReq;
       
  1157   p->RemoveHeader (ackReq);
       
  1158   uint16_t ackId = ackReq.GetAckId ();
       
  1159   /*
  1159   /*
  1160    * Get the node from IP address and get the DSR extension object
  1160    * Get the node from IP address and get the DSR extension object
  1161    */
  1161    */
  1162   Ptr<Node> node = GetNodeWithAddress (ipv4Address);
  1162   Ptr<Node> node = GetNodeWithAddress (ipv4Address);
  1163   Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
  1163   Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
  1178       /*
  1178       /*
  1179        * Get the node from Ip address and get the dsr routing object
  1179        * Get the node from Ip address and get the dsr routing object
  1180        */
  1180        */
  1181       Ptr<Node> node = GetNodeWithAddress (ipv4Address);
  1181       Ptr<Node> node = GetNodeWithAddress (ipv4Address);
  1182       Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
  1182       Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
  1183 
  1183       bool findSame = dsr->FindSamePackets (packet, source, destination, segsLeft);
  1184       bool findSame = dsr->FindSamePackets (packet, ipv4Header, source, destination, segsLeft);
       
  1185 
  1184 
  1186       if (findSame)
  1185       if (findSame)
  1187         {
  1186         {
  1188           NS_LOG_DEBUG ("Received one passive acknowledgment for data packet");
  1187           NS_LOG_DEBUG ("Received one passive acknowledgment for a successfully delivered packet");
  1189         }
  1188         }
  1190       if (ContainAddressAfter (ipv4Address, destAddress, nodeList))
  1189       if (ContainAddressAfter (ipv4Address, destAddress, nodeList))
  1191         {
  1190         {
  1192           NS_LOG_DEBUG ("Send back the gratuitous reply");
  1191           NS_LOG_DEBUG ("Send back the gratuitous reply");
  1193           dsr->SendGratuitousReply (source, srcAddress, nodeList, protocol);
  1192           dsr->SendGratuitousReply (source, srcAddress, nodeList, protocol);
  1203        * Get the number of address from the source route header
  1202        * Get the number of address from the source route header
  1204        */
  1203        */
  1205       uint8_t length = sourceRoute.GetLength ();
  1204       uint8_t length = sourceRoute.GetLength ();
  1206       uint8_t nextAddressIndex;
  1205       uint8_t nextAddressIndex;
  1207       Ipv4Address nextAddress;
  1206       Ipv4Address nextAddress;
  1208       /*
  1207 
  1209        * Send back acknowledgment packet to the earlier hop
  1208       // Get the option type value
  1210        */
  1209       uint32_t size = p->GetSize ();
  1211       m_ipv4Route = SetRoute (srcAddress, ipv4Address);
  1210       uint8_t *data = new uint8_t[size];
  1212       NS_LOG_DEBUG ("Send back ACK to the earlier hop " << srcAddress << " from us " << ipv4Address);
  1211       p->CopyData (data, size);
  1213       dsr->SendAck (ackId, srcAddress, source, destination, protocol, m_ipv4Route);
  1212       uint8_t optionType = 0;
       
  1213       optionType = *(data);
       
  1214       NS_LOG_DEBUG ("The packet size over here " << p->GetSize());
       
  1215 
       
  1216       NS_LOG_DEBUG ("The option type over here " << (uint32_t)optionType);
       
  1217       if (optionType == 160)
       
  1218         {
       
  1219           NS_LOG_DEBUG ("Here we remove the ack request header and add ack header to the packet");
       
  1220           // Here we remove the ack packet to the previous hop
       
  1221           DsrOptionAckReqHeader ackReq;
       
  1222           p->RemoveHeader (ackReq);
       
  1223           uint16_t ackId = ackReq.GetAckId ();
       
  1224           NS_LOG_DEBUG ("The type value " << (uint32_t)ackReq.GetType ());
       
  1225           /*
       
  1226            * Send back acknowledgment packet to the earlier hop
       
  1227            */
       
  1228           m_ipv4Route = SetRoute (srcAddress, ipv4Address);
       
  1229           NS_LOG_DEBUG ("Send back ACK to the earlier hop " << srcAddress << " from us " << ipv4Address);
       
  1230           dsr->SendAck (ackId, srcAddress, source, destination, protocol, m_ipv4Route);
       
  1231         }
  1214       /*
  1232       /*
  1215        * After send back ACK, check if the segments left value has turned to 0 or not, if yes, update the route entry
  1233        * After send back ACK, check if the segments left value has turned to 0 or not, if yes, update the route entry
  1216        * and return header length
  1234        * and return header length
  1217        */
  1235        */
  1218       if (segsLeft == 0)
  1236       if (segsLeft == 0)
  1219         {
  1237         {
       
  1238           NS_LOG_DEBUG ("This is the final destination");
  1220           isPromisc = false;
  1239           isPromisc = false;
  1221           return sourceRoute.GetSerializedSize ();
  1240           return sourceRoute.GetSerializedSize ();
  1222         }
  1241         }
  1223 
  1242 
  1224       if (length % 2 != 0)
  1243       if (length % 2 != 0)
  1272           m_dropTrace (packet);
  1291           m_dropTrace (packet);
  1273           return 0;
  1292           return 0;
  1274         }
  1293         }
  1275       // Set the route and forward the data packet
  1294       // Set the route and forward the data packet
  1276       SetRoute (nextAddress, ipv4Address);
  1295       SetRoute (nextAddress, ipv4Address);
       
  1296       NS_LOG_DEBUG ("dsr packet size " << dsrP->GetSize());
  1277       dsr->ForwardPacket (dsrP, newSourceRoute, ipv4Header, realSource, nextAddress, targetAddress, protocol, m_ipv4Route);
  1297       dsr->ForwardPacket (dsrP, newSourceRoute, ipv4Header, realSource, nextAddress, targetAddress, protocol, m_ipv4Route);
  1278     }
  1298     }
  1279   return sourceRoute.GetSerializedSize ();
  1299   return sourceRoute.GetSerializedSize ();
  1280 }
  1300 }
  1281 
  1301 
  1312 }
  1332 }
  1313 
  1333 
  1314 uint8_t DsrOptionRerr::Process (Ptr<Packet> packet, Ptr<Packet> dsrP, Ipv4Address ipv4Address, Ipv4Address source, Ipv4Header const& ipv4Header, uint8_t protocol, bool& isPromisc)
  1334 uint8_t DsrOptionRerr::Process (Ptr<Packet> packet, Ptr<Packet> dsrP, Ipv4Address ipv4Address, Ipv4Address source, Ipv4Header const& ipv4Header, uint8_t protocol, bool& isPromisc)
  1315 {
  1335 {
  1316   NS_LOG_FUNCTION (this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
  1336   NS_LOG_FUNCTION (this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
  1317 
       
  1318   Ptr<Packet> p = packet->Copy ();
  1337   Ptr<Packet> p = packet->Copy ();
  1319   uint32_t size = p->GetSize ();
  1338   uint32_t size = p->GetSize ();
  1320   uint8_t *data = new uint8_t[size];
  1339   uint8_t *data = new uint8_t[size];
  1321   p->CopyData (data, size);
  1340   p->CopyData (data, size);
  1322   uint8_t errorType = *(data + 2);
  1341   uint8_t errorType = *(data + 2);
  1323   /*
  1342   /*
  1324    * Get the node from Ip address and get the dsr extension object
  1343    * Get the node from Ip address and get the dsr extension object
  1325    */
  1344    */
  1326   Ptr<Node> node = GetNodeWithAddress (ipv4Address);
  1345   Ptr<Node> node = GetNodeWithAddress (ipv4Address);
  1327   Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
  1346   Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
  1328 
       
  1329   Ipv4Address srcAddress = ipv4Header.GetSource ();
       
  1330   Ipv4Address destAddress = ipv4Header.GetDestination ();
       
  1331   /*
  1347   /*
  1332    * The error serialized size
  1348    * The error serialized size
  1333    */
  1349    */
  1334   uint32_t rerrSize;
  1350   uint32_t rerrSize;
       
  1351   NS_LOG_DEBUG ("The error type value here " << (uint32_t)errorType);
  1335   if (errorType == 1) // unreachable ip address
  1352   if (errorType == 1) // unreachable ip address
  1336     {
  1353     {
  1337       /*
  1354       /*
  1338        * Remove the route error header from the packet, and get the error type
  1355        * Remove the route error header from the packet, and get the error type
  1339        */
  1356        */
  1353       rerrSize = rerrUnreach.GetSerializedSize ();
  1370       rerrSize = rerrUnreach.GetSerializedSize ();
  1354       /*
  1371       /*
  1355        * Delete all the routes including the unreachable node address from the route cache
  1372        * Delete all the routes including the unreachable node address from the route cache
  1356        */
  1373        */
  1357       Ptr<Node> node = GetNodeWithAddress (ipv4Address);
  1374       Ptr<Node> node = GetNodeWithAddress (ipv4Address);
  1358       Ptr<dsr::RouteCache> m_routeCache = node->GetObject<dsr::RouteCache> ();
  1375       dsr->DeleteAllRoutesIncludeLink (errorSource, unreachAddress, ipv4Address);
  1359       m_routeCache->DeleteAllRoutesIncludeLink (errorSource, unreachAddress, ipv4Address);
       
  1360 
  1376 
  1361       Ptr<Packet> newP = p->Copy ();
  1377       Ptr<Packet> newP = p->Copy ();
  1362       uint32_t serialized = DoSendError (newP, rerrUnreach, rerrSize, ipv4Address, protocol);
  1378       uint32_t serialized = DoSendError (newP, rerrUnreach, rerrSize, ipv4Address, protocol);
  1363       return serialized;
  1379       return serialized;
  1364     }
  1380     }
  1374        */
  1390        */
  1375       DsrOptionRerrUnsupportHeader rerrUnsupport;
  1391       DsrOptionRerrUnsupportHeader rerrUnsupport;
  1376       p->RemoveHeader (rerrUnsupport);
  1392       p->RemoveHeader (rerrUnsupport);
  1377       rerrSize = rerrUnsupport.GetSerializedSize ();
  1393       rerrSize = rerrUnsupport.GetSerializedSize ();
  1378 
  1394 
  1379       // This is for the other two error options TODO
  1395       // This is for the other two error options, not supporting for now TODO
  1380 //      uint32_t serialized = DoSendError (p, rerrUnsupport, rerrSize, ipv4Address, protocol);
  1396 //      uint32_t serialized = DoSendError (p, rerrUnsupport, rerrSize, ipv4Address, protocol);
  1381       uint32_t serialized = 0;
  1397       uint32_t serialized = 0;
  1382       return serialized;
  1398       return serialized;
  1383     }
  1399     }
  1384 }
  1400 }
  1403   Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
  1419   Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
  1404   /*
  1420   /*
  1405    * Get the segments left field and the next address
  1421    * Get the segments left field and the next address
  1406    */
  1422    */
  1407   uint8_t segmentsLeft = sourceRoute.GetSegmentsLeft ();
  1423   uint8_t segmentsLeft = sourceRoute.GetSegmentsLeft ();
  1408   NS_LOG_DEBUG ("The segments left " << (uint32_t)segmentsLeft);
       
  1409   uint8_t length = sourceRoute.GetLength ();
  1424   uint8_t length = sourceRoute.GetLength ();
  1410   NS_LOG_DEBUG ("The number of addresses we have " << (uint32_t)numberAddress);
       
  1411   uint8_t nextAddressIndex;
  1425   uint8_t nextAddressIndex;
  1412   Ipv4Address nextAddress;
  1426   Ipv4Address nextAddress;
  1413   /*
  1427   /*
  1414    * Get the route size and the error target address
  1428    * Get the route size and the error target address
  1415    */
  1429    */
  1436   /*
  1450   /*
  1437    * When the error packet has reached to the destination
  1451    * When the error packet has reached to the destination
  1438    */
  1452    */
  1439   if (segmentsLeft == 0 && targetAddress == ipv4Address)
  1453   if (segmentsLeft == 0 && targetAddress == ipv4Address)
  1440     {
  1454     {
  1441       NS_LOG_DEBUG ("This is the destination of the error");
  1455       NS_LOG_INFO ("This is the destination of the error, send error request");
  1442       dsr->SendErrorRequest (rerr, protocol);
  1456       dsr->SendErrorRequest (rerr, protocol);
  1443       return serializedSize;
  1457       return serializedSize;
  1444     }
  1458     }
  1445 
       
  1446   DsrOptionRerrUnreachHeader newUnreach;
       
  1447   newUnreach.SetErrorType (1);
       
  1448   newUnreach.SetErrorSrc (rerr.GetErrorSrc ());
       
  1449   newUnreach.SetUnreachNode (rerr.GetUnreachNode ());
       
  1450   newUnreach.SetErrorDst (rerr.GetErrorDst ());
       
  1451   newUnreach.SetSalvage (rerr.GetSalvage ()); // Set the value about whether to salvage a packet or not
       
  1452 
  1459 
  1453   // Get the next Router Address
  1460   // Get the next Router Address
  1454   DsrOptionSRHeader newSourceRoute;
  1461   DsrOptionSRHeader newSourceRoute;
  1455   newSourceRoute.SetSegmentsLeft (segmentsLeft - 1);
  1462   newSourceRoute.SetSegmentsLeft (segmentsLeft - 1);
  1456   nextAddressIndex = numberAddress - segmentsLeft;
  1463   nextAddressIndex = numberAddress - segmentsLeft;
  1466       return serializedSize;
  1473       return serializedSize;
  1467     }
  1474     }
  1468 
  1475 
  1469   // Set the route entry
  1476   // Set the route entry
  1470   SetRoute (nextAddress, ipv4Address);
  1477   SetRoute (nextAddress, ipv4Address);
  1471   dsr->ForwardErrPacket (newUnreach, newSourceRoute, nextAddress, protocol, m_ipv4Route);
  1478   dsr->ForwardErrPacket (rerr, newSourceRoute, nextAddress, protocol, m_ipv4Route);
  1472   return serializedSize;
  1479   return serializedSize;
  1473 }
  1480 }
  1474 
  1481 
  1475 NS_OBJECT_ENSURE_REGISTERED (DsrOptionAckReq);
  1482 NS_OBJECT_ENSURE_REGISTERED (DsrOptionAckReq);
  1476 
  1483 
  1519   /*
  1526   /*
  1520    * Get the node with ip address and get the dsr extension and reoute cache objects
  1527    * Get the node with ip address and get the dsr extension and reoute cache objects
  1521    */
  1528    */
  1522   Ptr<Node> node = GetNodeWithAddress (ipv4Address);
  1529   Ptr<Node> node = GetNodeWithAddress (ipv4Address);
  1523   Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
  1530   Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
  1524   Ptr<dsr::RouteCache> m_routeCache = node->GetObject<dsr::RouteCache> ();
       
  1525 
  1531 
  1526   NS_LOG_DEBUG ("The next header value " << (uint32_t)protocol);
  1532   NS_LOG_DEBUG ("The next header value " << (uint32_t)protocol);
  1527 
  1533 
  1528   Ipv4Address srcAddress = ipv4Header.GetSource ();
  1534   Ipv4Address srcAddress = ipv4Header.GetSource ();
  1529   SetRoute (srcAddress, ipv4Address);
  1535   SetRoute (srcAddress, ipv4Address);
  1581   /*
  1587   /*
  1582    * Get the node with ip address and get the dsr extension and route cache objects
  1588    * Get the node with ip address and get the dsr extension and route cache objects
  1583    */
  1589    */
  1584   Ptr<Node> node = GetNodeWithAddress (ipv4Address);
  1590   Ptr<Node> node = GetNodeWithAddress (ipv4Address);
  1585   Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
  1591   Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
  1586   Ptr<dsr::RouteCache> srcRouteCache = node->GetObject<dsr::RouteCache> ();
  1592   dsr->UpdateRouteEntry (realDst);
  1587   srcRouteCache->UpdateRouteEntry (realDst);
       
  1588   /*
  1593   /*
  1589    * Cancel the packet retransmit timer when receiving the ack packet
  1594    * Cancel the packet retransmit timer when receiving the ack packet
  1590    */
  1595    */
  1591   dsr->CallCancelPacketTimer (ackId, ipv4Header, realSrc, realDst);
  1596   dsr->CallCancelPacketTimer (ackId, ipv4Header, realSrc, realDst);
  1592   return ack.GetSerializedSize ();
  1597   return ack.GetSerializedSize ();