mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 21:59:54 +01:00
Always call the callback outside the plugin's lock.
This commit is contained in:
@@ -16,10 +16,7 @@ public interface TransportPlugin {
|
|||||||
Map<ContactId, Map<String, String>> remoteProperties,
|
Map<ContactId, Map<String, String>> remoteProperties,
|
||||||
Map<String, String> config) throws IOException;
|
Map<String, String> config) throws IOException;
|
||||||
|
|
||||||
/**
|
/** Stops the plugin. */
|
||||||
* Stops the plugin. No further connections will be passed to the callback
|
|
||||||
* after this method has returned.
|
|
||||||
*/
|
|
||||||
void stop() throws IOException;
|
void stop() throws IOException;
|
||||||
|
|
||||||
/** Updates the plugin's local transport properties. */
|
/** Updates the plugin's local transport properties. */
|
||||||
|
|||||||
@@ -54,13 +54,15 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void start(Map<String, String> localProperties,
|
public void start(Map<String, String> localProperties,
|
||||||
Map<ContactId, Map<String, String>> remoteProperties,
|
Map<ContactId, Map<String, String>> remoteProperties,
|
||||||
Map<String, String> config) throws IOException {
|
Map<String, String> config) throws IOException {
|
||||||
super.start(localProperties, remoteProperties, config);
|
|
||||||
// Initialise the Bluetooth stack
|
// Initialise the Bluetooth stack
|
||||||
try {
|
try {
|
||||||
localDevice = LocalDevice.getLocalDevice();
|
synchronized(this) {
|
||||||
|
super.start(localProperties, remoteProperties, config);
|
||||||
|
localDevice = LocalDevice.getLocalDevice();
|
||||||
|
}
|
||||||
} catch(UnsatisfiedLinkError e) {
|
} catch(UnsatisfiedLinkError e) {
|
||||||
// On Linux the user may need to install libbluetooth-dev
|
// On Linux the user may need to install libbluetooth-dev
|
||||||
if(OsUtils.isLinux())
|
if(OsUtils.isLinux())
|
||||||
@@ -89,14 +91,16 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
|||||||
|
|
||||||
private void bind() {
|
private void bind() {
|
||||||
String uuid;
|
String uuid;
|
||||||
|
LocalDevice ld;
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
if(!started) return;
|
if(!started) return;
|
||||||
uuid = config.get("uuid");
|
uuid = config.get("uuid");
|
||||||
if(uuid == null) uuid = createAndSetUuid();
|
ld = localDevice;
|
||||||
}
|
}
|
||||||
|
if(uuid == null) uuid = createAndSetUuid();
|
||||||
// Try to make the device discoverable (requires root on Linux)
|
// Try to make the device discoverable (requires root on Linux)
|
||||||
try {
|
try {
|
||||||
localDevice.setDiscoverable(DiscoveryAgent.GIAC);
|
ld.setDiscoverable(DiscoveryAgent.GIAC);
|
||||||
} catch(BluetoothStateException e) {
|
} catch(BluetoothStateException e) {
|
||||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||||
}
|
}
|
||||||
@@ -120,17 +124,20 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
streamConnectionNotifier = scn;
|
streamConnectionNotifier = scn;
|
||||||
setLocalBluetoothAddress(localDevice.getBluetoothAddress());
|
|
||||||
startListener();
|
startListener();
|
||||||
}
|
}
|
||||||
|
setLocalBluetoothAddress(ld.getBluetoothAddress());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String createAndSetUuid() {
|
private String createAndSetUuid() {
|
||||||
assert started;
|
|
||||||
byte[] b = new byte[16];
|
byte[] b = new byte[16];
|
||||||
new Random().nextBytes(b); // FIXME: Use a SecureRandom?
|
new Random().nextBytes(b); // FIXME: Use a SecureRandom?
|
||||||
String uuid = StringUtils.toHexString(b);
|
String uuid = StringUtils.toHexString(b);
|
||||||
Map<String, String> m = new TreeMap<String, String>(config);
|
Map<String, String> m;
|
||||||
|
synchronized(this) {
|
||||||
|
if(!started) return uuid;
|
||||||
|
m = new TreeMap<String, String>(config);
|
||||||
|
}
|
||||||
m.put("uuid", uuid);
|
m.put("uuid", uuid);
|
||||||
callback.setConfig(m);
|
callback.setConfig(m);
|
||||||
return uuid;
|
return uuid;
|
||||||
@@ -160,26 +167,18 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
|||||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
synchronized(this) {
|
BluetoothTransportConnection conn =
|
||||||
if(!started) {
|
new BluetoothTransportConnection(s);
|
||||||
try {
|
callback.incomingConnectionCreated(conn);
|
||||||
s.close();
|
|
||||||
} catch(IOException e) {
|
|
||||||
if(LOG.isLoggable(Level.WARNING))
|
|
||||||
LOG.warning(e.getMessage());
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
BluetoothTransportConnection conn =
|
|
||||||
new BluetoothTransportConnection(s);
|
|
||||||
callback.incomingConnectionCreated(conn);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setLocalBluetoothAddress(String address) {
|
private void setLocalBluetoothAddress(String address) {
|
||||||
assert started;
|
Map<String, String> m;
|
||||||
Map<String, String> m = new TreeMap<String, String>(localProperties);
|
synchronized(this) {
|
||||||
|
if(!started) return;
|
||||||
|
m = new TreeMap<String, String>(localProperties);
|
||||||
|
}
|
||||||
m.put("address", address);
|
m.put("address", address);
|
||||||
callback.setLocalProperties(m);
|
callback.setLocalProperties(m);
|
||||||
}
|
}
|
||||||
@@ -211,11 +210,7 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
|||||||
ContactId c = e.getKey();
|
ContactId c = e.getKey();
|
||||||
String url = e.getValue();
|
String url = e.getValue();
|
||||||
StreamTransportConnection conn = createConnection(c, url);
|
StreamTransportConnection conn = createConnection(c, url);
|
||||||
if(conn != null) {
|
if(conn != null) callback.outgoingConnectionCreated(c, conn);
|
||||||
synchronized(this) {
|
|
||||||
if(started) callback.outgoingConnectionCreated(c, conn);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -93,15 +93,10 @@ implements BatchTransportPlugin {
|
|||||||
if(f.length() < TransportConstants.MIN_CONNECTION_LENGTH) return;
|
if(f.length() < TransportConstants.MIN_CONNECTION_LENGTH) return;
|
||||||
try {
|
try {
|
||||||
FileInputStream in = new FileInputStream(f);
|
FileInputStream in = new FileInputStream(f);
|
||||||
synchronized(FilePlugin.this) {
|
callback.readerCreated(new FileTransportReader(f, in,
|
||||||
if(started) {
|
FilePlugin.this));
|
||||||
callback.readerCreated(new FileTransportReader(f, in,
|
|
||||||
FilePlugin.this));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,14 +69,17 @@ class SimpleSocketPlugin extends SocketPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setLocalSocketAddress(SocketAddress s) {
|
protected void setLocalSocketAddress(SocketAddress s) {
|
||||||
assert started;
|
Map<String, String> m;
|
||||||
|
synchronized(this) {
|
||||||
|
if(!started) return;
|
||||||
|
m = new TreeMap<String, String>(localProperties);
|
||||||
|
}
|
||||||
if(!(s instanceof InetSocketAddress))
|
if(!(s instanceof InetSocketAddress))
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
InetSocketAddress i = (InetSocketAddress) s;
|
InetSocketAddress i = (InetSocketAddress) s;
|
||||||
String host = i.getAddress().getHostAddress();
|
String host = i.getAddress().getHostAddress();
|
||||||
String port = String.valueOf(i.getPort());
|
String port = String.valueOf(i.getPort());
|
||||||
// FIXME: Special handling for private IP addresses?
|
// FIXME: Special handling for private IP addresses?
|
||||||
Map<String, String> m = new TreeMap<String, String>(localProperties);
|
|
||||||
m.put("host", host);
|
m.put("host", host);
|
||||||
m.put("port", port);
|
m.put("port", port);
|
||||||
callback.setLocalProperties(m);
|
callback.setLocalProperties(m);
|
||||||
|
|||||||
@@ -26,10 +26,11 @@ implements StreamTransportPlugin {
|
|||||||
// This field should be accessed with this's lock held
|
// This field should be accessed with this's lock held
|
||||||
protected ServerSocket socket = null;
|
protected ServerSocket socket = null;
|
||||||
|
|
||||||
|
protected abstract void setLocalSocketAddress(SocketAddress s);
|
||||||
|
|
||||||
// These methods should be called with this's lock held and started == true
|
// These methods should be called with this's lock held and started == true
|
||||||
protected abstract SocketAddress getLocalSocketAddress();
|
protected abstract SocketAddress getLocalSocketAddress();
|
||||||
protected abstract SocketAddress getSocketAddress(ContactId c);
|
protected abstract SocketAddress getSocketAddress(ContactId c);
|
||||||
protected abstract void setLocalSocketAddress(SocketAddress s);
|
|
||||||
protected abstract Socket createClientSocket() throws IOException;
|
protected abstract Socket createClientSocket() throws IOException;
|
||||||
protected abstract ServerSocket createServerSocket() throws IOException;
|
protected abstract ServerSocket createServerSocket() throws IOException;
|
||||||
|
|
||||||
@@ -104,8 +105,7 @@ implements StreamTransportPlugin {
|
|||||||
ServerSocket ss;
|
ServerSocket ss;
|
||||||
Socket s;
|
Socket s;
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
if(!started) return;
|
if(!started || socket == null) return;
|
||||||
if(socket == null) return;
|
|
||||||
ss = socket;
|
ss = socket;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
@@ -114,20 +114,8 @@ implements StreamTransportPlugin {
|
|||||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
synchronized(this) {
|
SocketTransportConnection conn = new SocketTransportConnection(s);
|
||||||
if(!started) {
|
callback.incomingConnectionCreated(conn);
|
||||||
try {
|
|
||||||
s.close();
|
|
||||||
} catch(IOException e) {
|
|
||||||
if(LOG.isLoggable(Level.WARNING))
|
|
||||||
LOG.warning(e.getMessage());
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
SocketTransportConnection conn =
|
|
||||||
new SocketTransportConnection(s);
|
|
||||||
callback.incomingConnectionCreated(conn);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,11 +167,7 @@ implements StreamTransportPlugin {
|
|||||||
|
|
||||||
private void connectAndCallBack(ContactId c) {
|
private void connectAndCallBack(ContactId c) {
|
||||||
StreamTransportConnection conn = createConnection(c);
|
StreamTransportConnection conn = createConnection(c);
|
||||||
if(conn != null) {
|
if(conn != null) callback.outgoingConnectionCreated(c, conn);
|
||||||
synchronized(this) {
|
|
||||||
if(started) callback.outgoingConnectionCreated(c, conn);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public StreamTransportConnection createConnection(ContactId c) {
|
public StreamTransportConnection createConnection(ContactId c) {
|
||||||
|
|||||||
Reference in New Issue
Block a user