Commit 1f921753 authored by Torsten Grote's avatar Torsten Grote

Merge branch '1567-rendezvous-poller' into 'master'

Create poller for rendezvous connections

Closes #1567

See merge request briar/briar!1121
parents 157b64e6 7439e557
Pipeline #3459 canceled with stage
......@@ -22,4 +22,11 @@ public class NullSafety {
@Nullable Object b) {
if ((a == null) == (b == null)) throw new AssertionError();
}
/**
* Checks that the argument is null.
*/
public static void requireNull(@Nullable Object o) {
if (o != null) throw new AssertionError();
}
}
......@@ -3,10 +3,11 @@ package org.briarproject.bramble.api.plugin.duplex;
import org.briarproject.bramble.api.data.BdfList;
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.ConnectionHandler;
import org.briarproject.bramble.api.plugin.Plugin;
import org.briarproject.bramble.api.properties.TransportProperties;
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
import org.briarproject.bramble.api.rendezvous.RendezvousHandler;
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
import javax.annotation.Nullable;
......@@ -49,8 +50,12 @@ public interface DuplexPlugin extends Plugin {
boolean supportsRendezvous();
/**
* Creates and returns a handler that uses the given key material to
* rendezvous with a pending contact.
* Creates and returns an endpoint that uses the given key material to
* rendezvous with a pending contact, and the given connection handler to
* handle incoming connections. Returns null if an endpoint cannot be
* created.
*/
RendezvousHandler createRendezvousHandler(KeyMaterialSource k);
@Nullable
RendezvousEndpoint createRendezvousEndpoint(KeyMaterialSource k,
boolean alice, ConnectionHandler incoming);
}
package org.briarproject.bramble.api.rendezvous;
public interface RendezvousConstants {
/**
* Label for deriving key material from the master key.
*/
String KEY_MATERIAL_LABEL =
"org.briarproject.bramble.rendezvous/KEY_MATERIAL";
}
......@@ -2,13 +2,14 @@ package org.briarproject.bramble.api.rendezvous;
import org.briarproject.bramble.api.properties.TransportProperties;
import java.io.Closeable;
import java.io.IOException;
/**
* An interface for making and accepting rendezvous connections with a pending
* contact over a given transport.
*/
public interface RendezvousHandler {
public interface RendezvousEndpoint extends Closeable {
/**
* Returns a set of transport properties for connecting to the pending
......@@ -20,5 +21,6 @@ public interface RendezvousHandler {
* Closes the handler and releases any resources held by it, such as
* network sockets.
*/
@Override
void close() throws IOException;
}
package org.briarproject.bramble.api.rendezvous.event;
import org.briarproject.bramble.api.contact.PendingContactId;
import org.briarproject.bramble.api.event.Event;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import javax.annotation.concurrent.Immutable;
/**
* An event that is broadcast when a rendezvous with a pending contact fails.
*/
@Immutable
@NotNullByDefault
public class RendezvousFailedEvent extends Event {
private final PendingContactId pendingContactId;
public RendezvousFailedEvent(PendingContactId pendingContactId) {
this.pendingContactId = pendingContactId;
}
public PendingContactId getPendingContactId() {
return pendingContactId;
}
}
......@@ -7,6 +7,7 @@ import org.briarproject.bramble.identity.IdentityModule;
import org.briarproject.bramble.lifecycle.LifecycleModule;
import org.briarproject.bramble.plugin.PluginModule;
import org.briarproject.bramble.properties.PropertiesModule;
import org.briarproject.bramble.rendezvous.RendezvousModule;
import org.briarproject.bramble.sync.validation.ValidationModule;
import org.briarproject.bramble.system.SystemModule;
import org.briarproject.bramble.transport.TransportModule;
......@@ -28,6 +29,8 @@ public interface BrambleCoreEagerSingletons {
void inject(PropertiesModule.EagerSingletons init);
void inject(RendezvousModule.EagerSingletons init);
void inject(SystemModule.EagerSingletons init);
void inject(TransportModule.EagerSingletons init);
......@@ -42,6 +45,7 @@ public interface BrambleCoreEagerSingletons {
inject(new DatabaseExecutorModule.EagerSingletons());
inject(new IdentityModule.EagerSingletons());
inject(new LifecycleModule.EagerSingletons());
inject(new RendezvousModule.EagerSingletons());
inject(new PluginModule.EagerSingletons());
inject(new PropertiesModule.EagerSingletons());
inject(new SystemModule.EagerSingletons());
......
......@@ -23,7 +23,7 @@ import org.briarproject.bramble.api.plugin.event.DisableBluetoothEvent;
import org.briarproject.bramble.api.plugin.event.EnableBluetoothEvent;
import org.briarproject.bramble.api.properties.TransportProperties;
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
import org.briarproject.bramble.api.rendezvous.RendezvousHandler;
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
import org.briarproject.bramble.api.settings.Settings;
import org.briarproject.bramble.api.settings.event.SettingsUpdatedEvent;
......@@ -398,7 +398,8 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
}
@Override
public RendezvousHandler createRendezvousHandler(KeyMaterialSource k) {
public RendezvousEndpoint createRendezvousEndpoint(KeyMaterialSource k,
boolean alice, ConnectionHandler incoming) {
throw new UnsupportedOperationException();
}
......
......@@ -13,7 +13,7 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
import org.briarproject.bramble.api.properties.TransportProperties;
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
import org.briarproject.bramble.api.rendezvous.RendezvousHandler;
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
import org.briarproject.bramble.util.IoUtils;
import java.io.IOException;
......@@ -309,7 +309,8 @@ abstract class TcpPlugin implements DuplexPlugin {
}
@Override
public RendezvousHandler createRendezvousHandler(KeyMaterialSource k) {
public RendezvousEndpoint createRendezvousEndpoint(KeyMaterialSource k,
boolean alice, ConnectionHandler incoming) {
throw new UnsupportedOperationException();
}
......
......@@ -26,7 +26,7 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
import org.briarproject.bramble.api.properties.TransportProperties;
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
import org.briarproject.bramble.api.rendezvous.RendezvousHandler;
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
import org.briarproject.bramble.api.settings.Settings;
import org.briarproject.bramble.api.settings.event.SettingsUpdatedEvent;
import org.briarproject.bramble.api.system.Clock;
......@@ -613,7 +613,8 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
}
@Override
public RendezvousHandler createRendezvousHandler(KeyMaterialSource k) {
public RendezvousEndpoint createRendezvousEndpoint(KeyMaterialSource k,
boolean alice, ConnectionHandler incoming) {
throw new UnsupportedOperationException();
}
......
package org.briarproject.bramble.rendezvous;
import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.MINUTES;
interface RendezvousConstants {
/**
* The current version of the rendezvous protocol.
*/
byte PROTOCOL_VERSION = 0;
/**
* How long to try to rendezvous with a pending contact before giving up.
*/
long RENDEZVOUS_TIMEOUT_MS = DAYS.toMillis(2);
/**
* How often to try to rendezvous with pending contacts.
*/
long POLLING_INTERVAL_MS = MINUTES.toMillis(1);
/**
* Label for deriving the rendezvous key from the static master key.
*/
String RENDEZVOUS_KEY_LABEL =
"org.briarproject.bramble.rendezvous/RENDEZVOUS_KEY";
/**
* Label for deriving key material from the rendezvous key.
*/
String KEY_MATERIAL_LABEL =
"org.briarproject.bramble.rendezvous/KEY_MATERIAL";
}
package org.briarproject.bramble.api.rendezvous;
package org.briarproject.bramble.rendezvous;
import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
@NotNullByDefault
public interface RendezvousCrypto {
interface RendezvousCrypto {
KeyMaterialSource createKeyMaterialSource(SecretKey masterKey,
SecretKey deriveRendezvousKey(SecretKey staticMasterKey);
KeyMaterialSource createKeyMaterialSource(SecretKey rendezvousKey,
TransportId t);
}
......@@ -5,12 +5,13 @@ import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
import org.briarproject.bramble.api.rendezvous.RendezvousCrypto;
import javax.annotation.concurrent.Immutable;
import javax.inject.Inject;
import static org.briarproject.bramble.api.rendezvous.RendezvousConstants.KEY_MATERIAL_LABEL;
import static org.briarproject.bramble.rendezvous.RendezvousConstants.KEY_MATERIAL_LABEL;
import static org.briarproject.bramble.rendezvous.RendezvousConstants.PROTOCOL_VERSION;
import static org.briarproject.bramble.rendezvous.RendezvousConstants.RENDEZVOUS_KEY_LABEL;
import static org.briarproject.bramble.util.StringUtils.toUtf8;
@Immutable
......@@ -25,10 +26,16 @@ class RendezvousCryptoImpl implements RendezvousCrypto {
}
@Override
public KeyMaterialSource createKeyMaterialSource(SecretKey masterKey,
public SecretKey deriveRendezvousKey(SecretKey staticMasterKey) {
return crypto.deriveKey(RENDEZVOUS_KEY_LABEL, staticMasterKey,
new byte[] {PROTOCOL_VERSION});
}
@Override
public KeyMaterialSource createKeyMaterialSource(SecretKey rendezvousKey,
TransportId t) {
SecretKey sourceKey = crypto.deriveKey(KEY_MATERIAL_LABEL, masterKey,
toUtf8(t.getString()));
SecretKey sourceKey = crypto.deriveKey(KEY_MATERIAL_LABEL,
rendezvousKey, toUtf8(t.getString()));
return new KeyMaterialSourceImpl(sourceKey);
}
}
package org.briarproject.bramble.rendezvous;
import org.briarproject.bramble.api.rendezvous.RendezvousCrypto;
import org.briarproject.bramble.api.event.EventBus;
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import javax.inject.Inject;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
......@@ -8,9 +12,23 @@ import dagger.Provides;
@Module
public class RendezvousModule {
public static class EagerSingletons {
@Inject
RendezvousPoller rendezvousPoller;
}
@Provides
RendezvousCrypto provideRendezvousCrypto(
RendezvousCryptoImpl rendezvousCrypto) {
return rendezvousCrypto;
}
@Provides
@Singleton
RendezvousPoller provideRendezvousPoller(LifecycleManager lifecycleManager,
EventBus eventBus, RendezvousPollerImpl rendezvousPoller) {
lifecycleManager.registerService(rendezvousPoller);
eventBus.addListener(rendezvousPoller);
return rendezvousPoller;
}
}
package org.briarproject.bramble.rendezvous;
/**
* Empty interface for injecting the rendezvous poller.
*/
interface RendezvousPoller {
}
......@@ -7,7 +7,7 @@ import org.jmock.api.Invocation;
public class RunAction implements Action {
@Override
public Object invoke(Invocation invocation) throws Throwable {
public Object invoke(Invocation invocation) {
Runnable task = (Runnable) invocation.getParameter(0);
task.run();
return null;
......
package org.briarproject.bramble.test;
import org.briarproject.bramble.api.db.CommitAction;
import org.briarproject.bramble.api.db.DbRunnable;
import org.briarproject.bramble.api.db.TaskAction;
import org.briarproject.bramble.api.db.Transaction;
import org.hamcrest.Description;
import org.jmock.api.Action;
import org.jmock.api.Invocation;
public class RunTransactionAction implements Action {
class RunTransactionAction implements Action {
private final Transaction txn;
@SuppressWarnings("WeakerAccess")
public RunTransactionAction(Transaction txn) {
RunTransactionAction(Transaction txn) {
this.txn = txn;
}
......@@ -19,6 +20,10 @@ public class RunTransactionAction implements Action {
public Object invoke(Invocation invocation) throws Throwable {
DbRunnable task = (DbRunnable) invocation.getParameter(1);
task.run(txn);
for (CommitAction action : txn.getActions()) {
if (action instanceof TaskAction)
((TaskAction) action).getTask().run();
}
return null;
}
......@@ -26,5 +31,4 @@ public class RunTransactionAction implements Action {
public void describeTo(Description description) {
description.appendText("runs a task inside a database transaction");
}
}
package org.briarproject.bramble.test;
import org.briarproject.bramble.api.db.CommitAction;
import org.briarproject.bramble.api.db.NullableDbCallable;
import org.briarproject.bramble.api.db.TaskAction;
import org.briarproject.bramble.api.db.Transaction;
import org.hamcrest.Description;
import org.jmock.api.Action;
import org.jmock.api.Invocation;
public class RunTransactionWithNullableResultAction implements Action {
class RunTransactionWithNullableResultAction implements Action {
private final Transaction txn;
public RunTransactionWithNullableResultAction(Transaction txn) {
RunTransactionWithNullableResultAction(Transaction txn) {
this.txn = txn;
}
......@@ -18,7 +20,12 @@ public class RunTransactionWithNullableResultAction implements Action {
public Object invoke(Invocation invocation) throws Throwable {
NullableDbCallable task =
(NullableDbCallable) invocation.getParameter(1);
return task.call(txn);
Object result = task.call(txn);
for (CommitAction action : txn.getActions()) {
if (action instanceof TaskAction)
((TaskAction) action).getTask().run();
}
return result;
}
@Override
......
package org.briarproject.bramble.test;
import org.briarproject.bramble.api.db.CommitAction;
import org.briarproject.bramble.api.db.DbCallable;
import org.briarproject.bramble.api.db.TaskAction;
import org.briarproject.bramble.api.db.Transaction;
import org.hamcrest.Description;
import org.jmock.api.Action;
import org.jmock.api.Invocation;
public class RunTransactionWithResultAction implements Action {
class RunTransactionWithResultAction implements Action {
private final Transaction txn;
public RunTransactionWithResultAction(Transaction txn) {
RunTransactionWithResultAction(Transaction txn) {
this.txn = txn;
}
@Override
public Object invoke(Invocation invocation) throws Throwable {
DbCallable task = (DbCallable) invocation.getParameter(1);
return task.call(txn);
Object result = task.call(txn);
for (CommitAction action : txn.getActions()) {
if (action instanceof TaskAction)
((TaskAction) action).getTask().run();
}
return result;
}
@Override
......
......@@ -14,7 +14,7 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
import org.briarproject.bramble.api.properties.TransportProperties;
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
import org.briarproject.bramble.api.rendezvous.RendezvousHandler;
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
import java.io.IOException;
import java.io.InputStream;
......@@ -192,7 +192,8 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
}
@Override
public RendezvousHandler createRendezvousHandler(KeyMaterialSource k) {
public RendezvousEndpoint createRendezvousEndpoint(KeyMaterialSource k,
boolean alice, ConnectionHandler incoming) {
throw new UnsupportedOperationException();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment