mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 21:29:54 +01:00
Always accept incoming connections.
This commit is contained in:
@@ -42,7 +42,6 @@ interface BluetoothConnectionLimiter {
|
|||||||
* Returns true if a contact connection can be opened. This method does not
|
* Returns true if a contact connection can be opened. This method does not
|
||||||
* need to be called for key agreement connections.
|
* need to be called for key agreement connections.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
|
||||||
boolean canOpenContactConnection();
|
boolean canOpenContactConnection();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,7 +50,8 @@ interface BluetoothConnectionLimiter {
|
|||||||
* <p/>
|
* <p/>
|
||||||
* Returns false if the limiter has closed the new connection.
|
* Returns false if the limiter has closed the new connection.
|
||||||
*/
|
*/
|
||||||
boolean contactConnectionOpened(DuplexTransportConnection conn);
|
boolean contactConnectionOpened(DuplexTransportConnection conn,
|
||||||
|
boolean incoming);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Informs the limiter that a key agreement connection has been opened.
|
* Informs the limiter that a key agreement connection has been opened.
|
||||||
|
|||||||
@@ -81,7 +81,8 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contactConnectionOpened(DuplexTransportConnection conn) {
|
public boolean contactConnectionOpened(DuplexTransportConnection conn,
|
||||||
|
boolean incoming) {
|
||||||
boolean accept;
|
boolean accept;
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
if (keyAgreementInProgress) {
|
if (keyAgreementInProgress) {
|
||||||
@@ -89,7 +90,7 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
|||||||
accept = false;
|
accept = false;
|
||||||
} else {
|
} else {
|
||||||
long now = clock.currentTimeMillis();
|
long now = clock.currentTimeMillis();
|
||||||
accept = isContactConnectionAllowedByLimit(now);
|
accept = incoming || isContactConnectionAllowedByLimit(now);
|
||||||
if (accept) {
|
if (accept) {
|
||||||
connections.add(new ConnectionRecord(conn, now));
|
connections.add(new ConnectionRecord(conn, now));
|
||||||
if (connections.size() > connectionLimit) {
|
if (connections.size() > connectionLimit) {
|
||||||
|
|||||||
@@ -232,7 +232,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LOG.info("Connection received");
|
LOG.info("Connection received");
|
||||||
if (connectionLimiter.contactConnectionOpened(conn)) {
|
if (connectionLimiter.contactConnectionOpened(conn, true)) {
|
||||||
backoff.reset();
|
backoff.reset();
|
||||||
callback.handleConnection(conn);
|
callback.handleConnection(conn);
|
||||||
}
|
}
|
||||||
@@ -328,7 +328,8 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
if (isNullOrEmpty(uuid)) return null;
|
if (isNullOrEmpty(uuid)) return null;
|
||||||
DuplexTransportConnection conn = connect(address, uuid);
|
DuplexTransportConnection conn = connect(address, uuid);
|
||||||
if (conn == null) return null;
|
if (conn == null) return null;
|
||||||
return connectionLimiter.contactConnectionOpened(conn) ? conn : null;
|
if (connectionLimiter.contactConnectionOpened(conn, false)) return conn;
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package org.briarproject.bramble.plugin.bluetooth;
|
package org.briarproject.bramble.plugin.bluetooth;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
|
||||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||||
import org.briarproject.bramble.api.sync.event.CloseSyncConnectionsEvent;
|
import org.briarproject.bramble.api.sync.event.CloseSyncConnectionsEvent;
|
||||||
import org.briarproject.bramble.api.system.Clock;
|
import org.briarproject.bramble.api.system.Clock;
|
||||||
@@ -29,10 +27,6 @@ public class BluetoothConnectionLimiterImplTest extends BrambleMockTestCase {
|
|||||||
context.mock(DuplexTransportConnection.class, "conn2");
|
context.mock(DuplexTransportConnection.class, "conn2");
|
||||||
private final DuplexTransportConnection conn3 =
|
private final DuplexTransportConnection conn3 =
|
||||||
context.mock(DuplexTransportConnection.class, "conn3");
|
context.mock(DuplexTransportConnection.class, "conn3");
|
||||||
private final TransportConnectionReader reader =
|
|
||||||
context.mock(TransportConnectionReader.class);
|
|
||||||
private final TransportConnectionWriter writer =
|
|
||||||
context.mock(TransportConnectionWriter.class);
|
|
||||||
|
|
||||||
private final long now = System.currentTimeMillis();
|
private final long now = System.currentTimeMillis();
|
||||||
|
|
||||||
@@ -61,35 +55,30 @@ public class BluetoothConnectionLimiterImplTest extends BrambleMockTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLimiterAllowsAttemptToRaiseLimitAtStartup()
|
public void testLimiterAllowsAttemptToRaiseLimitAtStartup() {
|
||||||
throws Exception {
|
|
||||||
// First outgoing connection is allowed - we're below the limit of 1
|
// First outgoing connection is allowed - we're below the limit of 1
|
||||||
assertTrue(limiter.canOpenContactConnection());
|
assertTrue(limiter.canOpenContactConnection());
|
||||||
assertTrue(limiter.contactConnectionOpened(conn1));
|
assertTrue(limiter.contactConnectionOpened(conn1, false));
|
||||||
|
|
||||||
// Second outgoing connection is allowed - it's time to try raising
|
// Second outgoing connection is allowed - it's time to try raising
|
||||||
// the limit to 2
|
// the limit to 2
|
||||||
assertTrue(limiter.canOpenContactConnection());
|
assertTrue(limiter.canOpenContactConnection());
|
||||||
assertTrue(limiter.contactConnectionOpened(conn2));
|
assertTrue(limiter.contactConnectionOpened(conn2, false));
|
||||||
|
|
||||||
// Third outgoing connection is not allowed - we're above the limit of 1
|
// Third outgoing connection is not allowed - we're above the limit of 1
|
||||||
assertFalse(limiter.canOpenContactConnection());
|
assertFalse(limiter.canOpenContactConnection());
|
||||||
|
|
||||||
// Third incoming connection is not allowed - we're above the limit of 1
|
|
||||||
expectCloseConnection(conn3);
|
|
||||||
assertFalse(limiter.contactConnectionOpened(conn3));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLimiterAllowsThirdConnectionAfterFirstTwoAreClosed() {
|
public void testLimiterAllowsThirdConnectionAfterFirstTwoAreClosed() {
|
||||||
// First outgoing connection is allowed - we're below the limit of 1
|
// First outgoing connection is allowed - we're below the limit of 1
|
||||||
assertTrue(limiter.canOpenContactConnection());
|
assertTrue(limiter.canOpenContactConnection());
|
||||||
assertTrue(limiter.contactConnectionOpened(conn1));
|
assertTrue(limiter.contactConnectionOpened(conn1, false));
|
||||||
|
|
||||||
// Second outgoing connection is allowed - it's time to try raising
|
// Second outgoing connection is allowed - it's time to try raising
|
||||||
// the limit to 2
|
// the limit to 2
|
||||||
assertTrue(limiter.canOpenContactConnection());
|
assertTrue(limiter.canOpenContactConnection());
|
||||||
assertTrue(limiter.contactConnectionOpened(conn2));
|
assertTrue(limiter.contactConnectionOpened(conn2, false));
|
||||||
|
|
||||||
// Third outgoing connection is not allowed - we're above the limit of 1
|
// Third outgoing connection is not allowed - we're above the limit of 1
|
||||||
assertFalse(limiter.canOpenContactConnection());
|
assertFalse(limiter.canOpenContactConnection());
|
||||||
@@ -105,19 +94,19 @@ public class BluetoothConnectionLimiterImplTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
// Third outgoing connection is allowed - we're below the limit of 1
|
// Third outgoing connection is allowed - we're below the limit of 1
|
||||||
assertTrue(limiter.canOpenContactConnection());
|
assertTrue(limiter.canOpenContactConnection());
|
||||||
assertTrue(limiter.contactConnectionOpened(conn3));
|
assertTrue(limiter.contactConnectionOpened(conn3, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLimiterRaisesLimitWhenConnectionsAreStable() {
|
public void testLimiterRaisesLimitWhenConnectionsAreStable() {
|
||||||
// First outgoing connection is allowed - we're below the limit of 1
|
// First outgoing connection is allowed - we're below the limit of 1
|
||||||
assertTrue(limiter.canOpenContactConnection());
|
assertTrue(limiter.canOpenContactConnection());
|
||||||
assertTrue(limiter.contactConnectionOpened(conn1));
|
assertTrue(limiter.contactConnectionOpened(conn1, false));
|
||||||
|
|
||||||
// Second outgoing connection is allowed - it's time to try raising
|
// Second outgoing connection is allowed - it's time to try raising
|
||||||
// the limit to 2
|
// the limit to 2
|
||||||
assertTrue(limiter.canOpenContactConnection());
|
assertTrue(limiter.canOpenContactConnection());
|
||||||
assertTrue(limiter.contactConnectionOpened(conn2));
|
assertTrue(limiter.contactConnectionOpened(conn2, false));
|
||||||
|
|
||||||
// Third outgoing connection is not allowed - we're above the limit of 1
|
// Third outgoing connection is not allowed - we're above the limit of 1
|
||||||
assertFalse(limiter.canOpenContactConnection());
|
assertFalse(limiter.canOpenContactConnection());
|
||||||
@@ -135,7 +124,7 @@ public class BluetoothConnectionLimiterImplTest extends BrambleMockTestCase {
|
|||||||
// Third outgoing connection is allowed - it's time to try raising
|
// Third outgoing connection is allowed - it's time to try raising
|
||||||
// the limit to 3
|
// the limit to 3
|
||||||
assertTrue(limiter.canOpenContactConnection());
|
assertTrue(limiter.canOpenContactConnection());
|
||||||
assertTrue(limiter.contactConnectionOpened(conn3));
|
assertTrue(limiter.contactConnectionOpened(conn3, false));
|
||||||
|
|
||||||
// Fourth outgoing connection is not allowed - we're above the limit
|
// Fourth outgoing connection is not allowed - we're above the limit
|
||||||
// of 2
|
// of 2
|
||||||
@@ -146,7 +135,7 @@ public class BluetoothConnectionLimiterImplTest extends BrambleMockTestCase {
|
|||||||
public void testLimiterIncreasesIntervalWhenConnectionFailsAboveLimit() {
|
public void testLimiterIncreasesIntervalWhenConnectionFailsAboveLimit() {
|
||||||
// First outgoing connection is allowed - we're below the limit of 1
|
// First outgoing connection is allowed - we're below the limit of 1
|
||||||
assertTrue(limiter.canOpenContactConnection());
|
assertTrue(limiter.canOpenContactConnection());
|
||||||
assertTrue(limiter.contactConnectionOpened(conn1));
|
assertTrue(limiter.contactConnectionOpened(conn1, false));
|
||||||
|
|
||||||
// Time passes
|
// Time passes
|
||||||
time.set(now + 1);
|
time.set(now + 1);
|
||||||
@@ -154,7 +143,7 @@ public class BluetoothConnectionLimiterImplTest extends BrambleMockTestCase {
|
|||||||
// Second outgoing connection is allowed - it's time to try raising
|
// Second outgoing connection is allowed - it's time to try raising
|
||||||
// the limit to 2
|
// the limit to 2
|
||||||
assertTrue(limiter.canOpenContactConnection());
|
assertTrue(limiter.canOpenContactConnection());
|
||||||
assertTrue(limiter.contactConnectionOpened(conn2));
|
assertTrue(limiter.contactConnectionOpened(conn2, false));
|
||||||
|
|
||||||
// Time passes - the first connection is stable, the second isn't
|
// Time passes - the first connection is stable, the second isn't
|
||||||
time.set(now + STABILITY_PERIOD_MS);
|
time.set(now + STABILITY_PERIOD_MS);
|
||||||
@@ -181,7 +170,7 @@ public class BluetoothConnectionLimiterImplTest extends BrambleMockTestCase {
|
|||||||
// Third outgoing connection is allowed - it's time to try raising the
|
// Third outgoing connection is allowed - it's time to try raising the
|
||||||
// limit to 2 again
|
// limit to 2 again
|
||||||
assertTrue(limiter.canOpenContactConnection());
|
assertTrue(limiter.canOpenContactConnection());
|
||||||
assertTrue(limiter.contactConnectionOpened(conn3));
|
assertTrue(limiter.contactConnectionOpened(conn3, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void expectCloseSyncConnectionsEvent() {
|
private void expectCloseSyncConnectionsEvent() {
|
||||||
@@ -190,16 +179,4 @@ public class BluetoothConnectionLimiterImplTest extends BrambleMockTestCase {
|
|||||||
CloseSyncConnectionsEvent.class)));
|
CloseSyncConnectionsEvent.class)));
|
||||||
}});
|
}});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void expectCloseConnection(DuplexTransportConnection conn)
|
|
||||||
throws Exception {
|
|
||||||
context.checking(new Expectations() {{
|
|
||||||
oneOf(conn).getReader();
|
|
||||||
will(returnValue(reader));
|
|
||||||
oneOf(reader).dispose(false, false);
|
|
||||||
oneOf(conn).getWriter();
|
|
||||||
will(returnValue(writer));
|
|
||||||
oneOf(writer).dispose(false);
|
|
||||||
}});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user