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/03/23 04:15:58 UTC

svn commit: r1304175 - in /hadoop/common/branches/branch-1: CHANGES.txt src/core/org/apache/hadoop/net/DNS.java

Author: eli
Date: Fri Mar 23 03:15:57 2012
New Revision: 1304175

URL: http://svn.apache.org/viewvc?rev=1304175&view=rev
Log:
HADOOP-7806. Support binding to sub-interfaces. Contributed by Harsh J, Eli Collins

Modified:
    hadoop/common/branches/branch-1/CHANGES.txt
    hadoop/common/branches/branch-1/src/core/org/apache/hadoop/net/DNS.java

Modified: hadoop/common/branches/branch-1/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-1/CHANGES.txt?rev=1304175&r1=1304174&r2=1304175&view=diff
==============================================================================
--- hadoop/common/branches/branch-1/CHANGES.txt (original)
+++ hadoop/common/branches/branch-1/CHANGES.txt Fri Mar 23 03:15:57 2012
@@ -7,6 +7,8 @@ Release 1.1.0 - unreleased
     MAPREDUCE-3118. Backport Gridmix and Rumen features to
                     branch-0.20-security (Ravi Gummadi via amarrk)
 
+    HADOOP-7806. Support binding to sub-interfaces. (harsh, eli via eli)
+
   BUG FIXES
 
     HDFS-2305. Running multiple 2NNs can result in corrupt file system. (atm)

Modified: hadoop/common/branches/branch-1/src/core/org/apache/hadoop/net/DNS.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-1/src/core/org/apache/hadoop/net/DNS.java?rev=1304175&r1=1304174&r2=1304175&view=diff
==============================================================================
--- hadoop/common/branches/branch-1/src/core/org/apache/hadoop/net/DNS.java (original)
+++ hadoop/common/branches/branch-1/src/core/org/apache/hadoop/net/DNS.java Fri Mar 23 03:15:57 2012
@@ -78,11 +78,35 @@ public class DNS {
   }
 
   /**
+   * @return NetworkInterface for the given subinterface name (eg eth0:0)
+   *    or null if no interface with the given name can be found
+   */
+  private static NetworkInterface getSubinterface(String strInterface)
+      throws SocketException {
+    Enumeration<NetworkInterface> nifs =
+      NetworkInterface.getNetworkInterfaces();
+
+    while (nifs.hasMoreElements()) {
+      Enumeration<NetworkInterface> subNifs =
+        nifs.nextElement().getSubInterfaces();
+
+      while (subNifs.hasMoreElements()) {
+        NetworkInterface nif = subNifs.nextElement();
+        if (nif.getName().equals(strInterface)) {
+          return nif;
+        }
+      }
+    }
+    return null;
+  }
+
+  /**
    * Returns all the IPs associated with the provided interface, if any, in
    * textual form.
    * 
    * @param strInterface
-   *            The name of the network interface to query (e.g. eth0)
+   *            The name of the network interface or subinterface to query
+   *            (eg eth0 or eth0:0) or the string "default"
    * @return A string vector of all the IPs associated with the provided
    *         interface
    * @throws UnknownHostException
@@ -97,16 +121,14 @@ public class DNS {
           InetAddress.getLocalHost().getHostAddress()
       };
     }
+    NetworkInterface netIf;
     try {
-      NetworkInterface netIF = NetworkInterface.getByName(strInterface);
-      if (netIF == null) {
-        throw new UnknownHostException("Unknown interface " + strInterface);
-      } else {
-        Vector<String> ips = new Vector<String>();
-        Enumeration<InetAddress> e = netIF.getInetAddresses();
-        while (e.hasMoreElements())
-          ips.add(e.nextElement().getHostAddress());
-        return ips.toArray(new String[] {});
+      netIf = NetworkInterface.getByName(strInterface);
+      if (netIf == null) {
+        netIf = getSubinterface(strInterface);
+        if (netIf == null) {
+          throw new UnknownHostException("Unknown interface " + strInterface);
+        }
       }
     } catch (SocketException e) {
       LOG.warn("Unable to get IP for interface " + strInterface, e);
@@ -114,6 +136,11 @@ public class DNS {
           InetAddress.getLocalHost().getHostAddress()
       };
     }
+    Vector<String> ips = new Vector<String>();
+    Enumeration<InetAddress> e = netIf.getInetAddresses();
+    while (e.hasMoreElements())
+      ips.add(e.nextElement().getHostAddress());
+    return ips.toArray(new String[] {});
   }
 
   /**
@@ -121,7 +148,8 @@ public class DNS {
    * network interface
    * 
    * @param strInterface
-   *            The name of the network interface to query (e.g. eth0)
+   *            The name of the network interface or subinterface to query
+   *            (e.g. eth0 or eth0:0) or the string "default"
    * @return The IP address in text form
    * @throws UnknownHostException
    *             If one is encountered in querying the default interface
@@ -137,7 +165,8 @@ public class DNS {
    * address bound to the specified network interface
    * 
    * @param strInterface
-   *            The name of the network interface to query (e.g. eth0)
+   *            The name of the network interface or subinterface to query
+   *            (e.g. eth0 or eth0:0) or the string "default"
    * @param nameserver
    *            The DNS host name
    * @return A string vector of all host names associated with the IPs tied to