mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Advertise the external address if the internal address and port match.
This commit is contained in:
@@ -1,32 +1,31 @@
|
||||
package net.sf.briar.plugins.tcp;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
class MappingResult {
|
||||
|
||||
private final InetAddress internal, external;
|
||||
private final int port;
|
||||
private final boolean succeeded;
|
||||
|
||||
MappingResult(InetAddress internal, InetAddress external,
|
||||
MappingResult(InetAddress internal, InetAddress external, int port,
|
||||
boolean succeeded) {
|
||||
this.internal = internal;
|
||||
this.external = external;
|
||||
this.port = port;
|
||||
this.succeeded = succeeded;
|
||||
}
|
||||
|
||||
InetAddress getInternal() {
|
||||
return internal;
|
||||
InetSocketAddress getInternal() {
|
||||
return isUsable() ? new InetSocketAddress(internal, port) : null;
|
||||
}
|
||||
|
||||
InetAddress getExternal() {
|
||||
return external;
|
||||
}
|
||||
|
||||
boolean getSucceeded() {
|
||||
return succeeded;
|
||||
InetSocketAddress getExternal() {
|
||||
return isUsable() ? new InetSocketAddress(external, port) : null;
|
||||
}
|
||||
|
||||
boolean isUsable() {
|
||||
return internal != null && external != null && succeeded;
|
||||
return internal != null && external != null && port != 0 && succeeded;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.sf.briar.plugins.tcp;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -44,7 +45,11 @@ class PortMapperImpl implements PortMapper {
|
||||
public void stop() {
|
||||
if(gateway == null) return;
|
||||
try {
|
||||
for(Integer port: ports) gateway.deletePortMapping(port, "TCP");
|
||||
for(Integer port: ports) {
|
||||
gateway.deletePortMapping(port, "TCP");
|
||||
if(LOG.isLoggable(INFO))
|
||||
LOG.info("Deleted mapping for port " + port);
|
||||
}
|
||||
} catch(IOException e) {
|
||||
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
|
||||
} catch(SAXException e) {
|
||||
@@ -71,12 +76,16 @@ class PortMapperImpl implements PortMapper {
|
||||
String externalString = gateway.getExternalIPAddress();
|
||||
if(externalString != null)
|
||||
external = InetAddress.getByName(externalString);
|
||||
if(LOG.isLoggable(INFO)) {
|
||||
if(succeeded) LOG.info("External address " + externalString);
|
||||
else LOG.info("Could not create port mapping");
|
||||
}
|
||||
} catch(IOException e) {
|
||||
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
|
||||
} catch(SAXException e) {
|
||||
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
|
||||
}
|
||||
if(succeeded) ports.add(port);
|
||||
return new MappingResult(internal, external, succeeded);
|
||||
return new MappingResult(internal, external, port, succeeded);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ class WanTcpPlugin extends TcpPlugin {
|
||||
@Override
|
||||
protected List<SocketAddress> getLocalSocketAddresses() {
|
||||
List<SocketAddress> addrs = new ArrayList<SocketAddress>();
|
||||
// Prefer a previously used external address and port if available
|
||||
// Prefer a previously used address and port if available
|
||||
TransportProperties p = callback.getLocalProperties();
|
||||
String addrString = p.get("address");
|
||||
String portString = p.get("port");
|
||||
@@ -90,6 +90,7 @@ class WanTcpPlugin extends TcpPlugin {
|
||||
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
|
||||
}
|
||||
}
|
||||
// Get a list of the device's network interfaces
|
||||
List<NetworkInterface> ifaces;
|
||||
try {
|
||||
ifaces = Collections.list(NetworkInterface.getNetworkInterfaces());
|
||||
@@ -107,11 +108,11 @@ class WanTcpPlugin extends TcpPlugin {
|
||||
if(!link && !site) addrs.add(new InetSocketAddress(a, 0));
|
||||
}
|
||||
}
|
||||
// Accept interfaces that can be port-mapped
|
||||
// Accept interfaces with local addresses that can be port-mapped
|
||||
if(port == 0) port = chooseEphemeralPort();
|
||||
mappingResult = portMapper.map(port);
|
||||
if(mappingResult != null && mappingResult.isUsable())
|
||||
addrs.add(new InetSocketAddress(mappingResult.getInternal(), port));
|
||||
addrs.add(mappingResult.getInternal());
|
||||
return addrs;
|
||||
}
|
||||
|
||||
@@ -121,13 +122,13 @@ class WanTcpPlugin extends TcpPlugin {
|
||||
|
||||
@Override
|
||||
protected void setLocalSocketAddress(InetSocketAddress a) {
|
||||
InetAddress addr = a.getAddress();
|
||||
if(mappingResult != null && mappingResult.isUsable()) {
|
||||
if(addr.equals(mappingResult.getInternal()))
|
||||
addr = mappingResult.getExternal();
|
||||
// Advertise the external address to contacts
|
||||
if(a.equals(mappingResult.getInternal()))
|
||||
a = mappingResult.getExternal();
|
||||
}
|
||||
TransportProperties p = new TransportProperties();
|
||||
p.put("address", addr.getHostAddress());
|
||||
p.put("address", a.getAddress().getHostAddress());
|
||||
p.put("port", String.valueOf(a.getPort()));
|
||||
callback.mergeLocalProperties(p);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user