From 167c5a51f0a24e8c168b38360d002c8081f3869f Mon Sep 17 00:00:00 2001 From: Michael Peter Christen Date: Tue, 28 Oct 2014 15:36:13 +0100 Subject: [PATCH] IPv6 fix --- source/net/yacy/cora/protocol/Domains.java | 8 +-- source/net/yacy/peers/Seed.java | 72 +++++++--------------- source/net/yacy/server/serverSwitch.java | 4 +- 3 files changed, 28 insertions(+), 56 deletions(-) diff --git a/source/net/yacy/cora/protocol/Domains.java b/source/net/yacy/cora/protocol/Domains.java index 7df76b942..5d115ff2b 100644 --- a/source/net/yacy/cora/protocol/Domains.java +++ b/source/net/yacy/cora/protocol/Domains.java @@ -153,9 +153,7 @@ public class Domains { // fill a cache of local host names for (final InetAddress a: myHostAddresses) { - String hostaddressP = a.getHostAddress(); - int p = hostaddressP.indexOf('%'); - if (p > 0) hostaddressP = hostaddressP.substring(0, p); + String hostaddressP = chopZoneID(a.getHostAddress()); Set hns = new LinkedHashSet<>(); // generate alternative representations of IPv6 addresses which are needed to check access on the interface (i.e. localhost check) if (hostaddressP.indexOf("::") < 0) { @@ -1072,7 +1070,7 @@ public class Domains { * @return list of all intranet addresses */ public static Set myIntranetIPs() { - if (myHostAddresses.size() < 1) try {Thread.sleep(1000);} catch (final InterruptedException e) {} + if (localHostAddresses.size() < 1) try {Thread.sleep(1000);} catch (final InterruptedException e) {} return localHostAddresses; } @@ -1106,7 +1104,7 @@ public class Domains { public static boolean isThisHostIP(final InetAddress clientAddress) { if (clientAddress == null) return false; if (clientAddress.isAnyLocalAddress() || clientAddress.isLoopbackAddress()) return true; - return myHostAddresses.contains(clientAddress); + return myHostAddresses.contains(clientAddress); // includes localHostAddresses } public static int getDomainID(final String host, final InetAddress hostaddress) { diff --git a/source/net/yacy/peers/Seed.java b/source/net/yacy/peers/Seed.java index c7babf536..4c1b2eca4 100644 --- a/source/net/yacy/peers/Seed.java +++ b/source/net/yacy/peers/Seed.java @@ -49,6 +49,7 @@ import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URL; import java.text.ParseException; +import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashSet; @@ -56,6 +57,7 @@ import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Random; import java.util.Set; +import java.util.StringTokenizer; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -321,6 +323,18 @@ public class Seed implements Cloneable, Comparable, Comparator return name.startsWith("_anon"); } + + private Set getIPv6Entries() { + String ip6s = this.dna.get(Seed.IP6); + final Set set = Collections.synchronizedSet(new HashSet()); + if (ip6s == null) return set; + final StringTokenizer st = new StringTokenizer(ip6s, "|"); + while (st.hasMoreTokens()) { + set.add(Domains.chopZoneID(st.nextToken().trim())); + } + return set; + } + /** * try to get the public IP
* @@ -329,29 +343,11 @@ public class Seed implements Cloneable, Comparable, Comparator @Deprecated public final String getIP() { final String ipx = this.dna.get(Seed.IP); // may contain both, IPv4 or IPv6 - final String ip6 = this.dna.get(Seed.IP6); - Set ip6s = MapTools.string2set(ip6, "|"); - - if (ip6s == null || ip6s.size() == 0) { - if (ipx != null && !ipx.isEmpty()) return Domains.chopZoneID(ipx); - } - if (ip6s != null && ip6s.size() == 1) { - // We prefer IPv6 - for (String s: ip6s) if (s.length() > 0) return Domains.chopZoneID(s); - if (ipx != null && !ipx.isEmpty()) return Domains.chopZoneID(ipx); - } - - // if we have more than one IPv6, then chances are high that one of them do not work. - // in that case we prefer the IPv4 if (ipx != null && !ipx.isEmpty()) return Domains.chopZoneID(ipx); - if (ip6s != null) for (String s: ip6s) if (s.length() > 0) return Domains.chopZoneID(s); - // in case that we don't have any address using the dna (i.e. a fresh peer), then use all locally known addresses - for (InetAddress i: Domains.myPublicIPv4()) return Domains.chopZoneID(i.getHostAddress()); - for (InetAddress i: Domains.myPublicIPv6()) return Domains.chopZoneID(i.getHostAddress()); - - // final chance - return Domains.LOCALHOST; + Set ip6s = getIPv6Entries(); + if (ip6s != null && ip6s.size() > 0) return ip6s.iterator().next(); + return null; } /** @@ -364,29 +360,11 @@ public class Seed implements Cloneable, Comparable, Comparator public final Set getIPs() { Set h = new LinkedHashSet<>(); final String ipx = this.dna.get(Seed.IP); // may contain both, IPv4 or IPv6 - final String ip6 = this.dna.get(Seed.IP6); - Set ip6s = MapTools.string2set(ip6, "|"); - - if (ip6s == null || ip6s.size() == 0) { - if (ipx != null && !ipx.isEmpty()) h.add(Domains.chopZoneID(ipx)); - } else if (ip6s != null && ip6s.size() == 1) { - // We add IPv6 first because then those addresses appear first - // in the LinkedHashSet and are preferred by methods using only the first one. - for (String s: ip6s) if (s.length() > 0) h.add(Domains.chopZoneID(s)); - if (ipx != null && !ipx.isEmpty()) h.add(Domains.chopZoneID(ipx)); - } else { - // if we have more than one IPv6, then chances are high that one of them do not work. - // in that case we prefer the IPv4 - if (ipx != null && !ipx.isEmpty()) h.add(Domains.chopZoneID(ipx)); - if (ip6s != null) for (String s: ip6s) if (s.length() > 0) h.add(Domains.chopZoneID(s)); - } + Set ip6s = getIPv6Entries(); - // in case that we don't have any address using the dna (i.e. a fresh peer), then use all locally known addresses - if (h.size() == 0) { - for (InetAddress i: Domains.myPublicIPv4()) h.add(Domains.chopZoneID(i.getHostAddress())); - for (InetAddress i: Domains.myPublicIPv6()) h.add(Domains.chopZoneID(i.getHostAddress())); - h.add(Domains.LOCALHOST); - } + // put IPs in h in such a way that we believe the mostfront have more chances to get connected + if (ipx != null && !ipx.isEmpty()) h.add(Domains.chopZoneID(ipx)); + h.addAll(ip6s); return h; } @@ -396,8 +374,7 @@ public class Seed implements Cloneable, Comparable, Comparator */ public final int countIPs() { final String ipx = this.dna.get(Seed.IP); // may contain both, IPv4 or IPv6 - final String ip6 = this.dna.get(Seed.IP6); - Set ip6s = MapTools.string2set(ip6, "|"); + Set ip6s = getIPv6Entries(); if (ip6s == null || ip6s.size() == 0) { return (ipx == null || ipx.isEmpty()) ? 0 : 1; @@ -412,10 +389,7 @@ public class Seed implements Cloneable, Comparable, Comparator */ public final boolean removeIP(String ip) { String ipx = Domains.chopZoneID(this.dna.get(Seed.IP)); // may contain both, IPv4 or IPv6 - final String ip6 = this.dna.get(Seed.IP6); - Set ip6s = MapTools.string2set(ip6, "|"); - Iterator i = ip6s.iterator(); - while (i.hasNext()) {String x = i.next(); if (x.indexOf('%') >= 0) i.remove();} + Set ip6s = getIPv6Entries(); if (ip6s == null || ip6s.size() == 0) { if (ipx != null && !ipx.isEmpty() && ipx.equals(ip)) { diff --git a/source/net/yacy/server/serverSwitch.java b/source/net/yacy/server/serverSwitch.java index 272309077..5238cb7a3 100644 --- a/source/net/yacy/server/serverSwitch.java +++ b/source/net/yacy/server/serverSwitch.java @@ -183,12 +183,12 @@ public class serverSwitch { for (InetAddress i : Domains.myPublicIPv6()) { String s = i.getHostAddress(); if (Seed.isProperIP(s)) - h.add(s); + h.add(Domains.chopZoneID(s)); } for (InetAddress i : Domains.myPublicIPv4()) { String s = i.getHostAddress(); if (Seed.isProperIP(s)) - h.add(s); + h.add(Domains.chopZoneID(s)); } return h; }