Bug 400: allow /32 addresses to be used on IPv4 interfaces
authorTom Henderson <tomh@tomh.org>
Fri Nov 28 06:05:27 2008 -0800 (14 months ago)
changeset 39560339a8ad5983
parent 3949 016c554c4f6d
child 3957 e2feb7992608
child 3958 7658bcc28d8d
Bug 400: allow /32 addresses to be used on IPv4 interfaces
src/internet-stack/ipv4-l3-protocol.cc
src/internet-stack/udp-socket-impl.cc
src/node/ipv4-address.cc
src/node/ipv4-address.h
     1.1 --- a/src/internet-stack/ipv4-l3-protocol.cc	Wed Nov 26 14:00:05 2008 -0800
     1.2 +++ b/src/internet-stack/ipv4-l3-protocol.cc	Fri Nov 28 06:05:27 2008 -0800
     1.3 @@ -852,6 +852,7 @@
     1.4            Ptr<Icmpv4L4Protocol> icmp = GetIcmp ();
     1.5            icmp->SendTimeExceededTtl (ipHeader, packet);
     1.6          }
     1.7 +      NS_LOG_WARN ("TTL exceeded.  Drop.");
     1.8        m_dropTrace (packet);
     1.9        return;
    1.10      }  
     2.1 --- a/src/internet-stack/udp-socket-impl.cc	Wed Nov 26 14:00:05 2008 -0800
     2.2 +++ b/src/internet-stack/udp-socket-impl.cc	Fri Nov 28 06:05:27 2008 -0800
     2.3 @@ -349,8 +349,13 @@
     2.4        }
     2.5    }
     2.6    //
     2.7 -  // If dest is sent to the limited broadcast address (all ones),
     2.8 -  // convert it to send a copy of the packet out of every interface
     2.9 +  // If dest is set to the limited broadcast address (all ones),
    2.10 +  // convert it to send a copy of the packet out of every 
    2.11 +  // interface as a subnet-directed broadcast.
    2.12 +  // Exception:  if the interface has a /32 address, there is no
    2.13 +  // valid subnet-directed broadcast, so send it as limited broadcast
    2.14 +  // Note also that some systems will only send limited broadcast packets
    2.15 +  // out of the "default" interface; here we send it out all interfaces
    2.16    //
    2.17    if (dest.IsBroadcast ())
    2.18      {
    2.19 @@ -359,13 +364,27 @@
    2.20          {
    2.21            Ipv4Address addri = ipv4->GetAddress (i);
    2.22            Ipv4Mask maski = ipv4->GetNetworkMask (i);
    2.23 -          Ipv4Address bcast = addri.GetSubnetDirectedBroadcast (maski);
    2.24 -          NS_LOG_LOGIC ("Sending one copy from " << addri << " to " << bcast
    2.25 -                        << " (mask is " << maski << ")");
    2.26 -          m_udp->Send (p->Copy (), addri, bcast,
    2.27 -                       m_endPoint->GetLocalPort (), port);
    2.28 -          NotifyDataSent (p->GetSize ());
    2.29 -          NotifySend (GetTxAvailable ());
    2.30 +          if (maski == Ipv4Mask::GetOnes ())
    2.31 +            {
    2.32 +              // if the network mask is 255.255.255.255, do not convert dest
    2.33 +              NS_LOG_LOGIC ("Sending one copy from " << addri << " to " << dest
    2.34 +                            << " (mask is " << maski << ")");
    2.35 +              m_udp->Send (p->Copy (), addri, dest,
    2.36 +                           m_endPoint->GetLocalPort (), port);
    2.37 +              NotifyDataSent (p->GetSize ());
    2.38 +              NotifySend (GetTxAvailable ());
    2.39 +            }
    2.40 +          else
    2.41 +            {
    2.42 +              // Convert to subnet-directed broadcast
    2.43 +              Ipv4Address bcast = addri.GetSubnetDirectedBroadcast (maski);
    2.44 +              NS_LOG_LOGIC ("Sending one copy from " << addri << " to " << bcast
    2.45 +                            << " (mask is " << maski << ")");
    2.46 +              m_udp->Send (p->Copy (), addri, bcast,
    2.47 +                           m_endPoint->GetLocalPort (), port);
    2.48 +              NotifyDataSent (p->GetSize ());
    2.49 +              NotifySend (GetTxAvailable ());
    2.50 +            }
    2.51          }
    2.52        NS_LOG_LOGIC ("Limited broadcast end.");
    2.53        return p->GetSize();
     3.1 --- a/src/node/ipv4-address.cc	Wed Nov 26 14:00:05 2008 -0800
     3.2 +++ b/src/node/ipv4-address.cc	Fri Nov 28 06:05:27 2008 -0800
     3.3 @@ -77,7 +77,6 @@
     3.4    }
     3.5  }
     3.6  
     3.7 -
     3.8  bool 
     3.9  Ipv4Mask::IsMatch (Ipv4Address a, Ipv4Address b) const
    3.10  {
    3.11 @@ -126,6 +125,12 @@
    3.12    static Ipv4Mask zero = Ipv4Mask ("0.0.0.0");
    3.13    return zero;
    3.14  }
    3.15 +Ipv4Mask
    3.16 +Ipv4Mask::GetOnes (void)
    3.17 +{
    3.18 +  static Ipv4Mask ones = Ipv4Mask ("255.255.255.255");
    3.19 +  return ones;
    3.20 +}
    3.21  
    3.22  Ipv4Address::Ipv4Address ()
    3.23    : m_address (0x66666666)
    3.24 @@ -164,12 +169,22 @@
    3.25  Ipv4Address 
    3.26  Ipv4Address::GetSubnetDirectedBroadcast (Ipv4Mask const &mask) const
    3.27  {
    3.28 +  if (mask == Ipv4Mask::GetOnes ())
    3.29 +    {
    3.30 +      NS_ASSERT_MSG (false, "Trying to get subnet-directed broadcast address with an all-ones netmask");
    3.31 +    }
    3.32    return Ipv4Address (Get () | mask.GetInverse ());
    3.33  }
    3.34  
    3.35  bool
    3.36  Ipv4Address::IsSubnetDirectedBroadcast (Ipv4Mask const &mask) const
    3.37  {
    3.38 +  if (mask == Ipv4Mask::GetOnes ())
    3.39 +    {
    3.40 +      // If the mask is 255.255.255.255, there is no subnet directed
    3.41 +      // broadcast for this address.
    3.42 +      return false;
    3.43 +    }
    3.44    return ( (Get () | mask.GetInverse ()) == Get () );
    3.45  }
    3.46  
     4.1 --- a/src/node/ipv4-address.h	Wed Nov 26 14:00:05 2008 -0800
     4.2 +++ b/src/node/ipv4-address.h	Fri Nov 28 06:05:27 2008 -0800
     4.3 @@ -91,7 +91,7 @@
     4.4    void Serialize (uint8_t buf[4]) const;
     4.5    /**
     4.6     * \param buf buffer to read address from
     4.7 -   * \returns an Ipv4Address
     4.8 +   * \return an Ipv4Address
     4.9     * 
    4.10     * The input address is expected to be in network byte order format.
    4.11     */
    4.12 @@ -103,8 +103,13 @@
    4.13     * \param os The output stream to which this Ipv4Address is printed
    4.14     */
    4.15    void Print (std::ostream &os) const;
    4.16 -
    4.17 +  /**
    4.18 +    * \return true if address is 255.255.255.255; false otherwise
    4.19 +    */
    4.20    bool IsBroadcast (void) const;
    4.21 +  /**
    4.22 +    * \return true only if address is in the range 224.0.0.0 - 239.255.255.255
    4.23 +    */
    4.24    bool IsMulticast (void) const;
    4.25    /**
    4.26     * \brief Combine this address with a network mask
    4.27 @@ -120,20 +125,63 @@
    4.28     * \brief Generate subnet-directed broadcast address corresponding to mask
    4.29     *
    4.30     * The subnet-directed broadcast address has the host bits set to all
    4.31 -   * ones.
    4.32 +   * ones.  If this method is called with a mask of 255.255.255.255,
    4.33 +   * (i.e., the address is a /32 address), the program will assert, since
    4.34 +   * there is no subnet associated with a /32 address.
    4.35     *
    4.36     * \param mask a network mask 
    4.37     */
    4.38    Ipv4Address GetSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
    4.39 +  /**
    4.40 +   * \brief Generate subnet-directed broadcast address corresponding to mask
    4.41 +   * 
    4.42 +   * The subnet-directed broadcast address has the host bits set to all
    4.43 +   * ones.  If this method is called with a mask of 255.255.255.255,
    4.44 +   * (i.e., the address is a /32 address), the program will assert, since
    4.45 +   * there is no subnet associated with a /32 address.
    4.46 +   *
    4.47 +   * \param mask a network mask 
    4.48 +   * \return true if the address, when combined with the input mask, has all
    4.49 +   * of its host bits set to one
    4.50 +   */
    4.51    bool IsSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
    4.52 -
    4.53 +  /**
    4.54 +   * \param address an address to compare type with
    4.55 +   *
    4.56 +   * \return true if the type of the address stored internally
    4.57 +   * is compatible with the type of the input address, false otherwise.
    4.58 +   */
    4.59    static bool IsMatchingType (const Address &address);
    4.60 +  /**
    4.61 +   * Convert an instance of this class to a polymorphic Address instance.
    4.62 +   *
    4.63 +   * \return a new Address instance
    4.64 +   */
    4.65    operator Address () const;
    4.66 +  /**
    4.67 +   * \param address a polymorphic address
    4.68 +   * \return a new Ipv4Address from the polymorphic address
    4.69 +   *
    4.70 +   * This function performs a type check and asserts if the
    4.71 +   * type of the input address is not compatible with an
    4.72 +   * Ipv4Address.
    4.73 +   */
    4.74    static Ipv4Address ConvertFrom (const Address &address);
    4.75 -
    4.76 +  /**
    4.77 +   * \return the 0.0.0.0 address
    4.78 +   */
    4.79    static Ipv4Address GetZero (void);
    4.80 +  /**
    4.81 +   * \return the 0.0.0.0 address
    4.82 +   */
    4.83    static Ipv4Address GetAny (void);
    4.84 +  /**
    4.85 +   * \return the 255.255.255.255 address
    4.86 +   */
    4.87    static Ipv4Address GetBroadcast (void);
    4.88 +  /**
    4.89 +   * \return the 127.0.0.1 address
    4.90 +   */
    4.91    static Ipv4Address GetLoopback (void);
    4.92  
    4.93  private:
    4.94 @@ -156,9 +204,17 @@
    4.95    Ipv4Mask ();
    4.96    Ipv4Mask (uint32_t mask);
    4.97    Ipv4Mask (char const *mask);
    4.98 -
    4.99 +  /**
   4.100 +   * \param a first address to compare
   4.101 +   * \param b second address to compare
   4.102 +   * \return true if both addresses are equal in their masked bits, 
   4.103 +   * corresponding to this mask
   4.104 +   */
   4.105    bool IsMatch (Ipv4Address a, Ipv4Address b) const;
   4.106 -
   4.107 +  /**
   4.108 +   * \param other a mask to compare 
   4.109 +   * \return true if the mask equals the mask passed as input parameter
   4.110 +   */
   4.111    bool IsEqual (Ipv4Mask other) const;
   4.112    /** 
   4.113     * Get the host-order 32-bit IP mask
   4.114 @@ -174,11 +230,25 @@
   4.115     * \brief Return the inverse mask in host order. 
   4.116     */
   4.117    uint32_t GetInverse (void) const;
   4.118 -
   4.119 +  /**
   4.120 +   * \brief Print this mask to the given output stream
   4.121 +   *
   4.122 +   * The print format is in the typical "255.255.255.0"
   4.123 +   * \param os The output stream to which this Ipv4Address is printed
   4.124 +   */
   4.125    void Print (std::ostream &os) const;
   4.126 -
   4.127 +  /**
   4.128 +   * \return the 255.0.0.0 mask corresponding to a typical loopback address
   4.129 +   */
   4.130    static Ipv4Mask GetLoopback (void);
   4.131 +  /**
   4.132 +   * \return the 0.0.0.0 mask
   4.133 +   */
   4.134    static Ipv4Mask GetZero (void);
   4.135 +  /**
   4.136 +   * \return the 255.255.255.255 mask
   4.137 +   */
   4.138 +  static Ipv4Mask GetOnes (void);
   4.139  
   4.140  private:
   4.141    uint32_t m_mask;