pull/1/head
Michael Peter Christen 10 years ago
parent d65e3f2b53
commit 167c5a51f0

@ -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<String> 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<InetAddress> 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) {

@ -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<Seed>, Comparator<Seed>
return name.startsWith("_anon");
}
private Set<String> getIPv6Entries() {
String ip6s = this.dna.get(Seed.IP6);
final Set<String> set = Collections.synchronizedSet(new HashSet<String>());
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<br>
*
@ -329,29 +343,11 @@ public class Seed implements Cloneable, Comparable<Seed>, Comparator<Seed>
@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<String> 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<String> ip6s = getIPv6Entries();
if (ip6s != null && ip6s.size() > 0) return ip6s.iterator().next();
return null;
}
/**
@ -364,29 +360,11 @@ public class Seed implements Cloneable, Comparable<Seed>, Comparator<Seed>
public final Set<String> getIPs() {
Set<String> 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<String> 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<String> 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<Seed>, Comparator<Seed>
*/
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<String> ip6s = MapTools.string2set(ip6, "|");
Set<String> 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<Seed>, Comparator<Seed>
*/
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<String> ip6s = MapTools.string2set(ip6, "|");
Iterator<String> i = ip6s.iterator();
while (i.hasNext()) {String x = i.next(); if (x.indexOf('%') >= 0) i.remove();}
Set<String> ip6s = getIPv6Entries();
if (ip6s == null || ip6s.size() == 0) {
if (ipx != null && !ipx.isEmpty() && ipx.equals(ip)) {

@ -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;
}

Loading…
Cancel
Save