Rtable cosmetics + RREQ header
authorPavel Boyko <boyko@iitp.ru>
Mon, 06 Jul 2009 12:44:34 +0400
changeset 5544 e149190912e4
parent 5543 be0a97484ad4
child 5545 f65511805a27
Rtable cosmetics + RREQ header
src/routing/aodv/aodv-packet.cc
src/routing/aodv/aodv-packet.h
src/routing/aodv/aodv-rtable.cc
src/routing/aodv/aodv-rtable.h
src/routing/aodv/wscript
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/routing/aodv/aodv-packet.cc	Mon Jul 06 12:44:34 2009 +0400
@@ -0,0 +1,142 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 1997, 1998 Carnegie Mellon University.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Authors: The AODV code developed by the CMU/MONARCH group was optimized and
+ * tuned by Samir Das and Mahesh Marina, University of Cincinnati.
+ * The work was partially done in Sun Microsystems.
+ *
+ * Ported to ns-3 by Elena Borovkova <borovkovaes@iitp.ru>
+ */
+#include "aodv-packet.h"
+#include "ns3/address-utils.h"
+
+namespace ns3 {
+namespace aodv {
+
+//-----------------------------------------------------------------------------
+// RREQ
+//-----------------------------------------------------------------------------
+RreqHeader::RreqHeader () : rq_flags(0), reserved(0), rq_hop_count(0), rq_bcast_id(0), rq_dst_seqno(0), rq_src_seqno(0)
+{
+  // TODO check defaults in AODV UU
+  SetGratiousRrep (false);
+  SetDestinationOnly (false);
+  SetUnknownSeqno (false);
+}
+
+uint32_t 
+RreqHeader::GetSerializedSize ()
+{
+  return 24;
+}
+
+void 
+RreqHeader::Serialize (Buffer::Iterator i)
+{
+  i.WriteU8 (type());
+  i.WriteU8 (rq_flags);
+  i.WriteU8 (reserved);
+  i.WriteU8 (rq_hop_count);
+  i.WriteHtonU32 (rq_bcast_id);
+  WriteTo (i, rq_dst);
+  i.WriteHtonU32 (rq_dst_seqno);
+  WriteTo (i, rq_src);
+  i.WriteHtonU32 (rq_src_seqno);
+}
+
+uint32_t 
+RreqHeader::Deserialize (Buffer::Iterator start)
+{
+  Buffer::Iterator i = start;
+  uint8_t t = i.ReadU8 ();
+  NS_ASSERT (t == type());
+  
+  rq_flags = i.ReadU8 ();
+  reserved = i.ReadU8 ();
+  rq_hop_count = i.ReadU8 ();
+  rq_bcast_id = i.ReadNtohU32 ();
+  ReadFrom (i, rq_dst);
+  rq_dst_seqno = i.ReadNtohU32 ();
+  ReadFrom (i, rq_src);
+  rq_src_seqno = i.ReadNtohU32 ();
+  
+  uint32_t dist = i.GetDistanceFrom (start);
+  NS_ASSERT (dist == GetSerializedSize ());
+  return dist;
+}
+
+void 
+RreqHeader::Print (std::ostream &os) const
+{
+  // TODO
+}
+
+std::ostream & operator<<(std::ostream & os, RreqHeader const & h)
+{
+  h.Print (os);
+  return os;
+}
+
+void 
+RreqHeader::SetGratiousRrep (bool f)
+{
+  if (f) rq_flags |= (1 << 2);
+  else   rq_flags &= ~(1 << 2);
+}
+
+bool 
+RreqHeader::GetGratiousRrep () const
+{
+  return (rq_flags & (1 << 2));
+}
+
+void 
+RreqHeader::SetDestinationOnly (bool f)
+{
+  if (f) rq_flags |= (1 << 3);
+  else   rq_flags &= ~(1 << 3);
+}
+
+bool 
+RreqHeader::GetDestinationOnly () const
+{
+  return (rq_flags & (1 << 3));
+}
+
+void 
+RreqHeader::SetUnknownSeqno (bool f)
+{
+  if (f) rq_flags |= (1 << 4);
+  else   rq_flags &= ~(1 << 4);
+}
+
+bool 
+RreqHeader::GetUnknownSeqno () const
+{
+  return (rq_flags & (1 << 4));
+}
+
+bool
+RreqHeader::operator==(RreqHeader const & o) const
+{
+  return (rq_flags == o.rq_flags && reserved == o.reserved &&
+      rq_hop_count == o.rq_hop_count && rq_bcast_id == o.rq_bcast_id &&
+      rq_dst == o.rq_dst && rq_dst_seqno == o.rq_dst_seqno && 
+      rq_src == o.rq_src && rq_src_seqno == o.rq_src_seqno);
+}
+
+}}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/routing/aodv/aodv-packet.h	Mon Jul 06 12:44:34 2009 +0400
@@ -0,0 +1,123 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 1997, 1998 Carnegie Mellon University.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Authors: The AODV code developed by the CMU/MONARCH group was optimized and
+ * tuned by Samir Das and Mahesh Marina, University of Cincinnati.
+ * The work was partially done in Sun Microsystems.
+ *
+ * Ported to ns-3 by Elena Borovkova <borovkovaes@iitp.ru>
+ */
+#ifndef AODVPACKET_H_
+#define AODVPACKET_H_
+
+#include <stdint.h>
+#include <iostream>
+#include "ns3/header.h"
+#include "ns3/ipv4-address.h"
+
+namespace ns3 {
+namespace aodv {
+
+/// AODV message types
+enum MessageType 
+{
+  AODVTYPE_HELLO = 0x01,
+  AODVTYPE_RREQ  = 0x02,
+  AODVTYPE_RREP  = 0x04,
+  AODVTYPE_RERR  = 0x08,
+  AODVTYPE_RREP_ACK = 0x10
+};
+
+
+/**
+ * \ingroup aodv
+ * \brief   Route Request (RREQ) Message Format
+
+    0                   1                   2                   3
+    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+   |     Type      |J|R|G|D|U|   Reserved          |   Hop Count   |
+   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+   |                            RREQ ID                            |
+   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+   |                    Destination IP Address                     |
+   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+   |                  Destination Sequence Number                  |
+   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+   |                    Originator IP Address                      |
+   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+   |                  Originator Sequence Number                   |
+   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ */
+class RreqHeader : public Header 
+{
+public:
+  RreqHeader ();
+  
+  ///\name Header serialization/deserialization
+  //\{
+  uint32_t GetSerializedSize ();
+  void Serialize (Buffer::Iterator start);
+  uint32_t Deserialize (Buffer::Iterator start);
+  void Print (std::ostream &os) const;
+  //\}
+  
+  ///\name Fields
+  //\{
+  void SetHopCount (uint8_t count) { rq_hop_count = count; }
+  uint8_t GetHopCount () const { return rq_hop_count; }
+  void SetId (uint32_t id) { rq_bcast_id = id; }
+  uint8_t GetId () const { return rq_bcast_id; }  
+  void SetDst (Ipv4Address a) { rq_dst = a; }
+  Ipv4Address GetDst () const { return rq_dst; }
+  void SetDstSeqno (uint32_t s) { rq_dst_seqno = s; }
+  uint32_t GetDstSeqno () const { return rq_dst_seqno; }
+  void SetSrc (Ipv4Address a) { rq_src = a; }
+  Ipv4Address GetSrc () const { return rq_src; }  
+  void SetSrcSeqno (uint32_t s) { rq_src_seqno = s; }
+  uint32_t GetSrcSeqno () const { return rq_src_seqno; }
+  //\}
+  
+  ///\name Flags
+  //\{
+  void SetGratiousRrep (bool f);
+  bool GetGratiousRrep () const;
+  void SetDestinationOnly (bool f);
+  bool GetDestinationOnly () const;
+  void SetUnknownSeqno (bool f);
+  bool GetUnknownSeqno () const;
+  //\}
+  
+  bool operator==(RreqHeader const & o) const;
+private:
+   static MessageType type() { return AODVTYPE_RREQ; }
+   
+   uint8_t         rq_flags;       ///< |J|R|G|D|U| bit flags, see RFC
+   uint8_t        reserved;       ///< Not used
+   uint8_t        rq_hop_count;   ///< Hop Count
+   uint32_t       rq_bcast_id;    ///< RREQ ID
+   Ipv4Address     rq_dst;         ///< Destination IP Address
+   uint32_t       rq_dst_seqno;   ///< Destination Sequence Number
+   Ipv4Address     rq_src;         ///< Source IP Address
+   uint32_t       rq_src_seqno;   ///< Source Sequence Number
+};
+
+std::ostream & operator<<(std::ostream & os, RreqHeader const &);
+
+}
+}
+#endif /* AODVPACKET_H_ */
--- a/src/routing/aodv/aodv-rtable.cc	Sun Jul 05 17:32:12 2009 +0400
+++ b/src/routing/aodv/aodv-rtable.cc	Mon Jul 06 12:44:34 2009 +0400
@@ -56,13 +56,12 @@
 aodv_rt_entry::aodv_rt_entry(Ipv4Address dst, bool vSeqNo, u_int32_t seqNo,
 		u_int16_t  hops,Ipv4Address nextHop, Time lifetime)
 {
-	rt_dst = dst;
-	validSeqNo = vSeqNo;
-	if(validSeqNo == true)
-		rt_seqno = seqNo;
-	rt_hops = hops;
-	rt_nexthop = nextHop;
-	rt_lifetime = lifetime;
+  rt_dst = dst;
+  validSeqNo = vSeqNo;
+  if (validSeqNo) rt_seqno = seqNo;
+  rt_hops = hops;
+  rt_nexthop = nextHop;
+  rt_lifetime = lifetime;
 }
 
 aodv_rt_entry::~aodv_rt_entry()
@@ -75,10 +74,10 @@
   AODV_Precursor p (id);
 
   if (! pc_lookup(id, p))
-  {
-  	rt_pclist.push_back(p);
-  	return true;
-  }
+    {
+      rt_pclist.push_back(p);
+      return true;
+    }
   else return false;
 }
 
@@ -100,9 +99,9 @@
   AODV_Precursor p(id);
   std::vector<AODV_Precursor>::iterator i = std::remove(rt_pclist.begin(), rt_pclist.end(), p);
   if(i == rt_pclist.end())
-  	return false;
+    return false;
   else
-  	rt_pclist.erase (i, rt_pclist.end());
+    rt_pclist.erase (i, rt_pclist.end());
   return true;
 }
 
@@ -134,9 +133,8 @@
 bool
 aodv_rtable::rt_delete(Ipv4Address dst)
 {
-	std::vector<aodv_rt_entry>::iterator i = std::remove(rthead.begin(), rthead.end(), dst);
-	if(i == rthead.end())
-		return false;
+  std::vector<aodv_rt_entry>::iterator i = std::remove(rthead.begin(), rthead.end(), dst);
+  if (i == rthead.end()) return false;
   rthead.erase (i, rthead.end());
   return true;
 }
@@ -152,67 +150,62 @@
 }
 
 #ifdef RUN_SELF_TESTS
-    /// Unit test for aodv_rqueue
-    struct AodvRtableTest : public Test
-    {
-      AodvRtableTest () : Test ("AODV/Rtable"), result(true) {}
-      virtual bool RunTests();
-      bool result;
-    };
-
-    /// Test instance
-    static AodvRtableTest g_AodvRtableTest;
+/// Unit test for aodv_rqueue
+struct AodvRtableTest : public Test
+{
+  AodvRtableTest () : Test ("AODV/Rtable"), result(true) {}
+  virtual bool RunTests();
+  bool result;
+};
 
-    bool
-    AodvRtableTest::RunTests ()
-    {
-    	AODV_Neighbor nb1(Ipv4Address("1.2.3.4"));
-    	AODV_Neighbor nb2(Ipv4Address("4.3.2.1"));
-    	NS_TEST_ASSERT(!(nb1==nb2));
-
-    	AODV_Neighbor nb3(Ipv4Address("3.3.3.3"));
-    	AODV_Neighbor nb4(Ipv4Address("3.3.3.3"), Seconds(1));
-    	NS_TEST_ASSERT(!(nb1==nb2));
-
-    	AODV_Precursor pc1(Ipv4Address("1.1.1.1"));
-    	AODV_Precursor pc2(Ipv4Address("2.2.2.2"));
-    	NS_TEST_ASSERT(!(pc1==pc2));
+/// Test instance
+static AodvRtableTest g_AodvRtableTest;
 
-    	aodv_rt_entry entry1;
-    	Ipv4Address dst1("3.3.3.3");
-    	Ipv4Address dst2("1.2.3.4");
-    	aodv_rt_entry entry2 = aodv_rt_entry(dst2, true, 34, 1, Ipv4Address("5.5.5.5"),  Seconds(5));
-    	NS_TEST_ASSERT( !(entry1 == dst1) );
-    	NS_TEST_ASSERT(entry2 == dst2);
-
-    	entry2.pc_insert(dst1);
-    	AODV_Precursor pc3(dst1);
-    	NS_TEST_ASSERT(entry2.pc_lookup(dst1, pc2));
-    	NS_TEST_ASSERT(pc3==pc2);
-    	NS_TEST_ASSERT(!entry2.pc_delete(dst2));
-    	NS_TEST_ASSERT(entry2.pc_delete(dst1));
-    	NS_TEST_ASSERT(entry2.pc_empty());
-    	entry2.pc_insert(dst2);
-    	NS_TEST_ASSERT(entry2.pc_insert(dst1));
-    	NS_TEST_ASSERT(!entry2.pc_insert(dst2));
-    	entry2.pc_delete();
-    	NS_TEST_ASSERT(entry2.pc_empty());
-
-    	aodv_rtable rtable;
-    	rtable.rt_add(entry2);
-    	NS_TEST_ASSERT(rtable.rt_lookup(dst2, entry1));
-    	NS_TEST_ASSERT(entry1 == dst2);
-    	NS_TEST_ASSERT(!rtable.rt_lookup(dst1, entry1));
-    	NS_TEST_ASSERT(rtable.rt_delete(dst2));
-    	NS_TEST_ASSERT(!rtable.rt_lookup(dst2, entry1));
-
-
-
-
-      return result;
-    }
-
-
+bool
+AodvRtableTest::RunTests ()
+{
+  AODV_Neighbor nb1(Ipv4Address("1.2.3.4"));
+  AODV_Neighbor nb2(Ipv4Address("4.3.2.1"));
+  NS_TEST_ASSERT(!(nb1==nb2));
+  
+  AODV_Neighbor nb3(Ipv4Address("3.3.3.3"));
+  AODV_Neighbor nb4(Ipv4Address("3.3.3.3"), Seconds(1));
+  NS_TEST_ASSERT(!(nb1==nb2));
+  
+  AODV_Precursor pc1(Ipv4Address("1.1.1.1"));
+  AODV_Precursor pc2(Ipv4Address("2.2.2.2"));
+  NS_TEST_ASSERT(!(pc1==pc2));
+  
+  aodv_rt_entry entry1;
+  Ipv4Address dst1("3.3.3.3");
+  Ipv4Address dst2("1.2.3.4");
+  aodv_rt_entry entry2 = aodv_rt_entry(dst2, true, 34, 1, Ipv4Address("5.5.5.5"),  Seconds(5));
+  NS_TEST_ASSERT( !(entry1 == dst1) );
+  NS_TEST_ASSERT(entry2 == dst2);
+  
+  entry2.pc_insert(dst1);
+  AODV_Precursor pc3(dst1);
+  NS_TEST_ASSERT(entry2.pc_lookup(dst1, pc2));
+  NS_TEST_ASSERT(pc3==pc2);
+  NS_TEST_ASSERT(!entry2.pc_delete(dst2));
+  NS_TEST_ASSERT(entry2.pc_delete(dst1));
+  NS_TEST_ASSERT(entry2.pc_empty());
+  entry2.pc_insert(dst2);
+  NS_TEST_ASSERT(entry2.pc_insert(dst1));
+  NS_TEST_ASSERT(!entry2.pc_insert(dst2));
+  entry2.pc_delete();
+  NS_TEST_ASSERT(entry2.pc_empty());
+  
+  aodv_rtable rtable;
+  rtable.rt_add(entry2);
+  NS_TEST_ASSERT(rtable.rt_lookup(dst2, entry1));
+  NS_TEST_ASSERT(entry1 == dst2);
+  NS_TEST_ASSERT(!rtable.rt_lookup(dst1, entry1));
+  NS_TEST_ASSERT(rtable.rt_delete(dst2));
+  NS_TEST_ASSERT(!rtable.rt_lookup(dst2, entry1));
+  
+  return result;
+}
 #endif
 
 }}
--- a/src/routing/aodv/aodv-rtable.h	Sun Jul 05 17:32:12 2009 +0400
+++ b/src/routing/aodv/aodv-rtable.h	Mon Jul 06 12:44:34 2009 +0400
@@ -141,7 +141,7 @@
   /// Hop Count (number of hops needed to reach destination)
   u_int16_t rt_hops;
   /// Last valid hop count
-  int rt_last_hop_count;
+  u_int16_t rt_last_hop_count;
   /// Next hop IP address
   Ipv4Address rt_nexthop;
   /// List of precursors
@@ -149,7 +149,7 @@
   /**
   * \brief Expiration or deletion time of the route
   *	Lifetime field in the routing table plays dual role --
-  *	for an active route it is the expiry time, and for an invalid route
+  *	for an active route it is the expiration time, and for an invalid route
   *	it is the deletion time.
   */
   Time rt_lifetime;
@@ -164,7 +164,7 @@
   double rt_disc_latency[MAX_HISTORY];
   char hist_indx;
   /// Last ttl value used
-  int rt_req_last_ttl;
+  uint16_t rt_req_last_ttl;
 };
 
 /**
--- a/src/routing/aodv/wscript	Sun Jul 05 17:32:12 2009 +0400
+++ b/src/routing/aodv/wscript	Mon Jul 06 12:44:34 2009 +0400
@@ -6,6 +6,7 @@
     module.source = [
         'aodv-rtable.cc',
         'aodv-rqueue.cc',
+        'aodv-packet.cc',
         ]
 
     headers = bld.new_task_gen('ns3header')
@@ -13,5 +14,6 @@
     headers.source = [
         'aodv-rtable.h',
         'aodv-rqueue.h',
+        'aodv-packet.h',
         ]