From e192d61972b90c185cbe4634ec0e0e9cb04f7081 Mon Sep 17 00:00:00 2001 From: orbiter Date: Mon, 29 Nov 2010 19:32:47 +0000 Subject: [PATCH] fix for latest commit git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7352 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- source/net/yacy/cora/protocol/Domains.java | 36 ++++++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/source/net/yacy/cora/protocol/Domains.java b/source/net/yacy/cora/protocol/Domains.java index 8955552da..5919bb463 100644 --- a/source/net/yacy/cora/protocol/Domains.java +++ b/source/net/yacy/cora/protocol/Domains.java @@ -570,7 +570,7 @@ public class Domains { if (nameCacheMiss.size() > maxNameCacheMissSize) nameCacheMiss.clear(); } - private static final String localHostName = "127.0.0.1"; + private static String localHostName = "127.0.0.1"; private static Set localHostAddresses = new HashSet(); static { try { @@ -604,15 +604,14 @@ public class Domains { } // now look up the host name - String lhn = localHostName; try { - lhn = getHostName(InetAddress.getLocalHost()); + localHostName = getHostName(InetAddress.getLocalHost()); } catch (UnknownHostException e) {} // after the host name was resolved, we try to look up more local addresses // using the host name: try { - InetAddress[] moreAddresses = InetAddress.getAllByName(lhn); + InetAddress[] moreAddresses = InetAddress.getAllByName(localHostName); if (moreAddresses != null) for (InetAddress a: moreAddresses) localHostAddresses.add(a); } catch (UnknownHostException e) { Log.logException(e); @@ -644,14 +643,23 @@ public class Domains { return a; } // there is only a local address, we filter out the possibly - // returned loopback address 127.0.0.1 + // returned loopback address 127.0.0.1 and all addresses without a name for (InetAddress a: localHostAddresses) { - if ((0Xff & a.getAddress()[0]) != 127 && a.getHostAddress().indexOf(":") < 0) return a; + if ((0Xff & a.getAddress()[0]) != 127 && + a.getHostAddress().indexOf(":") < 0 && + a.getHostName() != null && + a.getCanonicalHostName().length() > 0) return a; + } + // if no address has a name, then take any other than the loopback + for (InetAddress a: localHostAddresses) { + if ((0Xff & a.getAddress()[0]) != 127 && + a.getHostAddress().indexOf(":") < 0) return a; } // if all fails, give back whatever we have for (InetAddress a: localHostAddresses) { if (a.getHostAddress().indexOf(":") < 0) return a; } + // finally, just get any return localHostAddresses.iterator().next(); } @@ -756,6 +764,22 @@ public class Domains { } public static void main(String[] args) { + /* + try { + Enumeration nis = NetworkInterface.getNetworkInterfaces(); + while (nis.hasMoreElements()) { + NetworkInterface ni = nis.nextElement(); + Enumeration addrs = ni.getInetAddresses(); + while (addrs.hasMoreElements()) { + InetAddress addr = addrs.nextElement(); + System.out.println(addr); + } + } + } catch(SocketException e) { + System.err.println(e); + } + */ + System.out.println("myPublicLocalIP: " + myPublicLocalIP()); for (InetAddress a : myIntranetIPs()) { System.out.println("Intranet IP: " + a);