8299129: Enhance NameService lookups

Reviewed-by: ahgross, michaelm, rhalade, dfuchs
This commit is contained in:
Aleksei Efimov 2023-01-24 14:40:58 +00:00 committed by Henry Jen
parent 77df3152c8
commit eb8d8cdddd

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1995, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -1484,44 +1484,45 @@ public sealed class InetAddress implements Serializable permits Inet4Address, In
host = host.substring(1, host.length() -1); host = host.substring(1, host.length() -1);
ipv6Expected = true; ipv6Expected = true;
} else { } else {
// This was supposed to be a IPv6 address, but it's not! // This was supposed to be a IPv6 literal, but it's not
throw new UnknownHostException(host + ": invalid IPv6 address"); throw invalidIPv6LiteralException(host, false);
} }
} }
// if host is an IP address, we won't do further lookup // Check and try to parse host string as an IP address literal
if (IPAddressUtil.digit(host.charAt(0), 16) != -1 if (IPAddressUtil.digit(host.charAt(0), 16) != -1
|| (host.charAt(0) == ':')) { || (host.charAt(0) == ':')) {
byte[] addr; byte[] addr = null;
int numericZone = -1; int numericZone = -1;
String ifname = null; String ifname = null;
// see if it is IPv4 address
try { if (!ipv6Expected) {
addr = IPAddressUtil.validateNumericFormatV4(host); // check if it is IPv4 address only if host is not wrapped in '[]'
} catch (IllegalArgumentException iae) { try {
var uhe = new UnknownHostException(host); addr = IPAddressUtil.validateNumericFormatV4(host);
uhe.initCause(iae); } catch (IllegalArgumentException iae) {
throw uhe; var uhe = new UnknownHostException(host);
uhe.initCause(iae);
throw uhe;
}
} }
if (addr == null) { if (addr == null) {
// This is supposed to be an IPv6 literal // Try to parse host string as an IPv6 literal
// Check if a numeric or string zone id is present // Check if a numeric or string zone id is present first
int pos; int pos;
if ((pos=host.indexOf ('%')) != -1) { if ((pos = host.indexOf('%')) != -1) {
numericZone = checkNumericZone (host); numericZone = checkNumericZone(host);
if (numericZone == -1) { /* remainder of string must be an ifname */ if (numericZone == -1) { /* remainder of string must be an ifname */
ifname = host.substring (pos+1); ifname = host.substring(pos + 1);
} }
} }
if ((addr = IPAddressUtil.textToNumericFormatV6(host)) == null && host.contains(":")) { if ((addr = IPAddressUtil.textToNumericFormatV6(host)) == null &&
throw new UnknownHostException(host + ": invalid IPv6 address"); (host.contains(":") || ipv6Expected)) {
throw invalidIPv6LiteralException(host, ipv6Expected);
} }
} else if (ipv6Expected) {
// Means an IPv4 literal between brackets!
throw new UnknownHostException("["+host+"]");
} }
InetAddress[] ret = new InetAddress[1];
if(addr != null) { if(addr != null) {
InetAddress[] ret = new InetAddress[1];
if (addr.length == Inet4Address.INADDRSZ) { if (addr.length == Inet4Address.INADDRSZ) {
if (numericZone != -1 || ifname != null) { if (numericZone != -1 || ifname != null) {
// IPv4-mapped address must not contain zone-id // IPv4-mapped address must not contain zone-id
@ -1538,12 +1539,18 @@ public sealed class InetAddress implements Serializable permits Inet4Address, In
return ret; return ret;
} }
} else if (ipv6Expected) { } else if (ipv6Expected) {
// We were expecting an IPv6 Literal, but got something else // We were expecting an IPv6 Literal since host string starts
throw new UnknownHostException("["+host+"]"); // and ends with square brackets, but we got something else.
throw invalidIPv6LiteralException(host, true);
} }
return getAllByName0(host, true, true); return getAllByName0(host, true, true);
} }
private static UnknownHostException invalidIPv6LiteralException(String host, boolean wrapInBrackets) {
String hostString = wrapInBrackets ? "[" + host + "]" : host;
return new UnknownHostException(hostString + ": invalid IPv6 address literal");
}
/** /**
* Returns the loopback address. * Returns the loopback address.
* <p> * <p>