Skip to content

Commit 0cb9ab0

Browse files
committedAug 10, 2023
8313239: InetAddress.getCanonicalHostName may return ip address if reverse lookup fails
Reviewed-by: dfuchs, aefimov, alanb
1 parent 028b3ae commit 0cb9ab0

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed
 

‎src/java.base/share/classes/java/net/InetAddress.java

+25-15
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,8 @@ public boolean isReachable(NetworkInterface netif, int ttl,
731731
* <p>If this InetAddress was created with a host name,
732732
* this host name will be remembered and returned;
733733
* otherwise, a reverse name lookup will be performed
734-
* and the result will be returned based on the system
735-
* configured resolver. If a lookup of the name service
734+
* and the result will be returned based on the system-wide
735+
* resolver. If a lookup of the name service
736736
* is required, call
737737
* {@link #getCanonicalHostName() getCanonicalHostName}.
738738
*
@@ -785,9 +785,15 @@ String getHostName(boolean check) {
785785
}
786786

787787
/**
788-
* Gets the fully qualified domain name for this IP address.
789-
* Best effort method, meaning we may not be able to return
790-
* the FQDN depending on the underlying system configuration.
788+
* Gets the fully qualified domain name for this
789+
* {@linkplain InetAddress#getAddress() IP address} using the system-wide
790+
* {@linkplain InetAddressResolver resolver}.
791+
*
792+
* <p>The system-wide resolver will be used to do a reverse name lookup of the IP address.
793+
* The lookup can fail for many reasons that include the host not being registered with the name
794+
* service. If the resolver is unable to determine the fully qualified
795+
* domain name, this method returns the {@linkplain #getHostAddress() textual representation}
796+
* of the IP address.
791797
*
792798
* <p>If there is a security manager, this method first
793799
* calls its {@code checkConnect} method
@@ -797,9 +803,11 @@ String getHostName(boolean check) {
797803
* If the operation is not allowed, it will return
798804
* the textual representation of the IP address.
799805
*
800-
* @return the fully qualified domain name for this IP address,
801-
* or if the operation is not allowed by the security check,
802-
* the textual representation of the IP address.
806+
* @return the fully qualified domain name for this IP address.
807+
* If either the operation is not allowed by the security check
808+
* or the system-wide resolver wasn't able to determine the
809+
* fully qualified domain name for the IP address, the textual
810+
* representation of the IP address is returned instead.
803811
*
804812
* @see SecurityManager#checkConnect
805813
*
@@ -814,22 +822,24 @@ public String getCanonicalHostName() {
814822
}
815823

816824
/**
817-
* Returns the hostname for this address.
825+
* Returns the fully qualified domain name for the given address.
818826
*
819827
* <p>If there is a security manager, this method first
820828
* calls its {@code checkConnect} method
821829
* with the hostname and {@code -1}
822830
* as its arguments to see if the calling code is allowed to know
823-
* the hostname for this IP address, i.e., to connect to the host.
831+
* the hostname for the given IP address, i.e., to connect to the host.
824832
* If the operation is not allowed, it will return
825833
* the textual representation of the IP address.
826834
*
827-
* @return the host name for this IP address, or if the operation
828-
* is not allowed by the security check, the textual
829-
* representation of the IP address.
830-
*
831835
* @param check make security check if true
832836
*
837+
* @return the fully qualified domain name for the given IP address.
838+
* If either the operation is not allowed by the security check
839+
* or the system-wide resolver wasn't able to determine the
840+
* fully qualified domain name for the IP address, the textual
841+
* representation of the IP address is returned instead.
842+
*
833843
* @see SecurityManager#checkConnect
834844
*/
835845
private static String getHostFromNameService(InetAddress addr, boolean check) {
@@ -1570,7 +1580,7 @@ public static InetAddress getByName(String host)
15701580

15711581
/**
15721582
* Given the name of a host, returns an array of its IP addresses,
1573-
* based on the configured system {@linkplain InetAddressResolver resolver}.
1583+
* based on the system-wide {@linkplain InetAddressResolver resolver}.
15741584
*
15751585
* <p> The host name can either be a machine name, such as
15761586
* "{@code www.example.com}", or a textual representation of its IP

0 commit comments

Comments
 (0)
Please sign in to comment.