/// <summary>
 /// Compare two IpAddress classes
 /// </summary>
 /// <param name="other">IpAddress class to compare with</param>
 /// <returns>0 if classes are the same, -1 if current class is less then or 1 if greater then the class
 /// we are comparing against.</returns>
 public int CompareTo(IpAddress other)
 {
     byte[] b = other.GetData();
     if (_data == null)
     {
         return(-1);
     }
     if (b.Length != _data.Length)
     {
         if (_data.Length < b.Length)
         {
             return(-1);
         }
         else
         {
             return(1);
         }
     }
     else
     {
         for (int i = 0; i < _data.Length; i++)
         {
             if (_data[i] < b[i])
             {
                 return(-1);
             }
             else if (_data[i] > b[i])
             {
                 return(1);
             }
         }
         return(0);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Returns broadcast address for the objects IP and supplied subnet mask.
        ///
        /// Subnet mask validity is not confirmed before performing the new value calculation so
        /// you should make sure parameter is a valid subnet mask by using <see cref="IpAddress.IsValidMask"/>
        /// method.
        ///
        /// Broadcast address is calculated by changing every bit in the subnet mask that is set to
        /// value 0 into 1 in the address value.
        /// </summary>
        /// <param name="mask">Subnet mask to apply to the object</param>
        /// <returns>New IP address containing broadcast address</returns>
        public IpAddress GetBroadcastAddress(IpAddress mask)
        {
            IpAddress im = mask.Invert();

            byte[] ip  = _data;
            byte[] m   = im.GetData();
            byte[] res = new byte[4];
            for (int i = 0; i < 4; i++)
            {
                res[i] = (byte)(ip[i] | m[i]);
            }
            return(new IpAddress(res));
        }
Beispiel #3
0
        /// <summary> Copy constructor. Constructs a duplicate object
        /// based on the passed application string object.
        /// </summary>
        /// <param name="second">The object to copy.</param>
        /// <exception cref="ArgumentException">IpAddress argument is null</exception>
        public IpAddress(IpAddress second) : this()
        {
            if (second == null)
            {
                throw new ArgumentException("Constructor argument cannot be null.");
            }

            if (!second.Valid)
            {
                _data = new byte[] { 0, 0, 0, 0 };
            }
            else
            {
                Set(second.GetData());
            }
        }
Beispiel #4
0
        /// <summary> Copy constructor. Constructs a duplicate object 
        /// based on the passed application string object.
        /// </summary>
        /// <param name="second">The object to copy.</param>
        /// <exception cref="ArgumentException">IpAddress argument is null</exception>
        public IpAddress(IpAddress second)
            : this()
        {
            if( second == null )
                throw new ArgumentException("Constructor argument cannot be null.");

            if (!second.Valid)
            {
                _data = new byte[] { 0, 0, 0, 0 };
            }
            else
            {
                Set(second.GetData());
            }
        }