From 78e3568340f225c33f919214f93e470e0e4871db Mon Sep 17 00:00:00 2001 From: Eli Collins Date: Sun, 1 Apr 2012 20:07:03 +0000 Subject: [PATCH] HADOOP-8238. NetUtils#getHostNameOfIP blows up if given ip:port string w/o port. Contributed by Eli Collins git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1308192 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES.txt | 3 ++ .../java/org/apache/hadoop/net/NetUtils.java | 34 +++++++++---------- .../org/apache/hadoop/net/TestNetUtils.java | 12 +++++++ 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index d779a980fa..e0ccc9314c 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -302,6 +302,9 @@ Release 2.0.0 - UNRELEASED HADOOP-8218. RPC.closeProxy shouldn't throw error when closing a mock (todd) + HADOOP-8238. NetUtils#getHostNameOfIP blows up if given ip:port + string w/o port. (eli) + BREAKDOWN OF HADOOP-7454 SUBTASKS HADOOP-7455. HA: Introduce HA Service Protocol Interface. (suresh) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java index 0bfeaa7cfc..90c5f169c9 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java @@ -570,31 +570,29 @@ public class NetUtils { } } - private static final Pattern ipPattern = // Pattern for matching hostname to ip:port - Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:?\\d*"); + private static final Pattern ipPortPattern = // Pattern for matching ip[:port] + Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}(:\\d+)?"); /** - * Attempt to obtain the host name of a name specified by ip address. - * Check that the node name is an ip addr and if so, attempt to determine - * its host name. If the name is not an IP addr, or the actual name cannot - * be determined, return null. + * Attempt to obtain the host name of the given string which contains + * an IP address and an optional port. * - * @return Host name or null + * @param ipPort string of form ip[:port] + * @return Host name or null if the name can not be determined */ - public static String getHostNameOfIP(String ip) { - // If name is not an ip addr, don't bother looking it up - if(!ipPattern.matcher(ip).matches()) - return null; - - String hostname = ""; - try { - String n = ip.substring(0, ip.indexOf(':')); - hostname = InetAddress.getByName(n).getHostName(); - } catch (UnknownHostException e) { + public static String getHostNameOfIP(String ipPort) { + if (null == ipPort || !ipPortPattern.matcher(ipPort).matches()) { return null; } - return hostname; + try { + int colonIdx = ipPort.indexOf(':'); + String ip = (-1 == colonIdx) ? ipPort + : ipPort.substring(0, ipPort.indexOf(':')); + return InetAddress.getByName(ip).getHostName(); + } catch (UnknownHostException e) { + return null; + } } /** diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java index 557cdc8c77..e67dad93d3 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java @@ -499,6 +499,18 @@ public class TestNetUtils { assertEquals("scheme://host.a.b/path", uri.toString()); } + @Test + public void testGetHostNameOfIP() { + assertNull(NetUtils.getHostNameOfIP(null)); + assertNull(NetUtils.getHostNameOfIP("")); + assertNull(NetUtils.getHostNameOfIP("crazytown")); + assertNull(NetUtils.getHostNameOfIP("127.0.0.1:")); // no port + assertNull(NetUtils.getHostNameOfIP("127.0.0.1:-1")); // bogus port + assertNull(NetUtils.getHostNameOfIP("127.0.0.1:A")); // bogus port + assertNotNull(NetUtils.getHostNameOfIP("127.0.0.1")); + assertNotNull(NetUtils.getHostNameOfIP("127.0.0.1:1")); + } + private void assertBetterArrayEquals(T[] expect, T[]got) { String expectStr = StringUtils.join(expect, ", "); String gotStr = StringUtils.join(got, ", ");