fix for bug 588
authorTom Henderson <tomh@tomh.org>
Sat, 13 Jun 2009 15:57:04 -0700
changeset 4563 55948d1baa64
parent 4562 8539f55c6b55
child 4564 d44aeedf416a
fix for bug 588
src/internet-stack/ipv4-interface.cc
src/internet-stack/ipv4-interface.h
src/internet-stack/ipv4-l3-protocol.cc
src/internet-stack/ipv4-l3-protocol.h
src/node/ipv4-interface-address.h
src/node/ipv4.h
--- a/src/internet-stack/ipv4-interface.cc	Sat Jun 13 14:59:35 2009 -0700
+++ b/src/internet-stack/ipv4-interface.cc	Sat Jun 13 15:57:04 2009 -0700
@@ -262,13 +262,12 @@
   return m_ifaddrs.size();
 }
 
-uint32_t
+bool
 Ipv4Interface::AddAddress (Ipv4InterfaceAddress addr)
 {
   NS_LOG_FUNCTION_NOARGS ();
-  uint32_t index = m_ifaddrs.size ();
   m_ifaddrs.push_back (addr);
-  return index;
+  return true;
 }
 
 Ipv4InterfaceAddress
@@ -292,7 +291,7 @@
   return (addr);  // quiet compiler
 }
 
-void
+Ipv4InterfaceAddress
 Ipv4Interface::RemoveAddress (uint32_t index)
 {
   NS_LOG_FUNCTION_NOARGS ();
@@ -306,12 +305,15 @@
     {
       if (tmp  == index)
         {
+          Ipv4InterfaceAddress addr = *i;
           m_ifaddrs.erase (i);
-          return;
+          return addr;
         }
        ++tmp;
     }
   NS_ASSERT_MSG (false, "Address " << index << " not found");
+  Ipv4InterfaceAddress addr;
+  return (addr);  // quiet compiler
 }
 
 }; // namespace ns3
--- a/src/internet-stack/ipv4-interface.h	Sat Jun 13 14:59:35 2009 -0700
+++ b/src/internet-stack/ipv4-interface.h	Sat Jun 13 15:57:04 2009 -0700
@@ -125,9 +125,9 @@
 
   /**
    * \param address The Ipv4InterfaceAddress to add to the interface
-   * \returns The index of the newly-added Ipv4InterfaceAddress
+   * \returns true if succeeded
    */
-  uint32_t AddAddress (Ipv4InterfaceAddress address);
+  bool AddAddress (Ipv4InterfaceAddress address);
 
   /**
    * \param index Index of Ipv4InterfaceAddress to return
@@ -141,9 +141,10 @@
   uint32_t GetNAddresses (void) const;
 
   /**
-   * \param index index of Ipv4InterfaceAddress to remove from address list.
+   * \param index Index of Ipv4InterfaceAddress to remove
+   * \returns The Ipv4InterfaceAddress address whose index is index 
    */
-  void RemoveAddress (uint32_t index);
+  Ipv4InterfaceAddress RemoveAddress (uint32_t index);
 
 protected:
   virtual void DoDispose (void);
--- a/src/internet-stack/ipv4-l3-protocol.cc	Sat Jun 13 14:59:35 2009 -0700
+++ b/src/internet-stack/ipv4-l3-protocol.cc	Sat Jun 13 15:57:04 2009 -0700
@@ -731,17 +731,17 @@
     }
 }
 
-uint32_t 
+bool
 Ipv4L3Protocol::AddAddress (uint32_t i, Ipv4InterfaceAddress address)
 {
   NS_LOG_FUNCTION (this << i << address);
   Ptr<Ipv4Interface> interface = GetInterface (i);
-  uint32_t index = interface->AddAddress (address);
+  bool retVal = interface->AddAddress (address);
   if (m_routingProtocol != 0)
     {
       m_routingProtocol->NotifyAddAddress (i, address);
     }
-  return index;
+  return retVal;
 }
 
 Ipv4InterfaceAddress 
@@ -760,6 +760,23 @@
   return iface->GetNAddresses ();
 }
 
+bool
+Ipv4L3Protocol::RemoveAddress (uint32_t i, uint32_t addressIndex)
+{
+  NS_LOG_FUNCTION (this << i << addressIndex);
+  Ptr<Ipv4Interface> interface = GetInterface (i);
+  Ipv4InterfaceAddress address = interface->RemoveAddress (addressIndex);
+  if (address != Ipv4InterfaceAddress ())
+    {
+      if (m_routingProtocol != 0)
+        {
+          m_routingProtocol->NotifyRemoveAddress (i, address);
+        }
+      return true;
+    }
+  return false;
+}
+
 void 
 Ipv4L3Protocol::SetMetric (uint32_t i, uint16_t metric)
 {
--- a/src/internet-stack/ipv4-l3-protocol.h	Sat Jun 13 14:59:35 2009 -0700
+++ b/src/internet-stack/ipv4-l3-protocol.h	Sat Jun 13 15:57:04 2009 -0700
@@ -142,9 +142,10 @@
   int32_t GetInterfaceForPrefix (Ipv4Address addr, Ipv4Mask mask) const;
   int32_t GetInterfaceForDevice (Ptr<const NetDevice> device) const;
 
-  uint32_t AddAddress (uint32_t i, Ipv4InterfaceAddress address);
+  bool AddAddress (uint32_t i, Ipv4InterfaceAddress address);
   Ipv4InterfaceAddress GetAddress (uint32_t interfaceIndex, uint32_t addressIndex) const;
   uint32_t GetNAddresses (uint32_t interface) const;
+  bool RemoveAddress (uint32_t interfaceIndex, uint32_t addressIndex);
 
   void SetMetric (uint32_t i, uint16_t metric);
   uint16_t GetMetric (uint32_t i) const;
--- a/src/node/ipv4-interface-address.h	Sat Jun 13 14:59:35 2009 -0700
+++ b/src/node/ipv4-interface-address.h	Sat Jun 13 15:57:04 2009 -0700
@@ -77,10 +77,25 @@
 
   InterfaceAddressScope_e m_scope;   
   bool m_secondary;        // For use in multihoming
+
+  friend bool operator == (Ipv4InterfaceAddress const &a, Ipv4InterfaceAddress const &b);
+  friend bool operator != (Ipv4InterfaceAddress const &a, Ipv4InterfaceAddress const &b);
 };
 
 std::ostream& operator<< (std::ostream& os, const Ipv4InterfaceAddress &addr);
 
+inline bool operator == (const Ipv4InterfaceAddress &a, const Ipv4InterfaceAddress &b)
+{
+  return (a.m_local == b.m_local && a.m_mask == b.m_mask &&
+    a.m_broadcast == b.m_broadcast && a.m_scope == b.m_scope && a.m_secondary == b.m_secondary);
+}
+inline bool operator != (const Ipv4InterfaceAddress &a, const Ipv4InterfaceAddress &b)
+{
+  return (a.m_local != b.m_local || a.m_mask != b.m_mask ||
+    a.m_broadcast != b.m_broadcast || a.m_scope != b.m_scope || a.m_secondary != b.m_secondary);
+}
+
+
 } // namespace ns3
 
 #endif /* IPV4_ADDRESS_H */
--- a/src/node/ipv4.h	Sat Jun 13 14:59:35 2009 -0700
+++ b/src/node/ipv4.h	Sat Jun 13 15:57:04 2009 -0700
@@ -159,9 +159,9 @@
   /**
    * \param interface Interface number of an Ipv4 interface
    * \param address Ipv4InterfaceAddress address to associate with the underlying Ipv4 interface
-   * \returns The address index of the newly-added address
+   * \returns true if the operation succeeded
    */
-  virtual uint32_t AddAddress (uint32_t interface, Ipv4InterfaceAddress address) = 0;
+  virtual bool AddAddress (uint32_t interface, Ipv4InterfaceAddress address) = 0;
 
   /**
    * \param interface Interface number of an Ipv4 interface
@@ -170,6 +170,9 @@
   virtual uint32_t GetNAddresses (uint32_t interface) const = 0;
 
   /**
+   * Because addresses can be removed, the addressIndex is not guaranteed
+   * to be static across calls to this method.
+   * 
    * \param interface Interface number of an Ipv4 interface
    * \param addressIndex index of Ipv4InterfaceAddress 
    * \returns the Ipv4InterfaceAddress associated to the interface and addresIndex
@@ -177,6 +180,18 @@
   virtual Ipv4InterfaceAddress GetAddress (uint32_t interface, uint32_t addressIndex) const = 0;
 
   /**
+   * Remove the address at addressIndex on named interface.  The addressIndex
+   * for all higher indices will decrement by one after this method is called;
+   * so, for example, to remove 5 addresses from an interface i, one could
+   * call RemoveAddress (i, 0); 5 times.  
+   * 
+   * \param interface Interface number of an Ipv4 interface
+   * \param addressIndex index of Ipv4InterfaceAddress to remove 
+   * \returns true if the operation succeeded
+   */
+  virtual bool RemoveAddress (uint32_t interface, uint32_t addressIndex) = 0;
+
+  /**
    * \param interface The interface number of an Ipv4 interface
    * \param metric routing metric (cost) associated to the underlying 
    *          Ipv4 interface