3

What is the easiest way in Java 6 to determine whether a given address is a valid net mask? I have found one solution which basically creates an array of valid IPs to use in a comparison (i.e. "255.255.255.255", "255.255.255.254", "255.255.255.252", etc...). Is there an easier way or is this the best way?

wxkevin
  • 1,634
  • 2
  • 25
  • 44
  • are you looking for any valid IP or is there a set of IPs you want to check against? Any valid IP (as a String) would be 4 numbers separated by `.` with a value between (inclusive) `0` and `255` you can check for that case. As an number the ip has to be between 0 and 2^32-1. – twain249 Apr 10 '12 at 14:39

4 Answers4

4

What about this pseudocode:

function IS_VALID(MASK)
  boolean zeroStart = false
  for each byte in MASK
    for each bit
      if bit = 1 and zeroStart = false
        continue
      if bit = 1 and zeroStart = true
        return INVALID_MASK
      if bit = 0 and zeroStart = false
        zeroStart = true
      if bit = 0 and zeroStart = true
        continue
  return VALID_MASK
4

If you're happy to include an external dependency, then Apache's commons.net may have what you're looking for.

Have a look at SubnetUtils and its SubnetInfo nested class. You can construct a SubnetUtils with an IP address and a mask. The constructor throws an exception if your mask is invalid, and SubnetInfo.html#isInRange can tell you if an IP is in a mask's range.

serg10
  • 31,923
  • 16
  • 73
  • 94
  • Very nice. I have no restrictions or adversions to external dependencies. – wxkevin Apr 11 '12 at 13:05
  • This question is from a couple years ago, so this comment might not be valid, but I tried an experiment with intentionally using a malformed netmask (at least I thought it was malformed: 205.0.0.0). I didn't get an exception from the constructor, but I was able to use some tools in SubnetInfo to at least detect the problem. – Rich Jul 11 '14 at 15:07
1

I think Serg10's answer is the simplest.

If you need to avoid external dependencies, then you should use Inet4Address to convert the IP address String into an array of bytes, and then analyse the bytes. Trying to analyse the String using a regex or by comparing with a fixed set of strings is liable to give the wrong answer:

  • The components of an IP address can have leading zeros.
  • An IP address can also be written with three, two or even just one component.

Refer to the Inet4Address javadoc for a description of what an IP address string can look like.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks for the alternative to the external dependency. I am OK with the external dependency but I am sure there will be others who find their way by search here who won't or can't use the external dependency. – wxkevin Apr 11 '12 at 13:06
0

You could try using a regular expression

Pattern p = Pattern.compile("^((2[0-5][0-5]|1[\\d][\\d]|[\\d][\\d]|[\\d])\\.){3}(2[0-5][0-5]|1[\\d][\\d]|[\\d][\\d]|[\\d])$");
String ip = "255.255.255.0.2";
Matcher m = p.matcher(ip);
if(m.matches()) {
    //valid
}
ring bearer
  • 20,383
  • 7
  • 59
  • 72