NTCP bind interface

Adding support for binding to a specific IP in the NTCP configuration. Uses new config option i2np.ntcp.bindAddress.
This commit is contained in:
dream
2010-05-22 16:50:39 +00:00
parent 6786817fff
commit fdc83484fd
2 changed files with 23 additions and 4 deletions

View File

@ -54,7 +54,7 @@ cp *jbigi???* ../../lib/
echo 'Library copied to lib/' echo 'Library copied to lib/'
cd ../.. cd ../..
I2P=~/i2p I2P=~i2p
if [ ! -f $I2P/lib/i2p.jar ] if [ ! -f $I2P/lib/i2p.jar ]
then then
echo "I2P installation not found in $I2P - correct \$I2P definition in script to run speed test" echo "I2P installation not found in $I2P - correct \$I2P definition in script to run speed test"

View File

@ -2,6 +2,8 @@ package net.i2p.router.transport.ntcp;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.channels.ServerSocketChannel; import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel; import java.nio.channels.SocketChannel;
import java.text.DecimalFormat; import java.text.DecimalFormat;
@ -54,6 +56,9 @@ public class NTCPTransport extends TransportImpl {
*/ */
private final List<NTCPConnection> _establishing; private final List<NTCPConnection> _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 final NTCPSendFinisher _finisher;
private long _lastBadSkew; private long _lastBadSkew;
private static final long[] RATES = { 10*60*1000 }; private static final long[] RATES = { 10*60*1000 };
@ -486,15 +491,29 @@ public class NTCPTransport extends TransportImpl {
/** call from synchronized method */ /** call from synchronized method */
private RouterAddress bindAddress() { private RouterAddress bindAddress() {
if (_myAddress != null) { 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 { try {
ServerSocketChannel chan = ServerSocketChannel.open(); ServerSocketChannel chan = ServerSocketChannel.open();
chan.configureBlocking(false); chan.configureBlocking(false);
InetSocketAddress addr = null; InetSocketAddress addr = null;
//if (bindAllInterfaces()) if(bindToAddr==null)
addr = new InetSocketAddress(_myAddress.getPort()); addr = new InetSocketAddress(_myAddress.getPort());
//else else
// addr = new InetSocketAddress(_myAddress.getAddress(), _myAddress.getPort()); addr = new InetSocketAddress(bindToAddr, _myAddress.getPort());
chan.socket().bind(addr); chan.socket().bind(addr);
if (_log.shouldLog(Log.INFO)) if (_log.shouldLog(Log.INFO))
_log.info("Listening on " + addr); _log.info("Listening on " + addr);