Gets a hostname or an IP address and returns the textual representation of the IP address. This method can block as there is no API for asynchronous DNS resolution in the JDK. IPv4 / IPv6 handling: if (JVM is running with -Djava.net.preferIPv6Addresses=true && remote host ha
(final String host)
| 4574 | * or {@code null} if the address couldn't be resolved. |
| 4575 | */ |
| 4576 | private static String getIP(final String host) { |
| 4577 | final long start = System.nanoTime(); |
| 4578 | try { |
| 4579 | boolean preferV6 = Boolean.valueOf( |
| 4580 | System.getProperty("java.net.preferIPv6Addresses")); |
| 4581 | final String ip; |
| 4582 | if (preferV6) { |
| 4583 | LOG.debug("Trying to get IPv6 address for host: " + host); |
| 4584 | InetAddress ipv6 = null; |
| 4585 | LOG.debug("All resolved IPs for host: " + host + " are: " + |
| 4586 | Arrays.toString(InetAddress.getAllByName(host))); |
| 4587 | for (InetAddress ia : InetAddress.getAllByName(host)) { |
| 4588 | if (ia instanceof Inet6Address) { |
| 4589 | ipv6 = ia; |
| 4590 | break; |
| 4591 | } |
| 4592 | } |
| 4593 | ip = (ipv6 != null)? ipv6.getHostAddress() : |
| 4594 | InetAddress.getByName(host).getHostAddress(); |
| 4595 | } else { |
| 4596 | LOG.debug("Trying to get IPv4 address for host: " + host); |
| 4597 | ip = InetAddress.getByName(host).getHostAddress(); |
| 4598 | } |
| 4599 | LOG.info("Resolved IP address for host: " + host + " is: " + ip); |
| 4600 | |
| 4601 | |
| 4602 | final long latency = System.nanoTime() - start; |
| 4603 | if (latency > 500000/*ns*/ && LOG.isDebugEnabled()) { |
| 4604 | LOG.debug("Resolved IP of `" + host + "' to " |
| 4605 | + ip + " in " + latency + "ns"); |
| 4606 | } else if (latency >= 3000000/*ns*/) { |
| 4607 | LOG.warn("Slow DNS lookup! Resolved IP of `" + host + "' to " |
| 4608 | + ip + " in " + latency + "ns"); |
| 4609 | } |
| 4610 | return ip; |
| 4611 | } catch (UnknownHostException e) { |
| 4612 | LOG.error("Failed to resolve the IP of `" + host + "' in " |
| 4613 | + (System.nanoTime() - start) + "ns"); |
| 4614 | return null; |
| 4615 | } |
| 4616 | } |
| 4617 | |
| 4618 | /** |
| 4619 | * Parses a TCP port number from a string. |
no test coverage detected