检查输入范围内的IP地址

时间:2012-09-17 12:28:59

标签: c# ip

  

可能重复:
  How to check a input IP fall in a specific IP range

我想检查一下IP地址是否属于私有IP地址范围。我在.NET中找不到任何方法,我不确定如何自己构建它?

例如CheckIsIpPublic("192.168.0.1", upperIp, lowerIp) //should return false

例如CheckIsIpPublic("78.60.143.29", upperIp, lowerIp) //should return true

var upperIp = new List<IPAddress>
             {
               IPAddress.Parse("192.168.0.0"),
               IPAddress.Parse("10.0.0.0"),
               IPAddress.Parse("172.16.0.0"),
               IPAddress.Parse("224.0.0.0")
             };

var lowerIp = new List<IPAddress>
             {
               IPAddress.Parse("192.168.255.255"), 
               IPAddress.Parse("10.255.255.255"), 
               IPAddress.Parse("172.31.255.255"), 
               IPAddress.Parse("255.255.255.255")
             };

3 个答案:

答案 0 :(得分:7)

使用IPNetwork class from Codeplex,您可以执行以下操作:

//see http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses
ReadOnlyCollection<IPNetwork> reservedNetworks =
    new ReadOnlyCollection<IPNetwork>(new[]
        {
            IPNetwork.Parse("0.0.0.0/8"),
            IPNetwork.Parse("10.0.0.0/8"),
            IPNetwork.Parse("127.0.0.0/8"),
            IPNetwork.Parse("169.254.0.0/16"),
            IPNetwork.Parse("172.16.0.0/12"),
            IPNetwork.Parse("192.0.2.0/24"),
            IPNetwork.Parse("192.88.99.0/24"),
            IPNetwork.Parse("192.168.0.0/16"),
            IPNetwork.Parse("198.18.0.0/15"),
            IPNetwork.Parse("198.51.100.0/24"),
            IPNetwork.Parse("203.0.113.0/24"),
            IPNetwork.Parse("224.0.0.0/4"),
            IPNetwork.Parse("240.0.0.0/4")
        });

var isInRange = 
  !reservedNetworks.Any(n => IPNetwork.Contains(n, suspiciousIpAddress));

答案 1 :(得分:1)

public struct IpRange
{
    public IPAddress LowerIP;
    public IPAddress UpperIP;

    public IpRange(IPAddress lowerIP, IPAddress upperIP)
    {
        LowerIP = lowerIP;
        UpperIP = upperIP;
    }
}
    public bool CheckIsIpPublic(string adress, List<IpRange> rangeList)
    {
        foreach (var range in rangeList)
        {
            List<int> adressInt = adress.Split('.').Select(str => int.Parse(str)).ToList();
            List<int> lowerInt = range.LowerIP.ToString().Split('.').Select(str => int.Parse(str)).ToList();
            List<int> upperInt = range.UpperIP.ToString().Split('.').Select(str => int.Parse(str)).ToList();

            if (adressInt[0] >= lowerInt[0] && adressInt[0] < upperInt[0])
            {
                return true;
            }
            else if (adressInt[0] >= lowerInt[0] && adressInt[0] == upperInt[0])
            {
                if (adressInt[1] >= lowerInt[1] && adressInt[1] < upperInt[1])
                {
                    return true;
                }
                else if (adressInt[1] >= lowerInt[1] && adressInt[1] == upperInt[1])
                {
                    if (adressInt[2] >= lowerInt[2] && adressInt[2] < upperInt[2])
                    {
                        return true;
                    }
                    else if (adressInt[2] >= lowerInt[2] && adressInt[2] == upperInt[2])
                    {
                        if (adressInt[3] >= lowerInt[3] && adressInt[3] <= upperInt[3])
                        {
                            return true;
                        }
                    }

                }

            }
        }
        return false;
    }

并运行此东西你应该使用

var rangeList = new List<IpRange>();
rangeList.Add(new IpRange(IPAddress.Parse("192.168.0.0"),IPAddress.Parse("192.168.255.255")));
rangeList.Add(new IpRange(IPAddress.Parse("10.0.0.0"),IPAddress.Parse("10.255.255.255")));
rangeList.Add(new IpRange(IPAddress.Parse("172.16.0.0"),IPAddress.Parse("172.31.255.255")));
rangeList.Add(new IpRange(IPAddress.Parse("224.0.0.0"),IPAddress.Parse("255.255.255.255")));
bool result = CheckIsIpPublic("78.60.143.29", rangeList);

答案 2 :(得分:0)

以下内容可行(未编译):

public bool IsPrivateIp(string ipAddress)
{
    return (ipAddress.StartsWith("192.168") || ipaddress.Equals("127.0.0.1") || ipAddress.StartsWith("10.0")) ;
}

如果为内部IP地址(10.0.0.0 - 10.0.255.255)留出整个范围,效果最佳。