开发者

How to test if hostname refers to local machine

开发者 https://www.devze.com 2022-12-14 23:48 出处:网络
Can anyone think of an easy way to tell in win32 or .NET if hostname (string) resolves to a local computer? Such as:

Can anyone think of an easy way to tell in win32 or .NET if hostname (string) resolves to a local computer? Such as:

"myhostname"
"myhostname.mydomain.local"
"192.168.1.1"
"localhost"

The goal of this exercise is to produce a test which will tell if Windows security layer will treat access to machi开发者_如何学JAVAne as local or network


In .NET you can:

IPHostEntry iphostentry = Dns.GetHostEntry (Dns.GetHostName ());

Then for any host name, check if it resolves to one of the IPs in iphostEntry.AddressList (this is an IPAddress[]).

Here is a full program that will check the host names/IP addresses passed in the command line:

using System;
using System.Net;

class Test {
    static void Main (string [] args)
    {
        IPHostEntry iphostentry = Dns.GetHostEntry (Dns.GetHostName ());
        foreach (string str in args) {
            IPHostEntry other = null;
            try {
                other = Dns.GetHostEntry (str);
            } catch {
                Console.WriteLine ("Unknown host: {0}", str);
                continue;
            }
            foreach (IPAddress addr in other.AddressList) {
                if (IPAddress.IsLoopback (addr) || Array.IndexOf (iphostentry.AddressList, addr) != -1) {
                    Console.WriteLine ("{0} IsLocal", str);
                    break;
                } 
            }
        }
    }
}


This question has already been answered, but here's what I came up with to resolve both host name and IP address:

public static bool IsLocalHost(string host)
{
    IPHostEntry localHost = Dns.GetHostEntry(Dns.GetHostName());

    IPAddress ipAddress = null;

    if (IPAddress.TryParse(host, out ipAddress))
        return localHost.AddressList.Any(x => x.Equals(ipAddress));

    IPHostEntry hostEntry = Dns.GetHostEntry(host);

    return localHost.AddressList.Any(x => hostEntry.AddressList.Any(y => x.Equals(y)));
}


You can get the IP address that the hostname resolves to by writing Dns.Resolve(hostName).AddressList[0].ToString().

You can then compare that to 127.0.0.1 or to the computer's local IP address.

You can get the computer's local IP addresses by looping through System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList.


My slightly updated solution from PhilDulac, at first checks if localhost is aliased with some alias defined in hosts/etc, other part is solution from PhilDulac.

public static bool IsLocalHost(Uri uri)
{
    bool isLocalhost = false;
    string hostName = uri.Host;

    IPHostEntry localhost = Dns.GetHostEntry("127.0.0.1");
    if (uri.Host == localhost.HostName)
    {
        IPHostEntry hostEntry = Dns.GetHostEntry(hostName);
        isLocalhost = hostEntry.AddressList.Any(IPAddress.IsLoopback);
    }

    if (!isLocalhost)
    {
        localhost = Dns.GetHostEntry(Dns.GetHostName());

        IPAddress ipAddress = null;

        if (IPAddress.TryParse(hostName, out ipAddress))
            isLocalhost = localhost.AddressList.Any(x => x.Equals(ipAddress));

        if (!isLocalhost)
        {
            try
            {
                var hostEntry = Dns.GetHostEntry(hostName);
                isLocalhost = localhost.AddressList.Any(x => hostEntry.AddressList.Any(x.Equals));
            }
            catch (SocketException e)
            {
                Debug.WriteLine(e);
            }
        }
    }

    return isLocalhost;
}


In .net you should be able to use Request.ServerVariables(“REMOTE_ADDR”); to get the host ip address and then compare it with the resolved ip address of the hostname. Isn't that what you wanted?

0

精彩评论

暂无评论...
验证码 换一张
取 消