From a2f5f68f870935adcbdcc1fe89a1573591daff13 Mon Sep 17 00:00:00 2001 From: akwizgran <akwizgran@users.sourceforge.net> Date: Thu, 10 Apr 2014 12:18:14 +0100 Subject: [PATCH] Refactored TCP plugins, moving common code into superclass. --- .../plugins/tcp/LanTcpPlugin.java | 37 +++++-------------- .../briarproject/plugins/tcp/TcpPlugin.java | 23 +++++++++++- .../plugins/tcp/WanTcpPlugin.java | 35 ++++-------------- 3 files changed, 39 insertions(+), 56 deletions(-) diff --git a/briar-core/src/org/briarproject/plugins/tcp/LanTcpPlugin.java b/briar-core/src/org/briarproject/plugins/tcp/LanTcpPlugin.java index d67a6d22e2..d4d3c50624 100644 --- a/briar-core/src/org/briarproject/plugins/tcp/LanTcpPlugin.java +++ b/briar-core/src/org/briarproject/plugins/tcp/LanTcpPlugin.java @@ -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); diff --git a/briar-core/src/org/briarproject/plugins/tcp/TcpPlugin.java b/briar-core/src/org/briarproject/plugins/tcp/TcpPlugin.java index 827a509886..673828b438 100644 --- a/briar-core/src/org/briarproject/plugins/tcp/TcpPlugin.java +++ b/briar-core/src/org/briarproject/plugins/tcp/TcpPlugin.java @@ -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; + } } diff --git a/briar-core/src/org/briarproject/plugins/tcp/WanTcpPlugin.java b/briar-core/src/org/briarproject/plugins/tcp/WanTcpPlugin.java index 16c168c2e4..7a403b6368 100644 --- a/briar-core/src/org/briarproject/plugins/tcp/WanTcpPlugin.java +++ b/briar-core/src/org/briarproject/plugins/tcp/WanTcpPlugin.java @@ -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 -- GitLab