Explorar o código

HADOOP-8154. DNS#getIPs shouldn't silently return the local host IP for bogus interface names. Contributed by Eli Collins

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-1@1304174 13f79535-47bb-0310-9956-ffa450edef68
Eli Collins %!s(int64=13) %!d(string=hai) anos
pai
achega
3f05c6b1b8
Modificáronse 2 ficheiros con 23 adicións e 8 borrados
  1. 3 0
      CHANGES.txt
  2. 20 8
      src/core/org/apache/hadoop/net/DNS.java

+ 3 - 0
CHANGES.txt

@@ -101,6 +101,9 @@ Release 1.1.0 - unreleased
     MAPREDUCE-1740. NPE in getMatchingLevelForNodes when node locations are 
     variable depth (ahmed via tucu)
 
+    HADOOP-8154. DNS#getIPs shouldn't silently return the local host
+    IP for bogus interface names. (eli)
+
   IMPROVEMENTS
 
     MAPREDUCE-3597. [Rumen] Provide a way to access other info of history file

+ 20 - 8
src/core/org/apache/hadoop/net/DNS.java

@@ -30,6 +30,9 @@ import javax.naming.directory.Attributes;
 import javax.naming.directory.DirContext;
 import javax.naming.directory.InitialDirContext;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 /**
  * 
  * A class that provides direct and reverse lookup functionalities, allowing
@@ -39,6 +42,8 @@ import javax.naming.directory.InitialDirContext;
  */
 public class DNS {
 
+  public static final Log LOG = LogFactory.getLog(DNS.class);
+
   /**
    * Returns the hostname associated with the specified IP address by the
    * provided nameserver.
@@ -82,25 +87,32 @@ public class DNS {
    *         interface
    * @throws UnknownHostException
    *             If an UnknownHostException is encountered in querying the
-   *             default interface
+   *             default interface or the given interface can not be found
    * 
    */
   public static String[] getIPs(String strInterface)
     throws UnknownHostException {
+    if ("default".equals(strInterface)) {
+      return new String[] {
+          InetAddress.getLocalHost().getHostAddress()
+      };
+    }
     try {
       NetworkInterface netIF = NetworkInterface.getByName(strInterface);
-      if (netIF == null)
-        return new String[] { InetAddress.getLocalHost()
-                              .getHostAddress() };
-      else {
+      if (netIF == null) {
+        throw new UnknownHostException("Unknown interface " + strInterface);
+      } else {
         Vector<String> ips = new Vector<String>();
-        Enumeration e = netIF.getInetAddresses();
+        Enumeration<InetAddress> e = netIF.getInetAddresses();
         while (e.hasMoreElements())
-          ips.add(((InetAddress) e.nextElement()).getHostAddress());
+          ips.add(e.nextElement().getHostAddress());
         return ips.toArray(new String[] {});
       }
     } catch (SocketException e) {
-      return new String[] { InetAddress.getLocalHost().getHostAddress() };
+      LOG.warn("Unable to get IP for interface " + strInterface, e);
+      return new String[] {
+          InetAddress.getLocalHost().getHostAddress()
+      };
     }
   }