检查IP是否在LAN中(防火墙和路由器后面) [英] Check if IP is in LAN (behind firewalls and routers)

查看:115
本文介绍了检查IP是否在LAN中(防火墙和路由器后面)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经在网上爬了大约5个小时了,找不到我的问题的解决方案:



我的公司正在开发一款教育游戏,我正在使用Monotorrent为它编写一个autoupdater。该游戏将在学校中使用,但由于大多数学校只有非常弱的互联网连接,因此网络中只应有一台从httpseeder下载的计算机,其他计算机应该从一台从httpseed下载的计算机中获取。 / p>

所以我从追踪器中获取了大量的IP地址,只需要过滤掉局域网中的IP地址。



当然,学校有时对防火墙非常严格,学校的某些计算机之间会有大量的路由器和交换机。



我已经尝试过多数解决方案,比如

  NetworkInterface [] interfaces = NetworkInterface.GetAllNetworkInterfaces(); 

foreach(接口中的NetworkInterface iface)
{
IPInterfaceProperties properties = iface.GetIPProperties();

foreach(properties.UnicastAddresses中的UnicastIPAddressInformation地址)
{
Console.WriteLine(
{0}(掩码:{1}),
address.Address,
address.IPv4Mask
);
}
}

或者类似的技术只提供路由器的信息/切换/无论如何。



简而言之,我想做的是检查是否可以通过LAN访问给定的IP。



我真的很感激任何帮助,因为这个功能是剩下的最后一个:)

解决方案

你可以采取TTL的优势。如果TTL为1,数据包将无法进入互联网:

  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
});
返回rep.Status!= IPStatus.TtlExpired&& rep.Status!= IPStatus.TimedOut&& rep.Status!= IPStatus.TimeExceeded;
}

但请记住,出于某种原因,它被称为IPv4掩码 - 你可以将它用作一个(所以这是你的算法解决方案):

  private static bool IsLanIP(IPAddress address)
{
var interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach(接口中的var iface)
{
var properties = iface.GetIPProperties();
foreach(property.UnicastAddresses中的var ifAddr)
{
if(ifAddr.IPv4Mask!= null&&
ifAddr.Address.AddressFamily == AddressFamily.InterNetwork& &
CheckMask(ifAddr.Address,ifAddr.IPv4Mask,address))
返回true;
}
}
返回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]&米;
int b = bb [i]&米;

如果(a!= b)
返回false;
}

返回true;
}


I've been crawling in the web for about 5 hours now and couldn't find a solution for my problem:

My company is developing an educational game and I'm writing an autoupdater for it using Monotorrent. The game will be used in schools, but because most schools only have very weak internet connections there should only be one computer in the network that downloads from a httpseeder, and the others should leech from the one computer that is downloading from the httpseed.

So I get loads of IP-addresses from the tracker and need to filter out only the ones that are in the LAN.

Of course schools are sometimes quite strict with firewalls and there will be loads of routers and switches between some computers in a school.

I've already tried most solutions, things like

 NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();

    foreach (NetworkInterface iface in interfaces)
    {
        IPInterfaceProperties properties = iface.GetIPProperties();

        foreach (UnicastIPAddressInformation address in properties.UnicastAddresses)
        {
            Console.WriteLine(
                "{0} (Mask: {1})",
                address.Address,
                address.IPv4Mask
                );
        }
    }

Or similar techniques only deliver the information of the router/switch/whatever.

So in a nutshell, what I want to do is check if a given IP is accessible via LAN.

I'd really appreciate any help because this feature is the last one remaining :)

解决方案

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;
}

这篇关于检查IP是否在LAN中(防火墙和路由器后面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆