diff --git a/source/net/yacy/http/Jetty8HttpServerImpl.java b/source/net/yacy/http/Jetty8HttpServerImpl.java index 673a52ac2..46dad83a4 100644 --- a/source/net/yacy/http/Jetty8HttpServerImpl.java +++ b/source/net/yacy/http/Jetty8HttpServerImpl.java @@ -24,9 +24,13 @@ package net.yacy.http; +import java.net.Inet4Address; +import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.NetworkInterface; import java.net.SocketException; import java.util.EnumSet; +import java.util.Enumeration; import javax.servlet.DispatcherType; import net.yacy.cora.federate.solr.SolrServlet; import net.yacy.cora.federate.solr.SolrServlet.Servlet404; @@ -204,8 +208,54 @@ public class Jetty8HttpServerImpl implements YaCyHttpServer { } @Override - public InetSocketAddress generateSocketAddress(String port) throws SocketException { - return null; // TODO: + public InetSocketAddress generateSocketAddress(String extendedPortString) throws SocketException { + // parsing the port configuration + String bindIP = null; + int bindPort; + + int pos = extendedPortString.indexOf(':'); + if (pos != -1) { + bindIP = extendedPortString.substring(0,pos).trim(); + extendedPortString = extendedPortString.substring(pos+1); + + if (bindIP.length() > 0 && bindIP.charAt(0) == '#') { + final String interfaceName = bindIP.substring(1); + String hostName = null; + if (ConcurrentLog.isFine("SERVER")) ConcurrentLog.fine("SERVER","Trying to determine IP address of interface '" + interfaceName + "'."); + + final Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + if (interfaces != null) { + while (interfaces.hasMoreElements()) { + final NetworkInterface interf = interfaces.nextElement(); + if (interf.getName().equalsIgnoreCase(interfaceName)) { + final Enumeration addresses = interf.getInetAddresses(); + if (addresses != null) { + while (addresses.hasMoreElements()) { + final InetAddress address = addresses.nextElement(); + if (address instanceof Inet4Address) { + hostName = address.getHostAddress(); + break; + } + } + } + } + } + } + if (hostName == null) { + ConcurrentLog.warn("SERVER", "Unable to find interface with name '" + interfaceName + "'. Binding server to all interfaces"); + bindIP = null; + } else { + ConcurrentLog.info("SERVER", "Binding server to interface '" + interfaceName + "' with IP '" + hostName + "'."); + bindIP = hostName; + } + } + } + bindPort = Integer.parseInt(extendedPortString); + + return (bindIP == null) + ? new InetSocketAddress(bindPort) + : new InetSocketAddress(bindIP, bindPort); + } @Override diff --git a/source/net/yacy/server/serverCore.java b/source/net/yacy/server/serverCore.java index d5344ed80..0342cea38 100644 --- a/source/net/yacy/server/serverCore.java +++ b/source/net/yacy/server/serverCore.java @@ -250,6 +250,7 @@ public final class serverCore extends AbstractBusyThread implements BusyThread, return Integer.parseInt(extendedPortString); } + @Override public InetSocketAddress generateSocketAddress(String extendedPortString) throws SocketException { // parsing the port configuration @@ -971,6 +972,7 @@ public final class serverCore extends AbstractBusyThread implements BusyThread, if ((currentThread instanceof serverCore.Session) && ((serverCore.Session)currentThread).isStopped()) throw new InterruptedException(); } + @Override public void reconnect(final int delay) { final Thread restart = new Restarter(delay); restart.start();