diff --git a/src/internet-node/ipv4-l3-protocol.cc b/src/internet-node/ipv4-l3-protocol.cc index 4a46290e6..8328a9be6 100644 --- a/src/internet-node/ipv4-l3-protocol.cc +++ b/src/internet-node/ipv4-l3-protocol.cc @@ -551,12 +551,39 @@ Ipv4L3Protocol::SetUp (uint32_t i) { 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; + } + } + } } diff --git a/src/node/ipv4-address.cc b/src/node/ipv4-address.cc index f8ff9f7a6..8960293ee 100644 --- a/src/node/ipv4-address.cc +++ b/src/node/ipv4-address.cc @@ -145,6 +145,12 @@ Ipv4Address::IsEqual (Ipv4Address other) const } } +Ipv4Address +Ipv4Address::CombineMask (Ipv4Mask const &mask) const +{ + return Ipv4Address (GetHostOrder () & mask.GetHostOrder ()); +} + bool Ipv4Address::IsMulticast (void) { diff --git a/src/node/ipv4-address.h b/src/node/ipv4-address.h index dc834052a..37b1e23cd 100644 --- a/src/node/ipv4-address.h +++ b/src/node/ipv4-address.h @@ -27,6 +27,8 @@ namespace ns3 { +class Ipv4Mask; + /** Ipv4 addresses are stored in host order in * this class. */ @@ -82,6 +84,16 @@ public: 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);