IsLoopback() public static method

public static IsLoopback ( IPAddress address ) : bool
address IPAddress
return bool
Exemplo 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);
        }
Exemplo n.º 2
0
        public bool IsBypassed(Uri host)
        {
#if NET_2_0
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }
#endif

            if (host.IsLoopback && bypassOnLocal)
            {
                return(true);
            }

            if (address == null)
            {
                return(true);
            }

            string server = host.Host;
            if (bypassOnLocal && server.IndexOf('.') == -1)
            {
                return(true);
            }

            // LAMESPEC
            if (!bypassOnLocal)
            {
                if (String.Compare(server, "localhost", true, CultureInfo.InvariantCulture) == 0)
                {
                    return(true);
                }
                if (String.Compare(server, "loopback", true, CultureInfo.InvariantCulture) == 0)
                {
                    return(true);
                }

                try {
                    IPAddress addr = IPAddress.Parse(server);
                    if (IPAddress.IsLoopback(addr))
                    {
                        return(true);
                    }
                } catch {}
            }

            if (bypassList == null)
            {
                return(false);
            }

            try {
                string hostStr = host.Scheme + "://" + host.Authority;
                int    i       = 0;
                for (; i < bypassList.Count; i++)
                {
                    Regex regex = new Regex((string)bypassList [i],
                                            // TODO: RegexOptions.Compiled |  // not implemented yet by Regex
                                            RegexOptions.IgnoreCase |
                                            RegexOptions.Singleline);

                    if (regex.IsMatch(hostStr))
                    {
                        break;
                    }
                }

                if (i == bypassList.Count)
                {
                    return(false);
                }

                // continue checking correctness of regular expressions..
                // will throw expression when an invalid one is found
                for (; i < bypassList.Count; i++)
                {
                    new Regex((string)bypassList [i]);
                }

                return(true);
            } catch (ArgumentException) {
                return(false);
            }
        }
Exemplo n.º 3
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(Uri host)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }
            if (host.IsLoopback && bypassOnLocal)
            {
                return(true);
            }
            if (address == null)
            {
                return(true);
            }
            string host2 = host.Host;

            if (bypassOnLocal && host2.IndexOf('.') == -1)
            {
                return(true);
            }
            if (!bypassOnLocal)
            {
                if (string.Compare(host2, "localhost", ignoreCase: true, CultureInfo.InvariantCulture) == 0)
                {
                    return(true);
                }
                if (string.Compare(host2, "loopback", ignoreCase: true, CultureInfo.InvariantCulture) == 0)
                {
                    return(true);
                }
                IPAddress addr = null;
                if (IPAddress.TryParse(host2, out addr) && IPAddress.IsLoopback(addr))
                {
                    return(true);
                }
            }
            if (bypassList == null || bypassList.Count == 0)
            {
                return(false);
            }
            try
            {
                string input = host.Scheme + "://" + host.Authority;
                int    i;
                for (i = 0; i < bypassList.Count; i++)
                {
                    Regex regex = new Regex((string)bypassList[i], RegexOptions.IgnoreCase | RegexOptions.Singleline);
                    if (regex.IsMatch(input))
                    {
                        break;
                    }
                }
                if (i == bypassList.Count)
                {
                    return(false);
                }
                for (; i < bypassList.Count; i++)
                {
                    new Regex((string)bypassList[i]);
                }
                return(true);

IL_0189:
                bool result;
                return(result);
            }
            catch (ArgumentException)
            {
                return(false);

IL_0197:
                bool result;
                return(result);
            }
        }
Exemplo n.º 4
0
        /// <devdoc>
        /// Determines if the host Uri should be routed locally or go through the proxy.
        /// </devdoc>
        private bool IsLocal(Uri host)
        {
            string hostString = host.Host;

            // If and only if the host has a colon or is all numbers and dots, it is an IP address.
            // If no dot, it is local.  (If it has both a dot and a colon, undefined.)
            int  dot   = -1;
            bool isIp4 = true;
            bool isIp6 = false;

            for (int i = 0; i < hostString.Length; i++)
            {
                if (hostString[i] == '.')
                {
                    if (dot == -1)
                    {
                        dot = i;
                        if (!isIp4)
                        {
                            break;
                        }
                    }

                    continue;
                }

                if (hostString[i] == ':')
                {
                    isIp6 = true;
                    isIp4 = false;
                    break;
                }

                if (hostString[i] < '0' || hostString[i] > '9')
                {
                    isIp4 = false;
                    if (dot != -1)
                    {
                        break;
                    }
                }
            }

            // No dot?  Local.
            // The case of all digits is a little weird.  Technically it could be a v4 IP, but practically it can only
            // be interpreted as an intranet name.
            if (dot == -1 && !isIp6)
            {
                return(true);
            }

            if (isIp4 || isIp6)
            {
                // I don't see why this would fail, but in case it does, it's not an IP.
                try
                {
                    IPAddress hostAddress = IPAddress.Parse(hostString);
                    if (IPAddress.IsLoopback(hostAddress))
                    {
                        return(true);
                    }
                    return(NclUtilities.IsAddressLocal(hostAddress));
                }
                catch (FormatException) { }
            }

            // If it matches the primary domain, it's local.  (Whether or not the hostname matches.)
            string local = "." + IPGlobalProperties.InternalGetIPGlobalProperties().DomainName;

            if (local != null && local.Length == (hostString.Length - dot) &&
                string.Compare(local, 0, hostString, dot, local.Length, StringComparison.OrdinalIgnoreCase) == 0)
            {
                return(true);
            }
            return(false);
        }
 /// <summary>
 /// Determines whether the specified IPAdress is loopback.
 /// </summary>
 /// <param name="this">The this.</param>
 /// <returns>
 ///   <c>true</c> if the specified IPAdress is loopback; otherwise, <c>false</c>.
 /// </returns>
 public static bool IsLoopback(this IPAddress @this)
 => IPAddress.IsLoopback(@this) || new[] { IPAddress.Any, IPAddress.IPv6Any, IPAddress.Loopback, IPAddress.IPv6Loopback }.Contains(@this)
 ;
Exemplo n.º 6
0
        /*++
         *
         *  SetAddressList - Set the list of IP addresses for this service point.
         *
         *  This is a method called when the underlying code has resolved the
         *  destination to a list of IP addressess. We actually maintain some
         *  information about the viable IP addresses for this service point,
         *  like whether or not they're loopback, etc. So we walk through the
         *  list and set up an array of structures corresponding to the IP
         *  addresses.
         *
         *  Note that it's possible to have some IP addresses be loopbacked
         *  and some not. This can happen if a server name maps to multiple
         *  physical servers and we're one of those servers.
         *
         *  Input:
         *          addressList     - Array of resolved IP addresses.
         *
         *  Returns: Nothing.
         *
         * --*/
        private IPHostEntryInfo SetAddressList(IPHostEntry ipHostEntry)
        {
            GlobalLog.Print("SetAddressList(" + ipHostEntry.HostName + ")");
            //
            // Create an array of IPAddressInfo of the appropriate size, then
            // get a list of our local addresses. Walk through the input
            // address list. Copy each address in the address list into
            // our array, and if the address is a loopback address, mark it as
            // such.
            //
            IPAddress[]     addressList = ipHostEntry.AddressList;
            IPAddressInfo[] TempInfo    = new IPAddressInfo[addressList.Length];

            //
            // Walk through each member of the input list, copying it into our temp array.
            //
            int         i, k;
            IPHostEntry ipLocalHostEntry = null;

            try {
                ipLocalHostEntry = Dns.LocalHost;
            }
            catch (Exception exception) {
                GlobalLog.Print("ServicePoint#" + ValidationHelper.HashString(this) + "::SetAddressList() Dns.LocalHost threw exception:[" + ValidationHelper.ToString(exception) + "]");
            }
            for (i = 0; i < addressList.Length; i++)
            {
                TempInfo[i] = new IPAddressInfo(addressList[i]);

                // First, check to see if the current address is a  loopback
                // address.

                if (IPAddress.IsLoopback(TempInfo[i].Address))
                {
                    TempInfo[i].IsLoopback = true;
                    continue;
                }

                // See if the current IP address is a local address, and if
                // so mark it as such.
                if (ipLocalHostEntry != null)
                {
                    for (k = 0; k < ipLocalHostEntry.AddressList.Length; k++)
                    {
                        //
                        // IPv6 Changes: Use .Equals for this check !
                        //
                        if (TempInfo[i].Address.Equals(ipLocalHostEntry.AddressList[k]))
                        {
                            TempInfo[i].IsLoopback = true;
                            break;
                        }
                    }
                }
            }

            //
            // now look for connection group objects that don't have
            // an IP address associated with them. When it finds one, it updates
            // the IP address and tries to update the connection limit.
            //
            lock (this) {
                // Walk through the connection groups on this service
                // point, and update the ones we need to with an IP
                // address.
                //

                IDictionaryEnumerator CGEnumerator;

                CGEnumerator = m_ConnectionGroupList.GetEnumerator();

                while (CGEnumerator.MoveNext())
                {
                    ConnectionGroup CurrentCG = (ConnectionGroup)CGEnumerator.Value;

                    // If this connection group doesn't have an IP address
                    // assigned to it, give it one.

                    if (CurrentCG.RemoteIPAddress == null)
                    {
                        GlobalLog.Print("ServicePoint::UpdateConnectionGroupAddresses null CurrentCG.RemoteIPAddress");

                        // Update the address.

                        CurrentCG.RemoteIPAddress = TempInfo[0].Address;

                        // Now update the connection limit based on what we know.

                        CurrentCG.InternalConnectionLimit = TempInfo[0].IsLoopback ? LoopbackConnectionLimit : m_ConnectionLimit;

                        GlobalLog.Print("ServicePoint::UpdateConnectionGroupAddresses CurrentCG.InternalConnectionLimit:" + CurrentCG.InternalConnectionLimit.ToString());
                    }

                    GlobalLog.Print("ServicePoint::UpdateConnectionGroupAddresses CurrentCG.RemoteIPAddress:" + CurrentCG.RemoteIPAddress.ToString());
                }
            }

            IPHostEntryInfo ipAddressInfoList = new IPHostEntryInfo(TempInfo, ipHostEntry.HostName);

            return(ipAddressInfoList);
        }
Exemplo n.º 7
0
        public static bool IsLocal(this IPAddress ipAddress)
        {
            var addressList = Dns.GetHostEntry(string.Empty).AddressList;

            return(IPAddress.IsLoopback(ipAddress) || addressList.Any(localIp => localIp.Equals(ipAddress)));
        }