mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 13:19:52 +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;
|
package net.sf.briar.plugins.tcp;
|
||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
class MappingResult {
|
class MappingResult {
|
||||||
|
|
||||||
private final InetAddress internal, external;
|
private final InetAddress internal, external;
|
||||||
|
private final int port;
|
||||||
private final boolean succeeded;
|
private final boolean succeeded;
|
||||||
|
|
||||||
MappingResult(InetAddress internal, InetAddress external,
|
MappingResult(InetAddress internal, InetAddress external, int port,
|
||||||
boolean succeeded) {
|
boolean succeeded) {
|
||||||
this.internal = internal;
|
this.internal = internal;
|
||||||
this.external = external;
|
this.external = external;
|
||||||
|
this.port = port;
|
||||||
this.succeeded = succeeded;
|
this.succeeded = succeeded;
|
||||||
}
|
}
|
||||||
|
|
||||||
InetAddress getInternal() {
|
InetSocketAddress getInternal() {
|
||||||
return internal;
|
return isUsable() ? new InetSocketAddress(internal, port) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
InetAddress getExternal() {
|
InetSocketAddress getExternal() {
|
||||||
return external;
|
return isUsable() ? new InetSocketAddress(external, port) : null;
|
||||||
}
|
|
||||||
|
|
||||||
boolean getSucceeded() {
|
|
||||||
return succeeded;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isUsable() {
|
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;
|
package net.sf.briar.plugins.tcp;
|
||||||
|
|
||||||
|
import static java.util.logging.Level.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -44,7 +45,11 @@ class PortMapperImpl implements PortMapper {
|
|||||||
public void stop() {
|
public void stop() {
|
||||||
if(gateway == null) return;
|
if(gateway == null) return;
|
||||||
try {
|
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) {
|
} catch(IOException e) {
|
||||||
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
|
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
|
||||||
} catch(SAXException e) {
|
} catch(SAXException e) {
|
||||||
@@ -71,12 +76,16 @@ class PortMapperImpl implements PortMapper {
|
|||||||
String externalString = gateway.getExternalIPAddress();
|
String externalString = gateway.getExternalIPAddress();
|
||||||
if(externalString != null)
|
if(externalString != null)
|
||||||
external = InetAddress.getByName(externalString);
|
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) {
|
} catch(IOException e) {
|
||||||
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
|
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
|
||||||
} catch(SAXException e) {
|
} catch(SAXException e) {
|
||||||
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
|
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
|
||||||
}
|
}
|
||||||
if(succeeded) ports.add(port);
|
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
|
@Override
|
||||||
protected List<SocketAddress> getLocalSocketAddresses() {
|
protected List<SocketAddress> getLocalSocketAddresses() {
|
||||||
List<SocketAddress> addrs = new ArrayList<SocketAddress>();
|
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();
|
TransportProperties p = callback.getLocalProperties();
|
||||||
String addrString = p.get("address");
|
String addrString = p.get("address");
|
||||||
String portString = p.get("port");
|
String portString = p.get("port");
|
||||||
@@ -90,6 +90,7 @@ class WanTcpPlugin extends TcpPlugin {
|
|||||||
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
|
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Get a list of the device's network interfaces
|
||||||
List<NetworkInterface> ifaces;
|
List<NetworkInterface> ifaces;
|
||||||
try {
|
try {
|
||||||
ifaces = Collections.list(NetworkInterface.getNetworkInterfaces());
|
ifaces = Collections.list(NetworkInterface.getNetworkInterfaces());
|
||||||
@@ -107,11 +108,11 @@ class WanTcpPlugin extends TcpPlugin {
|
|||||||
if(!link && !site) addrs.add(new InetSocketAddress(a, 0));
|
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();
|
if(port == 0) port = chooseEphemeralPort();
|
||||||
mappingResult = portMapper.map(port);
|
mappingResult = portMapper.map(port);
|
||||||
if(mappingResult != null && mappingResult.isUsable())
|
if(mappingResult != null && mappingResult.isUsable())
|
||||||
addrs.add(new InetSocketAddress(mappingResult.getInternal(), port));
|
addrs.add(mappingResult.getInternal());
|
||||||
return addrs;
|
return addrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,13 +122,13 @@ class WanTcpPlugin extends TcpPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setLocalSocketAddress(InetSocketAddress a) {
|
protected void setLocalSocketAddress(InetSocketAddress a) {
|
||||||
InetAddress addr = a.getAddress();
|
|
||||||
if(mappingResult != null && mappingResult.isUsable()) {
|
if(mappingResult != null && mappingResult.isUsable()) {
|
||||||
if(addr.equals(mappingResult.getInternal()))
|
// Advertise the external address to contacts
|
||||||
addr = mappingResult.getExternal();
|
if(a.equals(mappingResult.getInternal()))
|
||||||
|
a = mappingResult.getExternal();
|
||||||
}
|
}
|
||||||
TransportProperties p = new TransportProperties();
|
TransportProperties p = new TransportProperties();
|
||||||
p.put("address", addr.getHostAddress());
|
p.put("address", a.getAddress().getHostAddress());
|
||||||
p.put("port", String.valueOf(a.getPort()));
|
p.put("port", String.valueOf(a.getPort()));
|
||||||
callback.mergeLocalProperties(p);
|
callback.mergeLocalProperties(p);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user