more ipv6 fixes

This commit is contained in:
Michael Peter Christen 2014-10-07 18:53:23 +02:00
parent 5cef88a315
commit 460858fb22
2 changed files with 29 additions and 29 deletions

View File

@ -78,7 +78,7 @@ public class Domains {
private static final Set<String> ccSLD_TLD = new HashSet<String>();
private static final String PRESENT = "";
private static final String LOCALHOST_IPv4_PATTERN = "(127\\..*)";
private static final String LOCALHOST_IPv6_PATTERN = "(\\[?fe80\\:\\:(/.*|\\z))|(\\[?0\\:0\\:0\\:0\\:0\\:0\\:0\\:1.*)|(\\[?\\:\\:1(/.*|\\z))";
private static final String LOCALHOST_IPv6_PATTERN = "((\\[?fe80\\:.*)|(\\[?0\\:0\\:0\\:0\\:0\\:0\\:0\\:1.*)|(\\[?\\:\\:1))(/.*|%.*|\\z)";
private static final String INTRANET_IPv4_PATTERN = "(10\\..*)|(172\\.(1[6-9]|2[0-9]|3[0-1])\\..*)|(169\\.254\\..*)|(192\\.168\\..*)";
private static final String INTRANET_IPv6_PATTERN = "(\\[?(fc|fd).*\\:.*)";
private static final Pattern LOCALHOST_PATTERNS = Pattern.compile("(localhost)|" + LOCALHOST_IPv4_PATTERN + "|" + LOCALHOST_IPv6_PATTERN, Pattern.CASE_INSENSITIVE);
@ -180,7 +180,7 @@ public class Domains {
if (isAnyLocalAddress || isLinkLocalAddress || isLoopbackAddress || isSiteLocalAddress) {
ConcurrentLog.info("Domain Init", "local host address: " + hostaddress + " (local)");
localHostAddresses.add(a);
if (hostname != null) {localHostNames.add(hostname); localHostNames.add(hostaddress);}
if (hostname != null) {localHostNames.add(chopZoneID(hostname)); localHostNames.add(chopZoneID(hostaddress));}
} else {
ConcurrentLog.info("Domain Init", "local host address: " + hostaddress + " (public)");
if (a instanceof Inet4Address) {
@ -1107,15 +1107,20 @@ public class Domains {
return (isLocal(host, hostaddress)) ? TLD_Local_ID : TLD_Generic_ID;
}
public static String chopZoneID(String ip) {
int i = ip.indexOf('%');
return i < 0 ? ip : ip.substring(0, i);
}
/**
* check the host ip string against localhost names
* @param host
* @return true if the host from the string is the localhost
*/
public static boolean isLocalhost(final String host) {
return host == null || // filesystems do not have host names
LOCALHOST_PATTERNS.matcher(host).matches() ||
localHostNames.contains(host);
public static boolean isLocalhost(String host) {
if (host == null) return true; // filesystems do not have host names
host = chopZoneID(host);
return LOCALHOST_PATTERNS.matcher(host).matches() || localHostNames.contains(host);
}
/**

View File

@ -332,22 +332,22 @@ public class Seed implements Cloneable, Comparable<Seed>, Comparator<Seed>
Set<String> ip6s = MapTools.string2set(ip6, "|");
if (ip6s == null || ip6s.size() == 0) {
if (ipx != null && !ipx.isEmpty()) return chopZoneID(ipx);
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 chopZoneID(s);
if (ipx != null && !ipx.isEmpty()) return chopZoneID(ipx);
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 chopZoneID(ipx);
if (ip6s != null) for (String s: ip6s) if (s.length() > 0) return chopZoneID(s);
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 chopZoneID(i.getHostAddress());
for (InetAddress i: Domains.myPublicIPv6()) return chopZoneID(i.getHostAddress());
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;
@ -367,23 +367,23 @@ public class Seed implements Cloneable, Comparable<Seed>, Comparator<Seed>
Set<String> ip6s = MapTools.string2set(ip6, "|");
if (ip6s == null || ip6s.size() == 0) {
if (ipx != null && !ipx.isEmpty()) h.add(chopZoneID(ipx));
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(chopZoneID(s));
if (ipx != null && !ipx.isEmpty()) h.add(chopZoneID(ipx));
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(chopZoneID(ipx));
if (ip6s != null) for (String s: ip6s) if (s.length() > 0) h.add(chopZoneID(s));
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));
}
// 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(chopZoneID(i.getHostAddress()));
for (InetAddress i: Domains.myPublicIPv6()) h.add(chopZoneID(i.getHostAddress()));
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);
}
return h;
@ -410,7 +410,7 @@ public class Seed implements Cloneable, Comparable<Seed>, Comparator<Seed>
* @return true if the IP was in the seed and had been removed. If the peer did not change, this returns false.
*/
public final boolean removeIP(String ip) {
String ipx = chopZoneID(this.dna.get(Seed.IP)); // may contain both, IPv4 or IPv6
String ipx = Domains.chopZoneID(this.dna.get(Seed.IP)); // may contain both, IPv4 or IPv6
final String ip6 = this.dna.get(Seed.IP6);
Set<String> ip6s = MapTools.string2set(ip6, "|");
Iterator<String> i = ip6s.iterator();
@ -430,18 +430,13 @@ public class Seed implements Cloneable, Comparable<Seed>, Comparator<Seed>
}
if (ipx != null && !ipx.isEmpty() && ipx.equals(ip)) {
ipx = ip6s.iterator().next();
this.dna.put(Seed.IP, chopZoneID(ipx));
this.dna.put(Seed.IP, Domains.chopZoneID(ipx));
ip6s.remove(ipx);
this.dna.put(Seed.IP6, MapTools.set2string(ip6s, "|", false));
return true;
}
return false;
}
private String chopZoneID(String ip) {
int i = ip.indexOf('%');
return i < 0 ? ip : ip.substring(0, i);
}
/**
* clash tests if any of the given ips are also contained in the Seeds ip set
@ -564,7 +559,7 @@ public class Seed implements Cloneable, Comparable<Seed>, Comparator<Seed>
* @param ip
*/
public final void setIP(String ip) {
ip = chopZoneID(ip);
ip = Domains.chopZoneID(ip);
if (!isProperIP(ip)) return;
String oldIP = this.dna.get(Seed.IP);
String oldIP6 = this.dna.get(Seed.IP6);
@ -599,7 +594,7 @@ public class Seed implements Cloneable, Comparable<Seed>, Comparator<Seed>
this.dna.put(Seed.IP6, MapTools.set2string(ipv6, "|", false));
}
} else {
this.dna.put(Seed.IP, chopZoneID(ipv4.iterator().next()));
this.dna.put(Seed.IP, Domains.chopZoneID(ipv4.iterator().next()));
this.dna.put(Seed.IP6, MapTools.set2string(ipv6, "|", false));
}
}