Check if IP is in LAN (behind firewalls and routers)

≡放荡痞女 提交于 2019-12-02 23:39:25

You could take advantage of TTL. With a TTL of 1 the packet won't be able to make it to the internet:

private static bool IsLanIP(IPAddress address)
{
    var ping = new Ping();
    var rep = ping.Send(address, 100, new byte[] { 1 }, new PingOptions()
    {
        DontFragment = true,
        Ttl = 1
    });
    return rep.Status != IPStatus.TtlExpired && rep.Status != IPStatus.TimedOut && rep.Status != IPStatus.TimeExceeded;
}

However, remember that it is called an IPv4 mask for a reason - you can use it as one (so here is your algorithmic solution):

private static bool IsLanIP(IPAddress address)
{
    var interfaces = NetworkInterface.GetAllNetworkInterfaces();
    foreach (var iface in interfaces)
    {
        var properties = iface.GetIPProperties();
        foreach (var ifAddr in properties.UnicastAddresses)
        {
            if (ifAddr.IPv4Mask != null && 
                ifAddr.Address.AddressFamily == AddressFamily.InterNetwork &&
                CheckMask(ifAddr.Address, ifAddr.IPv4Mask, address))
                return true;
        }
    }
    return false;
}

private static bool CheckMask(IPAddress address, IPAddress mask, IPAddress target)
{
    if (mask == null)
        return false;

    var ba = address.GetAddressBytes();
    var bm = mask.GetAddressBytes();
    var bb = target.GetAddressBytes();

    if (ba.Length != bm.Length || bm.Length != bb.Length)
        return false;

    for (var i = 0; i < ba.Length; i++)
    {
        int m = bm[i];

        int a = ba[i] & m;
        int b = bb[i] & m;

        if (a != b)
            return false;
    }

    return true;
}

Typically any IPs like 10.x.x.x (Class A) or 192.x.x.x (Class C) can be safely assumed to be inside a private local area network. IP Classications

One thing that you could possibly use is to try and communicate between clients using multicast. Most firewalls and routers would block multicast traffic (and ISPs most definitely), meaning that you wouldn't be able to join a multicast group if no other client is on the lan. A dumb switch would pass on the traffic, a layer 3-switch might block it, or could allow it depending on configuration. Either way, if the layer 3 switch block it, you are probably on different subnets altogether anyway so all other options would fail as well.

One technology that comes to mind is SSDP ( http://en.wikipedia.org/wiki/Simple_Service_Discovery_Protocol ) which would serve your purpose pretty good I believe. That way you don't really need to figure out if you are on a LAN or not, just search for another node that is actively downloading, if you can't find one, start downloading yourself.

Since SSDP is a standard used in uPnP you would probably be able to find decent implementations you could work with.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!