A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dsr-options.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 Yufei Cheng
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Yufei Cheng <yfcheng@ittc.ku.edu>
7 * Modified by: Tommaso Pecorella <tommaso.pecorella@unifi.it>
8 * Lorenzo Bartolini <l.bartolini02@gmail.com>
9 *
10 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
11 * ResiliNets Research Group https://resilinets.org/
12 * Information and Telecommunication Technology Center (ITTC)
13 * and Department of Electrical Engineering and Computer Science
14 * The University of Kansas Lawrence, KS USA.
15 *
16 * Work supported in part by NSF FIND (Future Internet Design) Program
17 * under grant CNS-0626918 (Postmodern Internet Architecture),
18 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
19 * US Department of Defense (DoD), and ITTC at The University of Kansas.
20 */
21
22#include "dsr-options.h"
23
24#include "dsr-option-header.h"
25#include "dsr-rcache.h"
26#include "dsr-routing-header.h"
27
28#include "ns3/assert.h"
29#include "ns3/fatal-error.h"
30#include "ns3/iana-internet-protocol-numbers.h"
31#include "ns3/icmpv4-l4-protocol.h"
32#include "ns3/ip-l4-protocol.h"
33#include "ns3/ipv4-address.h"
34#include "ns3/ipv4-header.h"
35#include "ns3/ipv4-interface.h"
36#include "ns3/ipv4-l3-protocol.h"
37#include "ns3/ipv4-route.h"
38#include "ns3/log.h"
39#include "ns3/node-list.h"
40#include "ns3/node.h"
41#include "ns3/object-vector.h"
42#include "ns3/pointer.h"
43#include "ns3/ptr.h"
44#include "ns3/trace-source-accessor.h"
45#include "ns3/udp-header.h"
46#include "ns3/uinteger.h"
47
48#include <algorithm>
49#include <ctime>
50#include <list>
51#include <map>
52
53namespace ns3
54{
55
56NS_LOG_COMPONENT_DEFINE("DsrOptions");
57
58namespace dsr
59{
60
62
63TypeId
65{
66 static TypeId tid = TypeId("ns3::dsr::DsrOptions")
68 .SetGroupName("Dsr")
69 .AddAttribute("OptionNumber",
70 "The Dsr option number.",
74 .AddTraceSource("Drop",
75 "Packet dropped.",
77 "ns3::Packet::TracedCallback")
78 .AddTraceSource("Rx",
79 "Receive DSR packet.",
81 "ns3::dsr::DsrOptionSRHeader::TracedCallback");
82 return tid;
83}
84
89
94
95void
97{
98 NS_LOG_FUNCTION(this << node);
99 m_node = node;
100}
101
104{
106 return m_node;
107}
108
109bool
111 Ipv4Address destAddress,
112 std::vector<Ipv4Address>& nodeList)
113{
114 NS_LOG_FUNCTION(this << ipv4Address << destAddress);
115 auto it = find(nodeList.begin(), nodeList.end(), destAddress);
116
117 for (auto i = it; i != nodeList.end(); ++i)
118 {
119 if ((ipv4Address == (*i)) && ((*i) != nodeList.back()))
120 {
121 return true;
122 }
123 }
124 return false;
125}
126
127std::vector<Ipv4Address>
128DsrOptions::CutRoute(Ipv4Address ipv4Address, std::vector<Ipv4Address>& nodeList)
129{
130 NS_LOG_FUNCTION(this << ipv4Address);
131 auto it = find(nodeList.begin(), nodeList.end(), ipv4Address);
132 std::vector<Ipv4Address> cutRoute;
133 for (auto i = it; i != nodeList.end(); ++i)
134 {
135 cutRoute.push_back(*i);
136 }
137 return cutRoute;
138}
139
142{
143 NS_LOG_FUNCTION(this << nextHop << srcAddress);
145 m_ipv4Route->SetDestination(nextHop);
146 m_ipv4Route->SetGateway(nextHop);
147 m_ipv4Route->SetSource(srcAddress);
148 return m_ipv4Route;
149}
150
151bool
152DsrOptions::ReverseRoutes(std::vector<Ipv4Address>& vec)
153{
154 NS_LOG_FUNCTION(this);
155
156 std::reverse(vec.begin(), vec.end());
157
158 return true;
159}
160
162DsrOptions::SearchNextHop(Ipv4Address ipv4Address, std::vector<Ipv4Address>& vec)
163{
164 NS_LOG_FUNCTION(this << ipv4Address);
165 Ipv4Address nextHop;
166 NS_LOG_DEBUG("the vector size " << vec.size());
167 if (vec.size() == 2)
168 {
169 NS_LOG_DEBUG("The two nodes are neighbors");
170 nextHop = vec[1];
171 return nextHop;
172 }
173
174 if (ipv4Address == vec.back())
175 {
176 NS_LOG_DEBUG("We have reached to the final destination " << ipv4Address << " "
177 << vec.back());
178 return ipv4Address;
179 }
180 for (auto i = vec.begin(); i != vec.end(); ++i)
181 {
182 if (ipv4Address == (*i))
183 {
184 nextHop = *(++i);
185 return nextHop;
186 }
187 }
188
189 NS_LOG_DEBUG("next hop address not found, route corrupted");
190 Ipv4Address none = "0.0.0.0";
191 return none;
192}
193
195DsrOptions::ReverseSearchNextHop(Ipv4Address ipv4Address, std::vector<Ipv4Address>& vec)
196{
197 NS_LOG_FUNCTION(this << ipv4Address);
198 Ipv4Address nextHop;
199 if (vec.size() == 2)
200 {
201 NS_LOG_DEBUG("The two nodes are neighbors");
202 nextHop = vec[0];
203 return nextHop;
204 }
205
206 for (auto ri = vec.rbegin(); ri != vec.rend(); ++ri)
207 {
208 if (ipv4Address == (*ri))
209 {
210 nextHop = *(++ri);
211 return nextHop;
212 }
213 }
214
215 NS_LOG_DEBUG("next hop address not found, route corrupted");
216 Ipv4Address none = "0.0.0.0";
217 return none;
218}
219
221DsrOptions::ReverseSearchNextTwoHop(Ipv4Address ipv4Address, std::vector<Ipv4Address>& vec)
222{
223 NS_LOG_FUNCTION(this << ipv4Address);
224 Ipv4Address nextTwoHop;
225 NS_LOG_DEBUG("The vector size " << vec.size());
226 NS_ASSERT(vec.size() > 2);
227 for (auto ri = vec.rbegin(); ri != vec.rend(); ++ri)
228 {
229 if (ipv4Address == (*ri))
230 {
231 nextTwoHop = *(ri + 2);
232 return nextTwoHop;
233 }
234 }
235 NS_FATAL_ERROR("next hop address not found, route corrupted");
236 Ipv4Address none = "0.0.0.0";
237 return none;
238}
239
240void
241DsrOptions::PrintVector(std::vector<Ipv4Address>& vec)
242{
243 NS_LOG_FUNCTION(this);
244 /*
245 * Check elements in a route vector
246 */
247 if (vec.empty())
248 {
249 NS_LOG_DEBUG("The vector is empty");
250 }
251 else
252 {
253 NS_LOG_DEBUG("Print all the elements in a vector");
254 for (auto i = vec.begin(); i != vec.end(); ++i)
255 {
256 NS_LOG_DEBUG("The ip address " << *i);
257 }
258 }
259}
260
261bool
262DsrOptions::IfDuplicates(std::vector<Ipv4Address>& vec, std::vector<Ipv4Address>& vec2)
263{
264 NS_LOG_FUNCTION(this);
265 for (auto i = vec.begin(); i != vec.end(); ++i)
266 {
267 for (auto j = vec2.begin(); j != vec2.end(); ++j)
268 {
269 if ((*i) == (*j))
270 {
271 return true;
272 }
273 }
274 }
275 return false;
276}
277
278bool
279DsrOptions::CheckDuplicates(Ipv4Address ipv4Address, std::vector<Ipv4Address>& vec)
280{
281 NS_LOG_FUNCTION(this << ipv4Address);
282 for (auto i = vec.begin(); i != vec.end(); ++i)
283 {
284 if ((*i) == ipv4Address)
285 {
286 return true;
287 }
288 }
289 return false;
290}
291
292void
293DsrOptions::RemoveDuplicates(std::vector<Ipv4Address>& vec)
294{
295 NS_LOG_FUNCTION(this);
296 // Remove duplicate ip address from the route if any, should not happen with normal behavior
297 // nodes
298 std::vector<Ipv4Address> vec2(vec); // declare vec2 as a copy of the vec
299 PrintVector(vec2); // Print all the ip address in the route
300 vec.clear(); // clear vec
301 for (auto i = vec2.begin(); i != vec2.end(); ++i)
302 {
303 if (vec.empty())
304 {
305 vec.push_back(*i);
306 continue;
307 }
308
309 for (auto j = vec.begin(); j != vec.end(); ++j)
310 {
311 if ((*i) == (*j))
312 {
313 if ((j + 1) != vec.end())
314 {
315 vec.erase(j + 1, vec.end()); // Automatic shorten the route
316 }
317
318 break;
319 }
320 else if (j == (vec.end() - 1))
321 {
322 vec.push_back(*i);
323 break;
324 }
325 }
326 }
327}
328
331{
332 NS_LOG_FUNCTION(this << ipv4Address);
333 int32_t nNodes = NodeList::GetNNodes();
334 for (int32_t i = 0; i < nNodes; ++i)
335 {
337 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
338 int32_t ifIndex = ipv4->GetInterfaceForAddress(ipv4Address);
339 if (ifIndex != -1)
340 {
341 return node;
342 }
343 }
344 return nullptr;
345}
346
348
349TypeId
351{
352 static TypeId tid = TypeId("ns3::dsr::DsrOptionPad1")
354 .SetGroupName("Dsr")
355 .AddConstructor<DsrOptionPad1>();
356 return tid;
357}
358
363
368
369uint8_t
371{
373
374 return OPT_NUMBER;
375}
376
377uint8_t
379 Ptr<Packet> dsrP,
380 Ipv4Address ipv4Address,
381 Ipv4Address source,
382 const Ipv4Header& ipv4Header,
383 uint8_t protocol,
384 bool& isPromisc,
385 Ipv4Address promiscSource)
386{
387 NS_LOG_FUNCTION(this << packet << dsrP << ipv4Address << source << ipv4Header
388 << (uint32_t)protocol << isPromisc);
389 Ptr<Packet> p = packet->Copy();
390 DsrOptionPad1Header pad1Header;
391 p->RemoveHeader(pad1Header);
392
393 isPromisc = false;
394
395 return pad1Header.GetSerializedSize();
396}
397
399
400TypeId
402{
403 static TypeId tid = TypeId("ns3::dsr::DsrOptionPadn")
405 .SetGroupName("Dsr")
406 .AddConstructor<DsrOptionPadn>();
407 return tid;
408}
409
414
419
420uint8_t
426
427uint8_t
429 Ptr<Packet> dsrP,
430 Ipv4Address ipv4Address,
431 Ipv4Address source,
432 const Ipv4Header& ipv4Header,
433 uint8_t protocol,
434 bool& isPromisc,
435 Ipv4Address promiscSource)
436{
437 NS_LOG_FUNCTION(this << packet << dsrP << ipv4Address << source << ipv4Header
438 << (uint32_t)protocol << isPromisc);
439
440 Ptr<Packet> p = packet->Copy();
441 DsrOptionPadnHeader padnHeader;
442 p->RemoveHeader(padnHeader);
443
444 isPromisc = false;
445
446 return padnHeader.GetSerializedSize();
447}
448
450
451TypeId
453{
454 static TypeId tid = TypeId("ns3::dsr::DsrOptionRreq")
456 .SetGroupName("Dsr")
457 .AddConstructor<DsrOptionRreq>();
458 return tid;
459}
460
465
470
471uint8_t
473{
475
476 return OPT_NUMBER;
477}
478
479uint8_t
481 Ptr<Packet> dsrP,
482 Ipv4Address ipv4Address,
483 Ipv4Address source,
484 const Ipv4Header& ipv4Header,
485 uint8_t protocol,
486 bool& isPromisc,
487 Ipv4Address promiscSource)
488{
489 NS_LOG_FUNCTION(this << packet << dsrP << ipv4Address << source << ipv4Header
490 << (uint32_t)protocol << isPromisc);
491 // Fields from IP header
492 /*
493 * \ when the ip source address is equal to the address of our own, this is request packet
494 * originated \ by the node itself, discard it
495 */
496 if (source == ipv4Address)
497 {
498 NS_LOG_DEBUG("Discard the packet since it was originated from same source address");
499 m_dropTrace(packet); // call the drop trace to show in the tracing
500 return 0;
501 }
502 /*
503 * Get the node associated with the ipv4 address and get several objects from the node and leave
504 * for further use
505 */
506 Ptr<Node> node = GetNodeWithAddress(ipv4Address);
507 Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting>();
508
509 Ptr<Packet> p =
510 packet->Copy(); // Note: The packet here doesn't contain the fixed size dsr header
511 /*
512 * @brief Get the number of routers' address field before removing the header
513 * @peek the packet and get the value
514 */
515 uint8_t buf[2];
516 p->CopyData(buf, sizeof(buf));
517 uint8_t numberAddress = (buf[1] - 6) / 4;
518 NS_LOG_DEBUG("The number of Ip addresses " << (uint32_t)numberAddress);
519 if (numberAddress >= 255)
520 {
521 NS_LOG_DEBUG("Discard the packet, malformed header since two many ip addresses in route");
522 m_dropTrace(packet); // call the drop trace to show in the tracing
523 return 0;
524 }
525
526 /*
527 * Create the dsr rreq header
528 */
530 /*
531 * Set the number of addresses with the value from peek data and remove the rreq header
532 */
533 rreq.SetNumberAddress(numberAddress);
534 // Remove the route request header
535 p->RemoveHeader(rreq);
536 // Verify the option length
537 if (rreq.GetLength() % 2 != 0)
538 {
539 NS_LOG_LOGIC("Malformed header. Drop!");
540 m_dropTrace(packet); // call drop trace
541 return 0;
542 }
543 // Check the rreq id for verifying the request id
544 uint16_t requestId = rreq.GetId();
545 // The target address is where we want to send the data packets
546 Ipv4Address targetAddress = rreq.GetTarget();
547 // Get the node list and source address from the route request header
548 std::vector<Ipv4Address> mainVector = rreq.GetNodesAddresses();
549 std::vector<Ipv4Address> nodeList(mainVector);
550 // Get the real source address of this request, it will be used when checking if we have
551 // received the save route request before or not
552 PrintVector(nodeList);
553
554 // check whether we have received this request or not, if not, it will save the request in the
555 // table for later use, if not found, return false, and push the newly received source request
556 // entry in the cache
557
558 // Get the TTL value, this is used to test if the packet will be forwarded or not
559 uint8_t ttl = ipv4Header.GetTtl();
560
561 bool dupRequest = false; // initialize the duplicate request check value
562 if (ttl > 1)
563 {
564 // if the ttl value is not greater than 1, then this request will be forwarded, and we need
565 // to save it in the source entry
566 dupRequest = dsr->FindSourceEntry(source, targetAddress, requestId);
567 }
568 /*
569 * Before processing the route request, we need to check two things
570 * 1. if this is the exact same request we have just received, ignore it
571 * 2. if our address is already in the path list, ignore it
572 * 3. otherwise process further
573 */
574 if (dupRequest)
575 {
576 // We have received this same route request before, not forwarding it now
577 NS_LOG_LOGIC("Duplicate request. Drop!");
578 m_dropTrace(packet); // call drop trace
579 return 0;
580 }
581 else if (CheckDuplicates(ipv4Address, nodeList))
582 {
583 /*
584 * if the route contains the node address already, drop the request packet
585 */
586 m_dropTrace(packet); // call drop trace
587 NS_LOG_DEBUG("Our node address is already seen in the route, drop the request");
588 return 0;
589 }
590 // A node ignores all RREQs received from any node in its blacklist
591 DsrRouteCacheEntry toPrev;
592 bool isRouteInCache = dsr->LookupRoute(targetAddress, toPrev);
594 toPrev.GetVector(); // The route from our own route cache to dst
595 PrintVector(ip);
596 std::vector<Ipv4Address> saveRoute(nodeList);
597 PrintVector(saveRoute);
598 bool areThereDuplicates = IfDuplicates(ip, saveRoute);
599 /*
600 * When the reverse route is created or updated, the following actions on the route are
601 * also carried out:
602 * 3. the next hop in the routing table becomes the node from which the RREQ was received
603 * 4. the hop count is copied from the Hop Count in the RREQ message;
604 *
605 * A node generates a RREP if either:
606 * (i) it is itself the destination,
607 * The target address equal to our own ip address
608 */
609 NS_LOG_DEBUG("The target address over here " << targetAddress << " and the ip address "
610 << ipv4Address << " and the source address "
611 << source);
612 if (targetAddress == ipv4Address)
613 {
614 Ipv4Address nextHop; // Declare the next hop address to use
615 if (nodeList.empty())
616 {
617 NS_LOG_DEBUG("These two nodes are neighbors");
618 m_finalRoute.clear();
619 // The source is not in the hop list; retrieve it from the IP header.
620 m_finalRoute.push_back(source); // push back the request originator's address
621 m_finalRoute.push_back(ipv4Address); // push back our own address
622 nextHop = source;
623 }
624 else
625 {
626 // Prepend the original source (from IP header) then the intermediate hops,
627 // and finally this node (the destination).
628 m_finalRoute.clear();
629 m_finalRoute.push_back(source); // original source from IP header
630 m_finalRoute.insert(m_finalRoute.end(), nodeList.begin(), nodeList.end());
631 m_finalRoute.push_back(ipv4Address); // destination (us)
633 nextHop = ReverseSearchNextHop(ipv4Address, m_finalRoute); // get the next hop
634 }
635
637 rrep.SetNodesAddress(m_finalRoute); // Set the node addresses in the route reply header
638 NS_LOG_DEBUG("The nextHop address " << nextHop);
639 /*
640 * This part add dsr header to the packet and send route reply packet
641 */
642 DsrRoutingHeader dsrRoutingHeader;
644 // Set the route for route reply
645 SetRoute(nextHop, ipv4Address);
646
647 dsrRoutingHeader.AddDsrOption(rrep);
648 Ptr<Packet> newPacket = Create<Packet>();
649 newPacket->AddHeader(dsrRoutingHeader);
650 dsr->ScheduleInitialReply(newPacket, ipv4Address, nextHop, m_ipv4Route);
651 /*
652 * Create the route entry to the rreq originator and save it to route cache, also need
653 * to reverse the route
654 */
657 {
659 Ipv4Address dst = m_finalRoute.back();
660 bool addRoute = false;
661 DsrRouteCacheEntry toSource(/*ip=*/m_finalRoute,
662 /*dst=*/dst,
663 /*exp=*/ActiveRouteTimeout);
664 if (dsr->IsLinkCache())
665 {
666 addRoute = dsr->AddRoute_Link(m_finalRoute, ipv4Address);
667 }
668 else
669 {
670 addRoute = dsr->AddRoute(toSource);
671 }
672
673 if (addRoute)
674 {
675 /*
676 * Found a route to the dst, construct the source route option header
677 */
678 DsrOptionSRHeader sourceRoute;
679 NS_LOG_DEBUG("The route length " << m_finalRoute.size());
680 sourceRoute.SetNodesAddress(m_finalRoute);
681
682 // TODO !!!!!!!!!!!!!!
683 // Think about this part, we just added the route,
684 // probability no need to increase stability now?????
685 sourceRoute.SetSegmentsLeft(m_finalRoute.size() - 2);
686 // The salvage value here is 0
687 sourceRoute.SetSalvage(0);
688 Ipv4Address nextHop =
689 SearchNextHop(ipv4Address, m_finalRoute); // Get the next hop address
690 NS_LOG_DEBUG("The nextHop address " << nextHop);
691
692 if (nextHop == "0.0.0.0")
693 {
694 dsr->PacketNewRoute(dsrP, source, dst, protocol);
695 return 0;
696 }
697 SetRoute(nextHop, ipv4Address);
698 /*
699 * Send the data packet from the send buffer
700 */
701 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
702 // Cancel the route request timer for destination after sending the data packet
703 dsr->CancelRreqTimer(dst, true);
704 }
705 else
706 {
707 NS_LOG_DEBUG("The route is failed to add in cache");
708 return 0;
709 }
710 }
711 else
712 {
713 NS_LOG_DEBUG("Unable to reverse route");
714 return 0;
715 }
716 isPromisc = false;
717 return rreq.GetSerializedSize();
718 }
719
720 /*
721 * (ii) or it has an active route to the destination, send reply based on request header and
722 * route cache, need to delay based on a random value from d = H * (h - 1 + r), which can
723 * avoid possible route reply storm. Also, verify if two vectors do not contain duplicates
724 * (part of the route to the destination from route cache and route collected so far). If
725 * so, do not use the route found and forward the route request.
726 */
727 else if (isRouteInCache && !areThereDuplicates)
728 {
729 m_finalRoute.clear(); // Clear the final route vector
730 /**
731 * Prepend original source from IP header, then intermediate hops,
732 * then the cached route to the destination.
733 */
734 m_finalRoute.push_back(source); // original source from IP header
735 m_finalRoute.insert(m_finalRoute.end(), saveRoute.begin(), saveRoute.end());
736 m_finalRoute.insert(m_finalRoute.end(), ip.begin(), ip.end());
737 /*
738 * Create the route entry to the rreq originator and save it to route cache, also need
739 * to reverse the route
740 */
741 bool addRoute = false;
742 std::vector<Ipv4Address> reverseRoute(m_finalRoute);
743
744 if (ReverseRoutes(reverseRoute))
745 {
746 // Reconstruct the route back to source: [srcAddress, intermediates..., thisNode]
747 saveRoute.insert(saveRoute.begin(), source);
748 saveRoute.push_back(ipv4Address);
749 ReverseRoutes(saveRoute);
750 Ipv4Address dst = saveRoute.back();
751 NS_LOG_DEBUG("This is the route save in route cache");
752 PrintVector(saveRoute);
753
754 DsrRouteCacheEntry toSource(/*ip=*/saveRoute,
755 /*dst=*/dst,
756 /*exp=*/ActiveRouteTimeout);
757 // Add the route entry in the route cache
758 if (dsr->IsLinkCache())
759 {
760 addRoute = dsr->AddRoute_Link(saveRoute, ipv4Address);
761 }
762 else
763 {
764 addRoute = dsr->AddRoute(toSource);
765 }
766
767 if (addRoute)
768 {
769 NS_LOG_LOGIC("We have added the route and search send buffer for packet with "
770 "destination "
771 << dst);
772 /*
773 * Found a route the dst, construct the source route option header
774 */
775 DsrOptionSRHeader sourceRoute;
776 PrintVector(saveRoute);
777
778 sourceRoute.SetNodesAddress(saveRoute);
779 sourceRoute.SetSegmentsLeft(saveRoute.size() - 2);
780 uint8_t salvage = 0;
781 sourceRoute.SetSalvage(salvage);
782 Ipv4Address nextHop =
783 SearchNextHop(ipv4Address, saveRoute); // Get the next hop address
784 NS_LOG_DEBUG("The nextHop address " << nextHop);
785
786 if (nextHop == "0.0.0.0")
787 {
788 dsr->PacketNewRoute(dsrP, source, dst, protocol);
789 return 0;
790 }
791 SetRoute(nextHop, ipv4Address);
792 /*
793 * Schedule the packet retry
794 */
795 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
796 // Cancel the route request timer for destination
797 dsr->CancelRreqTimer(dst, true);
798 }
799 else
800 {
801 NS_LOG_DEBUG("The route is failed to add in cache");
802 return 0;
803 }
804 }
805 else
806 {
807 NS_LOG_DEBUG("Unable to reverse the route");
808 return 0;
809 }
810
811 /*
812 * Need to first pin down the next hop address before removing duplicates
813 */
814 Ipv4Address nextHop = ReverseSearchNextHop(ipv4Address, m_finalRoute);
815 /*
816 * First remove the duplicate ip address to automatically shorten the route, and then
817 * reversely search the next hop address
818 */
819 // Set the route
820 SetRoute(nextHop, ipv4Address);
821
822 uint16_t hops = m_finalRoute.size();
824 rrep.SetNodesAddress(m_finalRoute); // Set the node addresses in the route reply header
826 /*
827 * This part add dsr header to the packet and send route reply packet
828 */
829 DsrRoutingHeader dsrRoutingHeader;
831
832 dsrRoutingHeader.AddDsrOption(rrep);
833
834 Ptr<Packet> newPacket = Create<Packet>();
835 newPacket->AddHeader(dsrRoutingHeader);
836 dsr->ScheduleCachedReply(newPacket, ipv4Address, nextHop, m_ipv4Route, hops);
837 isPromisc = false;
838 return rreq.GetSerializedSize();
839 }
840 /*
841 * (iii) no route in any type has been found
842 */
843
844 mainVector.push_back(ipv4Address);
845 NS_LOG_DEBUG("Print out the main vector");
846 PrintVector(mainVector);
847 rreq.SetNodesAddress(mainVector);
848
849 /*
850 * Construct the dsr routing header for later use
851 */
852 DsrRoutingHeader dsrRoutingHeader;
853
854 Ptr<Packet> errP = p->Copy();
855 if (errP->GetSize())
856 {
857 NS_LOG_DEBUG("Error header included");
858 dsrRoutingHeader.SetNextHeader(protocol);
859
861 p->RemoveHeader(rerr);
862 Ipv4Address errorSrc = rerr.GetErrorSrc();
863 Ipv4Address unreachNode = rerr.GetUnreachNode();
864 Ipv4Address errorDst = rerr.GetErrorDst();
865
866 if ((errorSrc == source) && (unreachNode == ipv4Address))
867 {
868 NS_LOG_DEBUG("The error link back to work again");
869 NS_LOG_DEBUG("The RREQ header length " << rreq.GetLength());
870 dsrRoutingHeader.AddDsrOption(rreq);
871 }
872 else
873 {
874 dsr->DeleteAllRoutesIncludeLink(errorSrc, unreachNode, ipv4Address);
875
877 newUnreach.SetErrorType(1);
878 newUnreach.SetErrorSrc(errorSrc);
879 newUnreach.SetUnreachNode(unreachNode);
880 newUnreach.SetErrorDst(errorDst);
881 newUnreach.SetSalvage(rerr.GetSalvage()); // Set the value about whether to
882 // salvage a packet or not
883 dsrRoutingHeader.AddDsrOption(rreq);
884 dsrRoutingHeader.AddDsrOption(newUnreach);
885 }
886 }
887 else
888 {
889 NS_LOG_DEBUG("The RREQ header length " << rreq.GetLength());
890
892 dsrRoutingHeader.AddDsrOption(rreq);
893 }
894 // Get the TTL value
895 ttl = ipv4Header.GetTtl();
896 /*
897 * Decrease the TTL value in the packet tag by one, this tag will go to ip layer 3 send
898 * function and drop packet when TTL value equals to 0
899 */
900 NS_LOG_DEBUG("The ttl value here " << (uint32_t)ttl);
901 if (ttl > 1)
902 {
903 Ptr<Packet> interP = Create<Packet>();
904 SocketIpTtlTag tag;
905 tag.SetTtl(ttl - 1);
906 interP->AddPacketTag(tag);
907 interP->AddHeader(dsrRoutingHeader);
908 dsr->ScheduleInterRequest(interP, source);
909 isPromisc = false;
910 }
911 return rreq.GetSerializedSize();
912}
913
915
916TypeId
918{
919 static TypeId tid = TypeId("ns3::dsr::DsrOptionRrep")
921 .SetGroupName("Dsr")
922 .AddConstructor<DsrOptionRrep>();
923 return tid;
924}
925
930
935
936uint8_t
938{
940
941 return OPT_NUMBER;
942}
943
944uint8_t
946 Ptr<Packet> dsrP,
947 Ipv4Address ipv4Address,
948 Ipv4Address source,
949 const Ipv4Header& ipv4Header,
950 uint8_t protocol,
951 bool& isPromisc,
952 Ipv4Address promiscSource)
953{
954 NS_LOG_FUNCTION(this << packet << dsrP << ipv4Address << source << ipv4Header
955 << (uint32_t)protocol << isPromisc);
956
957 Ptr<Packet> p = packet->Copy();
958
959 Ipv4Address srcAddress = ipv4Header.GetSource();
960
961 // Get the number of routers' address field
962 uint8_t buf[2];
963 p->CopyData(buf, sizeof(buf));
964 uint8_t numberAddress = (buf[1] - 2) / 4;
965
967 rrep.SetNumberAddress(numberAddress); // Set the number of ip address in the header to reserver
968 // space for deserialize header
969 p->RemoveHeader(rrep);
970
971 Ptr<Node> node = GetNodeWithAddress(ipv4Address);
972 Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting>();
973
974 NS_LOG_DEBUG("The next header value " << (uint32_t)protocol);
975
976 std::vector<Ipv4Address> nodeList = rrep.GetNodesAddress();
977 /**
978 * Get the destination address, which is the last element in the nodeList
979 */
980 Ipv4Address targetAddress = nodeList.front();
981 // If the RREP option has reached to the destination
982 if (targetAddress == ipv4Address)
983 {
984 RemoveDuplicates(nodeList); // This is for the route reply from intermediate node since we
985 // didn't remove duplicate there
986 if (nodeList.empty())
987 {
988 NS_LOG_DEBUG("The route we have contains 0 entries");
989 return 0;
990 }
991 /**
992 * Get the destination address for the data packet, which is the last element in the
993 * nodeList
994 */
995 Ipv4Address dst = nodeList.back();
996 /**
997 * Add the newly found route to the route cache
998 * The route looks like:
999 * \\ "srcAddress" + "intermediate node address" + "targetAddress"
1000 */
1001 DsrRouteCacheEntry toDestination(/*ip=*/nodeList,
1002 /*dst=*/dst,
1003 /*exp=*/ActiveRouteTimeout);
1004 bool addRoute = false;
1005 if (dsr->IsLinkCache())
1006 {
1007 addRoute = dsr->AddRoute_Link(nodeList, ipv4Address);
1008 }
1009 else
1010 {
1011 addRoute = dsr->AddRoute(toDestination);
1012 }
1013
1014 if (addRoute)
1015 {
1017 "We have added the route and search send buffer for packet with destination "
1018 << dst);
1019 /**
1020 * Found a route the dst, construct the source route option header
1021 */
1022 DsrOptionSRHeader sourceRoute;
1023 NS_LOG_DEBUG("The route length " << nodeList.size());
1024 sourceRoute.SetNodesAddress(nodeList);
1025 sourceRoute.SetSegmentsLeft(nodeList.size() - 2);
1026 sourceRoute.SetSalvage(0);
1027 Ipv4Address nextHop = SearchNextHop(ipv4Address, nodeList); // Get the next hop address
1028 NS_LOG_DEBUG("The nextHop address " << nextHop);
1029 if (nextHop == "0.0.0.0")
1030 {
1031 dsr->PacketNewRoute(dsrP, srcAddress, dst, protocol);
1032 return 0;
1033 }
1034 PrintVector(nodeList);
1035 SetRoute(nextHop, ipv4Address);
1036 // Cancel the route request timer for destination
1037 dsr->CancelRreqTimer(dst, true);
1038 /**
1039 * Schedule the packet retry
1040 */
1041 dsr->SendPacketFromBuffer(sourceRoute, nextHop, protocol);
1042 }
1043 else
1044 {
1045 NS_LOG_DEBUG("Failed to add the route");
1046 return 0;
1047 }
1048 }
1049 else
1050 {
1051 NS_LOG_DEBUG("The length of rrep option " << (uint32_t)(rrep.GetLength() - 2));
1052
1053 if ((rrep.GetLength() - 2) % 2 !=
1054 0) // The get length - 2 is to get aligned for the malformed header check
1055 {
1056 NS_LOG_LOGIC("Malformed header. Drop!");
1057 m_dropTrace(packet);
1058 return 0;
1059 }
1060 PrintVector(nodeList);
1061 /*
1062 * This node is only an intermediate node, but it needs to save the possible route to the
1063 * destination when cutting the route
1064 */
1065 std::vector<Ipv4Address> routeCopy = nodeList;
1066 std::vector<Ipv4Address> cutRoute = CutRoute(ipv4Address, nodeList);
1067 PrintVector(cutRoute);
1068 if (cutRoute.size() >= 2)
1069 {
1070 Ipv4Address dst = cutRoute.back();
1071 NS_LOG_DEBUG("The route destination after cut " << dst);
1072 DsrRouteCacheEntry toDestination(/*ip=*/cutRoute,
1073 /*dst=*/dst,
1074 /*exp=*/ActiveRouteTimeout);
1075 bool addRoute = false;
1076 if (dsr->IsLinkCache())
1077 {
1078 addRoute = dsr->AddRoute_Link(nodeList, ipv4Address);
1079 }
1080 else
1081 {
1082 addRoute = dsr->AddRoute(toDestination);
1083 }
1084 if (addRoute)
1085 {
1086 dsr->CancelRreqTimer(dst, true);
1087 }
1088 else
1089 {
1090 NS_LOG_DEBUG("The route not added");
1091 }
1092 }
1093 else
1094 {
1095 NS_LOG_DEBUG("The route is corrupted");
1096 }
1097 /*
1098 * Reverse search the vector for next hop address
1099 */
1100 Ipv4Address nextHop = ReverseSearchNextHop(ipv4Address, routeCopy);
1101 PrintVector(routeCopy);
1102 NS_LOG_DEBUG("The nextHop address " << nextHop << " and the source in the route reply "
1103 << source);
1104 /*
1105 * Set the route entry we will use to send reply
1106 */
1107 SetRoute(nextHop, ipv4Address);
1108 /*
1109 * This part add dsr routing header to the packet and send reply
1110 */
1111 DsrRoutingHeader dsrRoutingHeader;
1113
1114 NS_LOG_DEBUG("The reply header length " << (uint32_t)rrep.GetLength());
1115 dsrRoutingHeader.AddDsrOption(rrep);
1116
1117 Ptr<Packet> newPacket = Create<Packet>();
1118 newPacket->AddHeader(dsrRoutingHeader);
1119 dsr->SendReply(newPacket, ipv4Address, nextHop, m_ipv4Route);
1120 isPromisc = false;
1121 }
1122 return rrep.GetSerializedSize();
1123}
1124
1126
1127TypeId
1129{
1130 static TypeId tid = TypeId("ns3::dsr::DsrOptionSR")
1132 .SetGroupName("Dsr")
1133 .AddConstructor<DsrOptionSR>();
1134 return tid;
1135}
1136
1141
1146
1147uint8_t
1149{
1151 return OPT_NUMBER;
1152}
1153
1154uint8_t
1156 Ptr<Packet> dsrP,
1157 Ipv4Address ipv4Address,
1158 Ipv4Address source,
1159 const Ipv4Header& ipv4Header,
1160 uint8_t protocol,
1161 bool& isPromisc,
1162 Ipv4Address promiscSource)
1163{
1164 NS_LOG_FUNCTION(this << packet << dsrP << ipv4Address << source << ipv4Address << ipv4Header
1165 << (uint32_t)protocol << isPromisc);
1166 Ptr<Packet> p = packet->Copy();
1167 // Get the number of routers' address field
1168 uint8_t buf[2];
1169 p->CopyData(buf, sizeof(buf));
1170 uint8_t numberAddress = (buf[1] - 2) / 4;
1171 DsrOptionSRHeader sourceRoute;
1172 sourceRoute.SetNumberAddress(numberAddress);
1173 p->RemoveHeader(sourceRoute);
1174
1175 // The route saved in the source route
1176 std::vector<Ipv4Address> nodeList = sourceRoute.GetNodesAddress();
1177 uint8_t segsLeft = sourceRoute.GetSegmentsLeft();
1178 uint8_t salvage = sourceRoute.GetSalvage();
1179 /*
1180 * Get the node from IP address and get the DSR extension object
1181 */
1182 Ptr<Node> node = GetNodeWithAddress(ipv4Address);
1183 Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting>();
1184 /*
1185 * Get the source and destination address from ipv4 header
1186 */
1187 Ipv4Address srcAddress = ipv4Header.GetSource();
1188 Ipv4Address destAddress = ipv4Header.GetDestination();
1189
1190 // Get the node list destination
1191 Ipv4Address destination = nodeList.back();
1192 /*
1193 * If it's a promiscuous receive data packet,
1194 * 1. see if automatic route shortening possible or not
1195 * 2. see if it is a passive acknowledgment
1196 */
1197 if (isPromisc)
1198 {
1199 NS_LOG_LOGIC("We process promiscuous receipt data packet");
1200 if (ContainAddressAfter(ipv4Address, destAddress, nodeList))
1201 {
1202 NS_LOG_LOGIC("Send back the gratuitous reply");
1203 dsr->SendGratuitousReply(source, srcAddress, nodeList, protocol);
1204 }
1205
1206 uint16_t fragmentOffset = ipv4Header.GetFragmentOffset();
1207 uint16_t identification = ipv4Header.GetIdentification();
1208
1209 if (destAddress != destination)
1210 {
1211 NS_LOG_DEBUG("Process the promiscuously received packet");
1212 bool findPassive = false;
1213 int32_t nNodes = NodeList::GetNNodes();
1214 for (int32_t i = 0; i < nNodes; ++i)
1215 {
1216 NS_LOG_DEBUG("Working with node " << i);
1217
1218 Ptr<Node> node = NodeList::GetNode(i);
1219 Ptr<dsr::DsrRouting> dsrNode = node->GetObject<dsr::DsrRouting>();
1220 // The source and destination addresses here are the real source and destination for
1221 // the packet
1222 findPassive = dsrNode->PassiveEntryCheck(packet,
1223 source,
1224 destination,
1225 segsLeft,
1226 fragmentOffset,
1227 identification,
1228 false);
1229 if (findPassive)
1230 {
1231 break;
1232 }
1233 }
1234
1235 if (findPassive)
1236 {
1237 NS_LOG_DEBUG("We find one previously received passive entry");
1238 /*
1239 * Get the node from IP address and get the DSR extension object
1240 * the srcAddress would be the source address from ip header
1241 */
1242 PrintVector(nodeList);
1243
1244 NS_LOG_DEBUG("promisc source " << promiscSource);
1245 Ptr<Node> node = GetNodeWithAddress(promiscSource);
1246 Ptr<dsr::DsrRouting> dsrSrc = node->GetObject<dsr::DsrRouting>();
1247 dsrSrc->CancelPassiveTimer(packet, source, destination, segsLeft);
1248 }
1249 else
1250 {
1251 NS_LOG_DEBUG("Saved the entry for further use");
1252 dsr->PassiveEntryCheck(packet,
1253 source,
1254 destination,
1255 segsLeft,
1256 fragmentOffset,
1257 identification,
1258 true);
1259 }
1260 }
1261 // Safely terminate promiscuously received packet
1262 return 0;
1263 }
1264 else
1265 {
1266 /*
1267 * Get the number of address from the source route header
1268 */
1269 uint8_t nextAddressIndex;
1270 Ipv4Address nextAddress;
1271
1272 // Get the option type value
1273 uint32_t size = p->GetSize();
1274 auto data = new uint8_t[size];
1275 p->CopyData(data, size);
1276 uint8_t optionType = 0;
1277 optionType = *(data);
1278 // When the option type is 160, means there is ACK request header after the source route,
1279 // we need to send back acknowledgment
1280 if (optionType == 160)
1281 {
1282 NS_LOG_LOGIC("Remove the ack request header and add ack header to the packet");
1283 // Here we remove the ack packet to the previous hop
1284 DsrOptionAckReqHeader ackReq;
1285 p->RemoveHeader(ackReq);
1286 uint16_t ackId = ackReq.GetAckId();
1287 /*
1288 * Send back acknowledgment packet to the earlier hop
1289 * If the node list is not empty, find the previous hop from the node list,
1290 * otherwise, use srcAddress
1291 */
1292 Ipv4Address ackAddress = srcAddress;
1293 if (!nodeList.empty())
1294 {
1295 if (segsLeft > numberAddress) // The segmentsLeft field should not be larger than
1296 // the total number of ip addresses
1297 {
1298 NS_LOG_LOGIC("Malformed header. Drop!");
1299 m_dropTrace(packet);
1300 return 0;
1301 }
1302 // -fstrict-overflow sensitive, see bug 1868
1303 if (numberAddress - segsLeft < 2) // The index is invalid
1304 {
1305 NS_LOG_LOGIC("Malformed header. Drop!");
1306 m_dropTrace(packet);
1307 return 0;
1308 }
1309 ackAddress = nodeList[numberAddress - segsLeft - 2];
1310 }
1311 m_ipv4Route = SetRoute(ackAddress, ipv4Address);
1312 NS_LOG_DEBUG("Send back ACK to the earlier hop " << ackAddress << " from us "
1313 << ipv4Address);
1314 dsr->SendAck(ackId, ackAddress, source, destination, m_ipv4Route);
1315 }
1316 /*
1317 * After send back ACK, check if the segments left value has turned to 0 or not, if yes,
1318 * update the route entry and return header length
1319 */
1320 if (segsLeft == 0)
1321 {
1322 NS_LOG_DEBUG("This is the final destination");
1323 isPromisc = false;
1324 return sourceRoute.GetSerializedSize();
1325 }
1326
1327 if (sourceRoute.GetLength() % 2 != 0)
1328 {
1329 NS_LOG_LOGIC("Malformed header. Drop!");
1330 m_dropTrace(packet);
1331 return 0;
1332 }
1333
1334 if (segsLeft > numberAddress) // The segmentsLeft field should not be larger than the total
1335 // number of ip addresses
1336 {
1337 NS_LOG_LOGIC("Malformed header. Drop!");
1338 m_dropTrace(packet);
1339 return 0;
1340 }
1341
1342 DsrOptionSRHeader newSourceRoute;
1343 newSourceRoute.SetSegmentsLeft(segsLeft - 1);
1344 newSourceRoute.SetSalvage(salvage);
1345 newSourceRoute.SetNodesAddress(nodeList);
1346 nextAddressIndex = numberAddress - segsLeft;
1347 nextAddress = newSourceRoute.GetNodeAddress(nextAddressIndex);
1348 NS_LOG_DEBUG("The next address of source route option "
1349 << nextAddress << " and the nextAddressIndex: " << (uint32_t)nextAddressIndex
1350 << " and the segments left : " << (uint32_t)segsLeft);
1351 /*
1352 * Get the target Address in the node list
1353 */
1354 Ipv4Address targetAddress = nodeList.back();
1355 Ipv4Address realSource = nodeList.front();
1356 /*
1357 * Search the vector for next hop address
1358 */
1359 Ipv4Address nextHop = SearchNextHop(ipv4Address, nodeList);
1360 PrintVector(nodeList);
1361
1362 if (nextHop == "0.0.0.0")
1363 {
1364 NS_LOG_DEBUG("Before new packet " << *dsrP);
1365 dsr->PacketNewRoute(dsrP, realSource, targetAddress, protocol);
1366 return 0;
1367 }
1368
1369 if (ipv4Address == nextHop)
1370 {
1371 NS_LOG_DEBUG("We have reached the destination");
1372 newSourceRoute.SetSegmentsLeft(0);
1373 return newSourceRoute.GetSerializedSize();
1374 }
1375 // Verify the multicast address, leave it here for now
1376 if (nextAddress.IsMulticast() || destAddress.IsMulticast())
1377 {
1378 m_dropTrace(packet);
1379 return 0;
1380 }
1381 // Set the route and forward the data packet
1382 SetRoute(nextAddress, ipv4Address);
1383 NS_LOG_DEBUG("dsr packet size " << dsrP->GetSize());
1384 dsr->ForwardPacket(dsrP,
1385 newSourceRoute,
1386 ipv4Header,
1387 realSource,
1388 nextAddress,
1389 targetAddress,
1390 protocol,
1391 m_ipv4Route);
1392 }
1393 return sourceRoute.GetSerializedSize();
1394}
1395
1397
1398TypeId
1400{
1401 static TypeId tid = TypeId("ns3::dsr::DsrOptionRerr")
1403 .SetGroupName("Dsr")
1404 .AddConstructor<DsrOptionRerr>();
1405 return tid;
1406}
1407
1412
1417
1418uint8_t
1424
1425uint8_t
1427 Ptr<Packet> dsrP,
1428 Ipv4Address ipv4Address,
1429 Ipv4Address source,
1430 const Ipv4Header& ipv4Header,
1431 uint8_t protocol,
1432 bool& isPromisc,
1433 Ipv4Address promiscSource)
1434{
1435 NS_LOG_FUNCTION(this << packet << dsrP << ipv4Address << source << ipv4Header
1436 << (uint32_t)protocol << isPromisc);
1437 Ptr<Packet> p = packet->Copy();
1438 uint32_t size = p->GetSize();
1439 auto data = new uint8_t[size];
1440 p->CopyData(data, size);
1441 uint8_t errorType = *(data + 2);
1442 /*
1443 * Get the node from Ip address and get the dsr extension object
1444 */
1445 Ptr<Node> node = GetNodeWithAddress(ipv4Address);
1446 Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting>();
1447 NS_LOG_DEBUG("The error type value here " << (uint32_t)errorType);
1448 if (errorType == 1) // unreachable ip address
1449 {
1450 /*
1451 * Remove the route error header from the packet, and get the error type
1452 */
1453 DsrOptionRerrUnreachHeader rerrUnreach;
1454 p->RemoveHeader(rerrUnreach);
1455 /*
1456 * Get the error destination address
1457 */
1458 Ipv4Address unreachAddress = rerrUnreach.GetUnreachNode();
1459 Ipv4Address errorSource = rerrUnreach.GetErrorSrc();
1460
1461 NS_LOG_DEBUG("The error source is " << rerrUnreach.GetErrorDst()
1462 << "and the unreachable node is " << unreachAddress);
1463 /*
1464 * Get the serialized size of the rerr header
1465 */
1466 uint32_t rerrSize = rerrUnreach.GetSerializedSize();
1467 /*
1468 * Delete all the routes including the unreachable node address from the route cache
1469 */
1470 Ptr<Node> node = GetNodeWithAddress(ipv4Address);
1471 dsr->DeleteAllRoutesIncludeLink(errorSource, unreachAddress, ipv4Address);
1472
1473 Ptr<Packet> newP = p->Copy();
1474 uint32_t serialized = DoSendError(newP, rerrUnreach, rerrSize, ipv4Address, protocol);
1475 return serialized;
1476 }
1477 else
1478 {
1479 /*
1480 * Two other type of error headers:
1481 * 1. flow state not supported type-specific information
1482 * 2. unsupported option with option number
1483 *
1484 * Remove the route error header from the packet, and get the error type
1485 */
1486 DsrOptionRerrUnsupportedHeader rerrUnsupported;
1487 p->RemoveHeader(rerrUnsupported);
1488
1489 // TODO This is for the other two error options, not supporting for now
1490 // uint32_t rerrSize = rerrUnsupported.GetSerializedSize();
1491 // uint32_t serialized = DoSendError (p, rerrUnsupported, rerrSize, ipv4Address, protocol);
1492 uint32_t serialized = 0;
1493 return serialized;
1494 }
1495}
1496
1497uint8_t
1500 uint32_t rerrSize,
1501 Ipv4Address ipv4Address,
1502 uint8_t protocol)
1503{
1504 // Get the number of routers' address field
1505 uint8_t buf[2];
1506 p->CopyData(buf, sizeof(buf));
1507 uint8_t numberAddress = (buf[1] - 2) / 4;
1508
1509 // Here remove the source route header and schedule next hop error transmission
1510 NS_LOG_DEBUG("The number of addresses " << (uint32_t)numberAddress);
1511 DsrOptionSRHeader sourceRoute;
1512 sourceRoute.SetNumberAddress(numberAddress);
1513 p->RemoveHeader(sourceRoute);
1514 NS_ASSERT(p->GetSize() == 0);
1515 /*
1516 * Get the node from ip address and the dsr extension object
1517 */
1518 Ptr<Node> node = GetNodeWithAddress(ipv4Address);
1519 Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting>();
1520 /*
1521 * Get the segments left field and the next address
1522 */
1523 uint8_t segmentsLeft = sourceRoute.GetSegmentsLeft();
1524 uint8_t nextAddressIndex;
1525 Ipv4Address nextAddress;
1526 /*
1527 * Get the route size and the error target address
1528 */
1529 std::vector<Ipv4Address> nodeList = sourceRoute.GetNodesAddress();
1530 Ipv4Address targetAddress = nodeList.back();
1531 /*
1532 * The total serialized size for both the rerr and source route headers
1533 */
1534 uint32_t serializedSize = rerrSize + sourceRoute.GetSerializedSize();
1535
1536 if (sourceRoute.GetLength() % 2 != 0)
1537 {
1538 NS_LOG_LOGIC("Malformed header. Drop!");
1539 m_dropTrace(p);
1540 return 0;
1541 }
1542
1543 if (segmentsLeft > numberAddress)
1544 {
1545 NS_LOG_LOGIC("Malformed header. Drop!");
1546 m_dropTrace(p);
1547 return 0;
1548 }
1549 /*
1550 * When the error packet has reached to the destination
1551 */
1552 if (segmentsLeft == 0 && targetAddress == ipv4Address)
1553 {
1554 NS_LOG_INFO("This is the destination of the error, send error request");
1555 dsr->SendErrorRequest(rerr, protocol);
1556 return serializedSize;
1557 }
1558
1559 // Get the next Router Address
1560 DsrOptionSRHeader newSourceRoute;
1561 newSourceRoute.SetSegmentsLeft(segmentsLeft - 1);
1562 nextAddressIndex = numberAddress - segmentsLeft;
1563 nextAddress = sourceRoute.GetNodeAddress(nextAddressIndex);
1564 newSourceRoute.SetSalvage(sourceRoute.GetSalvage());
1565 newSourceRoute.SetNodesAddress(nodeList);
1566 nextAddress = newSourceRoute.GetNodeAddress(nextAddressIndex);
1567
1568 // to test if the next address is multicast or not
1569 if (nextAddress.IsMulticast() || targetAddress.IsMulticast())
1570 {
1571 m_dropTrace(p);
1572 return serializedSize;
1573 }
1574
1575 // Set the route entry
1576 SetRoute(nextAddress, ipv4Address);
1577 dsr->ForwardErrPacket(rerr, newSourceRoute, nextAddress, m_ipv4Route);
1578 return serializedSize;
1579}
1580
1582
1583TypeId
1585{
1586 static TypeId tid = TypeId("ns3::dsr::DsrOptionAckReq")
1588 .SetGroupName("Dsr")
1589 .AddConstructor<DsrOptionAckReq>();
1590 return tid;
1591}
1592
1597
1602
1603uint8_t
1609
1610uint8_t
1612 Ptr<Packet> dsrP,
1613 Ipv4Address ipv4Address,
1614 Ipv4Address source,
1615 const Ipv4Header& ipv4Header,
1616 uint8_t protocol,
1617 bool& isPromisc,
1618 Ipv4Address promiscSource)
1619{
1620 NS_LOG_FUNCTION(this << packet << dsrP << ipv4Address << source << ipv4Header
1621 << (uint32_t)protocol << isPromisc);
1622 /*
1623 * Current implementation of the ack request header processing is coded in source route header
1624 * processing
1625 *
1626 * Remove the ack request header
1627 */
1628 Ptr<Packet> p = packet->Copy();
1629 DsrOptionAckReqHeader ackReq;
1630 p->RemoveHeader(ackReq);
1631 /*
1632 * Get the node with ip address and get the dsr extension and route cache objects
1633 */
1634 Ptr<Node> node = GetNodeWithAddress(ipv4Address);
1635 Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting>();
1636
1637 NS_LOG_DEBUG("The next header value " << (uint32_t)protocol);
1638
1639 return ackReq.GetSerializedSize();
1640}
1641
1643
1644TypeId
1646{
1647 static TypeId tid = TypeId("ns3::dsr::DsrOptionAck")
1649 .SetGroupName("Dsr")
1650 .AddConstructor<DsrOptionAck>();
1651 return tid;
1652}
1653
1658
1663
1664uint8_t
1666{
1668 return OPT_NUMBER;
1669}
1670
1671uint8_t
1673 Ptr<Packet> dsrP,
1674 Ipv4Address ipv4Address,
1675 Ipv4Address source,
1676 const Ipv4Header& ipv4Header,
1677 uint8_t protocol,
1678 bool& isPromisc,
1679 Ipv4Address promiscSource)
1680{
1681 NS_LOG_FUNCTION(this << packet << dsrP << ipv4Address << source << ipv4Header
1682 << (uint32_t)protocol << isPromisc);
1683 /*
1684 * Remove the ACK header
1685 */
1686 Ptr<Packet> p = packet->Copy();
1688 p->RemoveHeader(ack);
1689 /*
1690 * Get the ACK source and destination address
1691 */
1692 Ipv4Address realSrc = ack.GetRealSrc();
1693 Ipv4Address realDst = ack.GetRealDst();
1694 uint16_t ackId = ack.GetAckId();
1695 /*
1696 * Get the node with ip address and get the dsr extension and route cache objects
1697 */
1698 Ptr<Node> node = GetNodeWithAddress(ipv4Address);
1699 Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting>();
1700 dsr->UpdateRouteEntry(realDst);
1701 /*
1702 * Cancel the packet retransmit timer when receiving the ack packet
1703 */
1704 dsr->CallCancelPacketTimer(ackId, ipv4Header, realSrc, realDst);
1705 return ack.GetSerializedSize();
1706}
1707
1708} // namespace dsr
1709} // namespace ns3
Ipv4 addresses are stored in host order in this class.
bool IsMulticast() const
Packet header for IPv4.
Definition ipv4-header.h:23
Ipv4Address GetSource() const
uint16_t GetIdentification() const
Ipv4Address GetDestination() const
uint16_t GetFragmentOffset() const
uint8_t GetTtl() const
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
static uint32_t GetNNodes()
Definition node-list.cc:247
static Ptr< Node > GetNode(uint32_t n)
Definition node-list.cc:240
Object()
Caller graph was not generated because of its size.
Definition object.cc:93
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
This class implements a tag that carries the socket-specific TTL of a packet to the IP layer.
Definition socket.h:1114
void SetTtl(uint8_t ttl)
Set the tag's TTL.
Definition socket.cc:593
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1003
Hold an unsigned integer type.
Definition uinteger.h:34
Header of Dsr Option ack.
uint16_t GetAckId() const
Set the Ack id number.
Ipv4Address GetRealDst() const
Get Error source ip address.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Ipv4Address GetRealSrc() const
Get Error source ip address.
Dsr Option Ack.
uint8_t Process(Ptr< Packet > packet, Ptr< Packet > dsrP, Ipv4Address ipv4Address, Ipv4Address source, const Ipv4Header &ipv4Header, uint8_t protocol, bool &isPromisc, Ipv4Address promiscSource) override
Process method.
static const uint8_t OPT_NUMBER
The Dsr Ack option number.
static TypeId GetTypeId()
Get the type ID.
uint8_t GetOptionNumber() const override
Get the option number.
Header of Dsr Option ack request.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
uint16_t GetAckId() const
Set the Ack request id number.
static TypeId GetTypeId()
Get the type ID.
uint8_t GetOptionNumber() const override
Get the option number.
uint8_t Process(Ptr< Packet > packet, Ptr< Packet > dsrP, Ipv4Address ipv4Address, Ipv4Address source, const Ipv4Header &ipv4Header, uint8_t protocol, bool &isPromisc, Ipv4Address promiscSource) override
Process method.
static const uint8_t OPT_NUMBER
Dsr ack request option number.
uint8_t GetLength() const
Get the option length.
Header of Dsr Option Pad1.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Dsr Option Pad1.
uint8_t GetOptionNumber() const override
Get the option number.
static TypeId GetTypeId()
Get the type ID.
uint8_t Process(Ptr< Packet > packet, Ptr< Packet > dsrP, Ipv4Address ipv4Address, Ipv4Address source, const Ipv4Header &ipv4Header, uint8_t protocol, bool &isPromisc, Ipv4Address promiscSource) override
Process method.
static const uint8_t OPT_NUMBER
Pad1 option number.
Header of Dsr Option Padn.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
IPv4 Option Padn.
static const uint8_t OPT_NUMBER
PadN option number.
uint8_t Process(Ptr< Packet > packet, Ptr< Packet > dsrP, Ipv4Address ipv4Address, Ipv4Address source, const Ipv4Header &ipv4Header, uint8_t protocol, bool &isPromisc, Ipv4Address promiscSource) override
Process method.
static TypeId GetTypeId()
Get the type ID.
uint8_t GetOptionNumber() const override
Get the option number.
void SetErrorType(uint8_t errorType)
Set the route error type.
Dsr Option Route Error.
uint8_t DoSendError(Ptr< Packet > p, DsrOptionRerrUnreachHeader &rerr, uint32_t rerrSize, Ipv4Address ipv4Address, uint8_t protocol)
Do Send error message.
static const uint8_t OPT_NUMBER
Dsr Route Error option number.
static TypeId GetTypeId()
Get the type ID.
uint8_t Process(Ptr< Packet > packet, Ptr< Packet > dsrP, Ipv4Address ipv4Address, Ipv4Address source, const Ipv4Header &ipv4Header, uint8_t protocol, bool &isPromisc, Ipv4Address promiscSource) override
Process method.
uint8_t GetOptionNumber() const override
Get the option number.
Route Error (RERR) Unreachable node address option Message Format.
Ipv4Address GetErrorSrc() const override
Get the route error source address.
void SetErrorDst(Ipv4Address errorDstAddress) override
Set the error destination ip address.
void SetErrorSrc(Ipv4Address errorSrcAddress) override
Set the route error source address.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
uint8_t GetSalvage() const override
Get the salvage value of the packet.
void SetUnreachNode(Ipv4Address unreachNode)
Set the unreachable node ip address.
void SetSalvage(uint8_t salvage) override
Set the salvage value of the packet.
Ipv4Address GetUnreachNode() const
Get the unreachable node ip address.
Ipv4Address GetErrorDst() const override
Get the error destination ip address.
Route Error (RERR) Unsupported option Message Format.
Header of Dsr Option Route Reply.
void SetNumberAddress(uint8_t n)
Set the number of ipv4 address.
std::vector< Ipv4Address > GetNodesAddress() const
Get the vector of ipv4 address.
void SetNodesAddress(std::vector< Ipv4Address > ipv4Address)
Set the vector of ipv4 address.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Dsr Option Route Reply.
static const uint8_t OPT_NUMBER
Router alert option number.
static TypeId GetTypeId()
Get the type ID.
uint8_t GetOptionNumber() const override
Get the option number.
uint8_t Process(Ptr< Packet > packet, Ptr< Packet > dsrP, Ipv4Address ipv4Address, Ipv4Address source, const Ipv4Header &ipv4Header, uint8_t protocol, bool &isPromisc, Ipv4Address promiscSource) override
Process method.
Header of Dsr Option Route Request.
void SetNodesAddress(std::vector< Ipv4Address > ipv4Address)
Set the vector of ipv4 address.
void SetNumberAddress(uint8_t n)
Set the number of ipv4 address.
Ipv4Address GetTarget()
Get the target ipv4 address.
std::vector< Ipv4Address > GetNodesAddresses() const
Get the vector of ipv4 address.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
uint16_t GetId() const
Set the request id number.
Dsr Option Rreq.
static TypeId GetTypeId()
Get the type ID.
static const uint8_t OPT_NUMBER
Rreq option number.
uint8_t Process(Ptr< Packet > packet, Ptr< Packet > dsrP, Ipv4Address ipv4Address, Ipv4Address source, const Ipv4Header &ipv4Header, uint8_t protocol, bool &isPromisc, Ipv4Address promiscSource) override
Process method.
uint8_t GetOptionNumber() const override
Get the option number.
~DsrOptionRreq() override
Destructor.
DsrOptionRreq()
Constructor.
Header of Dsr Option Source Route.
Ipv4Address GetNodeAddress(uint8_t index) const
Get a Node IPv4 Address.
std::vector< Ipv4Address > GetNodesAddress() const
Get the vector of ipv4 address.
void SetNumberAddress(uint8_t n)
Set the number of ipv4 address.
void SetSalvage(uint8_t salvage)
Set the salvage value for a packet.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
void SetSegmentsLeft(uint8_t segmentsLeft)
Set the number of segments left to send.
uint8_t GetSalvage() const
Get the salvage value for a packet.
void SetNodesAddress(std::vector< Ipv4Address > ipv4Address)
Set the vector of ipv4 address.
uint8_t GetSegmentsLeft() const
Get the number of segments left to send.
Dsr Option Source Route.
static const uint8_t OPT_NUMBER
Source Route option number.
uint8_t Process(Ptr< Packet > packet, Ptr< Packet > dsrP, Ipv4Address ipv4Address, Ipv4Address source, const Ipv4Header &ipv4Header, uint8_t protocol, bool &isPromisc, Ipv4Address promiscSource) override
Process method.
uint8_t GetOptionNumber() const override
Get the option number.
static TypeId GetTypeId()
Get the type ID.
Ipv4Address SearchNextHop(Ipv4Address ipv4Address, std::vector< Ipv4Address > &vec)
Search for the next hop in the route.
Ipv4Address ReverseSearchNextTwoHop(Ipv4Address ipv4Address, std::vector< Ipv4Address > &vec)
Reverse search for the next two hop in the route.
TracedCallback< Ptr< const Packet > > m_dropTrace
Drop trace callback.
Ptr< Node > GetNodeWithAddress(Ipv4Address ipv4Address)
Get the node object with Ipv4Address.
DsrOptions()
Constructor.
Ptr< Node > GetNode() const
Get the node.
Time ActiveRouteTimeout
The active route timeout value.
bool CheckDuplicates(Ipv4Address ipv4Address, std::vector< Ipv4Address > &vec)
Check if the route already contains the node ip address.
void SetNode(Ptr< Node > node)
Set the node.
virtual uint8_t GetOptionNumber() const =0
Get the option number.
~DsrOptions() override
Destructor.
std::vector< Ipv4Address > m_finalRoute
The vector of final Ipv4 address.
void PrintVector(std::vector< Ipv4Address > &vec)
Print out the elements in the route vector.
bool IfDuplicates(std::vector< Ipv4Address > &vec, std::vector< Ipv4Address > &vec2)
Check if the two vectors contain duplicate or not.
bool ReverseRoutes(std::vector< Ipv4Address > &vec)
Reverse the routes.
void RemoveDuplicates(std::vector< Ipv4Address > &vec)
Remove the duplicates from the route.
static TypeId GetTypeId()
Get the type identificator.
std::vector< Ipv4Address > CutRoute(Ipv4Address ipv4Address, std::vector< Ipv4Address > &nodeList)
Cut the route from ipv4Address to the end of the route vector.
bool ContainAddressAfter(Ipv4Address ipv4Address, Ipv4Address destAddress, std::vector< Ipv4Address > &nodeList)
Search for the ipv4 address in the node list.
TracedCallback< const DsrOptionSRHeader & > m_rxPacketTrace
The receive trace back, only triggered when final destination receive data packet.
Ipv4Address ReverseSearchNextHop(Ipv4Address ipv4Address, std::vector< Ipv4Address > &vec)
Reverse search for the next hop in the route.
Ptr< Ipv4Route > m_ipv4Route
The ipv4 route.
virtual Ptr< Ipv4Route > SetRoute(Ipv4Address nextHop, Ipv4Address srcAddress)
Set the route to use for data packets, used by the option headers when sending data/control packets.
Ptr< Node > m_node
the node
DsrRouteCacheEntry class for entries in the route cache.
Definition dsr-rcache.h:218
IP_VECTOR GetVector() const
Get the IP vector.
Definition dsr-rcache.h:298
std::vector< Ipv4Address > IP_VECTOR
Define the vector to hold Ip address.
Definition dsr-rcache.h:220
Dsr fixed size header Format.
void SetNextHeader(uint8_t protocol)
Set the "Next header" field.
void AddDsrOption(const DsrOptionHeader &option)
Serialize the option, prepending pad1 or padn option as necessary.
Dsr Routing base.
Definition dsr-routing.h:98
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:267
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:281
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:274
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:492
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
constexpr uint8_t NO_NEXT_HEADER
No Next Header for IPV4 and IPv6 [RFC8200].
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35
uint8_t data[writeSize]