简体   繁体   English

使用现有IP地址范围验证IP地址

[英]validating IP address with existing range of ip address

I need to check whether the ip address resides within the range of IP. 我需要检查IP地址是否位于IP范围内。

For example 10-20.3.5.6 contains IP as 例如10-20.3.5.6包含IP作为

10.3.5.6 11.3.5.6 . 10.3.5.6 11.3.5.6。 . . 20.3.5.6 20.3.5.6

I need to know the following things: 我需要知道以下几点:

1) 1-10.0-255.0.0-255 is the IP range I have in DB. 1)1-10.0-255.0.0-255是我在DB中拥有的IP范围。 I need to check if the IP "10.0.7.8" falls in the above specified range. 我需要检查IP“ 10.0.7.8”是否在上述指定范围内。

2) Consider another scenario is 10-20.10-20.10-20.10-20 I have in DB. 2)考虑另一种情况是我在DB中有10-20.10-20.10-20.10-20。 I need to check if any one of the IP in range 10-20.10.10-30.10-50 falls in above range? 我需要检查10-20.10.10-30.10-50范围内的任何IP是否在上述范围内?

Lets try something like this... 让我们尝试这样的事情...

 ArrayList<String> IPlist = getIPTable(); //IPTable has entries
 Pair IPranges[4] = new Pair[4]();

 //Pair isa class that holds two things, like numbers;

 //parse your DB entries into the IPranges
 IPranges[0].low = 10; IPranges[0].hi = 20;
 IPranges[1].low = 3; IPranges[1].hi = 3;
 IPranges[2].low = 5; IPranges[2].hi = 5;
 IPranges[3].low = 6; IPranges[3].hi = 6;

 for(String IP : IPlist)
 { 
      String octet[4]; //read the octets in the String[] octet using substring();
      boolean continue = true; 
      for(int i = 0 ; i < 4 && continue; i++)
          if(Integer.parseInt(octet[i]) >= IPtable[i].low && Integer.parseInt(octet[i]) <= IPtable[i].hi)
          {
              //this is good, do stuff here
          }
          else
             continue = false;  //the IP failed to pass the filter, no need to process the rest of it
         //safely add the IP to the lsit of valid IPs.
       validIP.add(IP);
}



private class Pair
{
    public int low, hi;

    public Pair(int low, int hi)
    {
        this.low = low; this.hi = hi;
    }
} //feel free to extend this class to do the checking for you.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM