mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 21:59:54 +01:00
Added null safety annotations to plugin interfaces.
This commit is contained in:
@@ -18,6 +18,8 @@ import org.briarproject.api.crypto.PseudoRandom;
|
|||||||
import org.briarproject.api.data.BdfList;
|
import org.briarproject.api.data.BdfList;
|
||||||
import org.briarproject.api.keyagreement.KeyAgreementConnection;
|
import org.briarproject.api.keyagreement.KeyAgreementConnection;
|
||||||
import org.briarproject.api.keyagreement.KeyAgreementListener;
|
import org.briarproject.api.keyagreement.KeyAgreementListener;
|
||||||
|
import org.briarproject.api.nullsafety.MethodsNotNullByDefault;
|
||||||
|
import org.briarproject.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.api.plugins.Backoff;
|
import org.briarproject.api.plugins.Backoff;
|
||||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||||
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
||||||
@@ -25,6 +27,7 @@ import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
|||||||
import org.briarproject.api.properties.TransportProperties;
|
import org.briarproject.api.properties.TransportProperties;
|
||||||
import org.briarproject.util.StringUtils;
|
import org.briarproject.util.StringUtils;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
@@ -63,6 +66,8 @@ import static java.util.logging.Level.WARNING;
|
|||||||
import static org.briarproject.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_BLUETOOTH;
|
import static org.briarproject.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_BLUETOOTH;
|
||||||
import static org.briarproject.util.PrivacyUtils.scrubMacAddress;
|
import static org.briarproject.util.PrivacyUtils.scrubMacAddress;
|
||||||
|
|
||||||
|
@MethodsNotNullByDefault
|
||||||
|
@ParametersNotNullByDefault
|
||||||
class DroidtoothPlugin implements DuplexPlugin {
|
class DroidtoothPlugin implements DuplexPlugin {
|
||||||
|
|
||||||
// Share an ID with the J2SE Bluetooth plugin
|
// Share an ID with the J2SE Bluetooth plugin
|
||||||
@@ -221,7 +226,7 @@ class DroidtoothPlugin implements DuplexPlugin {
|
|||||||
return UUID.fromString(uuid);
|
return UUID.fromString(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tryToClose(BluetoothServerSocket ss) {
|
private void tryToClose(@Nullable BluetoothServerSocket ss) {
|
||||||
try {
|
try {
|
||||||
if (ss != null) ss.close();
|
if (ss != null) ss.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -344,9 +349,9 @@ class DroidtoothPlugin implements DuplexPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tryToClose(BluetoothSocket s) {
|
private void tryToClose(@Nullable Closeable c) {
|
||||||
try {
|
try {
|
||||||
if (s != null) s.close();
|
if (c != null) c.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||||
}
|
}
|
||||||
@@ -424,7 +429,7 @@ class DroidtoothPlugin implements DuplexPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void closeSockets(final List<Future<BluetoothSocket>> futures,
|
private void closeSockets(final List<Future<BluetoothSocket>> futures,
|
||||||
final BluetoothSocket chosen) {
|
@Nullable final BluetoothSocket chosen) {
|
||||||
ioExecutor.execute(new Runnable() {
|
ioExecutor.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -458,6 +463,9 @@ class DroidtoothPlugin implements DuplexPlugin {
|
|||||||
@Override
|
@Override
|
||||||
public KeyAgreementListener createKeyAgreementListener(byte[] commitment) {
|
public KeyAgreementListener createKeyAgreementListener(byte[] commitment) {
|
||||||
if (!isRunning()) return null;
|
if (!isRunning()) return null;
|
||||||
|
// There's no point listening if we can't discover our own address
|
||||||
|
String address = AndroidUtils.getBluetoothAddress(appContext, adapter);
|
||||||
|
if (address.isEmpty()) return null;
|
||||||
// No truncation necessary because COMMIT_LENGTH = 16
|
// No truncation necessary because COMMIT_LENGTH = 16
|
||||||
UUID uuid = UUID.nameUUIDFromBytes(commitment);
|
UUID uuid = UUID.nameUUIDFromBytes(commitment);
|
||||||
if (LOG.isLoggable(INFO)) LOG.info("Key agreement UUID " + uuid);
|
if (LOG.isLoggable(INFO)) LOG.info("Key agreement UUID " + uuid);
|
||||||
@@ -472,8 +480,7 @@ class DroidtoothPlugin implements DuplexPlugin {
|
|||||||
}
|
}
|
||||||
BdfList descriptor = new BdfList();
|
BdfList descriptor = new BdfList();
|
||||||
descriptor.add(TRANSPORT_ID_BLUETOOTH);
|
descriptor.add(TRANSPORT_ID_BLUETOOTH);
|
||||||
String address = AndroidUtils.getBluetoothAddress(appContext, adapter);
|
descriptor.add(StringUtils.macToBytes(address));
|
||||||
if (!address.isEmpty()) descriptor.add(StringUtils.macToBytes(address));
|
|
||||||
return new BluetoothKeyAgreementListener(descriptor, ss);
|
return new BluetoothKeyAgreementListener(descriptor, ss);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -488,7 +495,6 @@ class DroidtoothPlugin implements DuplexPlugin {
|
|||||||
LOG.info("Invalid address in key agreement descriptor");
|
LOG.info("Invalid address in key agreement descriptor");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (address == null) return null;
|
|
||||||
// No truncation necessary because COMMIT_LENGTH = 16
|
// No truncation necessary because COMMIT_LENGTH = 16
|
||||||
UUID uuid = UUID.nameUUIDFromBytes(commitment);
|
UUID uuid = UUID.nameUUIDFromBytes(commitment);
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
@@ -498,9 +504,7 @@ class DroidtoothPlugin implements DuplexPlugin {
|
|||||||
return new DroidtoothTransportConnection(this, s);
|
return new DroidtoothTransportConnection(this, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private String parseAddress(BdfList descriptor) throws FormatException {
|
private String parseAddress(BdfList descriptor) throws FormatException {
|
||||||
if (descriptor.size() < 2) return null;
|
|
||||||
byte[] mac = descriptor.getRaw(1);
|
byte[] mac = descriptor.getRaw(1);
|
||||||
if (mac.length != 6) throw new FormatException();
|
if (mac.length != 6) throw new FormatException();
|
||||||
return StringUtils.macToString(mac);
|
return StringUtils.macToString(mac);
|
||||||
|
|||||||
@@ -7,16 +7,20 @@ import android.content.IntentFilter;
|
|||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.net.NetworkInfo;
|
import android.net.NetworkInfo;
|
||||||
|
|
||||||
|
import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.api.plugins.Backoff;
|
import org.briarproject.api.plugins.Backoff;
|
||||||
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import static android.content.Context.CONNECTIVITY_SERVICE;
|
import static android.content.Context.CONNECTIVITY_SERVICE;
|
||||||
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
|
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
|
||||||
import static android.net.ConnectivityManager.TYPE_WIFI;
|
import static android.net.ConnectivityManager.TYPE_WIFI;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
class AndroidLanTcpPlugin extends LanTcpPlugin {
|
class AndroidLanTcpPlugin extends LanTcpPlugin {
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
@@ -24,6 +28,7 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
|
|||||||
|
|
||||||
private final Context appContext;
|
private final Context appContext;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private volatile BroadcastReceiver networkStateReceiver = null;
|
private volatile BroadcastReceiver networkStateReceiver = null;
|
||||||
|
|
||||||
AndroidLanTcpPlugin(Executor ioExecutor, Backoff backoff,
|
AndroidLanTcpPlugin(Executor ioExecutor, Backoff backoff,
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import org.briarproject.api.event.Event;
|
|||||||
import org.briarproject.api.event.EventListener;
|
import org.briarproject.api.event.EventListener;
|
||||||
import org.briarproject.api.event.SettingsUpdatedEvent;
|
import org.briarproject.api.event.SettingsUpdatedEvent;
|
||||||
import org.briarproject.api.keyagreement.KeyAgreementListener;
|
import org.briarproject.api.keyagreement.KeyAgreementListener;
|
||||||
|
import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.api.plugins.Backoff;
|
import org.briarproject.api.plugins.Backoff;
|
||||||
import org.briarproject.api.plugins.TorConstants;
|
import org.briarproject.api.plugins.TorConstants;
|
||||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||||
@@ -60,6 +61,7 @@ import java.util.logging.Logger;
|
|||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.zip.ZipInputStream;
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import javax.net.SocketFactory;
|
import javax.net.SocketFactory;
|
||||||
|
|
||||||
import static android.content.Context.CONNECTIVITY_SERVICE;
|
import static android.content.Context.CONNECTIVITY_SERVICE;
|
||||||
@@ -76,6 +78,7 @@ import static net.freehaven.tor.control.TorControlCommands.HS_PRIVKEY;
|
|||||||
import static org.briarproject.api.plugins.TorConstants.CONTROL_PORT;
|
import static org.briarproject.api.plugins.TorConstants.CONTROL_PORT;
|
||||||
import static org.briarproject.util.PrivacyUtils.scrubOnion;
|
import static org.briarproject.util.PrivacyUtils.scrubOnion;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||||
|
|
||||||
private static final String PROP_ONION = "onion";
|
private static final String PROP_ONION = "onion";
|
||||||
@@ -104,9 +107,13 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
private final AtomicBoolean used = new AtomicBoolean(false);
|
private final AtomicBoolean used = new AtomicBoolean(false);
|
||||||
|
|
||||||
private volatile boolean running = false;
|
private volatile boolean running = false;
|
||||||
|
@Nullable
|
||||||
private volatile ServerSocket socket = null;
|
private volatile ServerSocket socket = null;
|
||||||
|
@Nullable
|
||||||
private volatile Socket controlSocket = null;
|
private volatile Socket controlSocket = null;
|
||||||
|
@Nullable
|
||||||
private volatile TorControlConnection controlConnection = null;
|
private volatile TorControlConnection controlConnection = null;
|
||||||
|
@Nullable
|
||||||
private volatile BroadcastReceiver networkStateReceiver = null;
|
private volatile BroadcastReceiver networkStateReceiver = null;
|
||||||
|
|
||||||
TorPlugin(Executor ioExecutor, Context appContext,
|
TorPlugin(Executor ioExecutor, Context appContext,
|
||||||
@@ -289,7 +296,7 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
return appContext.getResources().getAssets().open("torrc");
|
return appContext.getResources().getAssets().open("torrc");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tryToClose(Closeable c) {
|
private void tryToClose(@Nullable Closeable c) {
|
||||||
try {
|
try {
|
||||||
if (c != null) c.close();
|
if (c != null) c.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -297,7 +304,7 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tryToClose(Socket s) {
|
private void tryToClose(@Nullable Socket s) {
|
||||||
try {
|
try {
|
||||||
if (s != null) s.close();
|
if (s != null) s.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -385,7 +392,7 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tryToClose(ServerSocket ss) {
|
private void tryToClose(@Nullable ServerSocket ss) {
|
||||||
try {
|
try {
|
||||||
if (ss != null) ss.close();
|
if (ss != null) ss.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
@@ -2,28 +2,42 @@ package org.briarproject.api.plugins;
|
|||||||
|
|
||||||
import org.briarproject.api.TransportId;
|
import org.briarproject.api.TransportId;
|
||||||
import org.briarproject.api.contact.ContactId;
|
import org.briarproject.api.contact.ContactId;
|
||||||
|
import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
public interface Plugin {
|
public interface Plugin {
|
||||||
|
|
||||||
/** Returns the plugin's transport identifier. */
|
/**
|
||||||
|
* Returns the plugin's transport identifier.
|
||||||
|
*/
|
||||||
TransportId getId();
|
TransportId getId();
|
||||||
|
|
||||||
/** Returns the transport's maximum latency in milliseconds. */
|
/**
|
||||||
|
* Returns the transport's maximum latency in milliseconds.
|
||||||
|
*/
|
||||||
int getMaxLatency();
|
int getMaxLatency();
|
||||||
|
|
||||||
/** Returns the transport's maximum idle time in milliseconds. */
|
/**
|
||||||
|
* Returns the transport's maximum idle time in milliseconds.
|
||||||
|
*/
|
||||||
int getMaxIdleTime();
|
int getMaxIdleTime();
|
||||||
|
|
||||||
/** Starts the plugin and returns true if it started successfully. */
|
/**
|
||||||
|
* Starts the plugin and returns true if it started successfully.
|
||||||
|
*/
|
||||||
boolean start() throws IOException;
|
boolean start() throws IOException;
|
||||||
|
|
||||||
/** Stops the plugin. */
|
/**
|
||||||
|
* Stops the plugin.
|
||||||
|
*/
|
||||||
void stop() throws IOException;
|
void stop() throws IOException;
|
||||||
|
|
||||||
/** Returns true if the plugin is running. */
|
/**
|
||||||
|
* Returns true if the plugin is running.
|
||||||
|
*/
|
||||||
boolean isRunning();
|
boolean isRunning();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -4,19 +4,28 @@ import org.briarproject.api.contact.ContactId;
|
|||||||
import org.briarproject.api.crypto.PseudoRandom;
|
import org.briarproject.api.crypto.PseudoRandom;
|
||||||
import org.briarproject.api.data.BdfList;
|
import org.briarproject.api.data.BdfList;
|
||||||
import org.briarproject.api.keyagreement.KeyAgreementListener;
|
import org.briarproject.api.keyagreement.KeyAgreementListener;
|
||||||
|
import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.api.plugins.Plugin;
|
import org.briarproject.api.plugins.Plugin;
|
||||||
|
|
||||||
/** An interface for transport plugins that support duplex communication. */
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An interface for transport plugins that support duplex communication.
|
||||||
|
*/
|
||||||
|
@NotNullByDefault
|
||||||
public interface DuplexPlugin extends Plugin {
|
public interface DuplexPlugin extends Plugin {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to create and return a connection to the given contact using
|
* Attempts to create and return a connection to the given contact using
|
||||||
* the current transport and configuration properties. Returns null if a
|
* the current transport and configuration properties. Returns null if a
|
||||||
* connection could not be created.
|
* connection cannot be created.
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
DuplexTransportConnection createConnection(ContactId c);
|
DuplexTransportConnection createConnection(ContactId c);
|
||||||
|
|
||||||
/** Returns true if the plugin supports exchanging invitations. */
|
/**
|
||||||
|
* Returns true if the plugin supports exchanging invitations.
|
||||||
|
*/
|
||||||
boolean supportsInvitations();
|
boolean supportsInvitations();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -24,21 +33,27 @@ public interface DuplexPlugin extends Plugin {
|
|||||||
* peer. Returns null if no connection can be established within the given
|
* peer. Returns null if no connection can be established within the given
|
||||||
* time.
|
* time.
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
DuplexTransportConnection createInvitationConnection(PseudoRandom r,
|
DuplexTransportConnection createInvitationConnection(PseudoRandom r,
|
||||||
long timeout, boolean alice);
|
long timeout, boolean alice);
|
||||||
|
|
||||||
/** Returns true if the plugin supports short-range key agreement. */
|
/**
|
||||||
|
* Returns true if the plugin supports short-range key agreement.
|
||||||
|
*/
|
||||||
boolean supportsKeyAgreement();
|
boolean supportsKeyAgreement();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a listener that can be used to perform key agreement.
|
* Attempts to create and return a listener that can be used to perform key
|
||||||
|
* agreement. Returns null if a listener cannot be created.
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
KeyAgreementListener createKeyAgreementListener(byte[] localCommitment);
|
KeyAgreementListener createKeyAgreementListener(byte[] localCommitment);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to connect to the remote peer specified in the given descriptor.
|
* Attempts to connect to the remote peer specified in the given descriptor.
|
||||||
* Returns null if no connection can be established within the given time.
|
* Returns null if no connection can be established within the given time.
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
DuplexTransportConnection createKeyAgreementConnection(
|
DuplexTransportConnection createKeyAgreementConnection(
|
||||||
byte[] remoteCommitment, BdfList descriptor, long timeout);
|
byte[] remoteCommitment, BdfList descriptor, long timeout);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,32 @@
|
|||||||
package org.briarproject.api.plugins.simplex;
|
package org.briarproject.api.plugins.simplex;
|
||||||
|
|
||||||
import org.briarproject.api.contact.ContactId;
|
import org.briarproject.api.contact.ContactId;
|
||||||
|
import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.api.plugins.Plugin;
|
import org.briarproject.api.plugins.Plugin;
|
||||||
import org.briarproject.api.plugins.TransportConnectionReader;
|
import org.briarproject.api.plugins.TransportConnectionReader;
|
||||||
import org.briarproject.api.plugins.TransportConnectionWriter;
|
import org.briarproject.api.plugins.TransportConnectionWriter;
|
||||||
|
|
||||||
/** An interface for transport plugins that support simplex communication. */
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An interface for transport plugins that support simplex communication.
|
||||||
|
*/
|
||||||
|
@NotNullByDefault
|
||||||
public interface SimplexPlugin extends Plugin {
|
public interface SimplexPlugin extends Plugin {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to create and return a reader for the given contact using the
|
* Attempts to create and return a reader for the given contact using the
|
||||||
* current transport and configuration properties. Returns null if a reader
|
* current transport and configuration properties. Returns null if a reader
|
||||||
* could not be created.
|
* cannot be created.
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
TransportConnectionReader createReader(ContactId c);
|
TransportConnectionReader createReader(ContactId c);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to create and return a writer for the given contact using the
|
* Attempts to create and return a writer for the given contact using the
|
||||||
* current transport and configuration properties. Returns null if a writer
|
* current transport and configuration properties. Returns null if a writer
|
||||||
* could not be created.
|
* cannot be created.
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
TransportConnectionWriter createWriter(ContactId c);
|
TransportConnectionWriter createWriter(ContactId c);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package org.briarproject.plugins.file;
|
package org.briarproject.plugins.file;
|
||||||
|
|
||||||
import org.briarproject.api.contact.ContactId;
|
import org.briarproject.api.contact.ContactId;
|
||||||
|
import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.api.plugins.TransportConnectionReader;
|
import org.briarproject.api.plugins.TransportConnectionReader;
|
||||||
import org.briarproject.api.plugins.TransportConnectionWriter;
|
import org.briarproject.api.plugins.TransportConnectionWriter;
|
||||||
import org.briarproject.api.plugins.simplex.SimplexPlugin;
|
import org.briarproject.api.plugins.simplex.SimplexPlugin;
|
||||||
@@ -17,10 +18,13 @@ import java.util.concurrent.Executor;
|
|||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static org.briarproject.api.transport.TransportConstants.MIN_STREAM_LENGTH;
|
import static org.briarproject.api.transport.TransportConstants.MIN_STREAM_LENGTH;
|
||||||
|
|
||||||
public abstract class FilePlugin implements SimplexPlugin {
|
@NotNullByDefault
|
||||||
|
abstract class FilePlugin implements SimplexPlugin {
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
Logger.getLogger(FilePlugin.class.getName());
|
Logger.getLogger(FilePlugin.class.getName());
|
||||||
@@ -32,6 +36,7 @@ public abstract class FilePlugin implements SimplexPlugin {
|
|||||||
|
|
||||||
protected volatile boolean running = false;
|
protected volatile boolean running = false;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
protected abstract File chooseOutputDirectory();
|
protected abstract File chooseOutputDirectory();
|
||||||
protected abstract Collection<File> findFilesByName(String filename);
|
protected abstract Collection<File> findFilesByName(String filename);
|
||||||
protected abstract void writerFinished(File f);
|
protected abstract void writerFinished(File f);
|
||||||
@@ -82,6 +87,7 @@ public abstract class FilePlugin implements SimplexPlugin {
|
|||||||
return filename.toLowerCase(Locale.US).matches("[a-z]{8}\\.dat");
|
return filename.toLowerCase(Locale.US).matches("[a-z]{8}\\.dat");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private TransportConnectionWriter createWriter(String filename) {
|
private TransportConnectionWriter createWriter(String filename) {
|
||||||
if (!running) return null;
|
if (!running) return null;
|
||||||
File dir = chooseOutputDirectory();
|
File dir = chooseOutputDirectory();
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import org.briarproject.api.contact.ContactId;
|
|||||||
import org.briarproject.api.data.BdfList;
|
import org.briarproject.api.data.BdfList;
|
||||||
import org.briarproject.api.keyagreement.KeyAgreementConnection;
|
import org.briarproject.api.keyagreement.KeyAgreementConnection;
|
||||||
import org.briarproject.api.keyagreement.KeyAgreementListener;
|
import org.briarproject.api.keyagreement.KeyAgreementListener;
|
||||||
|
import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.api.plugins.Backoff;
|
import org.briarproject.api.plugins.Backoff;
|
||||||
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
||||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||||
@@ -29,14 +30,13 @@ import java.util.concurrent.Callable;
|
|||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
|
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static org.briarproject.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_LAN;
|
import static org.briarproject.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_LAN;
|
||||||
import static org.briarproject.util.ByteUtils.MAX_16_BIT_UNSIGNED;
|
import static org.briarproject.util.ByteUtils.MAX_16_BIT_UNSIGNED;
|
||||||
import static org.briarproject.util.PrivacyUtils.scrubSocketAddress;
|
import static org.briarproject.util.PrivacyUtils.scrubSocketAddress;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
class LanTcpPlugin extends TcpPlugin {
|
class LanTcpPlugin extends TcpPlugin {
|
||||||
|
|
||||||
static final TransportId ID = new TransportId("lan");
|
static final TransportId ID = new TransportId("lan");
|
||||||
@@ -211,7 +211,6 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
LOG.info("Invalid IP/port in key agreement descriptor");
|
LOG.info("Invalid IP/port in key agreement descriptor");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (remote == null) return null;
|
|
||||||
if (!isConnectable(remote)) {
|
if (!isConnectable(remote)) {
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO)) {
|
||||||
SocketAddress local = socket.getLocalSocketAddress();
|
SocketAddress local = socket.getLocalSocketAddress();
|
||||||
@@ -237,10 +236,8 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private InetSocketAddress parseSocketAddress(BdfList descriptor)
|
private InetSocketAddress parseSocketAddress(BdfList descriptor)
|
||||||
throws FormatException {
|
throws FormatException {
|
||||||
if (descriptor.size() < 3) return null;
|
|
||||||
byte[] address = descriptor.getRaw(1);
|
byte[] address = descriptor.getRaw(1);
|
||||||
int port = descriptor.getLong(2).intValue();
|
int port = descriptor.getLong(2).intValue();
|
||||||
if (port < 1 || port > MAX_16_BIT_UNSIGNED) throw new FormatException();
|
if (port < 1 || port > MAX_16_BIT_UNSIGNED) throw new FormatException();
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import org.briarproject.api.contact.ContactId;
|
|||||||
import org.briarproject.api.crypto.PseudoRandom;
|
import org.briarproject.api.crypto.PseudoRandom;
|
||||||
import org.briarproject.api.data.BdfList;
|
import org.briarproject.api.data.BdfList;
|
||||||
import org.briarproject.api.keyagreement.KeyAgreementListener;
|
import org.briarproject.api.keyagreement.KeyAgreementListener;
|
||||||
|
import org.briarproject.api.nullsafety.MethodsNotNullByDefault;
|
||||||
|
import org.briarproject.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.api.plugins.Backoff;
|
import org.briarproject.api.plugins.Backoff;
|
||||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||||
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
||||||
@@ -28,10 +30,14 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static org.briarproject.util.PrivacyUtils.scrubSocketAddress;
|
import static org.briarproject.util.PrivacyUtils.scrubSocketAddress;
|
||||||
|
|
||||||
|
@MethodsNotNullByDefault
|
||||||
|
@ParametersNotNullByDefault
|
||||||
abstract class TcpPlugin implements DuplexPlugin {
|
abstract class TcpPlugin implements DuplexPlugin {
|
||||||
|
|
||||||
private static final Pattern DOTTED_QUAD =
|
private static final Pattern DOTTED_QUAD =
|
||||||
@@ -141,7 +147,7 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void tryToClose(ServerSocket ss) {
|
protected void tryToClose(@Nullable ServerSocket ss) {
|
||||||
try {
|
try {
|
||||||
if (ss != null) ss.close();
|
if (ss != null) ss.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -252,6 +258,7 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
protected InetSocketAddress parseSocketAddress(String ipPort) {
|
protected InetSocketAddress parseSocketAddress(String ipPort) {
|
||||||
if (StringUtils.isNullOrEmpty(ipPort)) return null;
|
if (StringUtils.isNullOrEmpty(ipPort)) return null;
|
||||||
String[] split = ipPort.split(":");
|
String[] split = ipPort.split(":");
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package org.briarproject.plugins.tcp;
|
|||||||
|
|
||||||
import org.briarproject.api.TransportId;
|
import org.briarproject.api.TransportId;
|
||||||
import org.briarproject.api.contact.ContactId;
|
import org.briarproject.api.contact.ContactId;
|
||||||
|
import org.briarproject.api.nullsafety.MethodsNotNullByDefault;
|
||||||
|
import org.briarproject.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.api.plugins.Backoff;
|
import org.briarproject.api.plugins.Backoff;
|
||||||
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
||||||
import org.briarproject.api.properties.TransportProperties;
|
import org.briarproject.api.properties.TransportProperties;
|
||||||
@@ -14,6 +16,8 @@ import java.util.LinkedList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
@MethodsNotNullByDefault
|
||||||
|
@ParametersNotNullByDefault
|
||||||
class WanTcpPlugin extends TcpPlugin {
|
class WanTcpPlugin extends TcpPlugin {
|
||||||
|
|
||||||
static final TransportId ID = new TransportId("wan");
|
static final TransportId ID = new TransportId("wan");
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import org.briarproject.api.crypto.PseudoRandom;
|
|||||||
import org.briarproject.api.data.BdfList;
|
import org.briarproject.api.data.BdfList;
|
||||||
import org.briarproject.api.keyagreement.KeyAgreementConnection;
|
import org.briarproject.api.keyagreement.KeyAgreementConnection;
|
||||||
import org.briarproject.api.keyagreement.KeyAgreementListener;
|
import org.briarproject.api.keyagreement.KeyAgreementListener;
|
||||||
|
import org.briarproject.api.nullsafety.MethodsNotNullByDefault;
|
||||||
|
import org.briarproject.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.api.plugins.Backoff;
|
import org.briarproject.api.plugins.Backoff;
|
||||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||||
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
||||||
@@ -48,6 +50,8 @@ import static java.util.logging.Level.WARNING;
|
|||||||
import static javax.bluetooth.DiscoveryAgent.GIAC;
|
import static javax.bluetooth.DiscoveryAgent.GIAC;
|
||||||
import static org.briarproject.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_BLUETOOTH;
|
import static org.briarproject.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_BLUETOOTH;
|
||||||
|
|
||||||
|
@MethodsNotNullByDefault
|
||||||
|
@ParametersNotNullByDefault
|
||||||
class BluetoothPlugin implements DuplexPlugin {
|
class BluetoothPlugin implements DuplexPlugin {
|
||||||
|
|
||||||
// Share an ID with the Android Bluetooth plugin
|
// Share an ID with the Android Bluetooth plugin
|
||||||
@@ -164,7 +168,7 @@ class BluetoothPlugin implements DuplexPlugin {
|
|||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tryToClose(StreamConnectionNotifier ss) {
|
private void tryToClose(@Nullable StreamConnectionNotifier ss) {
|
||||||
try {
|
try {
|
||||||
if (ss != null) ss.close();
|
if (ss != null) ss.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -333,7 +337,7 @@ class BluetoothPlugin implements DuplexPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void closeSockets(final List<Future<StreamConnection>> futures,
|
private void closeSockets(final List<Future<StreamConnection>> futures,
|
||||||
final StreamConnection chosen) {
|
@Nullable final StreamConnection chosen) {
|
||||||
ioExecutor.execute(new Runnable() {
|
ioExecutor.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -388,7 +392,7 @@ class BluetoothPlugin implements DuplexPlugin {
|
|||||||
BdfList descriptor = new BdfList();
|
BdfList descriptor = new BdfList();
|
||||||
descriptor.add(TRANSPORT_ID_BLUETOOTH);
|
descriptor.add(TRANSPORT_ID_BLUETOOTH);
|
||||||
String address = localDevice.getBluetoothAddress();
|
String address = localDevice.getBluetoothAddress();
|
||||||
if (!address.isEmpty()) descriptor.add(StringUtils.macToBytes(address));
|
descriptor.add(StringUtils.macToBytes(address));
|
||||||
return new BluetoothKeyAgreementListener(descriptor, ss);
|
return new BluetoothKeyAgreementListener(descriptor, ss);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -403,7 +407,6 @@ class BluetoothPlugin implements DuplexPlugin {
|
|||||||
LOG.info("Invalid address in key agreement descriptor");
|
LOG.info("Invalid address in key agreement descriptor");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (address == null) return null;
|
|
||||||
// No truncation necessary because COMMIT_LENGTH = 16
|
// No truncation necessary because COMMIT_LENGTH = 16
|
||||||
String uuid = UUID.nameUUIDFromBytes(commitment).toString();
|
String uuid = UUID.nameUUIDFromBytes(commitment).toString();
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
@@ -414,9 +417,7 @@ class BluetoothPlugin implements DuplexPlugin {
|
|||||||
return new BluetoothTransportConnection(this, s);
|
return new BluetoothTransportConnection(this, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private String parseAddress(BdfList descriptor) throws FormatException {
|
private String parseAddress(BdfList descriptor) throws FormatException {
|
||||||
if (descriptor.size() < 2) return null;
|
|
||||||
byte[] mac = descriptor.getRaw(1);
|
byte[] mac = descriptor.getRaw(1);
|
||||||
if (mac.length != 6) throw new FormatException();
|
if (mac.length != 6) throw new FormatException();
|
||||||
return StringUtils.macToString(mac);
|
return StringUtils.macToString(mac);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package org.briarproject.plugins.file;
|
|||||||
|
|
||||||
import org.briarproject.api.TransportId;
|
import org.briarproject.api.TransportId;
|
||||||
import org.briarproject.api.contact.ContactId;
|
import org.briarproject.api.contact.ContactId;
|
||||||
|
import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.api.plugins.simplex.SimplexPluginCallback;
|
import org.briarproject.api.plugins.simplex.SimplexPluginCallback;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -15,6 +16,7 @@ import java.util.logging.Logger;
|
|||||||
|
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
class RemovableDrivePlugin extends FilePlugin
|
class RemovableDrivePlugin extends FilePlugin
|
||||||
implements RemovableDriveMonitor.Callback {
|
implements RemovableDriveMonitor.Callback {
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import org.briarproject.api.contact.ContactId;
|
|||||||
import org.briarproject.api.crypto.PseudoRandom;
|
import org.briarproject.api.crypto.PseudoRandom;
|
||||||
import org.briarproject.api.data.BdfList;
|
import org.briarproject.api.data.BdfList;
|
||||||
import org.briarproject.api.keyagreement.KeyAgreementListener;
|
import org.briarproject.api.keyagreement.KeyAgreementListener;
|
||||||
|
import org.briarproject.api.nullsafety.MethodsNotNullByDefault;
|
||||||
|
import org.briarproject.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.api.plugins.duplex.AbstractDuplexTransportConnection;
|
import org.briarproject.api.plugins.duplex.AbstractDuplexTransportConnection;
|
||||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||||
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
||||||
@@ -22,6 +24,8 @@ import java.util.logging.Logger;
|
|||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
|
|
||||||
|
@MethodsNotNullByDefault
|
||||||
|
@ParametersNotNullByDefault
|
||||||
class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
||||||
|
|
||||||
static final TransportId ID = new TransportId("modem");
|
static final TransportId ID = new TransportId("modem");
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import java.util.concurrent.FutureTask;
|
|||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||||
|
import static org.briarproject.api.keyagreement.KeyAgreementConstants.COMMIT_LENGTH;
|
||||||
import static org.briarproject.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_LAN;
|
import static org.briarproject.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_LAN;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
@@ -188,7 +189,9 @@ public class LanTcpPluginTest extends BriarTestCase {
|
|||||||
0, 0);
|
0, 0);
|
||||||
plugin.start();
|
plugin.start();
|
||||||
assertTrue(callback.propertiesLatch.await(5, SECONDS));
|
assertTrue(callback.propertiesLatch.await(5, SECONDS));
|
||||||
KeyAgreementListener kal = plugin.createKeyAgreementListener(null);
|
KeyAgreementListener kal =
|
||||||
|
plugin.createKeyAgreementListener(new byte[COMMIT_LENGTH]);
|
||||||
|
assertNotNull(kal);
|
||||||
Callable<KeyAgreementConnection> c = kal.listen();
|
Callable<KeyAgreementConnection> c = kal.listen();
|
||||||
FutureTask<KeyAgreementConnection> f = new FutureTask<>(c);
|
FutureTask<KeyAgreementConnection> f = new FutureTask<>(c);
|
||||||
new Thread(f).start();
|
new Thread(f).start();
|
||||||
@@ -258,8 +261,8 @@ public class LanTcpPluginTest extends BriarTestCase {
|
|||||||
descriptor.add(local.getAddress().getAddress());
|
descriptor.add(local.getAddress().getAddress());
|
||||||
descriptor.add(local.getPort());
|
descriptor.add(local.getPort());
|
||||||
// Connect to the port
|
// Connect to the port
|
||||||
DuplexTransportConnection d =
|
DuplexTransportConnection d = plugin.createKeyAgreementConnection(
|
||||||
plugin.createKeyAgreementConnection(null, descriptor, 5000);
|
new byte[COMMIT_LENGTH], descriptor, 5000);
|
||||||
assertNotNull(d);
|
assertNotNull(d);
|
||||||
// Check that the connection was accepted
|
// Check that the connection was accepted
|
||||||
assertTrue(latch.await(5, SECONDS));
|
assertTrue(latch.await(5, SECONDS));
|
||||||
|
|||||||
Reference in New Issue
Block a user