Skip to content

Commit 74148f8

Browse files
Alexei Voitylovgnu-andrew
authored andcommitted
8299129: Enhance NameService lookups
Reviewed-by: mbalao, andrew Backport-of: 1aef50354aaa0831b58de81db3d6bf30b9a277d1
1 parent 33758ae commit 74148f8

1 file changed

Lines changed: 32 additions & 25 deletions

File tree

jdk/src/share/classes/java/net/InetAddress.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1082,44 +1082,45 @@ private static InetAddress[] getAllByName(String host, InetAddress reqAddr)
10821082
host = host.substring(1, host.length() -1);
10831083
ipv6Expected = true;
10841084
} else {
1085-
// This was supposed to be a IPv6 address, but it's not!
1086-
throw new UnknownHostException(host + ": invalid IPv6 address");
1085+
// This was supposed to be a IPv6 literal, but it's not
1086+
throw invalidIPv6LiteralException(host, false);
10871087
}
10881088
}
10891089

1090-
// if host is an IP address, we won't do further lookup
1090+
// Check and try to parse host string as an IP address literal
10911091
if (IPAddressUtil.digit(host.charAt(0), 16) != -1
10921092
|| (host.charAt(0) == ':')) {
1093-
byte[] addr;
1093+
byte[] addr = null;
10941094
int numericZone = -1;
10951095
String ifname = null;
1096-
// see if it is IPv4 address
1097-
try {
1098-
addr = IPAddressUtil.validateNumericFormatV4(host);
1099-
} catch (IllegalArgumentException iae) {
1100-
UnknownHostException uhe = new UnknownHostException(host);
1101-
uhe.initCause(iae);
1102-
throw uhe;
1096+
1097+
if (!ipv6Expected) {
1098+
// check if it is IPv4 address only if host is not wrapped in '[]'
1099+
try {
1100+
addr = IPAddressUtil.validateNumericFormatV4(host);
1101+
} catch (IllegalArgumentException iae) {
1102+
UnknownHostException uhe = new UnknownHostException(host);
1103+
uhe.initCause(iae);
1104+
throw uhe;
1105+
}
11031106
}
11041107
if (addr == null) {
1105-
// This is supposed to be an IPv6 literal
1106-
// Check if a numeric or string zone id is present
1108+
// Try to parse host string as an IPv6 literal
1109+
// Check if a numeric or string zone id is present first
11071110
int pos;
1108-
if ((pos=host.indexOf ("%")) != -1) {
1109-
numericZone = checkNumericZone (host);
1111+
if ((pos = host.indexOf('%')) != -1) {
1112+
numericZone = checkNumericZone(host);
11101113
if (numericZone == -1) { /* remainder of string must be an ifname */
1111-
ifname = host.substring (pos+1);
1114+
ifname = host.substring(pos + 1);
11121115
}
11131116
}
1114-
if ((addr = IPAddressUtil.textToNumericFormatV6(host)) == null && host.contains(":")) {
1115-
throw new UnknownHostException(host + ": invalid IPv6 address");
1117+
if ((addr = IPAddressUtil.textToNumericFormatV6(host)) == null &&
1118+
(host.contains(":") || ipv6Expected)) {
1119+
throw invalidIPv6LiteralException(host, ipv6Expected);
11161120
}
1117-
} else if (ipv6Expected) {
1118-
// Means an IPv4 litteral between brackets!
1119-
throw new UnknownHostException("["+host+"]");
11201121
}
1121-
InetAddress[] ret = new InetAddress[1];
11221122
if(addr != null) {
1123+
InetAddress[] ret = new InetAddress[1];
11231124
if (addr.length == Inet4Address.INADDRSZ) {
11241125
if (numericZone != -1 || ifname != null) {
11251126
// IPv4-mapped address must not contain zone-id
@@ -1136,12 +1137,18 @@ private static InetAddress[] getAllByName(String host, InetAddress reqAddr)
11361137
return ret;
11371138
}
11381139
} else if (ipv6Expected) {
1139-
// We were expecting an IPv6 Litteral, but got something else
1140-
throw new UnknownHostException("["+host+"]");
1140+
// We were expecting an IPv6 Literal since host string starts
1141+
// and ends with square brackets, but we got something else.
1142+
throw invalidIPv6LiteralException(host, true);
11411143
}
11421144
return getAllByName0(host, reqAddr, true, true);
11431145
}
11441146

1147+
private static UnknownHostException invalidIPv6LiteralException(String host, boolean wrapInBrackets) {
1148+
String hostString = wrapInBrackets ? "[" + host + "]" : host;
1149+
return new UnknownHostException(hostString + ": invalid IPv6 address literal");
1150+
}
1151+
11451152
/**
11461153
* Returns the loopback address.
11471154
* <p>

0 commit comments

Comments
 (0)