private static String getMyAddress() { try { return DNS.getDefaultHost("default", "default"); } catch (UnknownHostException uhe) { LOG.error("cannot determine my address", uhe); return null; } }
String reverseDNS(InetAddress ipAddress) throws UnknownHostException { String hostName = this.reverseDNSCacheMap.get(ipAddress); if (hostName == null) { String ipAddressString = null; try { ipAddressString = DNS.reverseDns(ipAddress, null); } catch (Exception e) { // We can use InetAddress in case the jndi failed to pull up the reverse DNS entry from the // name service. Also, in case of ipv6, we need to use the InetAddress since resolving // reverse DNS using jndi doesn't work well with ipv6 addresses. ipAddressString = InetAddress.getByName(ipAddress.getHostAddress()).getHostName(); } if (ipAddressString == null) throw new UnknownHostException("No host found for " + ipAddress); hostName = Strings.domainNamePointerToHostName(ipAddressString); this.reverseDNSCacheMap.put(ipAddress, hostName); } return hostName; }
/** * Like {@link DNS#getIPs(String, boolean), but returns all * IPs associated with the given interface and its subinterfaces. */ public static String[] getIPs(String strInterface) throws UnknownHostException { return getIPs(strInterface, true); }
final List<String> hosts = new Vector<String>(); final List<InetAddress> addresses = getIPsAsInetAddressList(strInterface, true); for (InetAddress address : addresses) { try { hosts.add(reverseDns(address, nameserver)); } catch (NamingException ignored) {
netIf = NetworkInterface.getByName(strInterface); if (netIf == null) { netIf = getSubinterface(strInterface); allAddrs.addAll(Collections.list(netIf.getInetAddresses())); if (!returnSubinterfaces) { allAddrs.removeAll(getSubinterfaceInetAddrs(netIf));
/** * Returns the default (first) host name associated by the provided * nameserver with the address bound to the specified network interface * * @param strInterface * The name of the network interface to query (e.g. eth0) * @param nameserver * The DNS host name * @return The default host names associated with IPs bound to the network * interface * @throws UnknownHostException * If one is encountered while querying the default interface */ public static String getDefaultHost(String strInterface, String nameserver) throws UnknownHostException { if ("default".equals(strInterface)) { return cachedHostname; } if ("default".equals(nameserver)) { return getDefaultHost(strInterface); } String[] hosts = getHosts(strInterface, nameserver); return hosts[0]; }
/** * Test null interface name */ @Test public void testNullInterface() throws Exception { try { String host = DNS.getDefaultHost(null); fail("Expected a NullPointerException, got " + host); } catch (NullPointerException npe) { // Expected } try { String ip = DNS.getDefaultIP(null); fail("Expected a NullPointerException, got " + ip); } catch (NullPointerException npe) { // Expected } }
/** * Returns all the host names associated by the provided nameserver with the * address bound to the specified network interface * * @param strInterface * The name of the network interface to query (e.g. eth0) * @param nameserver * The DNS host name * @return A string vector of all host names associated with the IPs tied to * the specified interface * @throws UnknownHostException */ public static String[] getHosts(String strInterface, String nameserver) throws UnknownHostException { String[] ips = getIPs(strInterface); Vector<String> hosts = new Vector<String>(); for (int ctr = 0; ctr < ips.length; ctr++) try { hosts.add(reverseDns(InetAddress.getByName(ips[ctr]), nameserver)); } catch (Exception e) { } if (hosts.size() == 0) return new String[] { InetAddress.getLocalHost().getCanonicalHostName() }; else return hosts.toArray(new String[] {}); }
/** * Test the "default" IP addresses is the local IP addr */ @Test public void testGetIPWithDefault() throws Exception { String[] ips = DNS.getIPs(DEFAULT); assertEquals("Should only return 1 default IP", 1, ips.length); assertEquals(getLocalIPAddr().getHostAddress(), ips[0].toString()); String ip = DNS.getDefaultIP(DEFAULT); assertEquals(ip, ips[0].toString()); }
/** * Generate new blockpoolID. * * @return new blockpoolID */ static String newBlockPoolID() throws UnknownHostException{ String ip; try { ip = DNS.getDefaultIP("default"); } catch (UnknownHostException e) { LOG.warn("Could not find ip address of \"default\" inteface."); throw e; } int rand = DFSUtil.getSecureRandom().nextInt(Integer.MAX_VALUE); return "BP-" + rand + "-"+ ip + "-" + Time.now(); }
/** * Returns all the host names associated by the default nameserver with the * address bound to the specified network interface * * @param strInterface * The name of the network interface to query (e.g. eth0) * @return The list of host names associated with IPs bound to the network * interface * @throws UnknownHostException * If one is encountered while querying the default interface * */ public static String[] getHosts(String strInterface) throws UnknownHostException { return getHosts(strInterface, null, false); }
/** * Returns the default (first) host name associated by the provided * nameserver with the address bound to the specified network interface * * @param strInterface * The name of the network interface to query (e.g. eth0) * @param nameserver * The DNS host name * @return The default host names associated with IPs bound to the network * interface * @throws UnknownHostException * If one is encountered while querying the default interface */ public static String getDefaultHost(String strInterface, String nameserver) throws UnknownHostException { if ("default".equals(strInterface)) { return cachedHostname; } if ("default".equals(nameserver)) { return getDefaultHost(strInterface); } String[] hosts = getHosts(strInterface, nameserver); return hosts[0]; }
/** * Test null interface name */ @Test public void testNullInterface() throws Exception { try { String host = DNS.getDefaultHost(null); fail("Expected a NullPointerException, got " + host); } catch (NullPointerException npe) { // Expected } try { String ip = DNS.getDefaultIP(null); fail("Expected a NullPointerException, got " + ip); } catch (NullPointerException npe) { // Expected } }
/** * Returns all the host names associated by the provided nameserver with the * address bound to the specified network interface * * @param strInterface * The name of the network interface to query (e.g. eth0) * @param nameserver * The DNS host name * @return A string vector of all host names associated with the IPs tied to * the specified interface * @throws UnknownHostException */ public static String[] getHosts(String strInterface, String nameserver) throws UnknownHostException { String[] ips = getIPs(strInterface); Vector<String> hosts = new Vector<String>(); for (int ctr = 0; ctr < ips.length; ctr++) try { hosts.add(reverseDns(InetAddress.getByName(ips[ctr]), nameserver)); } catch (Exception e) { } if (hosts.size() == 0) return new String[] { InetAddress.getLocalHost().getCanonicalHostName() }; else return hosts.toArray(new String[] {}); }
/** * Test the "default" IP addresses is the local IP addr */ @Test public void testGetIPWithDefault() throws Exception { String[] ips = DNS.getIPs(DEFAULT); assertEquals("Should only return 1 default IP", 1, ips.length); assertEquals(getLocalIPAddr().getHostAddress(), ips[0].toString()); String ip = DNS.getDefaultIP(DEFAULT); assertEquals(ip, ips[0].toString()); }
static private String getUniqueRackPrefix() { String ip = "unknownIP"; try { ip = DNS.getDefaultIP("default"); } catch (UnknownHostException ignored) { System.out.println("Could not find ip address of \"default\" inteface."); } int rand = 0; try { rand = SecureRandom.getInstance("SHA1PRNG").nextInt(Integer.MAX_VALUE); } catch (NoSuchAlgorithmException e) { rand = (new Random()).nextInt(Integer.MAX_VALUE); } return "/Rack-" + rand + "-"+ ip + "-" + System.currentTimeMillis(); } }
netIf = NetworkInterface.getByName(strInterface); if (netIf == null) { netIf = getSubinterface(strInterface); allAddrs.addAll(Collections.list(netIf.getInetAddresses())); if (!returnSubinterfaces) { allAddrs.removeAll(getSubinterfaceInetAddrs(netIf));
final List<String> hosts = new Vector<String>(); final List<InetAddress> addresses = getIPsAsInetAddressList(strInterface, true); for (InetAddress address : addresses) { try { hosts.add(reverseDns(address, nameserver)); } catch (NamingException ignored) {
/** * Returns the default (first) host name associated by the provided * nameserver with the address bound to the specified network interface * * @param strInterface * The name of the network interface to query (e.g. eth0) * @param nameserver * The DNS host name * @return The default host names associated with IPs bound to the network * interface * @throws UnknownHostException * If one is encountered while querying the default interface */ public static String getDefaultHost(@Nullable String strInterface, @Nullable String nameserver, boolean tryfallbackResolution) throws UnknownHostException { if (strInterface == null || "default".equals(strInterface)) { return cachedHostname; } if (nameserver != null && "default".equals(nameserver)) { nameserver = null; } String[] hosts = getHosts(strInterface, nameserver, tryfallbackResolution); return hosts[0]; }
/** * Wrapper around DNS.getDefaultHost(String, String), calling * DNS.getDefaultHost(String, String, boolean) when available. * * @param strInterface The network interface to query. * @param nameserver The DNS host name. * @return The default host names associated with IPs bound to the network interface. */ public static String getDefaultHost(String strInterface, String nameserver) throws UnknownHostException { if (HAS_NEW_DNS_GET_DEFAULT_HOST_API) { try { // Hadoop-2.8 includes a String, String, boolean variant of getDefaultHost // which properly handles multi-homed systems with Kerberos. return (String) GET_DEFAULT_HOST_METHOD.invoke(null, strInterface, nameserver, true); } catch (Exception e) { // If we can't invoke the method as it should exist, throw an exception throw new RuntimeException("Failed to invoke DNS.getDefaultHost via reflection", e); } } else { return org.apache.hadoop.net.DNS.getDefaultHost(strInterface, nameserver); } } }