Only Alice should perform Bluetooth discovery.

This commit is contained in:
akwizgran
2020-10-27 14:22:30 +00:00
parent 8cbb38ee68
commit 922a52bf83
8 changed files with 26 additions and 15 deletions

View File

@@ -41,10 +41,12 @@ public interface DuplexPlugin extends Plugin {
/**
* Attempts to connect to the remote peer specified in the given descriptor.
* Returns null if no connection can be established.
*
* @param alice True if the local party is Alice
*/
@Nullable
DuplexTransportConnection createKeyAgreementConnection(
byte[] remoteCommitment, BdfList descriptor);
byte[] remoteCommitment, BdfList descriptor, boolean alice);
/**
* Returns true if the plugin supports rendezvous connections.

View File

@@ -118,8 +118,8 @@ class KeyAgreementConnector {
DuplexPlugin plugin = (DuplexPlugin) p;
byte[] commitment = remotePayload.getCommitment();
BdfList descriptor = d.getDescriptor();
connectionChooser.submit(new ReadableTask(
new ConnectorTask(plugin, commitment, descriptor)));
connectionChooser.submit(new ReadableTask(new ConnectorTask(
plugin, commitment, descriptor, alice)));
}
}
@@ -148,15 +148,17 @@ class KeyAgreementConnector {
private class ConnectorTask implements Callable<KeyAgreementConnection> {
private final DuplexPlugin plugin;
private final byte[] commitment;
private final BdfList descriptor;
private final DuplexPlugin plugin;
private final boolean alice;
private ConnectorTask(DuplexPlugin plugin, byte[] commitment,
BdfList descriptor) {
BdfList descriptor, boolean alice) {
this.plugin = plugin;
this.commitment = commitment;
this.descriptor = descriptor;
this.alice = alice;
}
@Nullable
@@ -166,7 +168,7 @@ class KeyAgreementConnector {
while (!stopped) {
DuplexTransportConnection conn =
plugin.createKeyAgreementConnection(commitment,
descriptor);
descriptor, alice);
if (conn != null) {
if (LOG.isLoggable(INFO))
LOG.info(plugin.getId() + ": Outgoing connection");

View File

@@ -430,15 +430,22 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
@Override
public DuplexTransportConnection createKeyAgreementConnection(
byte[] commitment, BdfList descriptor) {
byte[] commitment, BdfList descriptor, boolean alice) {
if (getState() != ACTIVE) return null;
// No truncation necessary because COMMIT_LENGTH = 16
String uuid = UUID.nameUUIDFromBytes(commitment).toString();
DuplexTransportConnection conn;
if (descriptor.size() == 1) {
if (LOG.isLoggable(INFO))
LOG.info("Discovering address for key agreement UUID " + uuid);
conn = discoverAndConnect(uuid);
if (alice) {
if (LOG.isLoggable(INFO)) {
LOG.info("Discovering address for key agreement UUID " +
uuid);
}
conn = discoverAndConnect(uuid);
} else {
LOG.info("No address in key agreement descriptor");
return null;
}
} else {
String address;
try {

View File

@@ -376,7 +376,7 @@ class LanTcpPlugin extends TcpPlugin {
@Override
public DuplexTransportConnection createKeyAgreementConnection(
byte[] commitment, BdfList descriptor) {
byte[] commitment, BdfList descriptor, boolean alice) {
ServerSocket ss = state.getServerSocket(true);
if (ss == null) return null;
InterfaceAddress local = getLocalInterfaceAddress(ss.getInetAddress());

View File

@@ -367,7 +367,7 @@ abstract class TcpPlugin implements DuplexPlugin, EventListener {
@Override
public DuplexTransportConnection createKeyAgreementConnection(
byte[] commitment, BdfList descriptor) {
byte[] commitment, BdfList descriptor, boolean alice) {
throw new UnsupportedOperationException();
}

View File

@@ -708,7 +708,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
@Override
public DuplexTransportConnection createKeyAgreementConnection(
byte[] commitment, BdfList descriptor) {
byte[] commitment, BdfList descriptor, boolean alice) {
throw new UnsupportedOperationException();
}

View File

@@ -276,7 +276,7 @@ public class LanTcpPluginTest extends BrambleTestCase {
descriptor.add(local.getPort());
// Connect to the port
DuplexTransportConnection d = plugin.createKeyAgreementConnection(
new byte[COMMIT_LENGTH], descriptor);
new byte[COMMIT_LENGTH], descriptor, true);
assertNotNull(d);
// Check that the connection was accepted
assertTrue(latch.await(5, SECONDS));

View File

@@ -198,7 +198,7 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
@Override
public DuplexTransportConnection createKeyAgreementConnection(
byte[] commitment, BdfList descriptor) {
byte[] commitment, BdfList descriptor, boolean alice) {
throw new UnsupportedOperationException();
}