public static boolean isIpv6InRange(String ip, String startIp, String endIp) { IPv6Address start = IPv6Address.fromString(startIp); IPv6Address end = IPv6Address.fromString(endIp); IPv6Address address = IPv6Address.fromString(ip); IPv6AddressRange range = IPv6AddressRange.fromFirstAndLast(start, end); return range.contains(address); }
public boolean contains(IPv6Address address) { return underlyingRange.contains(address); }
public boolean contains(IPv6AddressRange range) { return underlyingRange.contains(range); }
public boolean contains(IPv6Address address) { return underlyingRange.contains(address); }
public boolean contains(IPv6AddressRange range) { return underlyingRange.contains(range); }
private IPv6AddressRange findFreeRangeContaining(IPv6Network toAllocate) { // split around the subnet to allocate final SortedSet<IPv6AddressRange> head = freeRanges.headSet(toAllocate); final SortedSet<IPv6AddressRange> tail = freeRanges.tailSet(toAllocate); // the range containing the network to allocate is either the first of the tail, or the last of the head, or it doesn't exist if (!head.isEmpty() && head.last().contains(toAllocate)) { return head.last(); } else if (!tail.isEmpty() && tail.first().contains(toAllocate)) { return tail.first(); } else { return null; } }
private IPv6AddressRange findFreeRangeContaining(IPv6Network toAllocate) { // split around the subnet to allocate final SortedSet<IPv6AddressRange> head = freeRanges.headSet(toAllocate); final SortedSet<IPv6AddressRange> tail = freeRanges.tailSet(toAllocate); // the range containing the network to allocate is either the first of the tail, or the last of the head, or it doesn't exist if (!head.isEmpty() && head.last().contains(toAllocate)) { return head.last(); } else if (!tail.isEmpty() && tail.first().contains(toAllocate)) { return tail.first(); } else { return null; } }
/** * Private helper method to perform the allocation of a subnet within one of the free ranges. * * @param toAllocate subnet to allocate * @param rangeToAllocateFrom free range to allocate from * @return resulting pool */ private IPv6AddressPool doAllocate(final IPv6Network toAllocate, final IPv6AddressRange rangeToAllocateFrom) { assert freeRanges.contains(rangeToAllocateFrom); assert rangeToAllocateFrom.contains(toAllocate); final TreeSet<IPv6AddressRange> newFreeRanges = new TreeSet<IPv6AddressRange>(this.freeRanges); // remove range from free ranges newFreeRanges.remove(rangeToAllocateFrom); // from the range, remove the allocated subnet final List<IPv6AddressRange> newRanges = rangeToAllocateFrom.remove(toAllocate); // and add the resulting ranges as new free ranges newFreeRanges.addAll(newRanges); return new IPv6AddressPool(underlyingRange, allocationSubnetSize, newFreeRanges, toAllocate); }
/** * Private helper method to perform the allocation of a subnet within one of the free ranges. * * @param toAllocate subnet to allocate * @param rangeToAllocateFrom free range to allocate from * @return resulting pool */ private IPv6AddressPool doAllocate(final IPv6Network toAllocate, final IPv6AddressRange rangeToAllocateFrom) { assert freeRanges.contains(rangeToAllocateFrom); assert rangeToAllocateFrom.contains(toAllocate); final TreeSet<IPv6AddressRange> newFreeRanges = new TreeSet<IPv6AddressRange>(this.freeRanges); // remove range from free ranges newFreeRanges.remove(rangeToAllocateFrom); // from the range, remove the allocated subnet final List<IPv6AddressRange> newRanges = rangeToAllocateFrom.remove(toAllocate); // and add the resulting ranges as new free ranges newFreeRanges.addAll(newRanges); return new IPv6AddressPool(underlyingRange, allocationSubnetSize, newFreeRanges, toAllocate); }
public boolean isFree(final IPv6Network network) { if (network == null) throw new IllegalArgumentException("network invalid [null]"); if (!this.allocationSubnetSize.equals(network.getNetmask())) throw new IllegalArgumentException( "network of prefix length [" + network.getNetmask().asPrefixLength() + "] can not be free in a pool which uses prefix length [" + allocationSubnetSize + "]"); // find a free range that contains the network for (IPv6AddressRange freeRange : freeRanges) { if (freeRange.contains(network)) { return true; } } // nothing found return false; }
public boolean isFree(final IPv6Network network) { if (network == null) throw new IllegalArgumentException("network invalid [null]"); if (!this.allocationSubnetSize.equals(network.getNetmask())) throw new IllegalArgumentException( "network of prefix length [" + network.getNetmask().asPrefixLength() + "] can not be free in a pool which uses prefix length [" + allocationSubnetSize + "]"); // find a free range that contains the network for (IPv6AddressRange freeRange : freeRanges) { if (freeRange.contains(network)) { return true; } } // nothing found return false; }
/** * Remove an address from the range, resulting in one, none or two new ranges. If an address outside the range is removed, this has no * effect. If the first or last address is removed, a single new range is returned (potentially empty if the range only contained a * single address). If an address somewhere else in the range is removed, two new ranges are returned. * * @param address adddress to remove from the range * @return list of resulting ranges */ public List<IPv6AddressRange> remove(IPv6Address address) { if (address == null) throw new IllegalArgumentException("invalid address [null]"); if (!contains(address)) return Collections.singletonList(this); else if (address.equals(first) && address.equals(last)) return Collections.emptyList(); else if (address.equals(first)) return Collections.singletonList(fromFirstAndLast(first.add(1), last)); else if (address.equals(last)) return Collections.singletonList(fromFirstAndLast(first, last.subtract(1))); else return Arrays.asList(fromFirstAndLast(first, address.subtract(1)), fromFirstAndLast(address.add(1), last)); }
/** * Remove an address from the range, resulting in one, none or two new ranges. If an address outside the range is removed, this has no * effect. If the first or last address is removed, a single new range is returned (potentially empty if the range only contained a * single address). If an address somewhere else in the range is removed, two new ranges are returned. * * @param address adddress to remove from the range * @return list of resulting ranges */ public List<IPv6AddressRange> remove(IPv6Address address) { if (address == null) throw new IllegalArgumentException("invalid address [null]"); if (!contains(address)) return Collections.singletonList(this); else if (address.equals(first) && address.equals(last)) return Collections.emptyList(); else if (address.equals(first)) return Collections.singletonList(fromFirstAndLast(first.add(1), last)); else if (address.equals(last)) return Collections.singletonList(fromFirstAndLast(first, last.subtract(1))); else return Arrays.asList(fromFirstAndLast(first, address.subtract(1)), fromFirstAndLast(address.add(1), last)); }
/** * Remove a network from the range, resulting in one, none or two new ranges. If a network outside (or partially outside) the range is * removed, this has no effect. If the network which is removed is aligned with the beginning or end of the range, a single new ranges * is returned (potentially empty if the range was equal to the network which is removed from it). If a network somewhere else in the * range is removed, two new ranges are returned. * * @param network network to remove from the range * @return list of resulting ranges */ public List<IPv6AddressRange> remove(IPv6Network network) { if (network == null) throw new IllegalArgumentException("invalid network [null]"); if (!contains(network)) return Collections.singletonList(this); else if (this.equals(network)) return Collections.emptyList(); else if (first.equals(network.getFirst())) return Collections.singletonList(fromFirstAndLast(network.getLast().add(1), last)); else if (last.equals(network.getLast())) return Collections.singletonList(fromFirstAndLast(first, network.getFirst().subtract(1))); else return Arrays.asList(fromFirstAndLast(first, network.getFirst().subtract(1)), fromFirstAndLast(network.getLast().add(1), last)); }
/** * Remove a network from the range, resulting in one, none or two new ranges. If a network outside (or partially outside) the range is * removed, this has no effect. If the network which is removed is aligned with the beginning or end of the range, a single new ranges * is returned (potentially empty if the range was equal to the network which is removed from it). If a network somewhere else in the * range is removed, two new ranges are returned. * * @param network network to remove from the range * @return list of resulting ranges */ public List<IPv6AddressRange> remove(IPv6Network network) { if (network == null) throw new IllegalArgumentException("invalid network [null]"); if (!contains(network)) return Collections.singletonList(this); else if (this.equals(network)) return Collections.emptyList(); else if (first.equals(network.getFirst())) return Collections.singletonList(fromFirstAndLast(network.getLast().add(1), last)); else if (last.equals(network.getLast())) return Collections.singletonList(fromFirstAndLast(first, network.getFirst().subtract(1))); else return Arrays.asList(fromFirstAndLast(first, network.getFirst().subtract(1)), fromFirstAndLast(network.getLast().add(1), last)); }