mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
Merge branch '281-close-transport-connection' into 'master'
Close transport connection if tag isn't recognised. #281 Factored out common code from various DuplexTransportConnection implementations into an abstract superclass, and changed the logic for closing connections so that connections with unrecognised tags are closed immediately. This prevents deleted contacts from thinking they're connected to us when they're not. See merge request !132
This commit is contained in:
@@ -3,74 +3,33 @@ package org.briarproject.plugins.droidtooth;
|
||||
import android.bluetooth.BluetoothSocket;
|
||||
|
||||
import org.briarproject.api.plugins.Plugin;
|
||||
import org.briarproject.api.plugins.TransportConnectionReader;
|
||||
import org.briarproject.api.plugins.TransportConnectionWriter;
|
||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.api.plugins.duplex.AbstractDuplexTransportConnection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
class DroidtoothTransportConnection implements DuplexTransportConnection {
|
||||
class DroidtoothTransportConnection extends AbstractDuplexTransportConnection {
|
||||
|
||||
private final Plugin plugin;
|
||||
private final BluetoothSocket socket;
|
||||
private final Reader reader;
|
||||
private final Writer writer;
|
||||
private final AtomicBoolean halfClosed, closed;
|
||||
|
||||
DroidtoothTransportConnection(Plugin plugin, BluetoothSocket socket) {
|
||||
this.plugin = plugin;
|
||||
super(plugin);
|
||||
this.socket = socket;
|
||||
reader = new Reader();
|
||||
writer = new Writer();
|
||||
halfClosed = new AtomicBoolean(false);
|
||||
closed = new AtomicBoolean(false);
|
||||
}
|
||||
|
||||
public TransportConnectionReader getReader() {
|
||||
return reader;
|
||||
@Override
|
||||
protected InputStream getInputStream() throws IOException {
|
||||
return socket.getInputStream();
|
||||
}
|
||||
|
||||
public TransportConnectionWriter getWriter() {
|
||||
return writer;
|
||||
@Override
|
||||
protected OutputStream getOutputStream() throws IOException {
|
||||
return socket.getOutputStream();
|
||||
}
|
||||
|
||||
private class Reader implements TransportConnectionReader {
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return socket.getInputStream();
|
||||
}
|
||||
|
||||
public void dispose(boolean exception, boolean recognised)
|
||||
throws IOException {
|
||||
if (halfClosed.getAndSet(true) || exception)
|
||||
if (!closed.getAndSet(true)) socket.close();
|
||||
}
|
||||
}
|
||||
|
||||
private class Writer implements TransportConnectionWriter {
|
||||
|
||||
public int getMaxLatency() {
|
||||
return plugin.getMaxLatency();
|
||||
}
|
||||
|
||||
public int getMaxIdleTime() {
|
||||
return plugin.getMaxIdleTime();
|
||||
}
|
||||
|
||||
public long getCapacity() {
|
||||
return Long.MAX_VALUE;
|
||||
}
|
||||
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
return socket.getOutputStream();
|
||||
}
|
||||
|
||||
public void dispose(boolean exception) throws IOException {
|
||||
if (halfClosed.getAndSet(true) || exception)
|
||||
if (!closed.getAndSet(true)) socket.close();
|
||||
}
|
||||
@Override
|
||||
protected void closeConnection(boolean exception) throws IOException {
|
||||
socket.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,75 +1,34 @@
|
||||
package org.briarproject.plugins.tor;
|
||||
|
||||
import org.briarproject.api.plugins.Plugin;
|
||||
import org.briarproject.api.plugins.TransportConnectionReader;
|
||||
import org.briarproject.api.plugins.TransportConnectionWriter;
|
||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.api.plugins.duplex.AbstractDuplexTransportConnection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
class TorTransportConnection implements DuplexTransportConnection {
|
||||
class TorTransportConnection extends AbstractDuplexTransportConnection {
|
||||
|
||||
private final Plugin plugin;
|
||||
private final Socket socket;
|
||||
private final Reader reader;
|
||||
private final Writer writer;
|
||||
private final AtomicBoolean halfClosed, closed;
|
||||
|
||||
TorTransportConnection(Plugin plugin, Socket socket) {
|
||||
this.plugin = plugin;
|
||||
super(plugin);
|
||||
this.socket = socket;
|
||||
reader = new Reader();
|
||||
writer = new Writer();
|
||||
halfClosed = new AtomicBoolean(false);
|
||||
closed = new AtomicBoolean(false);
|
||||
}
|
||||
|
||||
public TransportConnectionReader getReader() {
|
||||
return reader;
|
||||
@Override
|
||||
protected InputStream getInputStream() throws IOException {
|
||||
return socket.getInputStream();
|
||||
}
|
||||
|
||||
public TransportConnectionWriter getWriter() {
|
||||
return writer;
|
||||
@Override
|
||||
protected OutputStream getOutputStream() throws IOException {
|
||||
return socket.getOutputStream();
|
||||
}
|
||||
|
||||
private class Reader implements TransportConnectionReader {
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return socket.getInputStream();
|
||||
}
|
||||
|
||||
public void dispose(boolean exception, boolean recognised)
|
||||
throws IOException {
|
||||
if (halfClosed.getAndSet(true) || exception)
|
||||
if (!closed.getAndSet(true)) socket.close();
|
||||
}
|
||||
}
|
||||
|
||||
private class Writer implements TransportConnectionWriter {
|
||||
|
||||
public int getMaxLatency() {
|
||||
return plugin.getMaxLatency();
|
||||
}
|
||||
|
||||
public int getMaxIdleTime() {
|
||||
return plugin.getMaxIdleTime();
|
||||
}
|
||||
|
||||
public long getCapacity() {
|
||||
return Long.MAX_VALUE;
|
||||
}
|
||||
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
return socket.getOutputStream();
|
||||
}
|
||||
|
||||
public void dispose(boolean exception) throws IOException {
|
||||
if (halfClosed.getAndSet(true) || exception)
|
||||
if (!closed.getAndSet(true)) socket.close();
|
||||
}
|
||||
@Override
|
||||
protected void closeConnection(boolean exception) throws IOException {
|
||||
socket.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package org.briarproject.api.plugins.duplex;
|
||||
|
||||
import org.briarproject.api.plugins.Plugin;
|
||||
import org.briarproject.api.plugins.TransportConnectionReader;
|
||||
import org.briarproject.api.plugins.TransportConnectionWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public abstract class AbstractDuplexTransportConnection
|
||||
implements DuplexTransportConnection {
|
||||
|
||||
private final Plugin plugin;
|
||||
private final Reader reader;
|
||||
private final Writer writer;
|
||||
private final AtomicBoolean halfClosed, closed;
|
||||
|
||||
protected AbstractDuplexTransportConnection(Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
reader = new Reader();
|
||||
writer = new Writer();
|
||||
halfClosed = new AtomicBoolean(false);
|
||||
closed = new AtomicBoolean(false);
|
||||
}
|
||||
|
||||
protected abstract InputStream getInputStream() throws IOException;
|
||||
|
||||
protected abstract OutputStream getOutputStream() throws IOException;
|
||||
|
||||
protected abstract void closeConnection(boolean exception)
|
||||
throws IOException;
|
||||
|
||||
@Override
|
||||
public TransportConnectionReader getReader() {
|
||||
return reader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportConnectionWriter getWriter() {
|
||||
return writer;
|
||||
}
|
||||
private class Reader implements TransportConnectionReader {
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return AbstractDuplexTransportConnection.this.getInputStream();
|
||||
}
|
||||
|
||||
public void dispose(boolean exception, boolean recognised)
|
||||
throws IOException {
|
||||
if (halfClosed.getAndSet(true) || exception || !recognised)
|
||||
if (!closed.getAndSet(true)) closeConnection(exception);
|
||||
}
|
||||
}
|
||||
|
||||
private class Writer implements TransportConnectionWriter {
|
||||
|
||||
public int getMaxLatency() {
|
||||
return plugin.getMaxLatency();
|
||||
}
|
||||
|
||||
public int getMaxIdleTime() {
|
||||
return plugin.getMaxIdleTime();
|
||||
}
|
||||
|
||||
public long getCapacity() {
|
||||
return Long.MAX_VALUE;
|
||||
}
|
||||
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
return AbstractDuplexTransportConnection.this.getOutputStream();
|
||||
}
|
||||
|
||||
public void dispose(boolean exception) throws IOException {
|
||||
if (halfClosed.getAndSet(true) || exception)
|
||||
if (!closed.getAndSet(true)) closeConnection(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,75 +1,34 @@
|
||||
package org.briarproject.plugins.tcp;
|
||||
|
||||
import org.briarproject.api.plugins.Plugin;
|
||||
import org.briarproject.api.plugins.TransportConnectionReader;
|
||||
import org.briarproject.api.plugins.TransportConnectionWriter;
|
||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.api.plugins.duplex.AbstractDuplexTransportConnection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
class TcpTransportConnection implements DuplexTransportConnection {
|
||||
class TcpTransportConnection extends AbstractDuplexTransportConnection {
|
||||
|
||||
private final Plugin plugin;
|
||||
private final Socket socket;
|
||||
private final Reader reader;
|
||||
private final Writer writer;
|
||||
private final AtomicBoolean halfClosed, closed;
|
||||
|
||||
TcpTransportConnection(Plugin plugin, Socket socket) {
|
||||
this.plugin = plugin;
|
||||
super(plugin);
|
||||
this.socket = socket;
|
||||
reader = new Reader();
|
||||
writer = new Writer();
|
||||
halfClosed = new AtomicBoolean(false);
|
||||
closed = new AtomicBoolean(false);
|
||||
}
|
||||
|
||||
public TransportConnectionReader getReader() {
|
||||
return reader;
|
||||
@Override
|
||||
protected InputStream getInputStream() throws IOException {
|
||||
return socket.getInputStream();
|
||||
}
|
||||
|
||||
public TransportConnectionWriter getWriter() {
|
||||
return writer;
|
||||
@Override
|
||||
protected OutputStream getOutputStream() throws IOException {
|
||||
return socket.getOutputStream();
|
||||
}
|
||||
|
||||
private class Reader implements TransportConnectionReader {
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return socket.getInputStream();
|
||||
}
|
||||
|
||||
public void dispose(boolean exception, boolean recognised)
|
||||
throws IOException {
|
||||
if (halfClosed.getAndSet(true) || exception)
|
||||
if (!closed.getAndSet(true)) socket.close();
|
||||
}
|
||||
}
|
||||
|
||||
private class Writer implements TransportConnectionWriter {
|
||||
|
||||
public int getMaxLatency() {
|
||||
return plugin.getMaxLatency();
|
||||
}
|
||||
|
||||
public int getMaxIdleTime() {
|
||||
return plugin.getMaxIdleTime();
|
||||
}
|
||||
|
||||
public long getCapacity() {
|
||||
return Long.MAX_VALUE;
|
||||
}
|
||||
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
return socket.getOutputStream();
|
||||
}
|
||||
|
||||
public void dispose(boolean exception) throws IOException {
|
||||
if (halfClosed.getAndSet(true) || exception)
|
||||
if (!closed.getAndSet(true)) socket.close();
|
||||
}
|
||||
@Override
|
||||
protected void closeConnection(boolean exception) throws IOException {
|
||||
socket.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,76 +1,35 @@
|
||||
package org.briarproject.plugins.bluetooth;
|
||||
|
||||
import org.briarproject.api.plugins.Plugin;
|
||||
import org.briarproject.api.plugins.TransportConnectionReader;
|
||||
import org.briarproject.api.plugins.TransportConnectionWriter;
|
||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.api.plugins.duplex.AbstractDuplexTransportConnection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import javax.microedition.io.StreamConnection;
|
||||
|
||||
class BluetoothTransportConnection implements DuplexTransportConnection {
|
||||
class BluetoothTransportConnection extends AbstractDuplexTransportConnection {
|
||||
|
||||
private final Plugin plugin;
|
||||
private final StreamConnection stream;
|
||||
private final Reader reader;
|
||||
private final Writer writer;
|
||||
private final AtomicBoolean halfClosed, closed;
|
||||
|
||||
BluetoothTransportConnection(Plugin plugin, StreamConnection stream) {
|
||||
this.plugin = plugin;
|
||||
super(plugin);
|
||||
this.stream = stream;
|
||||
reader = new Reader();
|
||||
writer = new Writer();
|
||||
halfClosed = new AtomicBoolean(false);
|
||||
closed = new AtomicBoolean(false);
|
||||
}
|
||||
|
||||
public TransportConnectionReader getReader() {
|
||||
return reader;
|
||||
@Override
|
||||
protected InputStream getInputStream() throws IOException {
|
||||
return stream.openInputStream();
|
||||
}
|
||||
|
||||
public TransportConnectionWriter getWriter() {
|
||||
return writer;
|
||||
@Override
|
||||
protected OutputStream getOutputStream() throws IOException {
|
||||
return stream.openOutputStream();
|
||||
}
|
||||
|
||||
private class Reader implements TransportConnectionReader {
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return stream.openInputStream();
|
||||
}
|
||||
|
||||
public void dispose(boolean exception, boolean recognised)
|
||||
throws IOException {
|
||||
if (halfClosed.getAndSet(true) || exception)
|
||||
if (!closed.getAndSet(true)) stream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private class Writer implements TransportConnectionWriter {
|
||||
|
||||
public int getMaxLatency() {
|
||||
return plugin.getMaxLatency();
|
||||
}
|
||||
|
||||
public int getMaxIdleTime() {
|
||||
return plugin.getMaxIdleTime();
|
||||
}
|
||||
|
||||
public long getCapacity() {
|
||||
return Long.MAX_VALUE;
|
||||
}
|
||||
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
return stream.openOutputStream();
|
||||
}
|
||||
|
||||
public void dispose(boolean exception) throws IOException {
|
||||
if (halfClosed.getAndSet(true) || exception)
|
||||
if (!closed.getAndSet(true)) stream.close();
|
||||
}
|
||||
@Override
|
||||
protected void closeConnection(boolean exception) throws IOException {
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,7 @@ import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.crypto.PseudoRandom;
|
||||
import org.briarproject.api.keyagreement.KeyAgreementListener;
|
||||
import org.briarproject.api.keyagreement.TransportDescriptor;
|
||||
import org.briarproject.api.plugins.TransportConnectionReader;
|
||||
import org.briarproject.api.plugins.TransportConnectionWriter;
|
||||
import org.briarproject.api.plugins.duplex.AbstractDuplexTransportConnection;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||
@@ -17,8 +16,6 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
@@ -180,23 +177,24 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
||||
}
|
||||
|
||||
private class ModemTransportConnection
|
||||
implements DuplexTransportConnection {
|
||||
extends AbstractDuplexTransportConnection {
|
||||
|
||||
private final AtomicBoolean halfClosed = new AtomicBoolean(false);
|
||||
private final AtomicBoolean closed = new AtomicBoolean(false);
|
||||
private final CountDownLatch disposalFinished = new CountDownLatch(1);
|
||||
private final Reader reader = new Reader();
|
||||
private final Writer writer = new Writer();
|
||||
|
||||
public TransportConnectionReader getReader() {
|
||||
return reader;
|
||||
private ModemTransportConnection() {
|
||||
super(ModemPlugin.this);
|
||||
}
|
||||
|
||||
public TransportConnectionWriter getWriter() {
|
||||
return writer;
|
||||
@Override
|
||||
protected InputStream getInputStream() throws IOException {
|
||||
return modem.getInputStream();
|
||||
}
|
||||
|
||||
private void hangUp(boolean exception) {
|
||||
@Override
|
||||
protected OutputStream getOutputStream() throws IOException {
|
||||
return modem.getOutputStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void closeConnection(boolean exception) {
|
||||
LOG.info("Call disconnected");
|
||||
try {
|
||||
modem.hangUp();
|
||||
@@ -205,43 +203,6 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
||||
exception = true;
|
||||
}
|
||||
if (exception) resetModem();
|
||||
disposalFinished.countDown();
|
||||
}
|
||||
|
||||
private class Reader implements TransportConnectionReader {
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return modem.getInputStream();
|
||||
}
|
||||
|
||||
public void dispose(boolean exception, boolean recognised) {
|
||||
if (halfClosed.getAndSet(true) || exception)
|
||||
if (!closed.getAndSet(true)) hangUp(exception);
|
||||
}
|
||||
}
|
||||
|
||||
private class Writer implements TransportConnectionWriter {
|
||||
|
||||
public int getMaxLatency() {
|
||||
return ModemPlugin.this.getMaxLatency();
|
||||
}
|
||||
|
||||
public int getMaxIdleTime() {
|
||||
return ModemPlugin.this.getMaxIdleTime();
|
||||
}
|
||||
|
||||
public long getCapacity() {
|
||||
return Long.MAX_VALUE;
|
||||
}
|
||||
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
return modem.getOutputStream();
|
||||
}
|
||||
|
||||
public void dispose(boolean exception) {
|
||||
if (halfClosed.getAndSet(true) || exception)
|
||||
if (!closed.getAndSet(true)) hangUp(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user