diff --git a/htroot/SettingsAck_p.java b/htroot/SettingsAck_p.java index a916a2a2e..42fbea48e 100644 --- a/htroot/SettingsAck_p.java +++ b/htroot/SettingsAck_p.java @@ -207,10 +207,6 @@ public class SettingsAck_p { } else if (staticIP.startsWith("https://")) { if (staticIP.length() > 8) { staticIP = staticIP.substring(8); } else { staticIP = ""; } } - // TODO IPv6 support! - if (staticIP.indexOf(':',0) > 0) { - staticIP = staticIP.substring(0, staticIP.indexOf(':',0)); - } if (staticIP.isEmpty()) { serverCore.useStaticIP = false; } else { diff --git a/source/net/yacy/cora/protocol/Domains.java b/source/net/yacy/cora/protocol/Domains.java index febb3ca1a..2f9556e12 100644 --- a/source/net/yacy/cora/protocol/Domains.java +++ b/source/net/yacy/cora/protocol/Domains.java @@ -638,7 +638,7 @@ public class Domains { host = host.toLowerCase().trim(); // try to simply parse the address - InetAddress ip = parseInetAddress(host); + InetAddress ip = InetAddress.getByName(host); if (ip != null) return ip; // trying to resolve host by doing a name cache lookup @@ -716,8 +716,14 @@ public class Domains { if (host0 == null || host0.isEmpty()) return null; final String host = host0.toLowerCase().trim(); // try to simply parse the address - InetAddress ip = parseInetAddress(host); - if (ip != null) return ip; + InetAddress ip; + try { + ip = InetAddress.getByName(host); + return ip; + } catch (UnknownHostException e1) { + // we expected that InetAddress.getByName may fail if this is not a raw address. + // We silently ignore this and go on. + } /* if (MemoryControl.shortStatus()) { @@ -834,30 +840,6 @@ public class Domains { NAME_CACHE_MISS.clear(); } catch (final IOException e) {} } - - - public static final InetAddress parseInetAddress(String ip) { - if (ip == null || ip.length() < 8) return null; - ip = ip.trim(); - if (ip.charAt(0) == '[' && ip.charAt(ip.length() - 1) == ']') ip = ip.substring(1, ip.length() - 1); - if ("localhost".equals(ip)) ip = "127.0.0.1"; // normalize to IPv4 here since that is the way to calculate the InetAddress - final String[] ips = CommonPattern.DOT.split(ip); - if (ips.length != 4) return null; // TODO: parse IPv6 addresses - final byte[] ipb = new byte[4]; - try { - ipb[0] = (byte) Integer.parseInt(ips[0]); - ipb[1] = (byte) Integer.parseInt(ips[1]); - ipb[2] = (byte) Integer.parseInt(ips[2]); - ipb[3] = (byte) Integer.parseInt(ips[3]); - } catch (final NumberFormatException e) { - return null; - } - try { - return InetAddress.getByAddress(ipb); - } catch (final UnknownHostException e) { - return null; - } - } /** * Returns the number of entries in the nameCacheHit map diff --git a/source/net/yacy/peers/Seed.java b/source/net/yacy/peers/Seed.java index 2061e1ebd..9a75d2197 100644 --- a/source/net/yacy/peers/Seed.java +++ b/source/net/yacy/peers/Seed.java @@ -303,12 +303,17 @@ public class Seed implements Cloneable, Comparable, Comparator if ( ipport == null ) { return; } - final int p = ipport.indexOf(':'); + final int p = ipport.lastIndexOf(':'); if ( p < 0 ) { this.alternativeIP = ipport; } else { this.alternativeIP = ipport.substring(0, p); } + + if (this.alternativeIP.charAt(0) == '[' && this.alternativeIP.charAt(this.alternativeIP.length() - 1) == ']') { + // IPv6 patch + this.alternativeIP = this.alternativeIP.substring(1, this.alternativeIP.length() - 1); + } } /** @@ -574,19 +579,26 @@ public class Seed implements Cloneable, Comparable, Comparator */ public final String getPublicAddress() { String ip = getIP(); - if ( ip == null || ip.length() < 8 || ip.length() > 60 ) { - ip = Domains.LOCALHOST; - } + if (ip == null) ip = Domains.LOCALHOST; // that should not happen final String port = this.dna.get(Seed.PORT); if ( port == null || port.length() < 2 || port.length() > 5 ) { return null; } - final StringBuilder sb = new StringBuilder(ip.length() + port.length() + 1); - sb.append(ip); - sb.append(':'); - sb.append(port); + final StringBuilder sb = new StringBuilder(ip.length() + port.length() + 3); + if (ip.indexOf(':') >= 0) { + // IPv6 Address!, see: http://en.wikipedia.org/wiki/IPv6_address#Literal_IPv6_addresses_in_network_resource_identifiers + sb.append('['); + sb.append(ip); + sb.append(']'); + sb.append(':'); + sb.append(port); + } else { + sb.append(ip); + sb.append(':'); + sb.append(port); + } return sb.toString(); } @@ -604,11 +616,24 @@ public class Seed implements Cloneable, Comparable, Comparator } final String port = this.dna.get(Seed.PORT); - if ( (port == null) || (port.length() < 2) ) { + if ( port == null || port.length() < 2 || port.length() > 5 ) { return null; } - return this.alternativeIP + ":" + port; + final StringBuilder sb = new StringBuilder(this.alternativeIP.length() + port.length() + 3); + if (this.alternativeIP.indexOf(':') >= 0) { + // IPv6 Address!, see: http://en.wikipedia.org/wiki/IPv6_address#Literal_IPv6_addresses_in_network_resource_identifiers + sb.append('['); + sb.append(this.alternativeIP); + sb.append(']'); + sb.append(':'); + sb.append(port); + } else { + sb.append(this.alternativeIP); + sb.append(':'); + sb.append(port); + } + return sb.toString(); } /** @@ -618,7 +643,7 @@ public class Seed implements Cloneable, Comparable, Comparator return Domains.dnsResolve(getIP()); } - /** @return the portnumber of this seed or -1 if not present */ + /** @return the port number of this seed or -1 if not present */ public final int getPort() { final String port = this.dna.get(Seed.PORT); if ( port == null ) { diff --git a/source/net/yacy/peers/SeedDB.java b/source/net/yacy/peers/SeedDB.java index 1bcaa1333..96b3b4f5f 100644 --- a/source/net/yacy/peers/SeedDB.java +++ b/source/net/yacy/peers/SeedDB.java @@ -717,7 +717,7 @@ public final class SeedDB implements AlternativeDomainNames { try { pw = new PrintWriter(new BufferedWriter(new FileWriter(seedFile))); - List seedlist = getSeedlist(Integer.MAX_VALUE, addMySeed); + List seedlist = getSeedlist(Integer.MAX_VALUE, addMySeed, false); String line; for (Seed seed: seedlist) { line = seed.genSeedStr(null); @@ -731,7 +731,7 @@ public final class SeedDB implements AlternativeDomainNames { return v; } - public ArrayList getSeedlist(int maxcount, boolean addMySeed) { + public ArrayList getSeedlist(int maxcount, boolean addMySeed, boolean nodeonly) { final ArrayList v = new ArrayList(this.seedActiveDB.size() + 1000); // store own peer seed @@ -742,7 +742,7 @@ public final class SeedDB implements AlternativeDomainNames { Iterator se = this.seedsConnected(true, false, null, (float) 0.0); while (se.hasNext() && v.size() < maxcount) { ys = se.next(); - if (ys != null) v.add(ys); + if (ys != null && (!nodeonly || ys.getFlagRootNode())) v.add(ys); } // store some of the not-so-old passive peer seeds (limit: 1 day) @@ -750,7 +750,7 @@ public final class SeedDB implements AlternativeDomainNames { final long timeout = System.currentTimeMillis() - (1000L * 60L * 60L * 24L); while (se.hasNext() && v.size() < maxcount) { ys = se.next(); - if (ys != null && ys.getLastSeenUTC() >= timeout) v.add(ys); + if (ys != null && ys.getLastSeenUTC() >= timeout && (!nodeonly || ys.getFlagRootNode())) v.add(ys); } final StringBuilder encoded = new StringBuilder(1024);