- Add getRequestedPort() to transports
This commit is contained in:
@ -64,9 +64,8 @@ public class Addresses {
|
|||||||
ia.isMulticastAddress() ||
|
ia.isMulticastAddress() ||
|
||||||
ia.isSiteLocalAddress() ||
|
ia.isSiteLocalAddress() ||
|
||||||
!(ia instanceof Inet4Address)) {
|
!(ia instanceof Inet4Address)) {
|
||||||
///////// testing
|
|
||||||
// System.err.println("Skipping: " + ia.getHostAddress());
|
// System.err.println("Skipping: " + ia.getHostAddress());
|
||||||
// return;
|
return;
|
||||||
}
|
}
|
||||||
String ip = ia.getHostAddress();
|
String ip = ia.getHostAddress();
|
||||||
set.add(ip);
|
set.add(ip);
|
||||||
|
@ -40,6 +40,7 @@ public interface Transport {
|
|||||||
public static final String SOURCE_CONFIG = "Configuration change";
|
public static final String SOURCE_CONFIG = "Configuration change";
|
||||||
public void externalAddressReceived(String source, byte[] ip, int port);
|
public void externalAddressReceived(String source, byte[] ip, int port);
|
||||||
public void forwardPortStatus(int port, boolean success, String reason);
|
public void forwardPortStatus(int port, boolean success, String reason);
|
||||||
|
public int getRequestedPort();
|
||||||
public void setListener(TransportEventListener listener);
|
public void setListener(TransportEventListener listener);
|
||||||
public String getStyle();
|
public String getStyle();
|
||||||
|
|
||||||
|
@ -445,6 +445,15 @@ public abstract class TransportImpl implements Transport {
|
|||||||
*/
|
*/
|
||||||
public void forwardPortStatus(int port, boolean success, String reason) {}
|
public void forwardPortStatus(int port, boolean success, String reason) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What port would the transport like to have forwarded by UPnP.
|
||||||
|
* This can't be passed via getCurrentAddress(), as we have to open the port
|
||||||
|
* before we can publish the address.
|
||||||
|
*
|
||||||
|
* @return port or -1 for none or 0 for any
|
||||||
|
*/
|
||||||
|
public int getRequestedPort() { return -1; }
|
||||||
|
|
||||||
/** Who to notify on message availability */
|
/** Who to notify on message availability */
|
||||||
public void setListener(TransportEventListener listener) { _listener = listener; }
|
public void setListener(TransportEventListener listener) { _listener = listener; }
|
||||||
/** Make this stuff pretty (only used in the old console) */
|
/** Make this stuff pretty (only used in the old console) */
|
||||||
|
@ -18,6 +18,7 @@ import java.util.HashMap;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
@ -139,6 +140,8 @@ public class TransportManager implements TransportEventListener {
|
|||||||
if (_log.shouldLog(Log.DEBUG))
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
_log.debug("Transport " + i + " (" + t.getStyle() + ") started");
|
_log.debug("Transport " + i + " (" + t.getStyle() + ") started");
|
||||||
}
|
}
|
||||||
|
// kick UPnP - Do this to get the ports opened even before UDP registers an address
|
||||||
|
transportAddressChanged();
|
||||||
_log.debug("Done start listening on transports");
|
_log.debug("Done start listening on transports");
|
||||||
_context.router().rebuildRouterInfo();
|
_context.router().rebuildRouterInfo();
|
||||||
}
|
}
|
||||||
@ -285,16 +288,47 @@ public class TransportManager implements TransportEventListener {
|
|||||||
return TransportImpl.getIP(dest);
|
return TransportImpl.getIP(dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
Map getAddresses() {
|
public Map<String, RouterAddress> getAddresses() {
|
||||||
Map rv = new HashMap(_transports.size());
|
Map<String, RouterAddress> rv = new HashMap(_transports.size());
|
||||||
for (int i = 0; i < _transports.size(); i++) {
|
for (Transport t : _transports) {
|
||||||
Transport t = (Transport)_transports.get(i);
|
|
||||||
if (t.getCurrentAddress() != null)
|
if (t.getCurrentAddress() != null)
|
||||||
rv.put(t.getStyle(), t.getCurrentAddress());
|
rv.put(t.getStyle(), t.getCurrentAddress());
|
||||||
}
|
}
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Include the published port, or the requested port, for each transport
|
||||||
|
* which we will pass along to UPnP
|
||||||
|
*/
|
||||||
|
private Map<String, Integer> getPorts() {
|
||||||
|
Map<String, Integer> rv = new HashMap(_transports.size());
|
||||||
|
for (Transport t : _transports) {
|
||||||
|
int port = t.getRequestedPort();
|
||||||
|
if (t.getCurrentAddress() != null) {
|
||||||
|
Properties opts = t.getCurrentAddress().getOptions();
|
||||||
|
if (opts != null) {
|
||||||
|
String s = opts.getProperty("port");
|
||||||
|
if (s != null) {
|
||||||
|
try {
|
||||||
|
port = Integer.parseInt(s);
|
||||||
|
} catch (NumberFormatException nfe) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Use UDP port for NTCP too - see comment in NTCPTransport.getRequestedPort() for why this is here
|
||||||
|
if (t.getStyle().equals(NTCPTransport.STYLE) && port <= 0 &&
|
||||||
|
Boolean.valueOf(_context.getProperty(CommSystemFacadeImpl.PROP_I2NP_NTCP_AUTO_PORT)).booleanValue()) {
|
||||||
|
Transport udp = getTransport(UDPTransport.STYLE);
|
||||||
|
if (udp != null)
|
||||||
|
port = t.getRequestedPort();
|
||||||
|
}
|
||||||
|
if (port > 0)
|
||||||
|
rv.put(t.getStyle(), Integer.valueOf(port));
|
||||||
|
}
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
public TransportBid getBid(OutNetMessage msg) {
|
public TransportBid getBid(OutNetMessage msg) {
|
||||||
List bids = getBids(msg);
|
List bids = getBids(msg);
|
||||||
if ( (bids == null) || (bids.size() <= 0) )
|
if ( (bids == null) || (bids.size() <= 0) )
|
||||||
@ -399,7 +433,7 @@ public class TransportManager implements TransportEventListener {
|
|||||||
|
|
||||||
public void transportAddressChanged() {
|
public void transportAddressChanged() {
|
||||||
if (_upnpManager != null)
|
if (_upnpManager != null)
|
||||||
_upnpManager.update(getAddresses());
|
_upnpManager.update(getPorts());
|
||||||
}
|
}
|
||||||
|
|
||||||
public List getMostRecentErrorMessages() {
|
public List getMostRecentErrorMessages() {
|
||||||
|
@ -7,7 +7,6 @@ package net.i2p.router.transport;
|
|||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import net.i2p.data.RouterAddress;
|
import net.i2p.data.RouterAddress;
|
||||||
@ -66,26 +65,14 @@ public class UPnPManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** call when the ports might have changed */
|
/** call when the ports might have changed */
|
||||||
public void update(Map<String, RouterAddress> addresses) {
|
public void update(Map<String, Integer> ports) {
|
||||||
if (_log.shouldLog(Log.DEBUG))
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
_log.debug("UPnP Update:");
|
_log.debug("UPnP Update:");
|
||||||
if (!_isRunning)
|
if (!_isRunning)
|
||||||
return;
|
return;
|
||||||
Set<ForwardPort> forwards = new HashSet(addresses.size());
|
Set<ForwardPort> forwards = new HashSet(ports.size());
|
||||||
for (String style : addresses.keySet()) {
|
for (String style : ports.keySet()) {
|
||||||
RouterAddress ra = addresses.get(style);
|
int port = ports.get(style).intValue();
|
||||||
if (ra == null)
|
|
||||||
continue;
|
|
||||||
Properties opts = ra.getOptions();
|
|
||||||
if (opts == null)
|
|
||||||
continue;
|
|
||||||
String s = opts.getProperty("port");
|
|
||||||
if (s == null)
|
|
||||||
continue;
|
|
||||||
int port = -1;
|
|
||||||
try {
|
|
||||||
port = Integer.parseInt(s);
|
|
||||||
} catch (NumberFormatException nfe) { continue; }
|
|
||||||
int protocol = -1;
|
int protocol = -1;
|
||||||
if ("SSU".equals(style))
|
if ("SSU".equals(style))
|
||||||
protocol = ForwardPort.PROTOCOL_UDP_IPV4;
|
protocol = ForwardPort.PROTOCOL_UDP_IPV4;
|
||||||
@ -144,7 +131,7 @@ public class UPnPManager {
|
|||||||
style = "NTCP";
|
style = "NTCP";
|
||||||
else
|
else
|
||||||
continue;
|
continue;
|
||||||
boolean success = fps.status >= ForwardPortStatus.PROBABLE_SUCCESS;
|
boolean success = fps.status >= ForwardPortStatus.MAYBE_SUCCESS;
|
||||||
_manager.forwardPortStatus(style, fp.portNumber, success, fps.reasonString);
|
_manager.forwardPortStatus(style, fp.portNumber, success, fps.reasonString);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -552,6 +552,14 @@ public class NTCPTransport extends TransportImpl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getRequestedPort() {
|
||||||
|
// would be nice to do this here but we can't easily get to the UDP transport.getRequested_Port()
|
||||||
|
// from here, so we do it in TransportManager.
|
||||||
|
// if (Boolean.valueOf(_context.getProperty(CommSystemFacadeImpl.PROP_I2NP_NTCP_AUTO_PORT)).booleanValue())
|
||||||
|
// return foo;
|
||||||
|
return _context.getProperty(CommSystemFacadeImpl.PROP_I2NP_NTCP_PORT, -1);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This doesn't (completely) block, caller should check isAlive()
|
* This doesn't (completely) block, caller should check isAlive()
|
||||||
* before calling startListening() or restartListening()
|
* before calling startListening() or restartListening()
|
||||||
|
@ -217,13 +217,6 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
|
|||||||
if (_externalListenPort <= 0) {
|
if (_externalListenPort <= 0) {
|
||||||
// no explicit external port, so lets try an internal one
|
// no explicit external port, so lets try an internal one
|
||||||
port = _context.getProperty(PROP_INTERNAL_PORT, DEFAULT_INTERNAL_PORT);
|
port = _context.getProperty(PROP_INTERNAL_PORT, DEFAULT_INTERNAL_PORT);
|
||||||
if (port <= 0) {
|
|
||||||
port = DEFAULT_INTERNAL_PORT;
|
|
||||||
//port = 1024 + _context.random().nextInt(31*1024);
|
|
||||||
//if (_log.shouldLog(Log.INFO))
|
|
||||||
// _log.info("Selecting an arbitrary port to bind to: " + port);
|
|
||||||
_context.router().setConfigSetting(PROP_INTERNAL_PORT, port+"");
|
|
||||||
}
|
|
||||||
// attempt to use it as our external port - this will be overridden by
|
// attempt to use it as our external port - this will be overridden by
|
||||||
// externalAddressReceived(...)
|
// externalAddressReceived(...)
|
||||||
_context.router().setConfigSetting(PROP_EXTERNAL_PORT, port+"");
|
_context.router().setConfigSetting(PROP_EXTERNAL_PORT, port+"");
|
||||||
@ -314,7 +307,12 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
|
|||||||
public int getLocalPort() { return _externalListenPort; }
|
public int getLocalPort() { return _externalListenPort; }
|
||||||
public InetAddress getLocalAddress() { return _externalListenHost; }
|
public InetAddress getLocalAddress() { return _externalListenHost; }
|
||||||
public int getExternalPort() { return _externalListenPort; }
|
public int getExternalPort() { return _externalListenPort; }
|
||||||
|
public int getRequestedPort() {
|
||||||
|
if (_externalListenPort > 0)
|
||||||
|
return _externalListenPort;
|
||||||
|
return _context.getProperty(PROP_INTERNAL_PORT, DEFAULT_INTERNAL_PORT);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If we have received an inbound connection in the last 2 minutes, don't allow
|
* If we have received an inbound connection in the last 2 minutes, don't allow
|
||||||
* our IP to change.
|
* our IP to change.
|
||||||
|
Reference in New Issue
Block a user