return "[" + toAddrString(ip) + "]"; return toAddrString(ip);
/** * Returns the {@link InetAddress} having the given string representation. * * <p>This deliberately avoids all nameservice lookups (e.g. no DNS). * * @param ipString {@code String} containing an IPv4 or IPv6 string literal, e.g. * {@code "192.168.0.1"} or {@code "2001:db8::1"} * @return {@link InetAddress} representing the argument * @throws IllegalArgumentException if the argument is not a valid IP string literal */ public static InetAddress forString(String ipString) { byte[] addr = ipStringToBytes(ipString); // The argument was malformed, i.e. not an IP string literal. if (addr == null) { throw new IllegalArgumentException(String.format(Locale.ROOT, "'%s' is not an IP string literal.", ipString)); } return bytesToInetAddress(addr); }
static String format(InetAddress address, int port) { Objects.requireNonNull(address); StringBuilder builder = new StringBuilder(); if (port != -1 && address instanceof Inet6Address) { builder.append(InetAddresses.toUriString(address)); } else { builder.append(InetAddresses.toAddrString(address)); } if (port != -1) { builder.append(':'); builder.append(port); } return builder.toString(); } }
ipString = convertDottedQuadToHex(ipString); if (ipString == null) { return null; return textToNumericFormatV6(ipString); } else if (hasDot) { return textToNumericFormatV4(ipString);
@Override public Query termQuery(Object value, @Nullable QueryShardContext context) { failIfNotIndexed(); if (value instanceof InetAddress) { return InetAddressPoint.newExactQuery(name(), (InetAddress) value); } else { if (value instanceof BytesRef) { value = ((BytesRef) value).utf8ToString(); } String term = value.toString(); if (term.contains("/")) { final Tuple<InetAddress, Integer> cidr = InetAddresses.parseCidr(term); return InetAddressPoint.newPrefixQuery(name(), cidr.v1(), cidr.v2()); } InetAddress address = InetAddresses.forString(term); return InetAddressPoint.newExactQuery(name(), address); } }
Range(String key, String mask) { final Tuple<InetAddress, Integer> cidr = InetAddresses.parseCidr(mask); final InetAddress address = cidr.v1(); final int prefixLength = cidr.v2(); // create the lower value by zeroing out the host portion, upper value by filling it with all ones. byte lower[] = address.getAddress(); byte upper[] = address.getAddress(); for (int i = prefixLength; i < 8 * lower.length; i++) { int m = 1 << (7 - (i & 7)); lower[i >> 3] &= ~m; upper[i >> 3] |= m; } this.key = key; try { InetAddress fromAddress = InetAddress.getByAddress(lower); if (fromAddress.equals(InetAddressPoint.MIN_VALUE)) { this.from = null; } else { this.from = InetAddresses.toAddrString(fromAddress); } InetAddress inclusiveToAddress = InetAddress.getByAddress(upper); if (inclusiveToAddress.equals(InetAddressPoint.MAX_VALUE)) { this.to = null; } else { this.to = InetAddresses.toAddrString(InetAddressPoint.nextUp(inclusiveToAddress)); } } catch (UnknownHostException bogus) { throw new AssertionError(bogus); } }
hextets[i] = (bytes[2 * i] & 255) << 8 | bytes[2 * i + 1] & 255; compressLongestRunOfZeroes(hextets); return hextetsToIPv6String(hextets);
+ "], which contains zero or more than one [/]"); InetAddress value = InetAddresses.forString(splits[0]); int prefixLength = Integer.parseInt(splits[1]); this.from = null; } else { this.from = InetAddresses.toAddrString(fromAddress); this.to = null; } else { this.to = InetAddresses.toAddrString(InetAddressPoint.nextUp(inclusiveToAddress));
public static boolean isInetAddress(String ipString) { return ipStringToBytes(ipString) != null; }
private static byte[] textToNumericFormatV4(String ipString) { String[] address = ipString.split("\\.", IPV4_PART_COUNT + 1); if (address.length != IPV4_PART_COUNT) { return null; } byte[] bytes = new byte[IPV4_PART_COUNT]; try { for (int i = 0; i < bytes.length; i++) { bytes[i] = parseOctet(address[i]); } } catch (NumberFormatException ex) { return null; } return bytes; }
private static Range parseIpRangeFromCidr(final XContentParser parser) throws IOException { final Tuple<InetAddress, Integer> cidr = InetAddresses.parseCidr(parser.text()); // create the lower value by zeroing out the host portion, upper value by filling it with all ones. byte[] lower = cidr.v1().getAddress(); byte[] upper = lower.clone(); for (int i = cidr.v2(); i < 8 * lower.length; i++) { int m = 1 << 7 - (i & 7); lower[i >> 3] &= ~m; upper[i >> 3] |= m; } try { return new Range(RangeType.IP, InetAddress.getByAddress(lower), InetAddress.getByAddress(upper), true, true); } catch (UnknownHostException bogus) { throw new AssertionError(bogus); } }
try { for (int i = 0; i < partsHi; i++) { rawBytes.putShort(parseHextet(parts[i])); rawBytes.putShort(parseHextet(parts[parts.length - i]));
ipString = convertDottedQuadToHex(ipString); if (ipString == null) { return null; return textToNumericFormatV6(ipString); } else if (hasDot) { return textToNumericFormatV4(ipString);
static String format(InetAddress address, int port) { Objects.requireNonNull(address); StringBuilder builder = new StringBuilder(); if (port != -1 && address instanceof Inet6Address) { builder.append(InetAddresses.toUriString(address)); } else { builder.append(InetAddresses.toAddrString(address)); } if (port != -1) { builder.append(':'); builder.append(port); } return builder.toString(); } }
@Override public Query termQuery(Object value, @Nullable QueryShardContext context) { failIfNotIndexed(); if (value instanceof InetAddress) { return InetAddressPoint.newExactQuery(name(), (InetAddress) value); } else { if (value instanceof BytesRef) { value = ((BytesRef) value).utf8ToString(); } String term = value.toString(); if (term.contains("/")) { final Tuple<InetAddress, Integer> cidr = InetAddresses.parseCidr(term); return InetAddressPoint.newPrefixQuery(name(), cidr.v1(), cidr.v2()); } InetAddress address = InetAddresses.forString(term); return InetAddressPoint.newExactQuery(name(), address); } }
Range(String key, String mask) { final Tuple<InetAddress, Integer> cidr = InetAddresses.parseCidr(mask); final InetAddress address = cidr.v1(); final int prefixLength = cidr.v2(); // create the lower value by zeroing out the host portion, upper value by filling it with all ones. byte lower[] = address.getAddress(); byte upper[] = address.getAddress(); for (int i = prefixLength; i < 8 * lower.length; i++) { int m = 1 << (7 - (i & 7)); lower[i >> 3] &= ~m; upper[i >> 3] |= m; } this.key = key; try { InetAddress fromAddress = InetAddress.getByAddress(lower); if (fromAddress.equals(InetAddressPoint.MIN_VALUE)) { this.from = null; } else { this.from = InetAddresses.toAddrString(fromAddress); } InetAddress inclusiveToAddress = InetAddress.getByAddress(upper); if (inclusiveToAddress.equals(InetAddressPoint.MAX_VALUE)) { this.to = null; } else { this.to = InetAddresses.toAddrString(InetAddressPoint.nextUp(inclusiveToAddress)); } } catch (UnknownHostException bogus) { throw new AssertionError(bogus); } }
hextets[i] = (bytes[2 * i] & 255) << 8 | bytes[2 * i + 1] & 255; compressLongestRunOfZeroes(hextets); return hextetsToIPv6String(hextets);
public static boolean isInetAddress(String ipString) { return ipStringToBytes(ipString) != null; }
private static byte[] textToNumericFormatV4(String ipString) { String[] address = ipString.split("\\.", IPV4_PART_COUNT + 1); if (address.length != IPV4_PART_COUNT) { return null; } byte[] bytes = new byte[IPV4_PART_COUNT]; try { for (int i = 0; i < bytes.length; i++) { bytes[i] = parseOctet(address[i]); } } catch (NumberFormatException ex) { return null; } return bytes; }