mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Refactored TCP plugins, moving common code into superclass.
This commit is contained in:
@@ -1,18 +1,12 @@
|
||||
package org.briarproject.plugins.tcp;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.SocketException;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
@@ -22,9 +16,6 @@ class LanTcpPlugin extends TcpPlugin {
|
||||
|
||||
static final TransportId ID = new TransportId("lan");
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(LanTcpPlugin.class.getName());
|
||||
|
||||
LanTcpPlugin(Executor pluginExecutor, DuplexPluginCallback callback,
|
||||
int maxFrameLength, long maxLatency, long pollingInterval) {
|
||||
super(pluginExecutor, callback, maxFrameLength, maxLatency,
|
||||
@@ -39,25 +30,15 @@ class LanTcpPlugin extends TcpPlugin {
|
||||
protected List<SocketAddress> getLocalSocketAddresses() {
|
||||
// Use the same address and port as last time if available
|
||||
TransportProperties p = callback.getLocalProperties();
|
||||
InetSocketAddress old = parseSocketAddress(p.get("address"),
|
||||
p.get("port"));
|
||||
// Get a list of the device's network interfaces
|
||||
List<NetworkInterface> ifaces;
|
||||
try {
|
||||
ifaces = Collections.list(NetworkInterface.getNetworkInterfaces());
|
||||
} catch(SocketException e) {
|
||||
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
return Collections.emptyList();
|
||||
}
|
||||
String oldAddress = p.get("address"), oldPort = p.get("port");
|
||||
InetSocketAddress old = parseSocketAddress(oldAddress, oldPort);
|
||||
List<SocketAddress> addrs = new LinkedList<SocketAddress>();
|
||||
for(NetworkInterface iface : ifaces) {
|
||||
for(InetAddress a : Collections.list(iface.getInetAddresses())) {
|
||||
if(isAcceptableAddress(a)) {
|
||||
// If this is the old address, try to use the same port
|
||||
if(old != null && old.getAddress().equals(a))
|
||||
addrs.add(0, new InetSocketAddress(a, old.getPort()));
|
||||
addrs.add(new InetSocketAddress(a, 0));
|
||||
}
|
||||
for(InetAddress a : getLocalIpAddresses()) {
|
||||
if(isAcceptableAddress(a)) {
|
||||
// If this is the old address, try to use the same port
|
||||
if(old != null && old.getAddress().equals(a))
|
||||
addrs.add(0, new InetSocketAddress(a, old.getPort()));
|
||||
addrs.add(new InetSocketAddress(a, 0));
|
||||
}
|
||||
}
|
||||
return addrs;
|
||||
@@ -77,7 +58,7 @@ class LanTcpPlugin extends TcpPlugin {
|
||||
if(remote.getPort() == 0) return false;
|
||||
if(!isAcceptableAddress(remote.getAddress())) return false;
|
||||
// Try to determine whether the address is on the same LAN as us
|
||||
if(socket == null) return true;
|
||||
if(socket == null) return false;
|
||||
byte[] localIp = socket.getInetAddress().getAddress();
|
||||
byte[] remoteIp = remote.getAddress().getAddress();
|
||||
return addressesAreOnSameLan(localIp, remoteIp);
|
||||
|
||||
@@ -6,11 +6,15 @@ import static java.util.logging.Level.WARNING;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
@@ -182,7 +186,10 @@ abstract class TcpPlugin implements DuplexPlugin {
|
||||
InetSocketAddress remote = getRemoteSocketAddress(c);
|
||||
if(remote == null) return null;
|
||||
if(!isConnectable(remote)) {
|
||||
if(LOG.isLoggable(INFO)) LOG.info(remote + " is not connectable");
|
||||
if(LOG.isLoggable(INFO)) {
|
||||
SocketAddress local = socket.getLocalSocketAddress();
|
||||
LOG.info(remote + " is not connectable from " + local);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
Socket s = new Socket();
|
||||
@@ -229,4 +236,18 @@ abstract class TcpPlugin implements DuplexPlugin {
|
||||
long timeout) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
protected Collection<InetAddress> getLocalIpAddresses() {
|
||||
List<NetworkInterface> ifaces;
|
||||
try {
|
||||
ifaces = Collections.list(NetworkInterface.getNetworkInterfaces());
|
||||
} catch(SocketException e) {
|
||||
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<InetAddress> addrs = new ArrayList<InetAddress>();
|
||||
for(NetworkInterface iface : ifaces)
|
||||
addrs.addAll(Collections.list(iface.getInetAddresses()));
|
||||
return addrs;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,12 @@
|
||||
package org.briarproject.plugins.tcp;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.SocketException;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
@@ -22,9 +16,6 @@ class WanTcpPlugin extends TcpPlugin {
|
||||
|
||||
static final TransportId ID = new TransportId("wan");
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(WanTcpPlugin.class.getName());
|
||||
|
||||
private final PortMapper portMapper;
|
||||
|
||||
private volatile MappingResult mappingResult;
|
||||
@@ -45,25 +36,15 @@ class WanTcpPlugin extends TcpPlugin {
|
||||
protected List<SocketAddress> getLocalSocketAddresses() {
|
||||
// Use the same address and port as last time if available
|
||||
TransportProperties p = callback.getLocalProperties();
|
||||
InetSocketAddress old = parseSocketAddress(p.get("address"),
|
||||
p.get("port"));
|
||||
// Get a list of the device's network interfaces
|
||||
List<NetworkInterface> ifaces;
|
||||
try {
|
||||
ifaces = Collections.list(NetworkInterface.getNetworkInterfaces());
|
||||
} catch(SocketException e) {
|
||||
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
return Collections.emptyList();
|
||||
}
|
||||
String oldAddress = p.get("address"), oldPort = p.get("port");
|
||||
InetSocketAddress old = parseSocketAddress(oldAddress, oldPort);
|
||||
List<SocketAddress> addrs = new LinkedList<SocketAddress>();
|
||||
for(NetworkInterface iface : ifaces) {
|
||||
for(InetAddress a : Collections.list(iface.getInetAddresses())) {
|
||||
if(isAcceptableAddress(a)) {
|
||||
// If this is the old address, try to use the same port
|
||||
if(old != null && old.getAddress().equals(a))
|
||||
addrs.add(0, new InetSocketAddress(a, old.getPort()));
|
||||
addrs.add(new InetSocketAddress(a, 0));
|
||||
}
|
||||
for(InetAddress a : getLocalIpAddresses()) {
|
||||
if(isAcceptableAddress(a)) {
|
||||
// If this is the old address, try to use the same port
|
||||
if(old != null && old.getAddress().equals(a))
|
||||
addrs.add(0, new InetSocketAddress(a, old.getPort()));
|
||||
addrs.add(new InetSocketAddress(a, 0));
|
||||
}
|
||||
}
|
||||
// Accept interfaces with local addresses that can be port-mapped
|
||||
|
||||
Reference in New Issue
Block a user