From fdc83484fdd9cf6f76f0e308e46468eee6784803 Mon Sep 17 00:00:00 2001 From: dream Date: Sat, 22 May 2010 16:50:39 +0000 Subject: [PATCH] NTCP bind interface Adding support for binding to a specific IP in the NTCP configuration. Uses new config option i2np.ntcp.bindAddress. --- core/c/jbigi/build.sh | 2 +- .../router/transport/ntcp/NTCPTransport.java | 25 ++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/core/c/jbigi/build.sh b/core/c/jbigi/build.sh index 13678a6d5..535e451f5 100755 --- a/core/c/jbigi/build.sh +++ b/core/c/jbigi/build.sh @@ -54,7 +54,7 @@ cp *jbigi???* ../../lib/ echo 'Library copied to lib/' cd ../.. -I2P=~/i2p +I2P=~i2p if [ ! -f $I2P/lib/i2p.jar ] then echo "I2P installation not found in $I2P - correct \$I2P definition in script to run speed test" diff --git a/router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java b/router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java index fea56423b..9d1c3a432 100644 --- a/router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java +++ b/router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java @@ -2,6 +2,8 @@ package net.i2p.router.transport.ntcp; import java.io.IOException; import java.net.InetSocketAddress; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.text.DecimalFormat; @@ -54,6 +56,9 @@ public class NTCPTransport extends TransportImpl { */ private final List _establishing; + /** this is rarely if ever used, default is to bind to wildcard address */ + public static final String PROP_BIND_INTERFACE = "i2np.ntcp.bindInterface"; + private final NTCPSendFinisher _finisher; private long _lastBadSkew; private static final long[] RATES = { 10*60*1000 }; @@ -486,15 +491,29 @@ public class NTCPTransport extends TransportImpl { /** call from synchronized method */ private RouterAddress bindAddress() { if (_myAddress != null) { + InetAddress bindToAddr = null; + String bindTo = _context.getProperty(PROP_BIND_INTERFACE); + if (bindTo != null) { + try { + bindToAddr = InetAddress.getByName(bindTo); + } catch (UnknownHostException uhe) { + _log.log(Log.CRIT, "Invalid SSU bind interface specified [" + bindTo + "]", uhe); + // this can be implemented later, just updates some stats + // see udp/UDPTransport.java + //setReachabilityStatus(CommSystemFacade.STATUS_HOSED); + return null; + } + } + try { ServerSocketChannel chan = ServerSocketChannel.open(); chan.configureBlocking(false); InetSocketAddress addr = null; - //if (bindAllInterfaces()) + if(bindToAddr==null) addr = new InetSocketAddress(_myAddress.getPort()); - //else - // addr = new InetSocketAddress(_myAddress.getAddress(), _myAddress.getPort()); + else + addr = new InetSocketAddress(bindToAddr, _myAddress.getPort()); chan.socket().bind(addr); if (_log.shouldLog(Log.INFO)) _log.info("Listening on " + addr);