diff --git a/bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPlugin.java b/bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPlugin.java index 2511aa3dc..ec7bd1941 100644 --- a/bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPlugin.java +++ b/bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPlugin.java @@ -19,7 +19,7 @@ import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; -import java.util.Collection; +import java.util.List; import java.util.concurrent.Executor; import java.util.logging.Logger; @@ -84,19 +84,19 @@ class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener { } @Override - protected Collection getLocalIpAddresses() { + protected List getUsableLocalInetAddresses() { // If the device doesn't have wifi, don't open any sockets if (wifiManager == null) return emptyList(); - // If we're connected to a wifi network, use that network + // If we're connected to a wifi network, return its address WifiInfo info = wifiManager.getConnectionInfo(); - if (info != null && info.getIpAddress() != 0) + if (info != null && info.getIpAddress() != 0) { return singletonList(intToInetAddress(info.getIpAddress())); + } // If we're running an access point, return its address - Collection all = super.getLocalIpAddresses(); - if (all.contains(WIFI_AP_ADDRESS)) - return singletonList(WIFI_AP_ADDRESS); - if (all.contains(WIFI_DIRECT_AP_ADDRESS)) - return singletonList(WIFI_DIRECT_AP_ADDRESS); + for (InetAddress addr : getLocalInetAddresses()) { + if (addr.equals(WIFI_AP_ADDRESS)) return singletonList(addr); + if (addr.equals(WIFI_DIRECT_AP_ADDRESS)) return singletonList(addr); + } // No suitable addresses return emptyList(); } @@ -136,7 +136,7 @@ class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener { private void updateConnectionStatus() { connectionStatusExecutor.execute(() -> { if (!running) return; - Collection addrs = getLocalIpAddresses(); + List addrs = getLocalInetAddresses(); if (addrs.contains(WIFI_AP_ADDRESS) || addrs.contains(WIFI_DIRECT_AP_ADDRESS)) { LOG.info("Providing wifi hotspot"); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPlugin.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPlugin.java index 068798efc..afa8dfcf6 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPlugin.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPlugin.java @@ -17,12 +17,11 @@ import java.io.IOException; import java.net.Inet4Address; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.InterfaceAddress; import java.net.ServerSocket; import java.net.Socket; -import java.net.SocketAddress; import java.net.UnknownHostException; import java.util.ArrayList; -import java.util.Comparator; import java.util.List; import java.util.Random; import java.util.concurrent.Executor; @@ -51,9 +50,6 @@ class LanTcpPlugin extends TcpPlugin { private static final Logger LOG = getLogger(LanTcpPlugin.class.getName()); - private static final LanAddressComparator ADDRESS_COMPARATOR = - new LanAddressComparator(); - private static final int MAX_ADDRESSES = 4; private static final String SEPARATOR = ","; @@ -111,28 +107,26 @@ class LanTcpPlugin extends TcpPlugin { @Override protected List getLocalSocketAddresses() { TransportProperties p = callback.getLocalProperties(); - int port = parsePortProperty(p.get(PROP_PORT)); + int preferredPort = parsePortProperty(p.get(PROP_PORT)); String oldIpPorts = p.get(PROP_IP_PORTS); List olds = parseSocketAddresses(oldIpPorts); + List locals = new ArrayList<>(); - for (InetAddress local : getLocalIpAddresses()) { - if (isAcceptableAddress(local)) { - // If we've used this address before, try to use the same port - boolean reused = false; - for (InetSocketAddress old : olds) { - if (old.getAddress().equals(local)) { - locals.add(new InetSocketAddress(local, old.getPort())); - reused = true; - break; - } + List fallbacks = new ArrayList<>(); + for (InetAddress local : getUsableLocalInetAddresses()) { + // If we've used this address before, try to use the same port + int port = preferredPort; + for (InetSocketAddress old : olds) { + if (old.getAddress().equals(local)) { + port = old.getPort(); + break; } - // Otherwise try to use our preferred port - if (!reused) locals.add(new InetSocketAddress(local, port)); - // Fall back to any available port - locals.add(new InetSocketAddress(local, 0)); } + locals.add(new InetSocketAddress(local, port)); + // Fall back to any available port + fallbacks.add(new InetSocketAddress(local, 0)); } - sort(locals, ADDRESS_COMPARATOR); + locals.addAll(fallbacks); return locals; } @@ -155,6 +149,20 @@ class LanTcpPlugin extends TcpPlugin { return addresses; } + protected List getUsableLocalInetAddresses() { + List ifAddrs = + new ArrayList<>(getLocalInterfaceAddresses()); + // Prefer longer network prefixes + sort(ifAddrs, (a, b) -> + b.getNetworkPrefixLength() - a.getNetworkPrefixLength()); + List addrs = new ArrayList<>(); + for (InterfaceAddress ifAddr : ifAddrs) { + InetAddress addr = ifAddr.getAddress(); + if (isAcceptableAddress(addr)) addrs.add(addr); + } + return addrs; + } + @Override protected void setLocalSocketAddress(InetSocketAddress a) { String ipPort = getIpPortString(a); @@ -218,55 +226,28 @@ class LanTcpPlugin extends TcpPlugin { } @Override - protected boolean isConnectable(InetSocketAddress remote) { + protected boolean isConnectable(InterfaceAddress local, + InetSocketAddress remote) { 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 false; - byte[] localIp = socket.getInetAddress().getAddress(); + byte[] localIp = local.getAddress().getAddress(); byte[] remoteIp = remote.getAddress().getAddress(); - return addressesAreOnSameLan(localIp, remoteIp); + int prefixLength = local.getNetworkPrefixLength(); + return areAddressesInSameNetwork(localIp, remoteIp, prefixLength); } // Package access for testing - boolean addressesAreOnSameLan(byte[] localIp, byte[] remoteIp) { - // 10.0.0.0/8 - if (isSlash8SiteLocal(localIp)) return isSlash8SiteLocal(remoteIp); - // 172.16.0.0/12 - if (isSlash12SiteLocal(localIp)) return isSlash12SiteLocal(remoteIp); - // 192.168.0.0/16 - if (isSlash16SiteLocal(localIp)) return isSlash16SiteLocal(remoteIp); - // 169.254.0.0/16 - if (isSlash16LinkLocal(localIp)) return isSlash16LinkLocal(remoteIp); - // Unrecognised prefix - return false; - } - - private static boolean isSlash8SiteLocal(byte[] ipv4) { - return ipv4[0] == 10; - } - - private static boolean isSlash12SiteLocal(byte[] ipv4) { - return ipv4[0] == (byte) 172 && (ipv4[1] & 0xF0) == 16; - } - - private static boolean isSlash16SiteLocal(byte[] ipv4) { - return ipv4[0] == (byte) 192 && ipv4[1] == (byte) 168; - } - - private static boolean isSlash16LinkLocal(byte[] ipv4) { - return ipv4[0] == (byte) 169 && ipv4[1] == (byte) 254; - } - - // Returns the prefix length for a link-local or site-local address, or 0 - // for any other address - private static int getPrefixLengthIfKnown(InetAddress addr) { - if (!(addr instanceof Inet4Address)) return 0; - byte[] ipv4 = addr.getAddress(); - if (isSlash8SiteLocal(ipv4)) return 8; - if (isSlash12SiteLocal(ipv4)) return 12; - if (isSlash16SiteLocal(ipv4) || isSlash16LinkLocal(ipv4)) return 16; - return 0; + static boolean areAddressesInSameNetwork(byte[] localIp, byte[] remoteIp, + int prefixLength) { + if (localIp.length != remoteIp.length) return false; + for (int i = 0; i < prefixLength; i++) { + int mask = 128 >> (i & 7); + if ((localIp[i >> 3] & mask) != (remoteIp[i >> 3] & mask)) { + return false; // Addresses differ at bit i + } + } + return true; } @Override @@ -307,6 +288,12 @@ class LanTcpPlugin extends TcpPlugin { public DuplexTransportConnection createKeyAgreementConnection( byte[] commitment, BdfList descriptor) { if (!isRunning()) return null; + ServerSocket ss = socket; + InterfaceAddress local = getLocalInterfaceAddress(ss.getInetAddress()); + if (local == null) { + LOG.warning("No interface for key agreement server socket"); + return null; + } InetSocketAddress remote; try { remote = parseSocketAddress(descriptor); @@ -314,12 +301,11 @@ class LanTcpPlugin extends TcpPlugin { LOG.info("Invalid IP/port in key agreement descriptor"); return null; } - if (!isConnectable(remote)) { + if (!isConnectable(local, remote)) { if (LOG.isLoggable(INFO)) { - SocketAddress local = socket.getLocalSocketAddress(); LOG.info(scrubSocketAddress(remote) + " is not connectable from " + - scrubSocketAddress(local)); + scrubSocketAddress(ss.getLocalSocketAddress())); } return null; } @@ -327,7 +313,7 @@ class LanTcpPlugin extends TcpPlugin { if (LOG.isLoggable(INFO)) LOG.info("Connecting to " + scrubSocketAddress(remote)); Socket s = createSocket(); - s.bind(new InetSocketAddress(socket.getInetAddress(), 0)); + s.bind(new InetSocketAddress(ss.getInetAddress(), 0)); s.connect(remote, connectionTimeout); s.setSoTimeout(socketTimeout); if (LOG.isLoggable(INFO)) @@ -377,19 +363,4 @@ class LanTcpPlugin extends TcpPlugin { IoUtils.tryToClose(ss, LOG, WARNING); } } - - static class LanAddressComparator implements Comparator { - - @Override - public int compare(InetSocketAddress a, InetSocketAddress b) { - // Prefer addresses with non-zero ports - int aPort = a.getPort(), bPort = b.getPort(); - if (aPort > 0 && bPort == 0) return -1; - if (aPort == 0 && bPort > 0) return 1; - // Prefer addresses with longer prefixes - int aPrefix = getPrefixLengthIfKnown(a.getAddress()); - int bPrefix = getPrefixLengthIfKnown(b.getAddress()); - return bPrefix - aPrefix; - } - } } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/TcpPlugin.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/TcpPlugin.java index 64ca4d8f4..4f11054a0 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/TcpPlugin.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/TcpPlugin.java @@ -19,10 +19,10 @@ import org.briarproject.bramble.util.IoUtils; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.InterfaceAddress; 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; @@ -36,7 +36,6 @@ import java.util.regex.Pattern; import javax.annotation.Nullable; -import static java.net.NetworkInterface.getNetworkInterfaces; import static java.util.Collections.emptyList; import static java.util.Collections.list; import static java.util.logging.Level.INFO; @@ -88,7 +87,8 @@ abstract class TcpPlugin implements DuplexPlugin { * Returns true if connections to the given address can be attempted. */ @SuppressWarnings("BooleanMethodIsAlwaysInverted") - protected abstract boolean isConnectable(InetSocketAddress remote); + protected abstract boolean isConnectable(InterfaceAddress local, + InetSocketAddress remote); TcpPlugin(Executor ioExecutor, Backoff backoff, PluginCallback callback, int maxLatency, int maxIdleTime, int connectionTimeout) { @@ -233,18 +233,23 @@ abstract class TcpPlugin implements DuplexPlugin { @Override public DuplexTransportConnection createConnection(TransportProperties p) { if (!isRunning()) return null; + ServerSocket ss = socket; + InterfaceAddress local = getLocalInterfaceAddress(ss.getInetAddress()); + if (local == null) { + LOG.warning("No interface for server socket"); + return null; + } for (InetSocketAddress remote : getRemoteSocketAddresses(p)) { // Don't try to connect to our own address if (!canConnectToOwnAddress() && - remote.getAddress().equals(socket.getInetAddress())) { + remote.getAddress().equals(ss.getInetAddress())) { continue; } - if (!isConnectable(remote)) { + if (!isConnectable(local, remote)) { if (LOG.isLoggable(INFO)) { - SocketAddress local = socket.getLocalSocketAddress(); LOG.info(scrubSocketAddress(remote) + " is not connectable from " + - scrubSocketAddress(local)); + scrubSocketAddress(ss.getLocalSocketAddress())); } continue; } @@ -252,7 +257,7 @@ abstract class TcpPlugin implements DuplexPlugin { if (LOG.isLoggable(INFO)) LOG.info("Connecting to " + scrubSocketAddress(remote)); Socket s = createSocket(); - s.bind(new InetSocketAddress(socket.getInetAddress(), 0)); + s.bind(new InetSocketAddress(ss.getInetAddress(), 0)); s.connect(remote, connectionTimeout); s.setSoTimeout(socketTimeout); if (LOG.isLoggable(INFO)) @@ -267,6 +272,14 @@ abstract class TcpPlugin implements DuplexPlugin { return null; } + @Nullable + InterfaceAddress getLocalInterfaceAddress(InetAddress a) { + for (InterfaceAddress ifAddr : getLocalInterfaceAddresses()) { + if (ifAddr.getAddress().equals(a)) return ifAddr; + } + return null; + } + // Override for testing protected boolean canConnectToOwnAddress() { return false; @@ -327,14 +340,27 @@ abstract class TcpPlugin implements DuplexPlugin { throw new UnsupportedOperationException(); } - Collection getLocalIpAddresses() { + List getLocalInterfaceAddresses() { + List addrs = new ArrayList<>(); + for (NetworkInterface iface : getNetworkInterfaces()) { + addrs.addAll(iface.getInterfaceAddresses()); + } + return addrs; + } + + List getLocalInetAddresses() { + List addrs = new ArrayList<>(); + for (NetworkInterface iface : getNetworkInterfaces()) { + addrs.addAll(list(iface.getInetAddresses())); + } + return addrs; + } + + private List getNetworkInterfaces() { try { - Enumeration ifaces = getNetworkInterfaces(); - if (ifaces == null) return emptyList(); - List addrs = new ArrayList<>(); - for (NetworkInterface iface : list(ifaces)) - addrs.addAll(list(iface.getInetAddresses())); - return addrs; + Enumeration ifaces = + NetworkInterface.getNetworkInterfaces(); + return ifaces == null ? emptyList() : list(ifaces); } catch (SocketException e) { logException(LOG, WARNING, e); return emptyList(); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/WanTcpPlugin.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/WanTcpPlugin.java index 6c415428c..0f4643cf1 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/WanTcpPlugin.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/WanTcpPlugin.java @@ -10,6 +10,7 @@ import org.briarproject.bramble.api.properties.TransportProperties; import java.net.Inet4Address; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.InterfaceAddress; import java.util.LinkedList; import java.util.List; import java.util.concurrent.Executor; @@ -47,7 +48,7 @@ class WanTcpPlugin extends TcpPlugin { TransportProperties p = callback.getLocalProperties(); InetSocketAddress old = parseSocketAddress(p.get(PROP_IP_PORT)); List addrs = new LinkedList<>(); - for (InetAddress a : getLocalIpAddresses()) { + for (InetAddress a : getLocalInetAddresses()) { if (isAcceptableAddress(a)) { // If this is the old address, try to use the same port if (old != null && old.getAddress().equals(a)) @@ -88,7 +89,8 @@ class WanTcpPlugin extends TcpPlugin { } @Override - protected boolean isConnectable(InetSocketAddress remote) { + protected boolean isConnectable(InterfaceAddress local, + InetSocketAddress remote) { if (remote.getPort() == 0) return false; return isAcceptableAddress(remote.getAddress()); } diff --git a/bramble-core/src/test/java/org/briarproject/bramble/plugin/tcp/LanTcpPluginTest.java b/bramble-core/src/test/java/org/briarproject/bramble/plugin/tcp/LanTcpPluginTest.java index 0d3f9c9ed..73a2f7151 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/plugin/tcp/LanTcpPluginTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/plugin/tcp/LanTcpPluginTest.java @@ -10,7 +10,6 @@ import org.briarproject.bramble.api.plugin.TransportConnectionWriter; import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection; import org.briarproject.bramble.api.properties.TransportProperties; import org.briarproject.bramble.api.settings.Settings; -import org.briarproject.bramble.plugin.tcp.LanTcpPlugin.LanAddressComparator; import org.briarproject.bramble.test.BrambleTestCase; import org.junit.Before; import org.junit.Test; @@ -22,7 +21,6 @@ import java.net.InetSocketAddress; import java.net.NetworkInterface; import java.net.ServerSocket; import java.net.Socket; -import java.util.Comparator; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicBoolean; @@ -33,6 +31,7 @@ import static java.util.concurrent.Executors.newCachedThreadPool; import static java.util.concurrent.TimeUnit.SECONDS; import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.COMMIT_LENGTH; import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_LAN; +import static org.briarproject.bramble.plugin.tcp.LanTcpPlugin.areAddressesInSameNetwork; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -59,44 +58,62 @@ public class LanTcpPluginTest extends BrambleTestCase { } @Test - public void testAddressesAreOnSameLan() { - // Local and remote in 10.0.0.0/8 should return true - assertTrue(plugin.addressesAreOnSameLan(makeAddress(10, 0, 0, 0), - makeAddress(10, 255, 255, 255))); - // Local and remote in 172.16.0.0/12 should return true - assertTrue(plugin.addressesAreOnSameLan(makeAddress(172, 16, 0, 0), - makeAddress(172, 31, 255, 255))); - // Local and remote in 192.168.0.0/16 should return true - assertTrue(plugin.addressesAreOnSameLan(makeAddress(192, 168, 0, 0), - makeAddress(192, 168, 255, 255))); - // Local and remote in 169.254.0.0/16 (link-local) should return true - assertTrue(plugin.addressesAreOnSameLan(makeAddress(169, 254, 0, 0), - makeAddress(169, 254, 255, 255))); - // Local and remote in different recognised prefixes should return false - assertFalse(plugin.addressesAreOnSameLan(makeAddress(10, 0, 0, 0), - makeAddress(172, 31, 255, 255))); - assertFalse(plugin.addressesAreOnSameLan(makeAddress(10, 0, 0, 0), - makeAddress(192, 168, 255, 255))); - assertFalse(plugin.addressesAreOnSameLan(makeAddress(172, 16, 0, 0), - makeAddress(10, 255, 255, 255))); - assertFalse(plugin.addressesAreOnSameLan(makeAddress(172, 16, 0, 0), - makeAddress(192, 168, 255, 255))); - assertFalse(plugin.addressesAreOnSameLan(makeAddress(192, 168, 0, 0), - makeAddress(10, 255, 255, 255))); - assertFalse(plugin.addressesAreOnSameLan(makeAddress(192, 168, 0, 0), - makeAddress(172, 31, 255, 255))); - assertFalse(plugin.addressesAreOnSameLan(makeAddress(169, 254, 0, 0), - makeAddress(192, 168, 255, 255))); - // Remote prefix unrecognised should return false - assertFalse(plugin.addressesAreOnSameLan(makeAddress(10, 0, 0, 0), - makeAddress(1, 2, 3, 4))); - assertFalse(plugin.addressesAreOnSameLan(makeAddress(172, 16, 0, 0), - makeAddress(1, 2, 3, 4))); - assertFalse(plugin.addressesAreOnSameLan(makeAddress(192, 168, 0, 0), - makeAddress(1, 2, 3, 4))); - // Both prefixes unrecognised should return false - assertFalse(plugin.addressesAreOnSameLan(makeAddress(1, 2, 3, 4), - makeAddress(5, 6, 7, 8))); + public void testAreAddressesInSameNetwork() { + // Local and remote in 10.0.0.0/8 + assertTrue(areAddressesInSameNetwork(makeAddress(10, 0, 0, 0), + makeAddress(10, 255, 255, 255), 8)); + assertFalse(areAddressesInSameNetwork(makeAddress(10, 0, 0, 0), + makeAddress(10, 255, 255, 255), 9)); + + // Local and remote in 172.16.0.0/12 + assertTrue(areAddressesInSameNetwork(makeAddress(172, 16, 0, 0), + makeAddress(172, 31, 255, 255), 12)); + assertFalse(areAddressesInSameNetwork(makeAddress(172, 16, 0, 0), + makeAddress(172, 31, 255, 255), 13)); + + // Local and remote in 192.168.0.0/16 + assertTrue(areAddressesInSameNetwork(makeAddress(192, 168, 0, 0), + makeAddress(192, 168, 255, 255), 16)); + assertFalse(areAddressesInSameNetwork(makeAddress(192, 168, 0, 0), + makeAddress(192, 168, 255, 255), 17)); + + // Local and remote in 169.254.0.0/16 + assertTrue(areAddressesInSameNetwork(makeAddress(169, 254, 0, 0), + makeAddress(169, 254, 255, 255), 16)); + assertFalse(areAddressesInSameNetwork(makeAddress(169, 254, 0, 0), + makeAddress(169, 254, 255, 255), 17)); + + // Local in 10.0.0.0/8, remote in a different network + assertFalse(areAddressesInSameNetwork(makeAddress(10, 0, 0, 0), + makeAddress(172, 31, 255, 255), 8)); + assertFalse(areAddressesInSameNetwork(makeAddress(10, 0, 0, 0), + makeAddress(192, 168, 255, 255), 8)); + assertFalse(areAddressesInSameNetwork(makeAddress(10, 0, 0, 0), + makeAddress(169, 254, 255, 255), 8)); + + // Local in 172.16.0.0/12, remote in a different network + assertFalse(areAddressesInSameNetwork(makeAddress(172, 16, 0, 0), + makeAddress(10, 255, 255, 255), 12)); + assertFalse(areAddressesInSameNetwork(makeAddress(172, 16, 0, 0), + makeAddress(192, 168, 255, 255), 12)); + assertFalse(areAddressesInSameNetwork(makeAddress(172, 16, 0, 0), + makeAddress(169, 254, 255, 255), 12)); + + // Local in 192.168.0.0/16, remote in a different network + assertFalse(areAddressesInSameNetwork(makeAddress(192, 168, 0, 0), + makeAddress(10, 255, 255, 255), 16)); + assertFalse(areAddressesInSameNetwork(makeAddress(192, 168, 0, 0), + makeAddress(172, 31, 255, 255), 16)); + assertFalse(areAddressesInSameNetwork(makeAddress(192, 168, 0, 0), + makeAddress(169, 254, 255, 255), 16)); + + // Local in 169.254.0.0/16, remote in a different network + assertFalse(areAddressesInSameNetwork(makeAddress(169, 254, 0, 0), + makeAddress(10, 255, 255, 255), 16)); + assertFalse(areAddressesInSameNetwork(makeAddress(169, 254, 0, 0), + makeAddress(172, 31, 255, 255), 16)); + assertFalse(areAddressesInSameNetwork(makeAddress(169, 254, 0, 0), + makeAddress(192, 168, 255, 255), 16)); } private byte[] makeAddress(int... parts) { @@ -266,54 +283,12 @@ public class LanTcpPluginTest extends BrambleTestCase { plugin.stop(); } - @Test - public void testComparatorPrefersNonZeroPorts() { - Comparator comparator = new LanAddressComparator(); - InetSocketAddress nonZero = new InetSocketAddress("1.2.3.4", 1234); - InetSocketAddress zero = new InetSocketAddress("1.2.3.4", 0); - - assertEquals(0, comparator.compare(nonZero, nonZero)); - assertTrue(comparator.compare(nonZero, zero) < 0); - - assertTrue(comparator.compare(zero, nonZero) > 0); - assertEquals(0, comparator.compare(zero, zero)); - } - - @Test - public void testComparatorPrefersLongerPrefixes() { - Comparator comparator = new LanAddressComparator(); - InetSocketAddress prefix169 = new InetSocketAddress("169.254.0.1", 0); - InetSocketAddress prefix192 = new InetSocketAddress("192.168.0.1", 0); - InetSocketAddress prefix172 = new InetSocketAddress("172.16.0.1", 0); - InetSocketAddress prefix10 = new InetSocketAddress("10.0.0.1", 0); - - assertEquals(0, comparator.compare(prefix169, prefix169)); - assertEquals(0, comparator.compare(prefix169, prefix192)); - assertTrue(comparator.compare(prefix169, prefix172) < 0); - assertTrue(comparator.compare(prefix169, prefix10) < 0); - - assertEquals(0, comparator.compare(prefix192, prefix192)); - assertEquals(0, comparator.compare(prefix192, prefix169)); - assertTrue(comparator.compare(prefix192, prefix172) < 0); - assertTrue(comparator.compare(prefix192, prefix10) < 0); - - assertTrue(comparator.compare(prefix172, prefix169) > 0); - assertTrue(comparator.compare(prefix172, prefix192) > 0); - assertEquals(0, comparator.compare(prefix172, prefix172)); - assertTrue(comparator.compare(prefix172, prefix10) < 0); - - assertTrue(comparator.compare(prefix10, prefix169) > 0); - assertTrue(comparator.compare(prefix10, prefix192) > 0); - assertTrue(comparator.compare(prefix10, prefix172) > 0); - assertEquals(0, comparator.compare(prefix10, prefix10)); - } - - @SuppressWarnings("BooleanMethodIsAlwaysInverted") private boolean systemHasLocalIpv4Address() throws Exception { for (NetworkInterface i : list(getNetworkInterfaces())) { for (InetAddress a : list(i.getInetAddresses())) { - if (a instanceof Inet4Address) + if (a instanceof Inet4Address) { return a.isLinkLocalAddress() || a.isSiteLocalAddress(); + } } } return false;