예제 #1
0
 private Host(HostFormat format, string domainName, IPAddress ipAddress = null, int?maskBits = null)
 {
     Format     = format;
     DomainName = domainName;
     IpAddress  = ipAddress;
     MaskBits   = maskBits;
 }
예제 #2
0
        public static bool IsHostFormat(string url,HostFormat format)
        {
            if (!IsHostFormat(url))
            {
                return false;
            }

            if (url.ToLower().EndsWith("."+format.ToString().Replace('_','.')))
            {
                return true;
            }

            return false;
        }
예제 #3
0
        public static bool TryParse(string value, HostFormat validFormats, out Host host)
        {
            host = null;

            HostFormat format;
            string     domainName;
            string     ipAddress;
            int?       maskBits;
            Match      match = _cidrRegex.Match(value);
            IPAddress  parsedIpAddress;

            if (match.Success)
            {
                ipAddress = match.Groups["IpAddress"].Value;
                maskBits  = int.Parse(match.Groups["Cidr"].Value);
            }
            else
            {
                ipAddress = value;
                maskBits  = null;
            }
            if (IPAddress.TryParse(ipAddress, out parsedIpAddress))
            {
                switch (parsedIpAddress.AddressFamily)
                {
                case AddressFamily.InterNetwork:
                    if (maskBits == null && !validFormats.HasFlag(HostFormat.IPv4) || maskBits != null && (!validFormats.HasFlag(HostFormat.IPv4Cidr) || maskBits > 32))
                    {
                        return(false);
                    }
                    format = maskBits != null ? HostFormat.IPv4Cidr : HostFormat.IPv4;
                    break;

                case AddressFamily.InterNetworkV6:
                    if (maskBits == null && !validFormats.HasFlag(HostFormat.IPv6) || maskBits != null && (!validFormats.HasFlag(HostFormat.IPv6Cidr) || maskBits > 128))
                    {
                        return(false);
                    }
                    format = maskBits != null ? HostFormat.IPv6Cidr : HostFormat.IPv6;
                    break;

                default:
                    throw new ArgumentException($"Unexpected address family '{parsedIpAddress.AddressFamily}'.");
                }

                domainName = null;
            }
            else
            {
                format = value.StartsWith(".", StringComparison.OrdinalIgnoreCase) ? HostFormat.DomainNameSuffix : HostFormat.DomainName;

                if (!validFormats.HasFlag(format))
                {
                    return(false);
                }

                domainName = value;
            }

            host = new Host(format, domainName, parsedIpAddress, maskBits);

            return(true);
        }