HADOOP-7806. Support binding to sub-interfaces. Contributed by Eli Collins

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1298700 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Eli Collins 2012-03-09 04:43:06 +00:00
parent d8cc362350
commit 07c3b02281
2 changed files with 40 additions and 11 deletions

View File

@ -234,6 +234,8 @@ Release 0.23.3 - UNRELEASED
HADOOP-7557 Make IPC header be extensible (sanjay radia) HADOOP-7557 Make IPC header be extensible (sanjay radia)
HADOOP-7806. Support binding to sub-interfaces (eli)
OPTIMIZATIONS OPTIMIZATIONS
BUG FIXES BUG FIXES

View File

@ -90,13 +90,36 @@ public class DNS {
return attribute.get("PTR").get().toString(); return attribute.get("PTR").get().toString();
} }
/**
* @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 * Returns all the IPs associated with the provided interface, if any, in
* textual form. * textual form.
* *
* @param strInterface * @param strInterface
* The name of the network interface to query (e.g. eth0) * The name of the network interface or sub-interface to query
* or the string "default" * (eg eth0 or eth0:0) or the string "default"
* @return A string vector of all the IPs associated with the provided * @return A string vector of all the IPs associated with the provided
* interface. The local host IP is returned if the interface * interface. The local host IP is returned if the interface
* name "default" is specified or there is an I/O error looking * name "default" is specified or there is an I/O error looking
@ -110,21 +133,24 @@ public class DNS {
if ("default".equals(strInterface)) { if ("default".equals(strInterface)) {
return new String[] { cachedHostAddress }; return new String[] { cachedHostAddress };
} }
NetworkInterface netIF; NetworkInterface netIf;
try { try {
netIF = NetworkInterface.getByName(strInterface); netIf = NetworkInterface.getByName(strInterface);
if (netIf == null) {
netIf = getSubinterface(strInterface);
}
} catch (SocketException e) { } catch (SocketException e) {
LOG.warn("I/O error finding interface " + strInterface + LOG.warn("I/O error finding interface " + strInterface +
": " + e.getMessage()); ": " + e.getMessage());
return new String[] { cachedHostAddress }; return new String[] { cachedHostAddress };
} }
if (netIF == null) { if (netIf == null) {
throw new UnknownHostException("No such interface " + strInterface); throw new UnknownHostException("No such interface " + strInterface);
} }
Vector<String> ips = new Vector<String>(); Vector<String> ips = new Vector<String>();
Enumeration<InetAddress> e = netIF.getInetAddresses(); Enumeration<InetAddress> addrs = netIf.getInetAddresses();
while (e.hasMoreElements()) { while (addrs.hasMoreElements()) {
ips.add(e.nextElement().getHostAddress()); ips.add(addrs.nextElement().getHostAddress());
} }
return ips.toArray(new String[] {}); return ips.toArray(new String[] {});
} }
@ -135,8 +161,8 @@ public class DNS {
* network interface or the local host IP if "default" is given. * network interface or the local host IP if "default" is given.
* *
* @param strInterface * @param strInterface
* The name of the network interface to query (e.g. eth0) * The name of the network interface or subinterface to query
* or the string "default" * (e.g. eth0 or eth0:0) or the string "default"
* @return The IP address in text form, the local host IP is returned * @return The IP address in text form, the local host IP is returned
* if the interface name "default" is specified * if the interface name "default" is specified
* @throws UnknownHostException * @throws UnknownHostException
@ -153,7 +179,8 @@ public class DNS {
* address bound to the specified network interface * address bound to the specified network interface
* *
* @param strInterface * @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)
* @param nameserver * @param nameserver
* The DNS host name * The DNS host name
* @return A string vector of all host names associated with the IPs tied to * @return A string vector of all host names associated with the IPs tied to