Ejemplo n.º 1
0
        private static void TestRoll()
        {
            IPv4 a = new IPv4(0x80808080);

            if ((uint)(a << 1) != 0x01010100)
            {
                throw new TestFailedException("<< 1");
            }

            if ((uint)(a << 2) != 0x02020200)
            {
                throw new TestFailedException("<< 2");
            }

            if ((uint)(a << 32) != 0)
            {
                throw new TestFailedException("<< 32");
            }

            if ((uint)(a >> 1) != 0x40404040)
            {
                throw new TestFailedException(">> 1");
            }

            if ((uint)(a >> 2) != 0x20202020)
            {
                throw new TestFailedException(">> 2");
            }

            if ((uint)(a >> 32) != 0)
            {
                throw new TestFailedException(">> 32");
            }
        }
Ejemplo n.º 2
0
        private static void TestParseOne(string ipString,
                                         IPv4 expectedIP,
                                         bool expectedSuccess)
        {
            bool success = false;
            IPv4 testIP  = IPv4.Zero;

            try {
                testIP  = IPv4.Parse(ipString);
                success = (testIP == expectedIP);
            }
            catch (FormatException) {
            }
            if (success != expectedSuccess)
            {
                throw new TestFailedException(ipString);
            }

            IPv4 dup = IPv4.Parse(testIP.ToString());

            if (testIP != dup)
            {
                throw new TestFailedException(
                          String.Format("ToString {0}", testIP)
                          );
            }
        }
Ejemplo n.º 3
0
        private static void TestBasics()
        {
            IPv4 a = new IPv4(0x1a2b3c4d);
            IPv4 b = IPv4.Parse("0x1a.0x2b.0x3c.0x4d");

            byte [] ipv4Bytes = new byte [4] {
                0x1a, 0x2b, 0x3c, 0x4d
            };
            IPv4 c = IPv4.ParseBytes(ipv4Bytes);

            if ((uint)a != 0x1a2b3c4d)
            {
                throw new TestFailedException("a");
            }

            if ((uint)b != 0x1a2b3c4d)
            {
                throw new TestFailedException("b");
            }

            if ((uint)c != 0x1a2b3c4d)
            {
                throw new TestFailedException("c");
            }

            if (a.Equals(b) == false)
            {
                throw new TestFailedException("a Equals b");
            }

            if ((a == c) == false || (a != c))
            {
                throw new TestFailedException("a c == !=");
            }

            for (int i = 0; i < ipv4Bytes.Length; i++)
            {
                if (ipv4Bytes[i] != c.GetAddressBytes()[i])
                {
                    throw new TestFailedException("GetAddressBytes");
                }
            }

            byte [] ipAddrBytes = ((IPAddress)c).GetAddressBytes();
            for (int i = 0; i < ipv4Bytes.Length; i++)
            {
                if (ipv4Bytes[i] != ipAddrBytes[i])
                {
                    throw new TestFailedException("IPAddress");
                }
            }

            IPAddress ipa = IPAddress.Parse(c.ToString());

            if (new IPv4(ipa) != c)
            {
                throw new TestFailedException("IPv4(IPAddress) Constructor");
            }
        }
Ejemplo n.º 4
0
 public IPv4Network(IPv4 ipNetwork, int maskLength)
 {
     if (maskLength < 0 || maskLength > IPv4.BitCount) {
         throw new ArgumentException();
     }
     this.maskLength = maskLength;
     this.ipNetwork  = ipNetwork & IPv4.NetMask(maskLength);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Get the mask length from an IPv4 address representing a netmask.
        /// </summary>
        /// <param name="netmask"></param>
        public static int GetMaskLength(IPv4 netmask)
        {
            int i = 0;

            while (netmask.GetBit(i) == true)
            {
                i++;
            }
            return(i);
        }
Ejemplo n.º 6
0
        private static void TestCompare()
        {
            IPv4 a = new IPv4(0x0a000001);
            IPv4 b = new IPv4(0x0a000000);

            if ((a == a) == false)
            {
                throw new TestFailedException("a == a");
            }

            if ((a != b) == false)
            {
                throw new TestFailedException("a == b");
            }

            if ((a > b) == false)
            {
                throw new TestFailedException("a > b");
            }

            if ((a >= b) == false)
            {
                throw new TestFailedException("a >= b");
            }

            if ((a >= a) == false)
            {
                throw new TestFailedException("a >= a");
            }

            if ((a > a) == true)
            {
                throw new TestFailedException("a > a");
            }

            if ((a < b) == true)
            {
                throw new TestFailedException("a < b");
            }

            if ((a <= b) == true)
            {
                throw new TestFailedException("a <= b");
            }

            if ((a <= a) != true)
            {
                throw new TestFailedException("a <= a");
            }

            if ((a < a) == true)
            {
                throw new TestFailedException("a < a");
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Converts an IP address string into an IPv4 instance.
 /// </summary>
 /// <exception cref="ArgumentNullException">Thrown if
 /// <c>ipString</c> is null.</exception>
 /// <returns> <c>true</c> on success, <c>false</c> on failure.</returns>
 public static bool Parse(string ipString, out IPv4 address)
 {
     try {
         address = Parse(ipString);
     }
     catch (FormatException) {
         address = IPv4.Zero;
         return(false);
     }
     return(true);
 }
Ejemplo n.º 8
0
        private static void TestBits()
        {
            IPv4 a = new IPv4(0x7f7f7f7f);
            IPv4 b = new IPv4(0xc1c1c1c1);

            if ((uint)(a | b) != ~0U)
            {
                throw new TestFailedException("OR");
            }

            if ((uint)(a & b) != 0x41414141)
            {
                throw new TestFailedException("AND");
            }

            if ((uint)(a ^ b) != 0xbebebebe)
            {
                throw new TestFailedException("XOR");
            }

            if ((uint)(~a) != 0x80808080)
            {
                throw new TestFailedException("NOT");
            }

            if ((uint)IPv4.NetMask(0) != 0)
            {
                throw new TestFailedException("NetMask(0)");
            }

            if ((uint)IPv4.NetMask(17) != 0xffff8000)
            {
                throw new TestFailedException("NetMask(17)");
            }

            if ((uint)IPv4.NetMask(32) != 0xffffffffu)
            {
                throw new TestFailedException("NetMask(32)");
            }

            try {
                IPv4 n = IPv4.NetMask(66);
                throw new TestFailedException("Bad Netmask +");
            }
            catch (ArgumentException) {
            }
            try {
                IPv4 n = IPv4.NetMask(-1);
                throw new TestFailedException("Bad Netmask -");
            }
            catch (ArgumentException) {
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        ///
        /// Parse an IPv4 network string representation into an IPv4Network.
        ///
        /// <para> The representation should either be <ipAddress/> or
        ///        <ipAddress/>/<masklength/>.
        /// </para>
        ///
        /// <para> Example forms of IPv4 Networks are: 10.0.2.0/24,
        ///        10.0.0.1.
        /// </para>
        ///
        /// </summary>
        ///
        /// <exception cref="ArgumentNullException"></exception>
        ///
        /// <exception cref="FormatException">
        /// Thrown when IP address component of format is invalid or too many
        /// slashes appear in string argument, or netmask length is not a valid
        /// integer.
        /// </exception>
        ///
        /// <exception cref="ArgumentException">
        /// Thrown when specified mask length is greater than
        /// <c>IPv4.BitCount</c>or less than zero.
        /// </exception>
        ///
        /// <exception cref="OverflowException">
        /// Netmask length overflows valid values for integers
        /// </exception>
        ///
        public static IPv4Network Parse(string ipNetwork)
        {
            if (ipNetwork == null)
                throw new ArgumentNullException("ipNetwork");

            string [] pieces = ipNetwork.Split('/');

            if (pieces.Length > 2)
                throw new FormatException("slash overload");

            int maskLength = IPv4.BitCount;
            if (pieces.Length == 2)
                maskLength = Int32.Parse(pieces[1]);

            return new IPv4Network(IPv4.Parse(pieces[0]), maskLength);
        }
Ejemplo n.º 10
0
 public int CompareTo(object other)
 {
     if (other == null)
     {
         return(1);
     }
     if (other is IPv4)
     {
         IPv4 value = (IPv4)other;
         if (this < value)
         {
             return(-1);
         }
         if (this > value)
         {
             return(+1);
         }
         return(0);
     }
     throw new ArgumentException("Arg_MustBeIPv4");
 }
Ejemplo n.º 11
0
        public static void TestCount()
        {
            for (int i = IPv4.BitCount; i >= 0; i--)
            {
                IPv4 addr = IPv4.NetMask(i);
                if (IPv4.GetMaskLength(addr) != i)
                {
                    throw new TestFailedException(
                              String.Format("TestCount {0}", i)
                              );
                }

                IPv4 addrDup = IPv4.Parse(addr.ToString());
                if (addrDup != addr)
                {
                    throw new TestFailedException(
                              String.Format("TestCountDup {0}", i)
                              );
                }
            }
        }
Ejemplo n.º 12
0
        private static void TestMath()
        {
            IPv4 a = new IPv4(0x0a000001);

            if ((uint)++a != 0x0a000002)
            {
                throw new TestFailedException("Increment");
            }

            if ((uint)--a != 0x0a000001)
            {
                throw new TestFailedException("Decrement 1");
            }

            if ((uint)--a != 0x0a000000)
            {
                throw new TestFailedException("Decrement 2");
            }

            if ((uint)--a != 0x09ffffff)
            {
                throw new TestFailedException("Decrement 3");
            }
        }
Ejemplo n.º 13
0
 public bool Contains(IPv4 ipAddress)
 {
     return (ipAddress & this.NetMask) == this.ipNetwork;
 }
Ejemplo n.º 14
0
 public IPv4Network(IPv4 networkAddress, IPv4 networkMask)
 {
     this.maskLength = IPv4.GetMaskLength(networkMask);
     this.ipNetwork  = networkAddress & IPv4.NetMask(this.maskLength);
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Create an IPv6 address representing an IPv4 node is IPv4
 /// and IPv6 capable.
 /// </summary>
 public static IPv6 CreateIPv4NodeAddress(IPv4 a)
 {
     return(new IPv6(0, 0, 0x0, (uint)a));
 }
Ejemplo n.º 16
0
        private static void TestBasics()
        {
            IPv4 address = IPv4.Parse("192.168.0.100");
            IPv4 mask    = IPv4.Parse("255.255.255.254");
            int  masklen = 31;

            if (new IPv4Network(address, masklen) !=
                new IPv4Network(address, mask))
            {
                throw new TestFailedException("Constructors");
            }

            if (IPv4Network.Parse("10.0.0.0/24") !=
                new IPv4Network(new IPv4(0x0a000000), 24))
            {
                throw new TestFailedException("10/24");
            }

            if (IPv4Network.Parse("10.0.0.0/32") !=
                new IPv4Network(new IPv4(0x0a000000), 32))
            {
                throw new TestFailedException("10/32");
            }

            if (IPv4Network.Parse("10.0.0.0/1") !=
                new IPv4Network(new IPv4(0x0a000000), 1))
            {
                throw new TestFailedException("10/1");
            }

            try {
                IPv4Network.Parse("10.0.0.1//2");
                throw new TestFailedException("double slash");
            }
            catch (FormatException) {
            }

            try {
                IPv4Network.Parse("10.0.0.0/33");
                throw new TestFailedException("netmask length");
            }
            catch (ArgumentException) {
            }

            try {
                IPv4Network.Parse("10.0.0.0/xx");
                throw new TestFailedException("netmask content");
            }
            catch (FormatException) {
            }

            try {
                IPv4Network.Parse("10.x.0.0/10");
                throw new TestFailedException("network content");
            }
            catch (FormatException) {
            }

            try {
                IPv4Network.Parse("10.0.0.0/3333333333333333333333333333");
                throw new TestFailedException("netmask overflow");
            }
            catch (OverflowException) {
            }
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Copy-constructor
 /// </summary>
 /// <param name="IPv4 other">Another IPv4 instance</param>
 public IPv4(IPv4 other)
 {
     addr = other.addr;
 }
Ejemplo n.º 18
0
        public static void TestCompare()
        {
            IPv4        address = IPv4.Parse("10.1.1.0");
            IPv4Network outer   = new IPv4Network(address, 24);
            IPv4Network inner   = new IPv4Network(address, 26);

            if (outer.Contains(outer) == false)
            {
                throw new TestFailedException("outer.Contains(outer)");
            }

            if (outer.Contains(inner) == false)
            {
                throw new TestFailedException("outer.Contains(inner)");
            }

            if (inner.Contains(outer) == true)
            {
                throw new TestFailedException("inner.Contains(outer)");
            }

            if (outer.Contains(address) == false)
            {
                throw new TestFailedException("outer.Contains(address)");
            }

            if (inner.Contains(address) == false)
            {
                throw new TestFailedException("inner.Contains(address)");
            }

            if (outer.IsMoreSpecificThan(inner) == true)
            {
                throw new TestFailedException("outer.IsMoreSpecificThan(inner)");
            }

            if (outer.IsLessSpecificThan(inner) == false)
            {
                throw new TestFailedException("outer.IsLessSpecificThan(inner)");
            }

            if ((outer == outer) == false)
            {
                throw new TestFailedException("operator==");
            }

            if ((outer != inner) == false)
            {
                throw new TestFailedException("operator!=");
            }

            if (outer.Contains(outer.UpperBound) == false)
            {
                throw new TestFailedException("outer.Contains(outer.UpperBound)");
            }

            if (outer.Contains(outer.LowerBound) == false)
            {
                throw new TestFailedException("outer.Contains(outer.LowerBound)");
            }
        }