0

I have the below code, that is basically printing something based on ipaddress.

    private static void getInfo(String ipAddress) {
for (String cidr : myNetworkList) {             
                if (InetAddressValidator.getInstance().isValid(ipAddress)) {                    
                    if (cidr.equals(ipAddress)) {
                        //Do something
                        break;
                    }
                }
                 else {
                    SubnetUtils subnetUtils = new SubnetUtils(cidr);
                    if (subnetUtils.getInfo().isInRange(ipAddress)) {                       
                        //Do something else             
                        break;
                    }
                }


            }       
}

The method works perfectly fine when I give a valid IP address as an argument such as - 17.151.126.28. If however, I give IP address in the CIDR notation, such as - 17.24.84.0/24, I get the below error -

Exception in thread "main" java.lang.IllegalArgumentException: Could not parse [17.24.84.0/24]

I have no idea how to resolve this. I read somewhere the issue is with Subnet since it doesn't support IpV4 addresses. I am not a networking expert, so I have no clue if the issue is with subnet, or if I should be using an alternate library?

  • At what line is the exception thrown? – LaurentG Jul 23 '13 at 05:54
  • It is thrown at this line Laurent - if (subnetUtils.getInfo().isInRange(ipAddress)) – user2609271 Jul 23 '13 at 06:01
  • SSCCE is exactly what I have pasted above. When I invoke the method from main, and give the IP address - 17.16.164.0/24, I get the exception. I am using apache-commons-3.0.1 – user2609271 Jul 23 '13 at 06:03
  • @user2609271: see my answer below. You haven't posted an almost [SSCCE](http://sscce.org): it's not self contained: If it were a SSCCE, I could just copy, paste, compile and run it to reproduce the error you are seeing. – jlordo Jul 23 '13 at 06:07

2 Answers2

1

Read JavaDoc of isInRange(String address)

Parameters: address - A dot-delimited IPv4 address, e.g. "192.168.0.1"

You are passing 17.24.84.0/24, which is not a dot-delimited IPv4 address.

Your ipAddress variable contains a String in CIDR-notation.

jlordo
  • 37,490
  • 6
  • 58
  • 83
  • I didn't post one since its sensitive information, and not relevant to the question. Yes, I agree you'd need one to reproduce the error. What you said above makes sense. Any alternate to checking if the IP address falls in that range, or may be check if it is a valid CIDR? – user2609271 Jul 23 '13 at 06:14
  • An SSCCE is short, so you don't need to include sensitive (and irrelevant) information it it. In this case it could have been 4-5 lines of code with hardcoded string literals that cause the error. If `SubnetUtils subnetUtils = new SubnetUtils(cidr);` doesn't throw an exception, then (obviously) `cidr` is a valid CIDR. – jlordo Jul 23 '13 at 06:24
  • Fair enough. Can you atleast tell me how to check if the IP address is in the CIDR range? – user2609271 Jul 23 '13 at 06:51
  • @user2609271: I don't understand your question here. You can use the `isInRange()` method to check if an IP address is in a CIDR range, but you're already doing that. – jlordo Jul 23 '13 at 07:08
  • Yes, but I am passing 17.24.84.0/24, which clearly is not valid. Like you just said. – user2609271 Jul 23 '13 at 07:21
  • Nope. I just don't know how to check the range. I see this post here - http://stackoverflow.com/questions/2942299/converting-cidr-address-to-subnet-mask-and-network-address where Yuriy has answered it. However, it is not working for me. Although I am using 3.0.1 too :( I put a println statement after the if block, and clearly it doesn't print since its failing at the if. Please help. I'd really appreciate it. – user2609271 Jul 23 '13 at 07:33
  • @user2609271: Do you want to check if a range is within another range? Best would be to accept my answer (green checkmark) and ask a new question which contains a real SSCCE to help us understand what you are trying to do and what the problem is. – jlordo Jul 23 '13 at 07:38
  • One last question jlordo. I just want to know what does the method isInRange for a given IP address do? It checks if the IP address is in a given range I understand - What is the upper limit, and the lower limit for this range? If lets say I have a IP address - 63.92.227.128/28, how do I get - System.out.println(utils.getInfo().isInRange("63.92.227.128/28"))? If you can, please put your comment in the answer section instead of chat. – user2609271 Jul 23 '13 at 09:11
  • @user2609271: `63.92.227.128/28` is not an IP Adress, it's a CIDR address. You have to pass an IP adress to `isInRange()`, something like `63.92.227.129` – jlordo Jul 23 '13 at 12:14
0

Here is how to do it.

public boolean testCIDRSubnet(String cidr, String cidrSubnetCandidate) {
    if ("0".equals(cidr.substring(cidr.indexOf("/")+1))) return true;
    if ("0".equals(cidrSubnetCandidate.substring(cidrSubnetCandidate.indexOf("/")+1))) return false;
    SubnetUtils utils = new SubnetUtils(cidrSubnetCandidate);
    String lowIp = utils.getInfo().getLowAddress();
    String highIp = utils.getInfo().getHighAddress();
    utils = new SubnetUtils(cidr);
    return utils.getInfo().isInRange(lowIp) && utils.getInfo().isInRange(highIp);
}

The second argument takes in a CIDR IP range subnet candidate, and the method returns true if it is a full subnet of the cidr. The first two checks for "/0" cidrs is because SubnetUtils does not recognize this as valid.

Peter
  • 131
  • 1
  • 4