mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 04:39:54 +01:00
Refactored transport component and renamed WritersModule.
The goal of the refactoring was to clean up the dependencies of IncomingBatchConnection and OutgoingBatchConnection.
This commit is contained in:
@@ -4,7 +4,7 @@ import net.sf.briar.api.protocol.writers.ProtocolWriterFactory;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
|
||||
public class WritersModule extends AbstractModule {
|
||||
public class ProtocolWritersModule extends AbstractModule {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
@@ -12,9 +12,6 @@ interface ConnectionEncrypter {
|
||||
/** Encrypts and writes the MAC for the current frame. */
|
||||
void writeMac(byte[] mac) throws IOException;
|
||||
|
||||
/**
|
||||
* Returns the number of bytes that can be encrypted without outputting
|
||||
* more than the given number of bytes, including encryption overhead.
|
||||
*/
|
||||
long getCapacity(long capacity);
|
||||
/** Returns the maximum number of bytes that can be written. */
|
||||
long getCapacity();
|
||||
}
|
||||
|
||||
@@ -22,10 +22,10 @@ implements ConnectionEncrypter {
|
||||
private final SecretKey frameKey;
|
||||
private final byte[] iv;
|
||||
|
||||
private long frame = 0L;
|
||||
private long capacity, frame = 0L;
|
||||
private boolean ivWritten = false, betweenFrames = false;
|
||||
|
||||
ConnectionEncrypterImpl(OutputStream out, boolean initiator,
|
||||
ConnectionEncrypterImpl(OutputStream out, long capacity, boolean initiator,
|
||||
int transportId, long connection, Cipher ivCipher,
|
||||
Cipher frameCipher, SecretKey ivKey, SecretKey frameKey) {
|
||||
super(out);
|
||||
@@ -40,6 +40,7 @@ implements ConnectionEncrypter {
|
||||
}
|
||||
if(ivCipher.getOutputSize(IV_LENGTH) != IV_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public OutputStream getOutputStream() {
|
||||
@@ -55,12 +56,12 @@ implements ConnectionEncrypter {
|
||||
} catch(IllegalBlockSizeException badCipher) {
|
||||
throw new RuntimeException(badCipher);
|
||||
}
|
||||
capacity -= mac.length;
|
||||
betweenFrames = true;
|
||||
}
|
||||
|
||||
public long getCapacity(long capacity) {
|
||||
if(capacity < 0L) throw new IllegalArgumentException();
|
||||
return ivWritten ? capacity : Math.max(0L, capacity - IV_LENGTH);
|
||||
public long getCapacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -69,6 +70,7 @@ implements ConnectionEncrypter {
|
||||
if(betweenFrames) initialiseCipher();
|
||||
byte[] ciphertext = frameCipher.update(new byte[] {(byte) b});
|
||||
if(ciphertext != null) out.write(ciphertext);
|
||||
capacity--;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -82,6 +84,7 @@ implements ConnectionEncrypter {
|
||||
if(betweenFrames) initialiseCipher();
|
||||
byte[] ciphertext = frameCipher.update(b, off, len);
|
||||
if(ciphertext != null) out.write(ciphertext);
|
||||
capacity -= len;
|
||||
}
|
||||
|
||||
private void writeIv() throws IOException {
|
||||
@@ -94,6 +97,7 @@ implements ConnectionEncrypter {
|
||||
} catch(IllegalBlockSizeException badCipher) {
|
||||
throw new RuntimeException(badCipher);
|
||||
}
|
||||
capacity -= iv.length;
|
||||
ivWritten = true;
|
||||
betweenFrames = true;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
|
||||
}
|
||||
|
||||
public ConnectionWriter createConnectionWriter(OutputStream out,
|
||||
boolean initiator, int transportId, long connection,
|
||||
long capacity, boolean initiator, int transportId, long connection,
|
||||
byte[] secret) {
|
||||
SecretKey macKey = crypto.deriveOutgoingMacKey(secret);
|
||||
SecretKey ivKey = crypto.deriveOutgoingIvKey(secret);
|
||||
@@ -37,8 +37,8 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
|
||||
throw new IllegalArgumentException(badKey);
|
||||
}
|
||||
ConnectionEncrypter encrypter = new ConnectionEncrypterImpl(out,
|
||||
initiator, transportId, connection, ivCipher, frameCipher,
|
||||
ivKey, frameKey);
|
||||
capacity, initiator, transportId, connection, ivCipher,
|
||||
frameCipher, ivKey, frameKey);
|
||||
return new ConnectionWriterImpl(encrypter, mac);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,10 +41,8 @@ implements ConnectionWriter {
|
||||
return this;
|
||||
}
|
||||
|
||||
public long getCapacity(long capacity) {
|
||||
if(capacity < 0L) throw new IllegalArgumentException();
|
||||
// Subtract the encryption overhead
|
||||
capacity = encrypter.getCapacity(capacity);
|
||||
public long getCapacity() {
|
||||
long capacity = encrypter.getCapacity();
|
||||
// If there's any data buffered, subtract it and its auth overhead
|
||||
int overheadPerFrame = header.length + mac.getMacLength();
|
||||
if(buf.size() > 0) capacity -= buf.size() + overheadPerFrame;
|
||||
|
||||
@@ -14,36 +14,23 @@ import net.sf.briar.api.protocol.ProtocolReaderFactory;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.transport.ConnectionReader;
|
||||
import net.sf.briar.api.transport.ConnectionReaderFactory;
|
||||
import net.sf.briar.api.transport.batch.BatchTransportReader;
|
||||
|
||||
class IncomingBatchConnection {
|
||||
|
||||
private final BatchTransportReader trans;
|
||||
private final ConnectionReaderFactory connFactory;
|
||||
private final ConnectionReader conn;
|
||||
private final DatabaseComponent db;
|
||||
private final ProtocolReaderFactory protoFactory;
|
||||
private final int transportId;
|
||||
private final long connection;
|
||||
private final ContactId contactId;
|
||||
|
||||
IncomingBatchConnection(BatchTransportReader trans,
|
||||
ConnectionReaderFactory connFactory, DatabaseComponent db,
|
||||
ProtocolReaderFactory protoFactory, int transportId,
|
||||
long connection, ContactId contactId) {
|
||||
this.trans = trans;
|
||||
this.connFactory = connFactory;
|
||||
IncomingBatchConnection(ConnectionReader conn, DatabaseComponent db,
|
||||
ProtocolReaderFactory protoFactory, ContactId contactId) {
|
||||
this.conn = conn;
|
||||
this.db = db;
|
||||
this.protoFactory = protoFactory;
|
||||
this.transportId = transportId;
|
||||
this.connection = connection;
|
||||
this.contactId = contactId;
|
||||
}
|
||||
|
||||
void read() throws DbException, IOException {
|
||||
byte[] secret = db.getSharedSecret(contactId);
|
||||
ConnectionReader conn = connFactory.createConnectionReader(
|
||||
trans.getInputStream(), false, transportId, connection, secret);
|
||||
InputStream in = conn.getInputStream();
|
||||
ProtocolReader proto = protoFactory.createProtocolReader(in);
|
||||
// Read packets until EOF
|
||||
|
||||
@@ -14,45 +14,32 @@ import net.sf.briar.api.protocol.writers.ProtocolWriterFactory;
|
||||
import net.sf.briar.api.protocol.writers.SubscriptionWriter;
|
||||
import net.sf.briar.api.protocol.writers.TransportWriter;
|
||||
import net.sf.briar.api.transport.ConnectionWriter;
|
||||
import net.sf.briar.api.transport.ConnectionWriterFactory;
|
||||
import net.sf.briar.api.transport.batch.BatchTransportWriter;
|
||||
|
||||
class OutgoingBatchConnection {
|
||||
|
||||
private final BatchTransportWriter trans;
|
||||
private final ConnectionWriterFactory connFactory;
|
||||
private final ConnectionWriter conn;
|
||||
private final DatabaseComponent db;
|
||||
private final ProtocolWriterFactory protoFactory;
|
||||
private final int transportId;
|
||||
private final long connection;
|
||||
private final ContactId contactId;
|
||||
|
||||
OutgoingBatchConnection(BatchTransportWriter trans,
|
||||
ConnectionWriterFactory connFactory, DatabaseComponent db,
|
||||
ProtocolWriterFactory protoFactory, int transportId,
|
||||
long connection, ContactId contactId) {
|
||||
this.trans = trans;
|
||||
this.connFactory = connFactory;
|
||||
OutgoingBatchConnection(ConnectionWriter conn, DatabaseComponent db,
|
||||
ProtocolWriterFactory protoFactory, ContactId contactId) {
|
||||
this.conn = conn;
|
||||
this.db = db;
|
||||
this.protoFactory = protoFactory;
|
||||
this.transportId = transportId;
|
||||
this.connection = connection;
|
||||
this.contactId = contactId;
|
||||
}
|
||||
|
||||
void write() throws DbException, IOException {
|
||||
byte[] secret = db.getSharedSecret(contactId);
|
||||
ConnectionWriter conn = connFactory.createConnectionWriter(
|
||||
trans.getOutputStream(), true, transportId, connection, secret);
|
||||
OutputStream out = conn.getOutputStream();
|
||||
// There should be enough space for a packet
|
||||
long capacity = conn.getCapacity(trans.getCapacity());
|
||||
long capacity = conn.getCapacity();
|
||||
if(capacity < MAX_PACKET_LENGTH) throw new IOException();
|
||||
// Write a transport update
|
||||
TransportWriter t = protoFactory.createTransportWriter(out);
|
||||
db.generateTransportUpdate(contactId, t);
|
||||
// If there's space, write a subscription update
|
||||
capacity = conn.getCapacity(trans.getCapacity());
|
||||
capacity = conn.getCapacity();
|
||||
if(capacity >= MAX_PACKET_LENGTH) {
|
||||
SubscriptionWriter s = protoFactory.createSubscriptionWriter(out);
|
||||
db.generateSubscriptionUpdate(contactId, s);
|
||||
@@ -60,14 +47,14 @@ class OutgoingBatchConnection {
|
||||
// Write acks until you can't write acks no more
|
||||
AckWriter a = protoFactory.createAckWriter(out);
|
||||
do {
|
||||
capacity = conn.getCapacity(trans.getCapacity());
|
||||
capacity = conn.getCapacity();
|
||||
int max = (int) Math.min(MAX_PACKET_LENGTH, capacity);
|
||||
a.setMaxPacketLength(max);
|
||||
} while(db.generateAck(contactId, a));
|
||||
// Write batches until you can't write batches no more
|
||||
BatchWriter b = protoFactory.createBatchWriter(out);
|
||||
do {
|
||||
capacity = conn.getCapacity(trans.getCapacity());
|
||||
capacity = conn.getCapacity();
|
||||
int max = (int) Math.min(MAX_PACKET_LENGTH, capacity);
|
||||
b.setMaxPacketLength(max);
|
||||
} while(db.generateBatch(contactId, b));
|
||||
|
||||
Reference in New Issue
Block a user