Search code examples
javanetwork-programmingipmaskcidr

Converting CIDR address to subnet mask and network address


Given a CIDR address, e.g. 192.168.10.0/24

  • How to determine mask length? (24)
  • How to determine mask address? (255.255.255.0)
  • How to determine network address? (192.168.10.0)

Solution

  • It is covered by apache utils.

    See this URL: http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/util/SubnetUtils.html

    String subnet = "192.168.0.3/31";
    SubnetUtils utils = new SubnetUtils(subnet);
    
    utils.getInfo().isInRange(address)
    

    Note: For use w/ /32 CIDR subnets, for exemple, one needs to add the following declaration :

    utils.setInclusiveHostCount(true);