public static boolean isIpv6RangeFull(String startIp, String endIp, long used) { BigInteger start = IPv6Address.fromString(startIp).toBigInteger(); BigInteger end = IPv6Address.fromString(endIp).toBigInteger(); return end.subtract(start).compareTo(new BigInteger(String.valueOf(used))) <= 0; }
public static BigInteger ipv6AddressToBigInteger(String ip) { return IPv6Address.fromString(ip).toBigInteger(); }
public static BigInteger getBigIntegerFromString(String ip) { return IPv6Address.fromString(ip).toBigInteger(); }
private IPv6Network calculateNext(IPv6Network current) { BigInteger next = current.address.toBigInteger().add(nbrAddressesPerSplit); return IPv6Network.fromAddressAndMask(IPv6Address.fromBigInteger(next), size); }
private IPv6Network calculateNext(IPv6Network current) { BigInteger next = current.address.toBigInteger().add(nbrAddressesPerSplit); return IPv6Network.fromAddressAndMask(IPv6Address.fromBigInteger(next), size); }
/** * Create an IPv6 address from a (positive) {@link java.math.BigInteger}. The magnitude of the {@link java.math.BigInteger} represents * the IPv6 address value. Or in other words, the {@link java.math.BigInteger} with value N defines the Nth possible IPv6 address. * * @param bigInteger {@link java.math.BigInteger} value * @return IPv6 address */ public static IPv6Address fromBigInteger(final BigInteger bigInteger) { if (bigInteger == null) throw new IllegalArgumentException("can not construct from [null]"); if (bigInteger.compareTo(BigInteger.ZERO) < 0) throw new IllegalArgumentException("can not construct from negative value"); if (bigInteger.compareTo(MAX.toBigInteger()) > 0) throw new IllegalArgumentException("bigInteger represents a value bigger than 2^128 - 1"); byte[] bytes = bigInteger.toByteArray(); if (bytes[0] == 0) { // a zero byte was added to represent the (always positive, hence zero) sign bit return fromByteArray(prefixWithZeroBytes(Arrays.copyOfRange(bytes, 1, bytes.length), N_BYTES)); } else { return fromByteArray(prefixWithZeroBytes(bytes, N_BYTES)); } }
/** * Create an IPv6 address from a (positive) {@link java.math.BigInteger}. The magnitude of the {@link java.math.BigInteger} represents * the IPv6 address value. Or in other words, the {@link java.math.BigInteger} with value N defines the Nth possible IPv6 address. * * @param bigInteger {@link java.math.BigInteger} value * @return IPv6 address */ public static IPv6Address fromBigInteger(final BigInteger bigInteger) { if (bigInteger == null) throw new IllegalArgumentException("can not construct from [null]"); if (bigInteger.compareTo(BigInteger.ZERO) < 0) throw new IllegalArgumentException("can not construct from negative value"); if (bigInteger.compareTo(MAX.toBigInteger()) > 0) throw new IllegalArgumentException("bigInteger represents a value bigger than 2^128 - 1"); byte[] bytes = bigInteger.toByteArray(); if (bytes[0] == 0) { // a zero byte was added to represent the (always positive, hence zero) sign bit return fromByteArray(prefixWithZeroBytes(Arrays.copyOfRange(bytes, 1, bytes.length), N_BYTES)); } else { return fromByteArray(prefixWithZeroBytes(bytes, N_BYTES)); } }
/** * Extracts Operational code (SRv6 endpoint function) from provided SID value. SID value consists of two parts. * First part is Locator defined by its IPv6 address and length (stored in mappingContext referenced * by locator name). Second part is Operational code (endpoint function). Locator length(number of bits) divides SID * address to bits used for locator value and bits used for endpoint function. * * @see <a href="https://tools.ietf.org/html/draft-filsfils-spring-srv6-network-programming-04"> * SRv6 network programming (SRv6 Segment)</a> * @see <a href="https://tools.ietf.org/html/draft-raza-spring-srv6-yang-01"> * * SRv6 Yang (SRv6 Types)</a> * * @param sid provided SRv6 SIDs IPv6 address * @param mappingContext mapping context which stores mapping for locator length * @param locName locator name used as a key to retrieve locator length from mapping context * @return operational code (endpoint function) of SRv6 SID address */ private Srv6FuncOpcodeUnreserved extractOpCode(Ipv6Address sid, MappingContext mappingContext, final String locName) { int locLength = LocatorContextManager.parseLength(locatorContext.getLocator(locName, mappingContext)); com.googlecode.ipv6.IPv6Address ip = com.googlecode.ipv6.IPv6Address.fromString(sid.getValue()); IPv6NetworkMask mask = IPv6NetworkMask.fromPrefixLength(locLength); com.googlecode.ipv6.IPv6Address locator = ip.maskWithNetworkMask(mask); long function = ip.toBigInteger().subtract(locator.toBigInteger()).longValue(); return new Srv6FuncOpcodeUnreserved(function); }