Add patch 49 (add/remove network route when bringing interface up/down)
authorTom Henderson <tomh@tomh.org>
Fri, 27 Jul 2007 10:48:10 -0700
changeset 979 cb805f7a2e8d
parent 971 7aba0cf0df19
child 980 469fb688f446
Add patch 49 (add/remove network route when bringing interface up/down)
src/internet-node/ipv4-l3-protocol.cc
src/node/ipv4-address.cc
src/node/ipv4-address.h
--- a/src/internet-node/ipv4-l3-protocol.cc	Thu Jul 26 16:04:54 2007 +0200
+++ b/src/internet-node/ipv4-l3-protocol.cc	Fri Jul 27 10:48:10 2007 -0700
@@ -551,12 +551,39 @@
 {
   Ipv4Interface *interface = GetInterface (i);
   interface->SetUp ();
+
+  // If interface address and network mask have been set, add a route
+  // to the network of the interface (like e.g. ifconfig does on a
+  // Linux box)
+  if ((interface->GetAddress ()) != (Ipv4Address ())
+      && (interface->GetNetworkMask ()) != (Ipv4Mask ()))
+    {
+      AddNetworkRouteTo (interface->GetAddress ().CombineMask (interface->GetNetworkMask ()),
+                         interface->GetNetworkMask (), i);
+    }
 }
 void 
-Ipv4L3Protocol::SetDown (uint32_t i)
+Ipv4L3Protocol::SetDown (uint32_t ifaceIndex)
 {
-  Ipv4Interface *interface = GetInterface (i);
+  Ipv4Interface *interface = GetInterface (ifaceIndex);
   interface->SetDown ();
+
+  // Remove all routes that are going through this interface
+  bool modified = true;
+  while (modified)
+    {
+      modified = false;
+      for (uint32_t i = 0; i < GetNRoutes (); i++)
+        {
+          Ipv4Route *route = GetRoute (i);
+          if (route->GetInterface () == ifaceIndex)
+            {
+              RemoveRoute (i);
+              modified = true;
+              break;
+            }
+        }
+    }
 }
 
 
--- a/src/node/ipv4-address.cc	Thu Jul 26 16:04:54 2007 +0200
+++ b/src/node/ipv4-address.cc	Fri Jul 27 10:48:10 2007 -0700
@@ -145,6 +145,12 @@
   }
 }
 
+Ipv4Address
+Ipv4Address::CombineMask (Ipv4Mask const &mask) const
+{
+  return Ipv4Address (GetHostOrder () & mask.GetHostOrder ());
+}
+
 bool 
 Ipv4Address::IsMulticast (void)
 {
--- a/src/node/ipv4-address.h	Thu Jul 26 16:04:54 2007 +0200
+++ b/src/node/ipv4-address.h	Fri Jul 27 10:48:10 2007 -0700
@@ -27,6 +27,8 @@
 
 namespace ns3 {
 
+class Ipv4Mask;
+
 /** Ipv4 addresses are stored in host order in
   * this class.
   */
@@ -82,6 +84,16 @@
 
   bool IsBroadcast (void);
   bool IsMulticast (void);
+  /**
+   * \brief Combine this address with a network mask
+   *
+   * This method returns an IPv4 address that is this address combined
+   * (bitwise and) with a network mask, yielding an IPv4 network
+   * address.
+   *
+   * \param a network mask 
+   */
+  Ipv4Address CombineMask (Ipv4Mask const &mask) const;
 
   static Ipv4Address GetZero (void);
   static Ipv4Address GetAny (void);