Skip to content

Commit 12d2d0e

Browse files
Alexei VoitylovRealCLanger
authored andcommitted
8299129: Enhance NameService lookups
Reviewed-by: mbalao Backport-of: 1aef50354aaa0831b58de81db3d6bf30b9a277d1
1 parent e78fe92 commit 12d2d0e

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

src/java.base/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, 2021, 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
@@ -1323,44 +1323,45 @@ private static InetAddress[] getAllByName(String host, InetAddress reqAddr)
13231323
host = host.substring(1, host.length() -1);
13241324
ipv6Expected = true;
13251325
} else {
1326-
// This was supposed to be a IPv6 address, but it's not!
1327-
throw new UnknownHostException(host + ": invalid IPv6 address");
1326+
// This was supposed to be a IPv6 literal, but it's not
1327+
throw invalidIPv6LiteralException(host, false);
13281328
}
13291329
}
13301330

1331-
// if host is an IP address, we won't do further lookup
1331+
// Check and try to parse host string as an IP address literal
13321332
if (IPAddressUtil.digit(host.charAt(0), 16) != -1
13331333
|| (host.charAt(0) == ':')) {
1334-
byte[] addr;
1334+
byte[] addr = null;
13351335
int numericZone = -1;
13361336
String ifname = null;
1337-
// see if it is IPv4 address
1338-
try {
1339-
addr = IPAddressUtil.validateNumericFormatV4(host);
1340-
} catch (IllegalArgumentException iae) {
1341-
var uhe = new UnknownHostException(host);
1342-
uhe.initCause(iae);
1343-
throw uhe;
1337+
1338+
if (!ipv6Expected) {
1339+
// check if it is IPv4 address only if host is not wrapped in '[]'
1340+
try {
1341+
addr = IPAddressUtil.validateNumericFormatV4(host);
1342+
} catch (IllegalArgumentException iae) {
1343+
var uhe = new UnknownHostException(host);
1344+
uhe.initCause(iae);
1345+
throw uhe;
1346+
}
13441347
}
13451348
if (addr == null) {
1346-
// This is supposed to be an IPv6 literal
1347-
// Check if a numeric or string zone id is present
1349+
// Try to parse host string as an IPv6 literal
1350+
// Check if a numeric or string zone id is present first
13481351
int pos;
1349-
if ((pos=host.indexOf ('%')) != -1) {
1350-
numericZone = checkNumericZone (host);
1352+
if ((pos = host.indexOf('%')) != -1) {
1353+
numericZone = checkNumericZone(host);
13511354
if (numericZone == -1) { /* remainder of string must be an ifname */
1352-
ifname = host.substring (pos+1);
1355+
ifname = host.substring(pos + 1);
13531356
}
13541357
}
1355-
if ((addr = IPAddressUtil.textToNumericFormatV6(host)) == null && host.contains(":")) {
1356-
throw new UnknownHostException(host + ": invalid IPv6 address");
1358+
if ((addr = IPAddressUtil.textToNumericFormatV6(host)) == null &&
1359+
(host.contains(":") || ipv6Expected)) {
1360+
throw invalidIPv6LiteralException(host, ipv6Expected);
13571361
}
1358-
} else if (ipv6Expected) {
1359-
// Means an IPv4 literal between brackets!
1360-
throw new UnknownHostException("["+host+"]");
13611362
}
1362-
InetAddress[] ret = new InetAddress[1];
13631363
if(addr != null) {
1364+
InetAddress[] ret = new InetAddress[1];
13641365
if (addr.length == Inet4Address.INADDRSZ) {
13651366
if (numericZone != -1 || ifname != null) {
13661367
// IPv4-mapped address must not contain zone-id
@@ -1377,12 +1378,18 @@ private static InetAddress[] getAllByName(String host, InetAddress reqAddr)
13771378
return ret;
13781379
}
13791380
} else if (ipv6Expected) {
1380-
// We were expecting an IPv6 Literal, but got something else
1381-
throw new UnknownHostException("["+host+"]");
1381+
// We were expecting an IPv6 Literal since host string starts
1382+
// and ends with square brackets, but we got something else.
1383+
throw invalidIPv6LiteralException(host, true);
13821384
}
13831385
return getAllByName0(host, reqAddr, true, true);
13841386
}
13851387

1388+
private static UnknownHostException invalidIPv6LiteralException(String host, boolean wrapInBrackets) {
1389+
String hostString = wrapInBrackets ? "[" + host + "]" : host;
1390+
return new UnknownHostException(hostString + ": invalid IPv6 address literal");
1391+
}
1392+
13861393
/**
13871394
* Returns the loopback address.
13881395
* <p>

0 commit comments

Comments
 (0)