Check if IP is in LAN (behind firewalls and routers)(检查 IP 是否在 LAN 中(在防火墙和路由器后面))
问题描述
我已经在网络上爬了大约 5 个小时,但找不到解决我的问题的方法:
I've been crawling in the web for about 5 hours now and couldn't find a solution for my problem:
我的公司正在开发一款教育游戏,我正在使用 Monotorrent 为其编写自动更新程序.该游戏将在学校使用,但由于大多数学校的互联网连接非常薄弱,因此网络中应该只有一台计算机从 httpsseed 下载,其他计算机应该从从 httpseed 下载的一台计算机窃取.
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.
所以我从跟踪器中获取了大量 IP 地址,并且只需要过滤掉 LAN 中的 IP 地址.
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.
所以简而言之,我想做的是检查给定的 IP 是否可以通过 LAN 访问.
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 :)
推荐答案
您可以利用 TTL.TTL 为 1 的数据包将无法访问互联网:
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;
}
但是,请记住,它被称为 IPv4 掩码是有原因的 - 您可以将其作为一个掩码使用(所以这是您的算法解决方案):
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 中(在防火墙和路由器后面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检查 IP 是否在 LAN 中(在防火墙和路由器后面)
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01