public static boolean isIpv6RangeOverlap(String startIp1, String endIp1, String startIp2, String endIp2) { try { IPv6Address s1 = IPv6Address.fromString(startIp1); IPv6Address e1 = IPv6Address.fromString(endIp1); IPv6Address s2 = IPv6Address.fromString(startIp2); IPv6Address e2 = IPv6Address.fromString(endIp2); IPv6AddressRange range1 = IPv6AddressRange.fromFirstAndLast(s1, e1); IPv6AddressRange range2 = IPv6AddressRange.fromFirstAndLast(s2, e2); return range1.overlaps(range2); } catch (Exception e) { return true; } }
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 static IPv6AddressRange fromFirstAndLast(IPv6Address first, IPv6Address last) { return new IPv6AddressRange(first, last); }
public static long getIpv6RangeSize(String startIp, String endIp) { IPv6Address start = IPv6Address.fromString(startIp); IPv6Address end = IPv6Address.fromString(endIp); IPv6AddressRange range = IPv6AddressRange.fromFirstAndLast(start, end); if (range.size().compareTo(IPv6Constants.IntegerMax) >= 0) { return Integer.MAX_VALUE; } else { return range.size().longValue(); } }
/** * 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)); }
newFreeRanges.remove(freeRangeAfterNetwork); newFreeRanges.add(IPv6AddressRange .fromFirstAndLast(freeRangeBeforeNetwork.getFirst(), freeRangeAfterNetwork.getLast())); newFreeRanges.add(IPv6AddressRange.fromFirstAndLast(freeRangeBeforeNetwork.getFirst(), toDeAllocate.getLast())); newFreeRanges.add(IPv6AddressRange.fromFirstAndLast(toDeAllocate.getFirst(), freeRangeAfterNetwork.getLast()));
public static List<String> getFreeIpv6InRange(String startIp, String endIp, List<String> usedIps, int limit, String start) { IPv6Address s = IPv6Address.fromString(startIp); IPv6Address e = IPv6Address.fromString(endIp); IPv6Address f = IPv6Address.fromString(start); IPv6AddressRange range = IPv6AddressRange.fromFirstAndLast(s, e); List<String> res = new ArrayList<String>(); while (s.compareTo(e) <= 0) { if (s.compareTo(f) <= 0) { s = s.add(1); continue; } if (usedIps.contains(s.toString())) { s = s.add(1); continue; } res.add(s.toString()); s = s.add(1); if (res.size() >= limit) { break; } } return res; }
public boolean overlaps(IPv6AddressRange range) { return underlyingRange.overlaps(range); }
/** * 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 IPv6Address getFirst() { return underlyingRange.getFirst(); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; IPv6AddressPool that = (IPv6AddressPool) o; if (allocationSubnetSize != null ? !allocationSubnetSize.equals(that.allocationSubnetSize) : that.allocationSubnetSize != null) return false; if (freeRanges != null ? !freeRanges.equals(that.freeRanges) : that.freeRanges != null) return false; if (lastAllocated != null ? !lastAllocated.equals(that.lastAllocated) : that.lastAllocated != null) return false; if (underlyingRange != null ? !underlyingRange.equals(that.underlyingRange) : that.underlyingRange != null) return false; return true; }
public IPv6Address getLast() { return underlyingRange.getLast(); }
/** * 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)); }
newFreeRanges.remove(freeRangeAfterNetwork); newFreeRanges.add(IPv6AddressRange .fromFirstAndLast(freeRangeBeforeNetwork.getFirst(), freeRangeAfterNetwork.getLast())); newFreeRanges.add(IPv6AddressRange.fromFirstAndLast(freeRangeBeforeNetwork.getFirst(), toDeAllocate.getLast())); newFreeRanges.add(IPv6AddressRange.fromFirstAndLast(toDeAllocate.getFirst(), freeRangeAfterNetwork.getLast()));
/** * Extend the range just enough at its head or tail such that the given address is included. * * @param address address to extend the range to * @return new (bigger) range */ public IPv6AddressRange extend(IPv6Address address) { if (address.compareTo(first) < 0) return fromFirstAndLast(address, last); else if (address.compareTo(last) > 0) return fromFirstAndLast(first, address); else return this; }
public boolean contains(IPv6AddressRange range) { return underlyingRange.contains(range); }
public boolean overlaps(IPv6AddressRange range) { return underlyingRange.overlaps(range); }
/** * 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); }