如何检查是否一个IP地址是一个特定的子网中 [英] How to check if an IP address is within a particular subnet

查看:283
本文介绍了如何检查是否一个IP地址是一个特定的子网中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的格式为10.132.0.0/20,并从ASP.Net请求对象的IP地址的子网。



有一个.NET框架的功能请检查IP地址为给定的子网内?



如果没有,怎么能做到呢?位操作,我猜?


解决方案

看看的 IP地址与MSDN博客C#计算。它包含一个扩展方法( IsInSameSubnet )应该满足您的需求,以及一些其他的东西。



<预类=郎-CS prettyprint-覆盖> 公共静态类IPAddressExtensions
{
公共静态ip地址GetBroadcastAddress(这个ip地址的地址,ip地址子网掩码)
{
字节[ ] ipAdressBytes = address.GetAddressBytes();
字节[] = subnetMaskBytes subnetMask.GetAddressBytes();

如果(ipAdressBytes.Length = subnetMaskBytes.Length!)
抛出新的ArgumentException(IP的长度地址和子网掩码不匹配。);

字节[] = broadcastAddress新的字节[ipAdressBytes.Length]
的for(int i = 0; I< broadcastAddress.Length;我++)
{
broadcastAddress [I] =(字节)(ipAdressBytes [我] |(subnetMaskBytes [I] ^ 255 ));
}
返回新的ip地址(broadcastAddress);
}

公共静态ip地址GetNetworkAddress(这个ip地址的地址,ip地址子网掩码)
{
字节[] = ipAdressBytes address.GetAddressBytes();
字节[] = subnetMaskBytes subnetMask.GetAddressBytes();

如果(ipAdressBytes.Length = subnetMaskBytes.Length!)
抛出新的ArgumentException(IP的长度地址和子网掩码不匹配。);

字节[] = broadcastAddress新的字节[ipAdressBytes.Length]
表示(INT I = 0; I&下; broadcastAddress.Length;我++)
{
broadcastAddress [I] =(字节)(ipAdressBytes [I]及(subnetMaskBytes [I]) );
}
返回新的ip地址(broadcastAddress);
}

公共静态布尔IsInSameSubnet(这个ip地址1地址,ip地址地址,ip地址子网掩码)
{
ip地址网络1 = address.GetNetworkAddress(子网掩码);
ip地址网络2 = address2.GetNetworkAddress(子网掩码);

返回network1.Equals(网络2);
}
}


I have a subnet in the format 10.132.0.0/20 and an IP address from the ASP.Net request object.

Is there a .NET framework function to check to see if the IP address is within the given subnet?

If not, how can it be done? Bit manipulation, I guess?

解决方案

Take a look at IP Address Calculations with C# on MSDN blogs. It contains an extension method (IsInSameSubnet) that should meet your needs as well as some other goodies.

public static class IPAddressExtensions
{
    public static IPAddress GetBroadcastAddress(this IPAddress address, IPAddress subnetMask)
    {
        byte[] ipAdressBytes = address.GetAddressBytes();
        byte[] subnetMaskBytes = subnetMask.GetAddressBytes();

        if (ipAdressBytes.Length != subnetMaskBytes.Length)
            throw new ArgumentException("Lengths of IP address and subnet mask do not match.");

        byte[] broadcastAddress = new byte[ipAdressBytes.Length];
        for (int i = 0; i < broadcastAddress.Length; i++)
        {
            broadcastAddress[i] = (byte)(ipAdressBytes[i] | (subnetMaskBytes[i] ^ 255));
        }
        return new IPAddress(broadcastAddress);
    }

    public static IPAddress GetNetworkAddress(this IPAddress address, IPAddress subnetMask)
    {
        byte[] ipAdressBytes = address.GetAddressBytes();
        byte[] subnetMaskBytes = subnetMask.GetAddressBytes();

        if (ipAdressBytes.Length != subnetMaskBytes.Length)
            throw new ArgumentException("Lengths of IP address and subnet mask do not match.");

        byte[] broadcastAddress = new byte[ipAdressBytes.Length];
        for (int i = 0; i < broadcastAddress.Length; i++)
        {
            broadcastAddress[i] = (byte)(ipAdressBytes[i] & (subnetMaskBytes[i]));
        }
        return new IPAddress(broadcastAddress);
    }

    public static bool IsInSameSubnet(this IPAddress address2, IPAddress address, IPAddress subnetMask)
    {
        IPAddress network1 = address.GetNetworkAddress(subnetMask);
        IPAddress network2 = address2.GetNetworkAddress(subnetMask);

        return network1.Equals(network2);
    }
}

这篇关于如何检查是否一个IP地址是一个特定的子网中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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