A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
dsr-options.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 Yufei Cheng
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Yufei Cheng <yfcheng@ittc.ku.edu>
19  *
20  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
21  * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
22  * Information and Telecommunication Technology Center (ITTC)
23  * and Department of Electrical Engineering and Computer Science
24  * The University of Kansas Lawrence, KS USA.
25  *
26  * Work supported in part by NSF FIND (Future Internet Design) Program
27  * under grant CNS-0626918 (Postmodern Internet Architecture),
28  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
29  * US Department of Defense (DoD), and ITTC at The University of Kansas.
30  */
31 
32 #define NS_LOG_APPEND_CONTEXT \
33  if (GetObject<Node> ()) { std::clog << "[node " << GetObject<Node> ()->GetId () << "] "; }
34 
35 #include <list>
36 #include <ctime>
37 #include <map>
38 
39 #include "ns3/ptr.h"
40 #include "ns3/log.h"
41 #include "ns3/assert.h"
42 #include "ns3/fatal-error.h"
43 #include "ns3/node.h"
44 #include "ns3/uinteger.h"
45 #include "ns3/trace-source-accessor.h"
46 #include "ns3/udp-header.h"
47 #include "ns3/pointer.h"
48 #include "ns3/node-list.h"
49 #include "ns3/uinteger.h"
50 #include "ns3/object-vector.h"
51 #include "ns3/ipv4-l3-protocol.h"
52 #include "ns3/ipv4-interface.h"
53 #include "ns3/ipv4-header.h"
54 #include "ns3/ipv4-address.h"
55 #include "ns3/ipv4-route.h"
56 #include "ns3/icmpv4-l4-protocol.h"
57 #include "ns3/ip-l4-protocol.h"
58 
59 #include "dsr-option-header.h"
60 #include "dsr-options.h"
61 #include "dsr-rcache.h"
62 
63 NS_LOG_COMPONENT_DEFINE ("DsrOptions");
64 
65 namespace ns3 {
66 namespace dsr {
67 
68 NS_OBJECT_ENSURE_REGISTERED (DsrOptions);
69 
71 {
72  static TypeId tid = TypeId ("ns3::dsr::DsrOptions")
73  .SetParent<Object> ()
74  .AddAttribute ("OptionNumber", "The Dsr option number.",
75  UintegerValue (0),
76  MakeUintegerAccessor (&DsrOptions::GetOptionNumber),
77  MakeUintegerChecker<uint8_t> ())
78  .AddTraceSource ("Rx", "Receive DSR packet.",
80  ;
81  return tid;
82 }
83 
85 {
87 }
88 
90 {
92 }
93 
95 {
96  NS_LOG_FUNCTION (this << node);
97  m_node = node;
98 }
99 
101 {
103  return m_node;
104 }
105 
106 bool DsrOptions::ContainAddressAfter (Ipv4Address ipv4Address, Ipv4Address destAddress, std::vector<Ipv4Address> &nodeList)
107 {
108  NS_LOG_FUNCTION (this << ipv4Address << destAddress);
109  std::vector<Ipv4Address>::iterator it = find (nodeList.begin (), nodeList.end (), destAddress);
110 
111  for (std::vector<Ipv4Address>::iterator i = it; i != nodeList.end (); ++i)
112  {
113  if ((ipv4Address == (*i)) && ((*i) != nodeList.back ()))
114  {
115  return true;
116  }
117  }
118  return false;
119 }
120 
121 std::vector<Ipv4Address>
122 DsrOptions::CutRoute (Ipv4Address ipv4Address, std::vector<Ipv4Address> &nodeList)
123 {
124  NS_LOG_FUNCTION (this << ipv4Address);
125  std::vector<Ipv4Address>::iterator it = find (nodeList.begin (), nodeList.end (), ipv4Address);
126  std::vector<Ipv4Address> cutRoute;
127  for (std::vector<Ipv4Address>::iterator i = it; i != nodeList.end (); ++i)
128  {
129  cutRoute.push_back (*i);
130  }
131  return cutRoute;
132 }
133 
135 {
136  NS_LOG_FUNCTION (this << nextHop << srcAddress);
137  m_ipv4Route = Create<Ipv4Route> ();
138  m_ipv4Route->SetDestination (nextHop);
139  m_ipv4Route->SetGateway (nextHop);
140  m_ipv4Route->SetSource (srcAddress);
141  return m_ipv4Route;
142 }
143 
144 bool DsrOptions::ReverseRoutes (std::vector<Ipv4Address> & vec)
145 {
146  NS_LOG_FUNCTION (this);
147  std::vector<Ipv4Address> vec2 (vec);
148  vec.clear (); // To ensure vec is empty before start
149  for (std::vector<Ipv4Address>::reverse_iterator ri = vec2.rbegin (); ri
150  != vec2.rend (); ++ri)
151  {
152  vec.push_back (*ri);
153  }
154 
155  if ((vec.size () == vec2.size ()) && (vec.front () == vec2.back ()))
156  {
157  return true;
158  }
159  return false;
160 }
161 
162 Ipv4Address DsrOptions::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  else
174  {
175  if (ipv4Address == vec.back ())
176  {
177  NS_LOG_DEBUG ("We have reached to the final destination " << ipv4Address << " " << vec.back ());
178  return ipv4Address;
179  }
180  for (std::vector<Ipv4Address>::const_iterator 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 
194 Ipv4Address DsrOptions::ReverseSearchNextHop (Ipv4Address ipv4Address, std::vector<Ipv4Address>& vec)
195 {
196  NS_LOG_FUNCTION (this << ipv4Address);
197  Ipv4Address nextHop;
198  if (vec.size () == 2)
199  {
200  NS_LOG_DEBUG ("The two nodes are neighbors");
201  nextHop = vec[0];
202  return nextHop;
203  }
204  else
205  {
206  for (std::vector<Ipv4Address>::reverse_iterator 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 
220 void DsrOptions::PrintVector (std::vector<Ipv4Address>& vec)
221 {
222  NS_LOG_FUNCTION (this);
223  /*
224  * Check elements in a route vector
225  */
226  if (!vec.size ())
227  {
228  NS_LOG_DEBUG ("The vector is empty");
229  }
230  else
231  {
232  NS_LOG_DEBUG ("Print all the elements in a vector");
233  for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
234  {
235  NS_LOG_DEBUG ("The ip address " << *i);
236  }
237  }
238 }
239 
240 bool DsrOptions::IfDuplicates (std::vector<Ipv4Address>& vec, std::vector<Ipv4Address>& vec2)
241 {
242  NS_LOG_FUNCTION (this);
243  NS_LOG_DEBUG ("The first vector ");
244  PrintVector (vec);
245  NS_LOG_DEBUG ("The second vector ");
246  PrintVector (vec2);
247  for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
248  {
249  for (std::vector<Ipv4Address>::const_iterator j = vec2.begin (); j != vec2.end (); ++j)
250  {
251  if ((*i) == (*j))
252  {
253  return true;
254  }
255  else
256  {
257  continue;
258  }
259  }
260  }
261  return false;
262 }
263 
264 bool DsrOptions::CheckDuplicates (Ipv4Address ipv4Address, std::vector<Ipv4Address>& vec)
265 {
266  NS_LOG_FUNCTION (this << ipv4Address);
267  for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
268  {
269  if ((*i) == ipv4Address)
270  {
271  return true;
272  }
273  else
274  {
275  continue;
276  }
277  }
278  return false;
279 }
280 
281 void DsrOptions::RemoveDuplicates (std::vector<Ipv4Address>& vec)
282 {
283  NS_LOG_FUNCTION (this);
284  //Remove duplicate ip address from the route if any, should not happen with normal behavior nodes
285  std::vector<Ipv4Address> vec2 (vec); // declare vec2 as a copy of the vec
286  PrintVector (vec2); // Print all the ip address in the route
287  vec.clear (); // clear vec
288  for (std::vector<Ipv4Address>::const_iterator i = vec2.begin (); i != vec2.end (); ++i)
289  {
290  if (vec.empty ())
291  {
292  vec.push_back (*i);
293  continue;
294  }
295  else
296  {
297  for (std::vector<Ipv4Address>::iterator j = vec.begin (); j != vec.end (); ++j)
298  {
299  if ((*i) == (*j))
300  {
301  if ((j + 1) != vec.end ())
302  {
303  vec.erase (j + 1, vec.end ()); // Automatic shorten the route
304  break;
305  }
306  else
307  {
308  break;
309  }
310  }
311  else if (j == (vec.end () - 1))
312  {
313  vec.push_back (*i);
314  break;
315  }
316  else
317  {
318  continue;
319  }
320  }
321  }
322  }
323 }
324 
325 uint32_t
327 {
328  NS_LOG_FUNCTION (this << address);
329  int32_t nNodes = NodeList::GetNNodes ();
330  for (int32_t i = 0; i < nNodes; ++i)
331  {
332  Ptr<Node> node = NodeList::GetNode (i);
333  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
334  if (ipv4->GetAddress (1, 0).GetLocal () == address)
335  {
336  return i;
337  }
338  }
339  return 255;
340 }
341 
343 {
344  NS_LOG_FUNCTION (this << ipv4Address);
345  int32_t nNodes = NodeList::GetNNodes ();
346  for (int32_t i = 0; i < nNodes; ++i)
347  {
348  Ptr<Node> node = NodeList::GetNode (i);
349  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
350  int32_t ifIndex = ipv4->GetInterfaceForAddress (ipv4Address);
351  if (ifIndex != -1)
352  {
353  return node;
354  }
355  }
356  return 0;
357 }
358 
360 
362 {
363  static TypeId tid = TypeId ("ns3::dsr::DsrOptionPad1")
364  .SetParent<DsrOptions> ()
365  .AddConstructor<DsrOptionPad1> ()
366  ;
367  return tid;
368 }
369 
371 {
373 }
374 
376 {
378 }
379 
381 {
383 
384  return OPT_NUMBER;
385 }
386 
387 uint8_t DsrOptionPad1::Process (Ptr<Packet> packet, Ptr<Packet> dsrP, Ipv4Address ipv4Address, Ipv4Address source, Ipv4Header const& ipv4Header, uint8_t protocol, bool& isPromisc)
388 {
389  NS_LOG_FUNCTION (this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
390  Ptr<Packet> p = packet->Copy ();
391  DsrOptionPad1Header pad1Header;
392  p->RemoveHeader (pad1Header);
393 
394  isPromisc = false;
395 
396  return pad1Header.GetSerializedSize ();
397 }
398 
400 
402 {
403  static TypeId tid = TypeId ("ns3::dsr::DsrOptionPadn")
404  .SetParent<DsrOptions> ()
405  .AddConstructor<DsrOptionPadn> ()
406  ;
407  return tid;
408 }
409 
411 {
413 }
414 
416 {
418 }
419 
421 {
423  return OPT_NUMBER;
424 }
425 
426 uint8_t DsrOptionPadn::Process (Ptr<Packet> packet, Ptr<Packet> dsrP, Ipv4Address ipv4Address, Ipv4Address source, Ipv4Header const& ipv4Header, uint8_t protocol, bool& isPromisc)
427 {
428  NS_LOG_FUNCTION (this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
429 
430  Ptr<Packet> p = packet->Copy ();
431  DsrOptionPadnHeader padnHeader;
432  p->RemoveHeader (padnHeader);
433 
434  isPromisc = false;
435 
436  return padnHeader.GetSerializedSize ();
437 }
438 
440 
442 {
443  static TypeId tid = TypeId ("ns3::dsr::DsrOptionRreq")
444  .SetParent<DsrOptions> ()
445  .AddConstructor<DsrOptionRreq> ()
446  ;
447  return tid;
448 }
449 
451 {
452  return GetTypeId ();
453 }
454 
456 {
458 }
459 
461 {
463 }
464 
466 {
468 
469  return OPT_NUMBER;
470 }
471 
472 uint8_t DsrOptionRreq::Process (Ptr<Packet> packet, Ptr<Packet> dsrP, Ipv4Address ipv4Address, Ipv4Address source, Ipv4Header const& ipv4Header, uint8_t protocol, bool& isPromisc)
473 {
474  NS_LOG_FUNCTION (this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
475  // Fields from IP header
476  Ipv4Address srcAddress = ipv4Header.GetSource ();
477  /*
478  * \ when the ip source address is equal to the address of our own, this is request packet originated
479  * \ by the node itself, discard it
480  */
481  if (srcAddress == ipv4Address)
482  {
483  NS_LOG_DEBUG ("Discard the packet");
484  m_dropTrace (packet); // call the drop trace to show in the tracing
485  return 0;
486  }
487  /*
488  * Get the node associated with the ipv4 address and get several objects from the node and leave for further use
489  */
490  Ptr<Node> node = GetNodeWithAddress (ipv4Address);
492 
493  Ptr<Packet> p = packet->Copy (); // The packet here doesn't contain the fixed size dsr header
494  /*
495  * \brief Get the number of routers' address field before removing the header
496  * \peek the packet and get the value
497  */
498  uint8_t buf[2];
499  p->CopyData (buf, sizeof(buf));
500  uint8_t numberAddress = (buf[1] - 6) / 4;
501  NS_LOG_DEBUG ("The number of Ip addresses " << (uint32_t)numberAddress);
502  if (numberAddress >= 255)
503  {
504  NS_LOG_DEBUG ("Discard the packet, malformed header since two many ip addresses in route");
505  m_dropTrace (packet); // call the drop trace to show in the tracing
506  return 0;
507  }
508 
509  /*
510  * Create the dsr rreq header
511  */
512  DsrOptionRreqHeader rreq;
513  /*
514  * Set the number of addresses with the value from peek data and remove the rreq header
515  */
516  rreq.SetNumberAddress (numberAddress);
517  // Remove the route request header
518  p->RemoveHeader (rreq);
519  // Verify the option length
520  uint8_t length = rreq.GetLength ();
521  if (length % 2 != 0)
522  {
523  NS_LOG_LOGIC ("Malformed header. Drop!");
524  m_dropTrace (packet); // call drop trace
525  return 0;
526  }
527  // The target address is where we want to send the data packets
528  Ipv4Address targetAddress = rreq.GetTarget ();
529  // Get the node list and source address from the route request header
530  std::vector<Ipv4Address> mainVector = rreq.GetNodesAddresses ();
531  std::vector<Ipv4Address> nodeList (mainVector);
532  PrintVector (nodeList);
533  /*
534  * Construct the dsr routing header for future use
535  */
536  DsrRoutingHeader dsrRoutingHeader;
537  dsrRoutingHeader.SetNextHeader (protocol);
538  dsrRoutingHeader.SetMessageType (1);
539  dsrRoutingHeader.SetSourceId (GetIDfromIP (source));
540  dsrRoutingHeader.SetDestId (255);
541 
542  if (CheckDuplicates (ipv4Address, nodeList))
543  {
544  /*
545  * if the route contains the node address already, drop the request packet
546  */
547  m_dropTrace (packet); // call drop trace
548  NS_LOG_DEBUG ("Our node address is already seen in the route, drop the request");
549  return 0;
550  }
551  else
552  {
553  // A node ignores all RREQs received from any node in its blacklist
554  RouteCacheEntry toPrev;
555  /*
556  * When the reverse route is created or updated, the following actions on the route are also carried out:
557  * 3. the next hop in the routing table becomes the node from which the RREQ was received
558  * 4. the hop count is copied from the Hop Count in the RREQ message;
559  */
560 
561  // A node generates a RREP if either:
562  // (i) it is itself the destination,
563  /*
564  * The target address equal to our own ip address
565  */
566  NS_LOG_DEBUG ("The target address over here " << targetAddress << " and the ip address " << ipv4Address << " and the source address " << mainVector[0]);
567  if (targetAddress == ipv4Address)
568  {
569  Ipv4Address nextHop; // Declare the next hop address to use
570  if (nodeList.size () == 1)
571  {
572  NS_LOG_DEBUG ("These two nodes are neighbors");
573  m_finalRoute.clear ();
574  m_finalRoute.push_back (srcAddress); // push back the request originator's address
575  m_finalRoute.push_back (ipv4Address); // push back our own address
576  nextHop = srcAddress;
577  }
578  else
579  {
580  std::vector<Ipv4Address> changeRoute (nodeList);
581  changeRoute.push_back (ipv4Address); // push back our own address
582  m_finalRoute.clear (); // get a clear route vector
583  for (std::vector<Ipv4Address>::iterator i = changeRoute.begin (); i != changeRoute.end (); ++i)
584  {
585  m_finalRoute.push_back (*i); // Get the full route from source to destination
586  }
588  nextHop = ReverseSearchNextHop (ipv4Address, m_finalRoute); // get the next hop
589  }
590 
591  DsrOptionRrepHeader rrep;
592  rrep.SetNodesAddress (m_finalRoute); // Set the node addresses in the route reply header
593  NS_LOG_DEBUG ("The nextHop address " << nextHop);
594  Ipv4Address replyDst = m_finalRoute.front ();
595  /*
596  * This part add dsr header to the packet and send route reply packet
597  */
598  DsrRoutingHeader dsrRoutingHeader;
599  dsrRoutingHeader.SetNextHeader (protocol);
600  dsrRoutingHeader.SetMessageType (1);
601  dsrRoutingHeader.SetSourceId (GetIDfromIP (ipv4Address));
602  dsrRoutingHeader.SetDestId (GetIDfromIP (replyDst));
603  // Set the route for route reply
604  SetRoute (nextHop, ipv4Address);
605 
606  uint8_t length = rrep.GetLength (); // Get the length of the rrep header excluding the type header
607  dsrRoutingHeader.SetPayloadLength (length + 2);
608  dsrRoutingHeader.AddDsrOption (rrep);
609  Ptr<Packet> newPacket = Create<Packet> ();
610  newPacket->AddHeader (dsrRoutingHeader);
611  dsr->ScheduleInitialReply (newPacket, ipv4Address, nextHop, m_ipv4Route);
612  /*
613  * Create the route entry to the rreq originator and save it to route cache, also need to reverse the route
614  */
617  {
619  Ipv4Address dst = m_finalRoute.back ();
620  bool addRoute = false;
621  if (numberAddress > 0)
622  {
623  RouteCacheEntry toSource (/*IP_VECTOR=*/ m_finalRoute, /*dst=*/
624  dst, /*expire time=*/ ActiveRouteTimeout);
625  if (dsr->IsLinkCache ())
626  {
627  addRoute = dsr->AddRoute_Link (m_finalRoute, ipv4Address);
628  }
629  else
630  {
631  addRoute = dsr->AddRoute (toSource);
632  }
633  }
634  else
635  {
636  NS_LOG_DEBUG ("Abnormal RouteRequest");
637  return 0;
638  }
639 
640  if (addRoute)
641  {
642  /*
643  * Found a route to the dst, construct the source route option header
644  */
645  DsrOptionSRHeader sourceRoute;
646  NS_LOG_DEBUG ("The route length " << m_finalRoute.size ());
647  sourceRoute.SetNodesAddress (m_finalRoute);
648  if (dsr->IsLinkCache ())
649  {
650  dsr->UseExtends (m_finalRoute);
651  }
652  sourceRoute.SetSegmentsLeft ((m_finalRoute.size () - 2));
653  // The salvage value here is 0
654  sourceRoute.SetSalvage (0);
655  Ipv4Address nextHop = SearchNextHop (ipv4Address, m_finalRoute); // Get the next hop address
656  NS_LOG_DEBUG ("The nextHop address " << nextHop);
657 
658  if (nextHop == "0.0.0.0")
659  {
660  dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
661  return 0;
662  }
663  SetRoute (nextHop, ipv4Address);
664  /*
665  * Send the data packet from the send buffer
666  */
667  dsr->SendPacketFromBuffer (sourceRoute, nextHop, protocol);
668  // Cancel the route request timer for destination after sending the data packet
669  dsr->CancelRreqTimer (dst, true);
670  }
671  else
672  {
673  NS_LOG_DEBUG ("The route is failed to add in cache");
674  return 0;
675  }
676  }
677  else
678  {
679  NS_LOG_DEBUG ("Unable to reverse route");
680  return 0;
681  }
682  isPromisc = false;
683  return rreq.GetSerializedSize ();
684  }
685 
686  /*
687  * (ii) or it has an active route to the destination, send reply based on request header and route cache,
688  * need to delay based on a random value from d = H * (h - 1 + r), which can avoid possible route
689  * reply storm.
690  */
691  else if (dsr->LookupRoute (targetAddress, toPrev))
692  {
693  RouteCacheEntry::IP_VECTOR ip = toPrev.GetVector (); // The route from our own route cache to dst
694  PrintVector (ip);
695  std::vector<Ipv4Address> saveRoute (nodeList);
696  PrintVector (saveRoute);
697  // Verify if the two vector contains duplicates, if so, do not use
698  // the route found and forward the route request
699  if (!(IfDuplicates (ip, saveRoute)))
700  {
701  m_finalRoute.clear (); // Clear the final route vector
705  for (std::vector<Ipv4Address>::iterator i = saveRoute.begin (); i != saveRoute.end (); ++i)
706  {
707  m_finalRoute.push_back (*i);
708  }
712  for (std::vector<Ipv4Address>::iterator j = ip.begin (); j != ip.end (); ++j)
713  {
714  m_finalRoute.push_back (*j);
715  }
716  /*
717  * Create the route entry to the rreq originator and save it to route cache, also need to reverse the route
718  */
719  bool addRoute = false;
720  std::vector<Ipv4Address> reverseRoute (m_finalRoute);
721 
722  if (ReverseRoutes (reverseRoute))
723  {
724  saveRoute.push_back (ipv4Address);
725  ReverseRoutes (saveRoute);
726  Ipv4Address dst = saveRoute.back ();
727  NS_LOG_DEBUG ("This is the route save in route cache");
728  PrintVector (saveRoute);
729 
730  RouteCacheEntry toSource (/*IP_VECTOR=*/ saveRoute, /*dst=*/ dst, /*expire time=*/ ActiveRouteTimeout);
731  NS_ASSERT (saveRoute.front () == ipv4Address);
732  // Add the route entry in the route cache
733  if (dsr->IsLinkCache ())
734  {
735  addRoute = dsr->AddRoute_Link (saveRoute, ipv4Address);
736  }
737  else
738  {
739  addRoute = dsr->AddRoute (toSource);
740  }
741 
742  if (addRoute)
743  {
744  NS_LOG_DEBUG ("We have added the route and search send buffer for packet with destination " << dst);
745  /*
746  * Found a route the dst, construct the source route option header
747  */
748  DsrOptionSRHeader sourceRoute;
749  PrintVector (saveRoute);
750 
751  sourceRoute.SetNodesAddress (saveRoute);
752  if (dsr->IsLinkCache ())
753  {
754  dsr->UseExtends (saveRoute);
755  }
756  sourceRoute.SetSegmentsLeft ((saveRoute.size () - 2));
757  uint8_t salvage = 0;
758  sourceRoute.SetSalvage (salvage);
759  Ipv4Address nextHop = SearchNextHop (ipv4Address, saveRoute); // Get the next hop address
760  NS_LOG_DEBUG ("The nextHop address " << nextHop);
761 
762  if (nextHop == "0.0.0.0")
763  {
764  dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
765  return 0;
766  }
767  SetRoute (nextHop, ipv4Address);
768  /*
769  * Schedule the packet retry
770  */
771  dsr->SendPacketFromBuffer (sourceRoute, nextHop, protocol);
772  // Cancel the route request timer for destination
773  dsr->CancelRreqTimer (dst, true);
774  }
775  else
776  {
777  NS_LOG_DEBUG ("The route is failed to add in cache");
778  return 0;
779  }
780  }
781  else
782  {
783  NS_LOG_DEBUG ("Unable to reverse the route");
784  return 0;
785  }
786 
787  /*
788  * Need to first pin down the next hop address before removing duplicates
789  */
790  Ipv4Address nextHop = ReverseSearchNextHop (ipv4Address, m_finalRoute);
791  NS_LOG_DEBUG ("The nextHop address " << nextHop);
792  /*
793  * First remove the duplicate ip address to automatically shorten the route, and then reversely
794  * search the next hop address
795  */
796  // Set the route
797  SetRoute (nextHop, ipv4Address);
798 
799  uint16_t hops = m_finalRoute.size ();
800  DsrOptionRrepHeader rrep;
801  rrep.SetNodesAddress (m_finalRoute); // Set the node addresses in the route reply header
802  // Get the real source of the reply
803  Ipv4Address realSource = m_finalRoute.back ();
805  NS_LOG_DEBUG ("This is the full route from " << realSource << " to " << m_finalRoute.front ());
806  /*
807  * This part add dsr header to the packet and send route reply packet
808  */
809  DsrRoutingHeader dsrRoutingHeader;
810  dsrRoutingHeader.SetNextHeader (protocol);
811  dsrRoutingHeader.SetMessageType (1);
812  dsrRoutingHeader.SetSourceId (GetIDfromIP (realSource));
813  dsrRoutingHeader.SetDestId (255);
814 
815  uint8_t length = rrep.GetLength (); // Get the length of the rrep header excluding the type header
816  dsrRoutingHeader.SetPayloadLength (length + 2);
817  dsrRoutingHeader.AddDsrOption (rrep);
818  Ptr<Packet> newPacket = Create<Packet> ();
819  newPacket->AddHeader (dsrRoutingHeader);
820  dsr->ScheduleCachedReply (newPacket, ipv4Address, nextHop, m_ipv4Route, hops);
821  isPromisc = false;
822  }
823  else
824  {
825  NS_LOG_DEBUG ("There is duplicate ip addresses in the two route parts");
826  }
827  return rreq.GetSerializedSize ();
828  }
829  /*
830  * (iii) no route in any type has been found
831  */
832  else
833  {
834  mainVector.push_back (ipv4Address);
835  NS_ASSERT (mainVector.front () == source);
836  NS_LOG_DEBUG ("Print out the main vector");
837  PrintVector (mainVector);
838  rreq.SetNodesAddress (mainVector);
839 
840  Ptr<Packet> errP = p->Copy ();
841  if (errP->GetSize ())
842  {
843  NS_LOG_DEBUG ("Error header included");
845  p->RemoveHeader (rerr);
846  Ipv4Address errorSrc = rerr.GetErrorSrc ();
847  Ipv4Address unreachNode = rerr.GetUnreachNode ();
848  Ipv4Address errorDst = rerr.GetErrorDst ();
849 
850  if ((errorSrc == srcAddress) && (unreachNode == ipv4Address))
851  {
852  NS_LOG_DEBUG ("The error link back to work again");
853  uint16_t length = rreq.GetLength ();
854  NS_LOG_DEBUG ("The RREQ header length " << length);
855  dsrRoutingHeader.AddDsrOption (rreq);
856  dsrRoutingHeader.SetPayloadLength (length + 2);
857  }
858  else
859  {
860  dsr->DeleteAllRoutesIncludeLink (errorSrc, unreachNode, ipv4Address);
861 
862  DsrOptionRerrUnreachHeader newUnreach;
863  newUnreach.SetErrorType (1);
864  newUnreach.SetErrorSrc (errorSrc);
865  newUnreach.SetUnreachNode (unreachNode);
866  newUnreach.SetErrorDst (errorDst);
867  newUnreach.SetSalvage (rerr.GetSalvage ()); // Set the value about whether to salvage a packet or not
868  uint16_t length = rreq.GetLength () + newUnreach.GetLength ();
869  NS_LOG_DEBUG ("The RREQ and newUnreach header length " << length);
870  dsrRoutingHeader.SetPayloadLength (length + 4);
871  dsrRoutingHeader.AddDsrOption (rreq);
872  dsrRoutingHeader.AddDsrOption (newUnreach);
873  }
874  }
875  else
876  {
877  uint16_t length = rreq.GetLength ();
878  NS_LOG_DEBUG ("The RREQ header length " << length);
879  dsrRoutingHeader.AddDsrOption (rreq);
880  dsrRoutingHeader.SetPayloadLength (length + 2);
881  }
882  // Get the TTL value
883  uint8_t ttl = ipv4Header.GetTtl ();
884  /*
885  * Decrease the TTL value in the packet tag by one, this tag will go to ip layer 3 send function
886  * and drop packet when TTL value equals to 0
887  */
888  NS_LOG_DEBUG ("The ttl value here " << (uint32_t)ttl);
889  if (ttl)
890  {
891  Ptr<Packet> interP = Create<Packet> ();
892  SocketIpTtlTag tag;
893  tag.SetTtl (ttl - 1);
894  interP->AddPacketTag (tag);
895  interP->AddHeader (dsrRoutingHeader);
896  dsr->ScheduleInterRequest (interP);
897  isPromisc = false;
898  }
899  return rreq.GetSerializedSize ();
900  }
901  }
902  return rreq.GetSerializedSize ();
903 }
904 
906 
908 {
909  static TypeId tid = TypeId ("ns3::dsr::DsrOptionRrep")
910  .SetParent<DsrOptions> ()
911  .AddConstructor<DsrOptionRrep> ()
912  ;
913  return tid;
914 }
915 
917 {
919 }
920 
922 {
924 }
925 
927 {
928  return GetTypeId ();
929 }
930 
932 {
934 
935  return OPT_NUMBER;
936 }
937 
938 uint8_t DsrOptionRrep::Process (Ptr<Packet> packet, Ptr<Packet> dsrP, Ipv4Address ipv4Address, Ipv4Address source, Ipv4Header const& ipv4Header, uint8_t protocol, bool& isPromisc)
939 {
940  NS_LOG_FUNCTION (this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
941 
942  Ptr<Packet> p = packet->Copy ();
943 
944  // Get the number of routers' address field
945  uint8_t buf[2];
946  p->CopyData (buf, sizeof(buf));
947  uint8_t numberAddress = (buf[1] - 2) / 4;
948 
949  DsrOptionRrepHeader rrep;
950  rrep.SetNumberAddress (numberAddress); // Set the number of ip address in the header to reserver space for deserialize header
951  p->RemoveHeader (rrep);
952 
953  Ptr<Node> node = GetNodeWithAddress (ipv4Address);
955 
956  NS_LOG_DEBUG ("The next header value " << (uint32_t)protocol);
957 
958  std::vector<Ipv4Address> nodeList = rrep.GetNodesAddress ();
962  Ipv4Address targetAddress = nodeList.front ();
963  // If the RREP option has reached to the destination
964  if (targetAddress == ipv4Address)
965  {
966  RemoveDuplicates (nodeList); // This is for the route reply from intermediate node since we didn't remove
967  // duplicate there
968  if (nodeList.size () == 0)
969  {
970  NS_LOG_DEBUG ("The route we have contains 0 entries");
971  return 0;
972  }
976  Ipv4Address dst = nodeList.back ();
982  RouteCacheEntry toDestination (/*IP_VECTOR=*/ nodeList, /*dst=*/ dst, /*expire time=*/ ActiveRouteTimeout);
983  NS_ASSERT (nodeList.front () == ipv4Address);
984  bool addRoute = false;
985  if (dsr->IsLinkCache ())
986  {
987  addRoute = dsr->AddRoute_Link (nodeList, ipv4Address);
988  }
989  else
990  {
991  addRoute = dsr->AddRoute (toDestination);
992  }
993 
994  if (addRoute)
995  {
996  NS_LOG_DEBUG ("We have added the route and search send buffer for packet with destination " << dst);
1000  DsrOptionSRHeader sourceRoute;
1001  NS_LOG_DEBUG ("The route length " << nodeList.size ());
1002  sourceRoute.SetNodesAddress (nodeList);
1003  sourceRoute.SetSegmentsLeft ((nodeList.size () - 2));
1004  sourceRoute.SetSalvage (0);
1005  Ipv4Address nextHop = SearchNextHop (ipv4Address, nodeList); // Get the next hop address
1006  NS_LOG_DEBUG ("The nextHop address " << nextHop);
1007  if (nextHop == "0.0.0.0")
1008  {
1009  dsr->PacketNewRoute (dsrP, ipv4Address, dst, protocol);
1010  return 0;
1011  }
1012  PrintVector (nodeList);
1013  SetRoute (nextHop, ipv4Address);
1014  // Cancel the route request timer for destination
1015  dsr->CancelRreqTimer (dst, true);
1019  dsr->SendPacketFromBuffer (sourceRoute, nextHop, protocol);
1020  }
1021  else
1022  {
1023  NS_LOG_DEBUG ("Failed to add the route");
1024  return 0;
1025  }
1026  }
1027  else
1028  {
1029  uint8_t length = rrep.GetLength () - 2; // The get length - 2 is to get aligned for the malformed header check
1030  NS_LOG_DEBUG ("The length of rrep option " << (uint32_t)length);
1031 
1032  if (length % 2 != 0)
1033  {
1034  NS_LOG_LOGIC ("Malformed header. Drop!");
1035  m_dropTrace (packet);
1036  return 0;
1037  }
1038  PrintVector (nodeList);
1039  /*
1040  * This node is only an intermediate node, but it needs to save the possible route to the destination when cutting the route
1041  */
1042  std::vector<Ipv4Address> routeCopy = nodeList;
1043  std::vector<Ipv4Address> cutRoute = CutRoute (ipv4Address, nodeList);
1044  PrintVector (cutRoute);
1045  if (cutRoute.size () >= 2)
1046  {
1047  Ipv4Address dst = cutRoute.back ();
1048  NS_LOG_DEBUG ("The route destination after cut " << dst);
1049  RouteCacheEntry toDestination (/*IP_VECTOR=*/ cutRoute, /*dst=*/ dst, /*expire time=*/ ActiveRouteTimeout);
1050  NS_ASSERT (cutRoute.front () == ipv4Address);
1051  bool addRoute = false;
1052  if (dsr->IsLinkCache ())
1053  {
1054  addRoute = dsr->AddRoute_Link (nodeList, ipv4Address);
1055  }
1056  else
1057  {
1058  addRoute = dsr->AddRoute (toDestination);
1059  }
1060  if (addRoute)
1061  {
1062  dsr->CancelRreqTimer (dst, true);
1063  }
1064  else
1065  {
1066  NS_LOG_DEBUG ("The route not added");
1067  }
1068  }
1069  else
1070  {
1071  NS_LOG_DEBUG ("The route is corrupted");
1072  }
1073  /*
1074  * Reverse search the vector for next hop address
1075  */
1076  Ipv4Address nextHop = ReverseSearchNextHop (ipv4Address, routeCopy);
1077  NS_ASSERT (routeCopy.back () == source);
1078  PrintVector (routeCopy);
1079  NS_LOG_DEBUG ("The nextHop address " << nextHop << " and the source in the route reply " << source);
1080  /*
1081  * Set the route entry we will use to send reply
1082  */
1083  SetRoute (nextHop, ipv4Address);
1084  /*
1085  * This part add dsr routing header to the packet and send reply
1086  */
1087  DsrRoutingHeader dsrRoutingHeader;
1088  dsrRoutingHeader.SetNextHeader (protocol);
1089 
1090  length = rrep.GetLength (); // Get the length of the rrep header excluding the type header
1091  NS_LOG_DEBUG ("The reply header length " << (uint32_t)length);
1092  dsrRoutingHeader.SetPayloadLength (length + 2);
1093  dsrRoutingHeader.SetMessageType (1);
1094  dsrRoutingHeader.SetSourceId (GetIDfromIP (source));
1095  dsrRoutingHeader.SetDestId (GetIDfromIP (targetAddress));
1096  dsrRoutingHeader.AddDsrOption (rrep);
1097  Ptr<Packet> newPacket = Create<Packet> ();
1098  newPacket->AddHeader (dsrRoutingHeader);
1099  dsr->SendReply (newPacket, ipv4Address, nextHop, m_ipv4Route);
1100  isPromisc = false;
1101  }
1102  return rrep.GetSerializedSize ();
1103 }
1104 
1106 
1108 {
1109  static TypeId tid = TypeId ("ns3::dsr::DsrOptionSR")
1110  .SetParent<DsrOptions> ()
1111  .AddConstructor<DsrOptionSR> ()
1112  ;
1113  return tid;
1114 }
1115 
1117 {
1119 }
1120 
1122 {
1124 }
1125 
1127 {
1128  return GetTypeId ();
1129 }
1130 
1132 {
1134  return OPT_NUMBER;
1135 }
1136 
1137 uint8_t DsrOptionSR::Process (Ptr<Packet> packet, Ptr<Packet> dsrP, Ipv4Address ipv4Address, Ipv4Address source, Ipv4Header const& ipv4Header, uint8_t protocol, bool& isPromisc)
1138 {
1139  NS_LOG_FUNCTION (this << packet << dsrP << ipv4Address << source << ipv4Address << ipv4Header << (uint32_t)protocol << isPromisc);
1140  Ptr<Packet> p = packet->Copy ();
1141  // Get the number of routers' address field
1142  uint8_t buf[2];
1143  p->CopyData (buf, sizeof(buf));
1144  uint8_t numberAddress = (buf[1] - 2) / 4;
1145  DsrOptionSRHeader sourceRoute;
1146  sourceRoute.SetNumberAddress (numberAddress);
1147  p->RemoveHeader (sourceRoute);
1148 
1149  // The route size saved in the source route
1150  std::vector<Ipv4Address> nodeList = sourceRoute.GetNodesAddress ();
1151  uint8_t segsLeft = sourceRoute.GetSegmentsLeft ();
1152  uint8_t salvage = sourceRoute.GetSalvage ();
1153  /*
1154  * Get the node from IP address and get the DSR extension object
1155  */
1156  Ptr<Node> node = GetNodeWithAddress (ipv4Address);
1158  /*
1159  * Get the source and destination address from ipv4 header
1160  */
1161  Ipv4Address srcAddress = ipv4Header.GetSource ();
1162  Ipv4Address destAddress = ipv4Header.GetDestination ();
1163  // Get the node list destination
1164  Ipv4Address destination = nodeList.back ();
1165  /*
1166  * If it's a promiscuous receive data packet,
1167  * 1. see if automatic route shortening possible or not
1168  * 2. see if it is a passive acknowledgment
1169  */
1170  if (isPromisc)
1171  {
1172  /*
1173  * Get the node from Ip address and get the dsr routing object
1174  */
1175  Ptr<Node> node = GetNodeWithAddress (ipv4Address);
1177  bool findSame = dsr->FindSamePackets (packet, source, destination, segsLeft);
1178 
1179  if (findSame)
1180  {
1181  NS_LOG_DEBUG ("Received one passive acknowledgment for a successfully delivered packet");
1182  }
1183  if (ContainAddressAfter (ipv4Address, destAddress, nodeList))
1184  {
1185  NS_LOG_DEBUG ("Send back the gratuitous reply");
1186  dsr->SendGratuitousReply (source, srcAddress, nodeList, protocol);
1187  }
1188  else
1189  {
1190  return 0;
1191  }
1192  }
1193  else
1194  {
1195  /*
1196  * Get the number of address from the source route header
1197  */
1198  uint8_t length = sourceRoute.GetLength ();
1199  uint8_t nextAddressIndex;
1200  Ipv4Address nextAddress;
1201 
1202  // Get the option type value
1203  uint32_t size = p->GetSize ();
1204  uint8_t *data = new uint8_t[size];
1205  p->CopyData (data, size);
1206  uint8_t optionType = 0;
1207  optionType = *(data);
1208  NS_LOG_DEBUG ("The packet size over here " << p->GetSize ());
1209 
1210  NS_LOG_DEBUG ("The option type over here " << (uint32_t)optionType);
1211  if (optionType == 160)
1212  {
1213  NS_LOG_DEBUG ("Here we remove the ack request header and add ack header to the packet");
1214  // Here we remove the ack packet to the previous hop
1215  DsrOptionAckReqHeader ackReq;
1216  p->RemoveHeader (ackReq);
1217  uint16_t ackId = ackReq.GetAckId ();
1218  NS_LOG_DEBUG ("The type value " << (uint32_t)ackReq.GetType ());
1219  /*
1220  * Send back acknowledgment packet to the earlier hop
1221  */
1222  m_ipv4Route = SetRoute (srcAddress, ipv4Address);
1223  NS_LOG_DEBUG ("Send back ACK to the earlier hop " << srcAddress << " from us " << ipv4Address);
1224  dsr->SendAck (ackId, srcAddress, source, destination, protocol, m_ipv4Route);
1225  }
1226  /*
1227  * After send back ACK, check if the segments left value has turned to 0 or not, if yes, update the route entry
1228  * and return header length
1229  */
1230  if (segsLeft == 0)
1231  {
1232  NS_LOG_DEBUG ("This is the final destination");
1233  isPromisc = false;
1234  return sourceRoute.GetSerializedSize ();
1235  }
1236 
1237  if (length % 2 != 0)
1238  {
1239  NS_LOG_LOGIC ("Malformed header. Drop!");
1240  m_dropTrace (packet);
1241  return 0;
1242  }
1243 
1244  if (segsLeft > numberAddress) // The segmentsLeft field should not be larger than the total number of ip addresses
1245  {
1246  NS_LOG_LOGIC ("Malformed header. Drop!");
1247  m_dropTrace (packet);
1248  return 0;
1249  }
1250 
1251  DsrOptionSRHeader newSourceRoute;
1252  newSourceRoute.SetSegmentsLeft (segsLeft - 1);
1253  newSourceRoute.SetSalvage (salvage);
1254  newSourceRoute.SetNodesAddress (nodeList);
1255  nextAddressIndex = numberAddress - segsLeft;
1256  nextAddress = newSourceRoute.GetNodeAddress (nextAddressIndex);
1257  NS_LOG_DEBUG ("The next address of source route option " << nextAddress << " and the nextAddressIndex: " << (uint32_t)nextAddressIndex << " and the segments left : " << (uint32_t)segsLeft);
1258  /*
1259  * Get the target Address in the node list
1260  */
1261  Ipv4Address targetAddress = nodeList.back ();
1262  Ipv4Address realSource = nodeList.front ();
1263  /*
1264  * Search the vector for next hop address
1265  */
1266  Ipv4Address nextHop = SearchNextHop (ipv4Address, nodeList);
1267  PrintVector (nodeList);
1268 
1269  if (nextHop == "0.0.0.0")
1270  {
1271  NS_LOG_DEBUG ("Before new packet " << *dsrP);
1272  dsr->PacketNewRoute (dsrP, realSource, targetAddress, protocol);
1273  return 0;
1274  }
1275 
1276  if (ipv4Address == nextHop)
1277  {
1278  NS_LOG_DEBUG ("We have reached the destination");
1279  newSourceRoute.SetSegmentsLeft (0);
1280  return newSourceRoute.GetSerializedSize ();
1281  }
1282  // Verify the multicast address, leave it here for now
1283  if (nextAddress.IsMulticast () || destAddress.IsMulticast ())
1284  {
1285  m_dropTrace (packet);
1286  return 0;
1287  }
1288  // Set the route and forward the data packet
1289  SetRoute (nextAddress, ipv4Address);
1290  NS_LOG_DEBUG ("dsr packet size " << dsrP->GetSize ());
1291  dsr->ForwardPacket (dsrP, newSourceRoute, ipv4Header, realSource, nextAddress, targetAddress, protocol, m_ipv4Route);
1292  }
1293  return sourceRoute.GetSerializedSize ();
1294 }
1295 
1297 
1299 {
1300  static TypeId tid = TypeId ("ns3::dsr::DsrOptionRerr")
1301  .SetParent<DsrOptions> ()
1302  .AddConstructor<DsrOptionRerr> ()
1303  ;
1304  return tid;
1305 }
1306 
1308 {
1310 }
1311 
1313 {
1315 }
1316 
1318 {
1319  return GetTypeId ();
1320 }
1321 
1323 {
1325  return OPT_NUMBER;
1326 }
1327 
1328 uint8_t DsrOptionRerr::Process (Ptr<Packet> packet, Ptr<Packet> dsrP, Ipv4Address ipv4Address, Ipv4Address source, Ipv4Header const& ipv4Header, uint8_t protocol, bool& isPromisc)
1329 {
1330  NS_LOG_FUNCTION (this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
1331  Ptr<Packet> p = packet->Copy ();
1332  uint32_t size = p->GetSize ();
1333  uint8_t *data = new uint8_t[size];
1334  p->CopyData (data, size);
1335  uint8_t errorType = *(data + 2);
1336  /*
1337  * Get the node from Ip address and get the dsr extension object
1338  */
1339  Ptr<Node> node = GetNodeWithAddress (ipv4Address);
1341  /*
1342  * The error serialized size
1343  */
1344  uint32_t rerrSize;
1345  NS_LOG_DEBUG ("The error type value here " << (uint32_t)errorType);
1346  if (errorType == 1) // unreachable ip address
1347  {
1348  /*
1349  * Remove the route error header from the packet, and get the error type
1350  */
1351  DsrOptionRerrUnreachHeader rerrUnreach;
1352  p->RemoveHeader (rerrUnreach);
1353  /*
1354  * Get the error destination address
1355  */
1356  Ipv4Address unreachAddress = rerrUnreach.GetUnreachNode ();
1357  Ipv4Address errorSource = rerrUnreach.GetErrorSrc ();
1358 
1359  NS_LOG_DEBUG ("The destination address and the unreachable node " << rerrUnreach.GetErrorDst () << " " << unreachAddress);
1360  /*
1361  * Get the serialized size of the rerr header
1362  */
1363  rerrSize = rerrUnreach.GetSerializedSize ();
1364  /*
1365  * Delete all the routes including the unreachable node address from the route cache
1366  */
1367  Ptr<Node> node = GetNodeWithAddress (ipv4Address);
1368  dsr->DeleteAllRoutesIncludeLink (errorSource, unreachAddress, ipv4Address);
1369 
1370  Ptr<Packet> newP = p->Copy ();
1371  uint32_t serialized = DoSendError (newP, rerrUnreach, rerrSize, ipv4Address, protocol);
1372  return serialized;
1373  }
1374  else
1375  {
1376  /*
1377  * Two other type of error headers:
1378  * 1. flow state not supported type-specific information
1379  * 2. unsupported option with option number
1380  */
1381  /*
1382  * Remove the route error header from the packet, and get the error type
1383  */
1384  DsrOptionRerrUnsupportHeader rerrUnsupport;
1385  p->RemoveHeader (rerrUnsupport);
1386  rerrSize = rerrUnsupport.GetSerializedSize ();
1387 
1388  // This is for the other two error options, not supporting for now TODO
1389 // uint32_t serialized = DoSendError (p, rerrUnsupport, rerrSize, ipv4Address, protocol);
1390  uint32_t serialized = 0;
1391  return serialized;
1392  }
1393 }
1394 
1395 uint8_t DsrOptionRerr::DoSendError (Ptr<Packet> p, DsrOptionRerrUnreachHeader &rerr, uint32_t rerrSize, Ipv4Address ipv4Address, uint8_t protocol)
1396 {
1397  // Get the number of routers' address field
1398  uint8_t buf[2];
1399  p->CopyData (buf, sizeof(buf));
1400  uint8_t numberAddress = (buf[1] - 2) / 4;
1401 
1402  // Here remove the source route header and schedule next hop error transmission
1403  NS_LOG_DEBUG ("The number of addresses " << (uint32_t)numberAddress);
1404  DsrOptionSRHeader sourceRoute;
1405  sourceRoute.SetNumberAddress (numberAddress);
1406  p->RemoveHeader (sourceRoute);
1407  NS_ASSERT (p->GetSize () == 0);
1408  /*
1409  * Get the node from ip address and the dsr extension object
1410  */
1411  Ptr<Node> node = GetNodeWithAddress (ipv4Address);
1412  Ptr<dsr::DsrRouting> dsr = node->GetObject<dsr::DsrRouting> ();
1413  /*
1414  * Get the segments left field and the next address
1415  */
1416  uint8_t segmentsLeft = sourceRoute.GetSegmentsLeft ();
1417  uint8_t length = sourceRoute.GetLength ();
1418  uint8_t nextAddressIndex;
1419  Ipv4Address nextAddress;
1420  /*
1421  * Get the route size and the error target address
1422  */
1423  std::vector<Ipv4Address> nodeList = sourceRoute.GetNodesAddress ();
1424  Ipv4Address targetAddress = nodeList.back ();
1425  /*
1426  * The total serialized size for both the rerr and source route headers
1427  */
1428  uint32_t serializedSize = rerrSize + sourceRoute.GetSerializedSize ();
1429 
1430  if (length % 2 != 0)
1431  {
1432  NS_LOG_LOGIC ("Malformed header. Drop!");
1433  m_dropTrace (p);
1434  return 0;
1435  }
1436 
1437  if (segmentsLeft > numberAddress)
1438  {
1439  NS_LOG_LOGIC ("Malformed header. Drop!");
1440  m_dropTrace (p);
1441  return 0;
1442  }
1443  /*
1444  * When the error packet has reached to the destination
1445  */
1446  if (segmentsLeft == 0 && targetAddress == ipv4Address)
1447  {
1448  NS_LOG_INFO ("This is the destination of the error, send error request");
1449  dsr->SendErrorRequest (rerr, protocol);
1450  return serializedSize;
1451  }
1452 
1453  // Get the next Router Address
1454  DsrOptionSRHeader newSourceRoute;
1455  newSourceRoute.SetSegmentsLeft (segmentsLeft - 1);
1456  nextAddressIndex = numberAddress - segmentsLeft;
1457  nextAddress = sourceRoute.GetNodeAddress (nextAddressIndex);
1458  newSourceRoute.SetSalvage (sourceRoute.GetSalvage ());
1459  newSourceRoute.SetNodesAddress (nodeList);
1460  nextAddress = newSourceRoute.GetNodeAddress (nextAddressIndex);
1461 
1462  // / to test if the next address is multicast or not
1463  if (nextAddress.IsMulticast () || targetAddress.IsMulticast ())
1464  {
1465  m_dropTrace (p);
1466  return serializedSize;
1467  }
1468 
1469  // Set the route entry
1470  SetRoute (nextAddress, ipv4Address);
1471  dsr->ForwardErrPacket (rerr, newSourceRoute, nextAddress, protocol, m_ipv4Route);
1472  return serializedSize;
1473 }
1474 
1476 
1478 {
1479  static TypeId tid = TypeId ("ns3::dsr::DsrOptionAckReq")
1480  .SetParent<DsrOptions> ()
1481  .AddConstructor<DsrOptionAckReq> ()
1482  ;
1483  return tid;
1484 }
1485 
1487 {
1489 }
1490 
1492 {
1494 }
1495 
1497 {
1498  return GetTypeId ();
1499 }
1500 
1502 {
1504  return OPT_NUMBER;
1505 }
1506 
1507 uint8_t DsrOptionAckReq::Process (Ptr<Packet> packet, Ptr<Packet> dsrP, Ipv4Address ipv4Address, Ipv4Address source, Ipv4Header const& ipv4Header, uint8_t protocol, bool& isPromisc)
1508 {
1509  NS_LOG_FUNCTION (this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
1510  /*
1511  * Current implementation of the ack request header processing is coded in source route header processing
1512  */
1513  /*
1514  * Remove the ack request header
1515  */
1516  Ptr<Packet> p = packet->Copy ();
1517  DsrOptionAckReqHeader ackReq;
1518  p->RemoveHeader (ackReq);
1519  /*
1520  * Get the node with ip address and get the dsr extension and reoute cache objects
1521  */
1522  Ptr<Node> node = GetNodeWithAddress (ipv4Address);
1524 
1525  NS_LOG_DEBUG ("The next header value " << (uint32_t)protocol);
1526 
1527  Ipv4Address srcAddress = ipv4Header.GetSource ();
1528  SetRoute (srcAddress, ipv4Address);
1529 
1530  return ackReq.GetSerializedSize ();
1531 }
1532 
1534 
1536 {
1537  static TypeId tid = TypeId ("ns3::dsr::DsrOptionAck")
1538  .SetParent<DsrOptions> ()
1539  .AddConstructor<DsrOptionAck> ()
1540  ;
1541  return tid;
1542 }
1543 
1545 {
1547 }
1548 
1550 {
1552 }
1553 
1555 {
1556  return GetTypeId ();
1557 }
1558 
1560 {
1562  return OPT_NUMBER;
1563 }
1564 
1565 uint8_t DsrOptionAck::Process (Ptr<Packet> packet, Ptr<Packet> dsrP, Ipv4Address ipv4Address, Ipv4Address source, Ipv4Header const& ipv4Header, uint8_t protocol, bool& isPromisc)
1566 {
1567  NS_LOG_FUNCTION (this << packet << dsrP << ipv4Address << source << ipv4Header << (uint32_t)protocol << isPromisc);
1568  /*
1569  * Remove the ACK header
1570  */
1571  Ptr<Packet> p = packet->Copy ();
1572  DsrOptionAckHeader ack;
1573  p->RemoveHeader (ack);
1574  /*
1575  * Get the ACK source and destination address
1576  */
1577  Ipv4Address realSrc = ack.GetRealSrc ();
1578  Ipv4Address realDst = ack.GetRealDst ();
1579  uint16_t ackId = ack.GetAckId ();
1580  /*
1581  * Get the node with ip address and get the dsr extension and route cache objects
1582  */
1583  Ptr<Node> node = GetNodeWithAddress (ipv4Address);
1585  dsr->UpdateRouteEntry (realDst);
1586  /*
1587  * Cancel the packet retransmit timer when receiving the ack packet
1588  */
1589  dsr->CallCancelPacketTimer (ackId, ipv4Header, realSrc, realDst);
1590  return ack.GetSerializedSize ();
1591 }
1592 
1593 } // namespace dsr
1594 } // namespace ns3