I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit 04544ac9 authored by zzz's avatar zzz
Browse files

* Transports: Bind only to a single interface if specified

                as the host address and it's available (ticket #591)
parent 669bcbd1
No related branches found
No related tags found
No related merge requests found
...@@ -13,6 +13,7 @@ import java.util.Comparator; ...@@ -13,6 +13,7 @@ import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
...@@ -30,6 +31,7 @@ import net.i2p.router.transport.CommSystemFacadeImpl; ...@@ -30,6 +31,7 @@ import net.i2p.router.transport.CommSystemFacadeImpl;
import net.i2p.router.transport.Transport; import net.i2p.router.transport.Transport;
import net.i2p.router.transport.TransportBid; import net.i2p.router.transport.TransportBid;
import net.i2p.router.transport.TransportImpl; import net.i2p.router.transport.TransportImpl;
import net.i2p.util.Addresses;
import net.i2p.util.ConcurrentHashSet; import net.i2p.util.ConcurrentHashSet;
import net.i2p.util.Log; import net.i2p.util.Log;
import net.i2p.util.Translate; import net.i2p.util.Translate;
...@@ -515,6 +517,23 @@ public class NTCPTransport extends TransportImpl { ...@@ -515,6 +517,23 @@ public class NTCPTransport extends TransportImpl {
if (_myAddress != null) { if (_myAddress != null) {
InetAddress bindToAddr = null; InetAddress bindToAddr = null;
String bindTo = _context.getProperty(PROP_BIND_INTERFACE); String bindTo = _context.getProperty(PROP_BIND_INTERFACE);
if (bindTo == null) {
// If we are configured with a fixed IP address,
// AND it's one of our local interfaces,
// bind only to that.
boolean isFixed = _context.getProperty(CommSystemFacadeImpl.PROP_I2NP_NTCP_AUTO_IP, "true")
.toLowerCase(Locale.US).equals("false");
String fixedHost = _context.getProperty(CommSystemFacadeImpl.PROP_I2NP_NTCP_HOSTNAME);
if (isFixed && fixedHost != null) {
try {
String testAddr = InetAddress.getByName(fixedHost).getHostAddress();
if (Addresses.getAddresses().contains(testAddr))
bindTo = testAddr;
} catch (UnknownHostException uhe) {}
}
}
if (bindTo != null) { if (bindTo != null) {
try { try {
bindToAddr = InetAddress.getByName(bindTo); bindToAddr = InetAddress.getByName(bindTo);
...@@ -532,10 +551,13 @@ public class NTCPTransport extends TransportImpl { ...@@ -532,10 +551,13 @@ public class NTCPTransport extends TransportImpl {
chan.configureBlocking(false); chan.configureBlocking(false);
InetSocketAddress addr = null; InetSocketAddress addr = null;
if(bindToAddr==null) if(bindToAddr==null) {
addr = new InetSocketAddress(_myAddress.getPort()); addr = new InetSocketAddress(_myAddress.getPort());
else } else {
addr = new InetSocketAddress(bindToAddr, _myAddress.getPort()); addr = new InetSocketAddress(bindToAddr, _myAddress.getPort());
if (_log.shouldLog(Log.WARN))
_log.warn("Binding only to " + bindToAddr);
}
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);
......
...@@ -35,6 +35,7 @@ import net.i2p.router.transport.Transport; ...@@ -35,6 +35,7 @@ import net.i2p.router.transport.Transport;
import net.i2p.router.transport.TransportBid; import net.i2p.router.transport.TransportBid;
import net.i2p.router.transport.TransportImpl; import net.i2p.router.transport.TransportImpl;
import net.i2p.router.util.RandomIterator; import net.i2p.router.util.RandomIterator;
import net.i2p.util.Addresses;
import net.i2p.util.ConcurrentHashSet; import net.i2p.util.ConcurrentHashSet;
import net.i2p.util.Log; import net.i2p.util.Log;
import net.i2p.util.SimpleScheduler; import net.i2p.util.SimpleScheduler;
...@@ -255,6 +256,21 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority ...@@ -255,6 +256,21 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
// bind host // bind host
String bindTo = _context.getProperty(PROP_BIND_INTERFACE); String bindTo = _context.getProperty(PROP_BIND_INTERFACE);
if (bindTo == null) {
// If we are configured with a fixed IP address,
// AND it's one of our local interfaces,
// bind only to that.
String fixedHost = _context.getProperty(PROP_EXTERNAL_HOST);
if (fixedHost != null && fixedHost.length() > 0) {
try {
String testAddr = InetAddress.getByName(fixedHost).getHostAddress();
if (Addresses.getAddresses().contains(testAddr))
bindTo = testAddr;
} catch (UnknownHostException uhe) {}
}
}
InetAddress bindToAddr = null; InetAddress bindToAddr = null;
if (bindTo != null) { if (bindTo != null) {
try { try {
...@@ -281,6 +297,8 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority ...@@ -281,6 +297,8 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
} else { } else {
port = _externalListenPort; port = _externalListenPort;
} }
if (bindToAddr != null && _log.shouldLog(Log.WARN))
_log.warn("Binding only to " + bindToAddr);
if (_log.shouldLog(Log.INFO)) if (_log.shouldLog(Log.INFO))
_log.info("Binding to the port: " + port); _log.info("Binding to the port: " + port);
if (_endpoint == null) { if (_endpoint == null) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment