protected String encodeIPv4() { SubnetV4Utils su = new SubnetV4Utils(low, size); return su.getInfo().getAddress(); }
/** * 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 SubnetV4Utils(String address, String mask) { calculate(toCidrNotation(address, mask)); }
public boolean match(String address) { if( ! SubnetV4Utils.isAddress(address)) return false; SubnetV4Utils su = new SubnetV4Utils(low, size); return su.getInfo().isInRange(address); }
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 long getAddressCountLong() { long b = broadcastLong(); long n = networkLong(); long count = b - n + (isInclusiveHostCount() ? 1 : -1); return count < 0 ? 0 : count; }
/** * 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 SubnetV4Utils(String cidrNotation) { calculate(cidrNotation); }
private static int matchAddress(Matcher matcher) { int addr = 0; for (int i = 1; i <= 4; ++i) { int n = (rangeCheck(Integer.parseInt(matcher.group(i)), 0, 255)); addr |= ((n & 0xff) << 8*(4-i)); } return addr; }
/** * @return the number of bits of the CIDR block */ public int getMaskSize() { return pop(netmask()); }
public static int toInteger(String address) { Matcher matcher = addressPattern.matcher(address); if (matcher.matches()) { return matchAddress(matcher); } else { throw new IllegalArgumentException("Could not parse [" + address + "]"); } }
/** * Returns true if the parameter <code>address</code> is in the * range of usable endpoint addresses for this subnet. This excludes the * network and broadcast adresses. * @param address A dot-delimited IPv4 address, e.g. "192.168.0.1" * @return True if in range, false otherwise */ public boolean isInRange(String address) { return isInRange(toInteger(address)); }
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 long getAddressCountLong() { long b = broadcastLong(); long n = networkLong(); long count = b - n + (isInclusiveHostCount() ? 1 : -1); return count < 0 ? 0 : count; }
/** * 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 SubnetV4Utils(String cidrNotation) { calculate(cidrNotation); }
private static int matchAddress(Matcher matcher) { int addr = 0; for (int i = 1; i <= 4; ++i) { int n = (rangeCheck(Integer.parseInt(matcher.group(i)), 0, 255)); addr |= ((n & 0xff) << 8*(4-i)); } return addr; }
/** * @return the number of bits of the CIDR block */ public int getMaskSize() { return pop(netmask()); }
public static int toInteger(String address) { Matcher matcher = addressPattern.matcher(address); if (matcher.matches()) { return matchAddress(matcher); } else { throw new IllegalArgumentException("Could not parse [" + address + "]"); } }
/** * Returns true if the parameter <code>address</code> is in the * range of usable endpoint addresses for this subnet. This excludes the * network and broadcast adresses. * @param address A dot-delimited IPv4 address, e.g. "192.168.0.1" * @return True if in range, false otherwise */ public boolean isInRange(String address) { return isInRange(toInteger(address)); }
protected String encodeIPv4() { SubnetV4Utils su = new SubnetV4Utils(low, size); return su.getInfo().getAddress(); }