You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by el...@apache.org on 2012/04/01 22:07:03 UTC
svn commit: r1308192 - in
/hadoop/common/trunk/hadoop-common-project/hadoop-common: CHANGES.txt
src/main/java/org/apache/hadoop/net/NetUtils.java
src/test/java/org/apache/hadoop/net/TestNetUtils.java
Author: eli
Date: Sun Apr 1 20:07:03 2012
New Revision: 1308192
URL: http://svn.apache.org/viewvc?rev=1308192&view=rev
Log:
HADOOP-8238. NetUtils#getHostNameOfIP blows up if given ip:port string w/o port. Contributed by Eli Collins
Modified:
hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt
hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java
hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java
Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt?rev=1308192&r1=1308191&r2=1308192&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt (original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt Sun Apr 1 20:07:03 2012
@@ -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)
Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java?rev=1308192&r1=1308191&r2=1308192&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java (original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java Sun Apr 1 20:07:03 2012
@@ -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())
+ public static String getHostNameOfIP(String ipPort) {
+ if (null == ipPort || !ipPortPattern.matcher(ipPort).matches()) {
return null;
+ }
- String hostname = "";
try {
- String n = ip.substring(0, ip.indexOf(':'));
- hostname = InetAddress.getByName(n).getHostName();
+ int colonIdx = ipPort.indexOf(':');
+ String ip = (-1 == colonIdx) ? ipPort
+ : ipPort.substring(0, ipPort.indexOf(':'));
+ return InetAddress.getByName(ip).getHostName();
} catch (UnknownHostException e) {
return null;
}
-
- return hostname;
}
/**
Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java?rev=1308192&r1=1308191&r2=1308192&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java (original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java Sun Apr 1 20:07:03 2012
@@ -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 <T> void assertBetterArrayEquals(T[] expect, T[]got) {
String expectStr = StringUtils.join(expect, ", ");
String gotStr = StringUtils.join(got, ", ");