コード例 #1
0
ファイル: IPv4Network.cs プロジェクト: Paul1nh0/Singularity
        /// <summary>
        /// Exception-lite IPv4 network address parser.
        /// </summary>
        ///
        /// <returns><c>true</c> on success, <c>false</c> if supplied address
        /// is not a valid IPv4 string representation.
        /// </returns>
        ///
        /// <exception cref = "ArgumentNullException"></exception>
        ///
        public static bool Parse(string ipNetwork, out IPv4Network network)
        {
            try {
                network = Parse(ipNetwork);
                return(true);
            }
            catch (ArgumentException) {
            }
            catch (FormatException) {
            }
            catch (OverflowException) {
            }

            network = Default;
            return(false);
        }
コード例 #2
0
ファイル: IPv4Network.cs プロジェクト: Paul1nh0/Singularity
 public bool Contains(IPv4Network network)
 {
     return ( (maskLength <= network.maskLength) &&
              ((network.ipNetwork & this.NetMask) == this.ipNetwork) );
 }
コード例 #3
0
ファイル: IPv4Network.cs プロジェクト: Paul1nh0/Singularity
 public bool IsLessSpecificThan(IPv4Network other)
 {
     return maskLength < other.maskLength;
 }
コード例 #4
0
ファイル: IPv4Network.cs プロジェクト: Paul1nh0/Singularity
 public bool IsMoreSpecificThan(IPv4Network other)
 {
     return maskLength > other.maskLength;
 }
コード例 #5
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)");
            }
        }
コード例 #6
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) {
            }
        }