Пример #1
0
        public bool TryParse(string range, out IIPAddressRangeValidator rangeValidator)
        {
            if (range == null)
            {
                throw new ArgumentNullException(nameof(range));
            }

            var cidrMatch = CidrBlockRegex.Match(range);

            if (cidrMatch.Success)
            {
                var baseAdrBytes = IPAddress.Parse(cidrMatch.Groups["adr"].Value).GetAddressBytes();
                var maskLen      = int.Parse(cidrMatch.Groups["maskLen"].Value, CultureInfo.InvariantCulture);
                if (baseAdrBytes.Length * 8 < maskLen)
                {
                    rangeValidator = null;
                    return(false);
                }

                var maskBytes = BitMaskHelper.GetBitMask(baseAdrBytes.Length, maskLen);
                baseAdrBytes   = baseAdrBytes.And(maskBytes).ToArray();
                rangeValidator = new IPAddressRangeValidator(new IPAddress(baseAdrBytes), new IPAddress(baseAdrBytes.Or(maskBytes.Not()).ToArray()));
                return(true);
            }

            rangeValidator = null;
            return(false);
        }
Пример #2
0
        public bool TryParse(string range, out IIPAddressRangeValidator rangeValidator)
        {
            if (range == null)
            {
                throw new ArgumentNullException(nameof(range));
            }

            var beginEndMatch = BeginEndRangeRegex.Match(range);

            if (beginEndMatch.Success)
            {
                rangeValidator = new IPAddressRangeValidator(IPAddress.Parse(beginEndMatch.Groups["begin"].Value), IPAddress.Parse(beginEndMatch.Groups["end"].Value));
                return(true);
            }

            rangeValidator = null;
            return(false);
        }
Пример #3
0
        public bool TryParse(string range, out IIPAddressRangeValidator rangeValidator)
        {
            if (range == null)
            {
                throw new ArgumentNullException(nameof(range));
            }

            var bitMaskMatch = BitMaskRangeRegex.Match(range);

            if (bitMaskMatch.Success)
            {
                var baseAdrBytes             = IPAddress.Parse(bitMaskMatch.Groups["adr"].Value).GetAddressBytes();
                IEnumerable <byte> maskBytes = IPAddress.Parse(bitMaskMatch.Groups["bitmask"].Value).GetAddressBytes();
                baseAdrBytes   = baseAdrBytes.And(maskBytes).ToArray();
                rangeValidator = new IPAddressRangeValidator(new IPAddress(baseAdrBytes), new IPAddress(baseAdrBytes.Or(maskBytes.Not()).ToArray()));
                return(true);
            }

            rangeValidator = null;
            return(false);
        }