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 static String getNextIp6InRange(final String currentIp, final String ipRange) { final String[] ips = ipRange.split("-"); final String startIp = ips[0]; String endIp = null; if (ips.length > 1) { endIp = ips[1]; } final IPv6Address start = IPv6Address.fromString(startIp); final IPv6Address end = IPv6Address.fromString(endIp); final IPv6Address current = IPv6Address.fromString(currentIp); IPv6Address result = null; if (current.equals(end)) { result = start; } else { result = current.add(1); } String resultIp = null; if (result != null) { resultIp = result.toString(); } return resultIp; }
public static String getGatewayOfNetworkCidr(String cidr) { IPv6Network network = IPv6Network.fromString(cidr); if (network.getNetmask().asPrefixLength() <= 127) { return network.getFirst().add(1).toString(); } else { return network.getFirst().toString(); } }
public static String getStartIpOfNetworkCidr(String cidr) { IPv6Network network = IPv6Network.fromString(cidr); if (network.getNetmask().asPrefixLength() < 127) { return network.getFirst().add(2).toString(); } else { return network.getFirst().toString(); } }
@Override public IPv6Address next() { if (hasNext()) { IPv6Address result = current; current = current.add(1); return result; } else { throw new NoSuchElementException(); } }
@Override public IPv6Address next() { if (hasNext()) { IPv6Address result = current; current = current.add(1); return result; } else { throw new NoSuchElementException(); } }
public int numberOfTrailingOnes() { // count trailing ones in "value" by counting the trailing zeroes in "value + 1" final IPv6Address plusOne = this.add(1); return plusOne.getLowBits() == 0 ? Long.numberOfTrailingZeros(plusOne.getHighBits()) + 64 : Long.numberOfTrailingZeros(plusOne.getLowBits()); }
public int numberOfTrailingOnes() { // count trailing ones in "value" by counting the trailing zeroes in "value + 1" final IPv6Address plusOne = this.add(1); return plusOne.getLowBits() == 0 ? Long.numberOfTrailingZeros(plusOne.getHighBits()) + 64 : Long.numberOfTrailingZeros(plusOne.getLowBits()); }
private Ipv6Address parseSrv6SidAddress(final String locatorIp, final String locatorLength, final Long opcode) { com.googlecode.ipv6.IPv6Address ip = com.googlecode.ipv6.IPv6Address.fromString(locatorIp); IPv6NetworkMask mask = IPv6NetworkMask.fromPrefixLength(parseInt(locatorLength)); com.googlecode.ipv6.IPv6Address srv6Sid = ip.maskWithNetworkMask(mask); return new Ipv6Address(srv6Sid.add(opcode.intValue()).toString()); }
/** * Private helper method to find the free range just before the given network. */ private IPv6AddressRange findFreeRangeBefore(IPv6Network network) { for (IPv6AddressRange freeRange : freeRanges) { if (freeRange.getLast().add(1).equals(network.getFirst())) { return freeRange; } } // not found return null; }
/** * Private helper method to find the free range just before the given network. */ private IPv6AddressRange findFreeRangeBefore(IPv6Network network) { for (IPv6AddressRange freeRange : freeRanges) { if (freeRange.getLast().add(1).equals(network.getFirst())) { return freeRange; } } // not found return null; }
/** * 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)); }
Ipv6Address resolveSidAddress(@Nonnull final Prefix locPrefix, @Nonnull Sid localSid) { com.googlecode.ipv6.IPv6Address ip = com.googlecode.ipv6.IPv6Address.fromString(locPrefix.getAddress().getValue()); IPv6NetworkMask mask = IPv6NetworkMask.fromPrefixLength(locPrefix.getLength().getValue()); // strip function part if present ip = ip.maskWithNetworkMask(mask); //add new function part based on opcode String locIp = ip.add(localSid.getOpcode().getValue().intValue()).toString(); return new Ipv6Address(locIp); }
public static String getNextIp6InRange(final String currentIp, final String ipRange) { final String[] ips = ipRange.split("-"); final String startIp = ips[0]; String endIp = null; if (ips.length > 1) { endIp = ips[1]; } final IPv6Address start = IPv6Address.fromString(startIp); final IPv6Address end = IPv6Address.fromString(endIp); final IPv6Address current = IPv6Address.fromString(currentIp); IPv6Address result = null; if (current.equals(end)) { result = start; } else { result = current.add(1); } String resultIp = null; if (result != null) { resultIp = result.toString(); } return resultIp; }
/** * 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)); }
base = next.getLast().add(1);
base = next.getLast().add(1);