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
This commit is contained in:
parent
2c47463cba
commit
78e3568340
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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, ", ");
|
||||
|
|
Loading…
Reference in New Issue