System.Net.IPAddress.IsLoopback C# (CSharp) Method

IsLoopback() public static method

public static IsLoopback ( IPAddress address ) : bool
address IPAddress
return bool
        public static bool IsLoopback(IPAddress address)
        {
            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            if (address.IsIPv6)
            {
                // Do Equals test for IPv6 addresses
                return address.Equals(IPv6Loopback);
            }
            else
            {
                return ((address.PrivateAddress & LoopbackMask) == (Loopback.PrivateAddress & LoopbackMask));
            }
        }

Usage Example

Esempio n. 1
0
        /// <summary>Indicates whether to use the proxy server for the specified host.</summary>
        /// <returns>true if the proxy server should not be used for <paramref name="host" />; otherwise, false.</returns>
        /// <param name="host">The <see cref="T:System.Uri" /> instance of the host to check for proxy use. </param>
        public bool IsBypassed(System.Uri host)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }
            if (host.IsLoopback && this.bypassOnLocal)
            {
                return(true);
            }
            if (this.address == null)
            {
                return(true);
            }
            string host2 = host.Host;

            if (this.bypassOnLocal && host2.IndexOf('.') == -1)
            {
                return(true);
            }
            if (!this.bypassOnLocal)
            {
                if (string.Compare(host2, "localhost", true, CultureInfo.InvariantCulture) == 0)
                {
                    return(true);
                }
                if (string.Compare(host2, "loopback", true, CultureInfo.InvariantCulture) == 0)
                {
                    return(true);
                }
                IPAddress addr = null;
                if (IPAddress.TryParse(host2, out addr) && IPAddress.IsLoopback(addr))
                {
                    return(true);
                }
            }
            if (this.bypassList == null || this.bypassList.Count == 0)
            {
                return(false);
            }
            bool result;

            try
            {
                string input = host.Scheme + "://" + host.Authority;
                int    i;
                for (i = 0; i < this.bypassList.Count; i++)
                {
                    System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex((string)this.bypassList[i], System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline);
                    if (regex.IsMatch(input))
                    {
                        break;
                    }
                }
                if (i == this.bypassList.Count)
                {
                    result = false;
                }
                else
                {
                    while (i < this.bypassList.Count)
                    {
                        new System.Text.RegularExpressions.Regex((string)this.bypassList[i]);
                        i++;
                    }
                    result = true;
                }
            }
            catch (ArgumentException)
            {
                result = false;
            }
            return(result);
        }
All Usage Examples Of System.Net.IPAddress::IsLoopback