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 su...@apache.org on 2011/09/28 19:47:21 UTC
svn commit: r1176986 - 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: suresh
Date: Wed Sep 28 17:47:20 2011
New Revision: 1176986
URL: http://svn.apache.org/viewvc?rev=1176986&view=rev
Log:
HADOOP-7668. Add a NetUtils method that can tell if an InetAddress belongs to local host. Contributed by Suresh Srinivas.
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=1176986&r1=1176985&r2=1176986&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt (original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt Wed Sep 28 17:47:20 2011
@@ -19,6 +19,9 @@ Trunk (unreleased changes)
HADOOP-7635. RetryInvocationHandler should release underlying resources on
close (atm)
+
+ HADOOP-7668. Add a NetUtils method that can tell if an InetAddress
+ belongs to local host. (suresh)
BUGS
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=1176986&r1=1176985&r2=1176986&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 Wed Sep 28 17:47:20 2011
@@ -516,4 +516,27 @@ public class NetUtils {
} catch (UnknownHostException ignore) { }
return addr;
}
+
+ /**
+ * Given an InetAddress, checks to see if the address is a local address, by
+ * comparing the address with all the interfaces on the node.
+ * @param addr address to check if it is local node's address
+ * @return true if the address corresponds to the local node
+ */
+ public static boolean isLocalAddress(InetAddress addr) {
+ // Check if the address is any local or loop back
+ boolean local = addr.isAnyLocalAddress() || addr.isLoopbackAddress();
+ System.out.println("address is any or loopback address " + addr);
+
+ // Check if the address is defined on any interface
+ if (!local) {
+ try {
+ local = NetworkInterface.getByInetAddress(addr) != null;
+ } catch (SocketException e) {
+ local = false;
+ }
+ }
+ System.out.println("address " + addr + " is local " + local);
+ return local;
+ }
}
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=1176986&r1=1176985&r2=1176986&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 Wed Sep 28 17:47:20 2011
@@ -18,13 +18,17 @@
package org.apache.hadoop.net;
import org.junit.Test;
+
import static org.junit.Assert.*;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
import java.net.Socket;
import java.net.ConnectException;
import java.net.SocketException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
+import java.util.Enumeration;
import org.apache.hadoop.conf.Configuration;
@@ -88,4 +92,32 @@ public class TestNetUtils {
fail("NetUtils.verifyHostnames threw unexpected UnknownHostException");
}
}
+
+ /**
+ * Test for {@link NetUtils#isLocalAddress(java.net.InetAddress)}
+ */
+ @Test
+ public void testIsLocalAddress() throws Exception {
+ // Test - local host is local address
+ assertTrue(NetUtils.isLocalAddress(InetAddress.getLocalHost()));
+
+ // Test - all addresses bound network interface is local address
+ Enumeration<NetworkInterface> interfaces = NetworkInterface
+ .getNetworkInterfaces();
+ if (interfaces != null) { // Iterate through all network interfaces
+ while (interfaces.hasMoreElements()) {
+ NetworkInterface i = interfaces.nextElement();
+ Enumeration<InetAddress> addrs = i.getInetAddresses();
+ if (addrs == null) {
+ continue;
+ }
+ // Iterate through all the addresses of a network interface
+ while (addrs.hasMoreElements()) {
+ InetAddress addr = addrs.nextElement();
+ assertTrue(NetUtils.isLocalAddress(addr));
+ }
+ }
+ }
+ assertFalse(NetUtils.isLocalAddress(InetAddress.getByName("8.8.8.8")));
+ }
}