/** * Converts an IP address to a subnet using the provided * mask * @param address The address to convert into a subnet * @return The subnet as an integer */ private int toSubnet(InetAddress address) { return toInt(address) & subnetMask; }
/** * Converts an IP address to a subnet using the provided mask * * @param address * The address to convert into a subnet * @return The subnet as an integer */ private long toSubnet(InetAddress address) { if (address instanceof Inet4Address) { return toInt(address) & (int) subnetMask; } else { return toLong(address) & subnetMask; } }
/** * Creates a subnet from CIDR notation. For example, the subnet * 192.168.0.0/24 would be created using the {@link InetAddress} * 192.168.0.0 and the mask 24. * @param subnet The {@link InetAddress} of the subnet * @param mask The mask */ public Subnet(InetAddress subnet, int mask) { if(subnet == null) { throw new NullPointerException("Subnet address can not be null"); } if(!(subnet instanceof Inet4Address)) { throw new IllegalArgumentException("Only IPv4 supported"); } if(mask < 0 || mask > 32) { throw new IllegalArgumentException("Mask has to be an integer between 0 and 32"); } this.subnet = subnet; this.subnetInt = toInt(subnet); this.suffix = mask; // binary mask for this subnet this.subnetMask = IP_MASK >> (mask - 1); }
/** * Converts an IP address to a subnet using the provided mask * * @param address * The address to convert into a subnet * @return The subnet as an integer */ private long toSubnet(InetAddress address) { if (address instanceof Inet4Address) { return toInt(address) & (int) subnetMask; } else { return toLong(address) & subnetMask; } }
} else { this.subnet = subnet; subnetInt = toInt(subnet); this.suffix = mask;
} else { this.subnet = subnet; subnetInt = toInt(subnet); this.suffix = mask;