/** * Constructor that takes a dotted decimal address and a dotted decimal mask. * @param address An IP address, e.g. "192.168.0.1" * @param mask A dotted decimal netmask e.g. "255.255.0.0" * @throws IllegalArgumentException if the address or mask is invalid, * i.e. does not match n.n.n.n where n=1-3 decimal digits and the mask is not all zeros */ public SubnetUtils(String address, String mask) { calculate(toCidrNotation(address, mask)); }
private int toInteger(String address) { Matcher matcher = addressPattern.matcher(address); if (matcher.matches()) { return matchAddress(matcher); } else { throw new IllegalArgumentException("Could not parse [" + address + "]"); } }
private void setNetmask(InterfaceAddress it) throws Exception { if ( ( isLoopback || ( it.getNetworkPrefixLength() != 0 ) ) && ( address instanceof Inet4Address) ) { String subnet = ipAddress() + "/" + it.getNetworkPrefixLength(); if ( isLoopback ) { subnet = ipAddress() + "/8"; // because getNetworkPrefixLength() incorrectly returns 0 for IPv4 loopback } SubnetUtils utils = new SubnetUtils(subnet); netmask = utils.getInfo().getNetmask(); } else if ( (it.getNetworkPrefixLength() != 0 ) && ( address instanceof Inet6Address) ) { netmask = SocketUtilsIPV6.getIPV6NetMask(ipAddress() + "/" + it.getNetworkPrefixLength()); } }
private void calculate(String mask) { Matcher matcher = cidrPattern.matcher(mask); if (matcher.matches()) { address = matchAddress(matcher); /* Create a binary netmask from the number of bits specification /x */ int cidrPart = rangeCheck(Integer.parseInt(matcher.group(5)), 0, NBITS); for (int j = 0; j < cidrPart; ++j) { netmask |= (1 << 31-j); } /* Calculate base network address */ network = (address & netmask); /* Calculate broadcast address */ broadcast = network | ~(netmask); } else { throw new IllegalArgumentException("Could not parse [" + mask + "]"); } }
/** * Get the count of available addresses. * Will be zero for CIDR/31 and CIDR/32 if the inclusive flag is false. * @return the count of addresses, may be zero. */ public int getAddressCount() { int count = broadcast() - network() + (isInclusiveHostCount() ? 1 : -1); return count < 0 ? 0 : count; }
private int matchAddress(Matcher matcher) { int addr = 0; for (int i = 1; i <= 4; ++i) { int n = (rangeCheck(Integer.parseInt(matcher.group(i)), -1, 255)); addr |= ((n & 0xff) << 8*(4-i)); } return addr; }
/** * Constructor that takes a CIDR-notation string, e.g. "192.168.0.1/16" * @param cidrNotation A CIDR-notation string, e.g. "192.168.0.1/16" * @throws IllegalArgumentException if the parameter is invalid, * i.e. does not match n.n.n.n/m where n=1-3 decimal digits, m = 1-3 decimal digits in range 1-32 */ public SubnetUtils(String cidrNotation) { calculate(cidrNotation); }
private void setNetmask(InterfaceAddress it) throws Exception { if ( ( isLoopback || ( it.getNetworkPrefixLength() != 0 ) ) && ( address instanceof Inet4Address) ) { String subnet = ipAddress() + "/" + it.getNetworkPrefixLength(); if ( isLoopback ) { subnet = ipAddress() + "/8"; // because getNetworkPrefixLength() incorrectly returns 0 for IPv4 loopback } SubnetUtils utils = new SubnetUtils(subnet); netmask = utils.getInfo().getNetmask(); } else if ( (it.getNetworkPrefixLength() != 0 ) && ( address instanceof Inet6Address) ) { netmask = SocketUtilsIPV6.getIPV6NetMask(ipAddress() + "/" + it.getNetworkPrefixLength()); } }
private void calculate(String mask) { Matcher matcher = cidrPattern.matcher(mask); if (matcher.matches()) { address = matchAddress(matcher); /* Create a binary netmask from the number of bits specification /x */ int cidrPart = rangeCheck(Integer.parseInt(matcher.group(5)), 0, NBITS); for (int j = 0; j < cidrPart; ++j) { netmask |= (1 << 31-j); } /* Calculate base network address */ network = (address & netmask); /* Calculate broadcast address */ broadcast = network | ~(netmask); } else { throw new IllegalArgumentException("Could not parse [" + mask + "]"); } }
/** * Get the count of available addresses. * Will be zero for CIDR/31 and CIDR/32 if the inclusive flag is false. * @return the count of addresses, may be zero. */ public int getAddressCount() { int count = broadcast() - network() + (isInclusiveHostCount() ? 1 : -1); return count < 0 ? 0 : count; }
private int matchAddress(Matcher matcher) { int addr = 0; for (int i = 1; i <= 4; ++i) { int n = (rangeCheck(Integer.parseInt(matcher.group(i)), -1, 255)); addr |= ((n & 0xff) << 8*(4-i)); } return addr; }
/** * Constructor that takes a CIDR-notation string, e.g. "192.168.0.1/16" * @param cidrNotation A CIDR-notation string, e.g. "192.168.0.1/16" * @throws IllegalArgumentException if the parameter is invalid, * i.e. does not match n.n.n.n/m where n=1-3 decimal digits, m = 1-3 decimal digits in range 1-32 */ public SubnetUtils(String cidrNotation) { calculate(cidrNotation); }
/** * Constructor that takes a dotted decimal address and a dotted decimal mask. * @param address An IP address, e.g. "192.168.0.1" * @param mask A dotted decimal netmask e.g. "255.255.0.0" * @throws IllegalArgumentException if the address or mask is invalid, * i.e. does not match n.n.n.n where n=1-3 decimal digits and the mask is not all zeros */ public SubnetUtils(String address, String mask) { calculate(toCidrNotation(address, mask)); }
private int high() { return (isInclusiveHostCount() ? broadcast() : broadcast() - network() > 1 ? broadcast() -1 : 0); }
private int toInteger(String address) { Matcher matcher = addressPattern.matcher(address); if (matcher.matches()) { return matchAddress(matcher); } else { throw new IllegalArgumentException("Could not parse [" + address + "]"); } }
private int high() { return (isInclusiveHostCount() ? broadcast() : broadcast() - network() > 1 ? broadcast() -1 : 0); }
private int low() { return (isInclusiveHostCount() ? network() : broadcast() - network() > 1 ? network() + 1 : 0); }
private int low() { return (isInclusiveHostCount() ? network() : broadcast() - network() > 1 ? network() + 1 : 0); }