- avoid unnecessary host name lookups for localhost

- avoid unnecessary reverse domain name lookups for remote access

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@6481 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 15 years ago
parent 141712ec95
commit 013f337d3f

@ -31,6 +31,7 @@
import java.io.File;
import java.util.regex.Pattern;
import net.yacy.kelondro.util.Domains;
import net.yacy.kelondro.workflow.InstantBusyThread;
import de.anomic.data.translator;
@ -127,7 +128,7 @@ public class ConfigBasic {
final int idx = host.indexOf(":");
if (idx != -1) host = host.substring(0,idx);
} else {
host = serverSwitch.myPublicLocalIP().getHostAddress();
host = Domains.myPublicLocalIP().getHostAddress();
}
prop.put("reconnect", "1");

@ -33,6 +33,7 @@ import java.util.Date;
import net.yacy.kelondro.io.ByteCountInputStream;
import net.yacy.kelondro.io.ByteCountOutputStream;
import net.yacy.kelondro.util.DateFormatter;
import net.yacy.kelondro.util.Domains;
import net.yacy.kelondro.util.Formatter;
import net.yacy.kelondro.util.MemoryControl;
import net.yacy.kelondro.workflow.WorkflowProcessor;
@ -161,7 +162,7 @@ public class Status {
} else {
prop.put("extPortFormat", "0");
}
InetAddress hostIP = serverSwitch.myPublicLocalIP();
InetAddress hostIP = Domains.myPublicLocalIP();
prop.put("host", hostIP!=null ? hostIP.getHostAddress() : "Unkown IP");
// ssl support

@ -600,7 +600,6 @@ public final class HTTPDFileHandler {
if (argsString != null) {
env.put("QUERY_STRING", argsString);
}
env.put("REMOTE_HOST", InetAddress.getByName(clientIP).getHostName());
env.put("REMOTE_ADDR", clientIP);
// env.put("AUTH_TYPE", ""); // TODO: implement
// env.put("REMOTE_USER", ""); // TODO: implement

@ -1147,10 +1147,10 @@ public final class HTTPDemon implements serverHandler, Cloneable {
// check if ip is local ip address
final InetAddress hostAddress = Domains.dnsResolve(clientIP);
if (hostAddress == null) {
tp.put("host", serverSwitch.myPublicLocalIP().getHostAddress());
tp.put("host", Domains.myPublicLocalIP().getHostAddress());
tp.put("port", serverCore.getPortNr(switchboard.getConfig("port", "8080")));
} else if (hostAddress.isSiteLocalAddress() || hostAddress.isLoopbackAddress()) {
tp.put("host", serverSwitch.myPublicLocalIP().getHostAddress());
tp.put("host", Domains.myPublicLocalIP().getHostAddress());
tp.put("port", serverCore.getPortNr(switchboard.getConfig("port", "8080")));
} else {
tp.put("host", switchboard.myPublicIP());
@ -1445,9 +1445,8 @@ public final class HTTPDemon implements serverHandler, Cloneable {
if (clientAddress.isAnyLocalAddress() || clientAddress.isLoopbackAddress()) return true;
final InetAddress[] localAddress = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
for (int i=0; i<localAddress.length; i++) {
if (localAddress[i].equals(clientAddress)) {
for (int i = 0; i < Domains.localHostAddresses.length; i++) {
if (Domains.localHostAddresses[i].equals(clientAddress)) {
isThisHostIP = true;
break;
}
@ -1463,9 +1462,8 @@ public final class HTTPDemon implements serverHandler, Cloneable {
try {
if (clientAddress.isAnyLocalAddress() || clientAddress.isLoopbackAddress()) return true;
final InetAddress[] localAddress = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
for (int i=0; i<localAddress.length; i++) {
if (localAddress[i].equals(clientAddress)) {
for (int i = 0; i < Domains.localHostAddresses.length; i++) {
if (Domains.localHostAddresses[i].equals(clientAddress)) {
isThisHostIP = true;
break;
}

@ -27,7 +27,6 @@
package de.anomic.net;
import java.io.IOException;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
@ -37,6 +36,7 @@ import net.sbbi.upnp.devices.UPNPRootDevice;
import net.sbbi.upnp.impls.InternetGatewayDevice;
import net.sbbi.upnp.messages.UPNPResponseException;
import net.yacy.kelondro.logging.Log;
import net.yacy.kelondro.util.Domains;
import de.anomic.search.Switchboard;
import de.anomic.search.SwitchboardConstants;
@ -72,7 +72,7 @@ public class UPnP {
boolean init = true;
try {
if (IGDs == null) IGDs = InternetGatewayDevice.getDevices(discoveryTimeout);
localHostIP = InetAddress.getLocalHost().getHostAddress();
localHostIP = Domains.localHostAddress.getHostAddress();
} catch (IOException e) {
init = false;
}

@ -67,8 +67,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.yacy.kelondro.logging.Log;
import de.anomic.server.serverSwitch;
import net.yacy.kelondro.util.Domains;
public class ftpc {
@ -2123,7 +2122,7 @@ public class ftpc {
// save ip address in high byte order
// byte[] Bytes = LocalIp.getAddress();
final byte[] Bytes = serverSwitch.myPublicLocalIP().getHostAddress().getBytes();
final byte[] Bytes = Domains.myPublicLocalIP().getHostAddress().getBytes();
// bytes greater than 127 should not be printed as negative
final short[] Shorts = new short[4];

@ -360,7 +360,7 @@ public class TextSnippet {
sb.toIndexer(entry);
// read resource body (if it is there)
final byte []resourceArray = entry.getContent();
final byte[] resourceArray = entry.getContent();
if (resourceArray != null) {
resContent = new ByteArrayInputStream(resourceArray);
resContentLength = resourceArray.length;

@ -60,6 +60,7 @@ import javax.net.ssl.SSLSocketFactory;
import net.yacy.kelondro.logging.Log;
import net.yacy.kelondro.util.ByteBuffer;
import net.yacy.kelondro.util.Domains;
import net.yacy.kelondro.workflow.AbstractBusyThread;
import net.yacy.kelondro.workflow.BusyThread;
@ -299,7 +300,7 @@ public final class serverCore extends AbstractBusyThread implements BusyThread {
}
public void open() {
this.log.logConfig("* server started on " + serverSwitch.myPublicLocalIP() + ":" + this.extendedPort);
this.log.logConfig("* server started on " + Domains.myPublicLocalIP() + ":" + this.extendedPort);
}
public void freemem() {
@ -1031,7 +1032,7 @@ public final class serverCore extends AbstractBusyThread implements BusyThread {
try {
sslsock=(SSLSocket)this.sslSocketFactory.createSocket(
sock,
sock.getInetAddress().getHostName(),
sock.getInetAddress().getHostAddress(),
sock.getPort(),
true);

@ -24,7 +24,6 @@ package de.anomic.server;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
@ -33,6 +32,7 @@ import java.util.SortedMap;
import java.util.TreeMap;
import net.yacy.kelondro.logging.Log;
import net.yacy.kelondro.util.Domains;
import net.yacy.kelondro.util.FileUtils;
import net.yacy.kelondro.workflow.BusyThread;
import net.yacy.kelondro.workflow.WorkflowThread;
@ -158,67 +158,10 @@ public class serverSwitch {
}
// otherwise we return the real IP address of this host
final InetAddress pLIP = myPublicLocalIP();
final InetAddress pLIP = Domains.myPublicLocalIP();
if (pLIP != null) return pLIP.getHostAddress();
return null;
}
public static InetAddress myPublicLocalIP() {
try {
String hostName;
try {
hostName = InetAddress.getLocalHost().getHostName();
} catch (final java.net.UnknownHostException e) {
hostName = "localhost"; // hopin' nothing serious happened only the hostname changed while running yacy
System.err.println("ERROR: (internal) " + e.getMessage());
}
// list all addresses
final InetAddress[] ia = InetAddress.getAllByName(hostName);
// for (int i = 0; i < ia.length; i++) System.out.println("IP: " +
// ia[i].getHostAddress()); // DEBUG
if (ia.length == 0) {
try {
return InetAddress.getLocalHost();
} catch (final UnknownHostException e) {
try {
return InetAddress.getByName("127.0.0.1");
} catch (final UnknownHostException ee) {
return null;
}
}
}
if (ia.length == 1) {
// only one network connection available
return ia[0];
}
// we have more addresses, find an address that is not local
int b0, b1;
for (int i = 0; i < ia.length; i++) {
b0 = 0Xff & ia[i].getAddress()[0];
b1 = 0Xff & ia[i].getAddress()[1];
if ((b0 != 10) && // class A reserved
(b0 != 127) && // loopback
((b0 != 172) || (b1 < 16) || (b1 > 31)) && // class B reserved
((b0 != 192) || (b1 != 168)) && // class C reserved
(ia[i].getHostAddress().indexOf(":") < 0))
return ia[i];
}
// there is only a local address, we filter out the possibly
// returned loopback address 127.0.0.1
for (int i = 0; i < ia.length; i++) {
if (((0Xff & ia[i].getAddress()[0]) != 127) && (ia[i].getHostAddress().indexOf(":") < 0)) return ia[i];
}
// if all fails, give back whatever we have
for (int i = 0; i < ia.length; i++) {
if (ia[i].getHostAddress().indexOf(":") < 0) return ia[i];
}
return ia[0];
} catch (final java.net.UnknownHostException e) {
System.err.println("ERROR: (internal) " + e.getMessage());
Log.logException(e);
return null;
}
}
// a logger for this switchboard
public void setLog(final Log log) {

@ -40,7 +40,7 @@ public final class CachedFileWriter extends AbstractWriter implements Writer {
this.name = file.getName();
this.file = file;
this.RAFile = new RandomAccessFile(this.file, "rw");
this.cache = new byte[8192];
this.cache = new byte[32768];
this.cachestart = 0;
this.cachelen = 0;
}

@ -518,13 +518,60 @@ public class Domains {
if (nameCacheMiss.size() > maxNameCacheMissSize) nameCacheMiss.clear();
}
private static InetAddress[] localAddresses = null;
public static InetAddress localHostAddress;
public static String localHostName;
public static InetAddress[] localHostAddresses;
static {
try {
localAddresses = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
} catch (final UnknownHostException e) {
localAddresses = new InetAddress[0];
localHostAddress = InetAddress.getByName("127.0.0.1");
} catch (UnknownHostException e1) {}
try {
localHostAddress = InetAddress.getLocalHost();
localHostName = localHostAddress.getHostName();
} catch (UnknownHostException e) {
localHostName = "localhost";
}
try {
localHostAddresses = InetAddress.getAllByName(localHostName);
} catch (UnknownHostException e) {
localHostAddresses = new InetAddress[0];
}
}
public static InetAddress myPublicLocalIP() {
// list all addresses
final InetAddress[] ia = Domains.localHostAddresses;
// for (int i = 0; i < ia.length; i++) System.out.println("IP: " +
// ia[i].getHostAddress()); // DEBUG
if (ia.length == 0) {
return Domains.localHostAddress;
}
if (ia.length == 1) {
// only one network connection available
return ia[0];
}
// we have more addresses, find an address that is not local
int b0, b1;
for (int i = 0; i < ia.length; i++) {
b0 = 0Xff & ia[i].getAddress()[0];
b1 = 0Xff & ia[i].getAddress()[1];
if ((b0 != 10) && // class A reserved
(b0 != 127) && // loopback
((b0 != 172) || (b1 < 16) || (b1 > 31)) && // class B reserved
((b0 != 192) || (b1 != 168)) && // class C reserved
(ia[i].getHostAddress().indexOf(":") < 0))
return ia[i];
}
// there is only a local address, we filter out the possibly
// returned loopback address 127.0.0.1
for (int i = 0; i < ia.length; i++) {
if (((0Xff & ia[i].getAddress()[0]) != 127) && (ia[i].getHostAddress().indexOf(":") < 0)) return ia[i];
}
// if all fails, give back whatever we have
for (int i = 0; i < ia.length; i++) {
if (ia[i].getHostAddress().indexOf(":") < 0) return ia[i];
}
return ia[0];
}
public static int getDomainID(final String host) {
@ -569,8 +616,8 @@ public class Domains {
// finally check if there are other local IP adresses that are not in
// the standard IP range
for (int i = 0; i < localAddresses.length; i++) {
if (localAddresses[i].equals(clientAddress)) return true;
for (int i = 0; i < localHostAddresses.length; i++) {
if (localHostAddresses[i].equals(clientAddress)) return true;
}
// the address must be a global address

@ -26,6 +26,7 @@
package net.yacy.kelondro.util;
import java.util.ConcurrentModificationException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
@ -116,15 +117,23 @@ public class SortStack<E> {
}
public stackElement remove(final int hashcode) {
final Iterator<Map.Entry<Long, E>> i = this.onstack.entrySet().iterator();
Map.Entry<Long, E> entry;
stackElement se;
while (i.hasNext()) {
entry = i.next();
if (entry.getValue().hashCode() == hashcode) {
se = new stackElement(entry.getValue(), entry.getKey());
this.onstack.remove(se.weight);
return se;
int retry = 3;
retryloop : while (retry-- > 0) {
try {
final Iterator<Map.Entry<Long, E>> i = this.onstack.entrySet().iterator();
while (i.hasNext()) {
entry = i.next();
if (entry.getValue().hashCode() == hashcode) {
se = new stackElement(entry.getValue(), entry.getKey());
this.onstack.remove(se.weight);
return se;
}
}
break retryloop;
} catch (ConcurrentModificationException e) {
continue retryloop;
}
}
return null;

Loading…
Cancel
Save