public static String getIpv6AddressCanonicalString(String ip) { IPv6Address ip6 = IPv6Address.fromString(ip); return ip6.toString(); }
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 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 boolean isIpv6UnicastAddress(String ip) { try { IPv6Address address = IPv6Address.fromString(ip); if (address.isMulticast() || address.isLinkLocal() || address.isSiteLocal()) { return false; } else { return true; } } catch (Exception e){ return false; } }
public static BigInteger ipv6AddressToBigInteger(String ip) { return IPv6Address.fromString(ip).toBigInteger(); }
public static boolean isIpv6Address(String ip) { try { IPv6Address.fromString(ip); return true; } catch (Exception e){ return false; } }
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()); }
public static String getIp6FromRange(final String ip6Range) { final String[] ips = ip6Range.split("-"); final String startIp = ips[0]; final IPv6Address start = IPv6Address.fromString(startIp); final BigInteger gap = countIp6InRange(ip6Range); BigInteger next = new BigInteger(gap.bitLength(), s_rand); while (next.compareTo(gap) >= 0) { next = new BigInteger(gap.bitLength(), s_rand); } InetAddress resultAddr = null; final BigInteger startInt = convertIPv6AddressToBigInteger(start); if (startInt != null) { final BigInteger resultInt = startInt.add(next); try { resultAddr = InetAddress.getByAddress(resultInt.toByteArray()); } catch (final UnknownHostException e) { return null; } } if( resultAddr != null) { final IPv6Address ip = IPv6Address.fromInetAddress(resultAddr); return ip.toString(); } return null; }
public static String getEndIpOfNetworkCidr(String cidr) { IPv6Network network = IPv6Network.fromString(cidr); return network.getLast().toString(); }
public static boolean isIp6InRange(final String ip6, final String ip6Range) { if (ip6Range == null) { return false; } final String[] ips = ip6Range.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 ip = IPv6Address.fromString(ip6); if (start.compareTo(ip) <= 0 && end.compareTo(ip) >= 0) { return true; } 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)); }
private Ipv6Address extractLocator(Ipv6Address sid, final MappingContext mappingContext, String locName) { /* * TODO(HC2VPP-353): VPP does not support locator length, therefore it is necessary to use default value for * locator length, if there is no other way of getting the value (e.g. hc2vpp starts with configuration already * present in VPP). * */ int locLength = locName == null ? DEFAULT_LOCATOR_LENGTH : LocatorContextManager.parseLength(locatorCtx.getLocator(locName, mappingContext)); com.googlecode.ipv6.IPv6Address ip = com.googlecode.ipv6.IPv6Address.fromString(sid.getValue()); IPv6NetworkMask mask = IPv6NetworkMask.fromPrefixLength(locLength); // strip function part if present ip = ip.maskWithNetworkMask(mask); return new Ipv6AddressNoZone(ip.toString()); }
@Override public boolean hasNext() { // there is a next subnet as long as we didn't reach the end of the range return base != null && (base.compareTo(last) <= 0); }
public static String ipv6AddressToString(BigInteger ip) { return IPv6Address.fromBigInteger(ip).toString(); }
/** * 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); }
/** * 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; }
public static Integer getPrefixLengthFromNetwork(String mask) { if (isIpv4Address(mask)) { return validNetmasks.get(mask); } else { try { IPv6Address addr = IPv6Address.fromString(mask); return addr.numberOfLeadingOnes(); } catch (Exception e) { throw e; } } } }
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()); }