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 |
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 |
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) |
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 */ |
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; |