Use predicates to specify records to accept or ignore.

This commit is contained in:
akwizgran
2019-04-08 18:13:26 +01:00
parent 9ce71088e2
commit cc49648e37
9 changed files with 263 additions and 223 deletions

View File

@@ -1,6 +1,7 @@
package org.briarproject.bramble.contact;
import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.Predicate;
import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.contact.ContactExchangeTask;
import org.briarproject.bramble.api.contact.ContactId;
@@ -61,6 +62,20 @@ class ContactExchangeTaskImpl extends Thread implements ContactExchangeTask {
private static final String SIGNING_LABEL_EXCHANGE =
"org.briarproject.briar.contact/EXCHANGE";
// Accept records with current protocol version, known record type
private static final Predicate<Record> ACCEPT = r ->
r.getProtocolVersion() == PROTOCOL_VERSION &&
isKnownRecordType(r.getRecordType());
// Ignore records with current protocol version, unknown record type
private static final Predicate<Record> IGNORE = r ->
r.getProtocolVersion() == PROTOCOL_VERSION &&
!isKnownRecordType(r.getRecordType());
private static boolean isKnownRecordType(byte type) {
return type == CONTACT_INFO;
}
private final DatabaseComponent db;
private final ClientHelper clientHelper;
private final RecordReaderFactory recordReaderFactory;
@@ -191,11 +206,7 @@ class ContactExchangeTaskImpl extends Thread implements ContactExchangeTask {
// Send EOF on the outgoing stream
streamWriter.sendEndOfStream();
// Skip any remaining records from the incoming stream
try {
while (true) recordReader.readRecord();
} catch (EOFException expected) {
LOG.info("End of stream");
}
recordReader.readRecord(r -> false, IGNORE);
} catch (IOException e) {
logException(LOG, WARNING, e);
eventBus.broadcast(new ContactExchangeFailedEvent());
@@ -268,12 +279,8 @@ class ContactExchangeTaskImpl extends Thread implements ContactExchangeTask {
private ContactInfo receiveContactInfo(RecordReader recordReader)
throws IOException {
Record record;
do {
record = recordReader.readRecord();
if (record.getProtocolVersion() != PROTOCOL_VERSION)
throw new FormatException();
} while (record.getRecordType() != CONTACT_INFO);
Record record = recordReader.readRecord(ACCEPT, IGNORE);
if (record == null) throw new EOFException();
LOG.info("Received contact info");
BdfList payload = clientHelper.toList(record.getPayload());
checkSize(payload, 4);

View File

@@ -1,5 +1,6 @@
package org.briarproject.bramble.keyagreement;
import org.briarproject.bramble.api.Predicate;
import org.briarproject.bramble.api.keyagreement.KeyAgreementConnection;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.TransportId;
@@ -10,6 +11,7 @@ import org.briarproject.bramble.api.record.RecordReaderFactory;
import org.briarproject.bramble.api.record.RecordWriter;
import org.briarproject.bramble.api.record.RecordWriterFactory;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -31,6 +33,20 @@ class KeyAgreementTransport {
private static final Logger LOG =
Logger.getLogger(KeyAgreementTransport.class.getName());
// Accept records with current protocol version, known record type
private static Predicate<Record> ACCEPT = r ->
r.getProtocolVersion() == PROTOCOL_VERSION &&
isKnownRecordType(r.getRecordType());
// Ignore records with current protocol version, unknown record type
private static Predicate<Record> IGNORE = r ->
r.getProtocolVersion() == PROTOCOL_VERSION &&
!isKnownRecordType(r.getRecordType());
private static boolean isKnownRecordType(byte type) {
return type == KEY || type == CONFIRM || type == ABORT;
}
private final KeyAgreementConnection kac;
private final RecordReader reader;
private final RecordWriter writer;
@@ -94,22 +110,15 @@ class KeyAgreementTransport {
}
private byte[] readRecord(byte expectedType) throws AbortException {
while (true) {
try {
Record record = reader.readRecord();
// Reject unrecognised protocol version
if (record.getProtocolVersion() != PROTOCOL_VERSION)
throw new AbortException(false);
byte type = record.getRecordType();
if (type == ABORT) throw new AbortException(true);
if (type == expectedType) return record.getPayload();
// Reject recognised but unexpected record type
if (type == KEY || type == CONFIRM)
throw new AbortException(false);
// Skip unrecognised record type
} catch (IOException e) {
throw new AbortException(e);
}
try {
Record record = reader.readRecord(ACCEPT, IGNORE);
if (record == null) throw new AbortException(new EOFException());
byte type = record.getRecordType();
if (type == ABORT) throw new AbortException(true);
if (type != expectedType) throw new AbortException(false);
return record.getPayload();
} catch (IOException e) {
throw new AbortException(e);
}
}
}

View File

@@ -1,15 +1,18 @@
package org.briarproject.bramble.record;
import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.Predicate;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.record.Record;
import org.briarproject.bramble.api.record.RecordReader;
import org.briarproject.bramble.util.ByteUtils;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
import static org.briarproject.bramble.api.record.Record.MAX_RECORD_PAYLOAD_BYTES;
@@ -23,6 +26,7 @@ class RecordReaderImpl implements RecordReader {
private final byte[] header = new byte[RECORD_HEADER_BYTES];
RecordReaderImpl(InputStream in) {
if (!in.markSupported()) in = new BufferedInputStream(in, 1);
this.in = new DataInputStream(in);
}
@@ -39,8 +43,27 @@ class RecordReaderImpl implements RecordReader {
return new Record(protocolVersion, recordType, payload);
}
@Nullable
@Override
public Record readRecord(Predicate<Record> accept, Predicate<Record> ignore)
throws IOException {
while (true) {
if (eof()) return null;
Record r = readRecord();
if (accept.test(r)) return r;
if (!ignore.test(r)) throw new FormatException();
}
}
@Override
public void close() throws IOException {
in.close();
}
private boolean eof() throws IOException {
in.mark(1);
int next = in.read();
in.reset();
return next == -1;
}
}

View File

@@ -1,6 +1,7 @@
package org.briarproject.bramble.sync;
import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.Predicate;
import org.briarproject.bramble.api.UniqueId;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.record.Record;
@@ -14,7 +15,6 @@ import org.briarproject.bramble.api.sync.Request;
import org.briarproject.bramble.api.sync.SyncRecordReader;
import org.briarproject.bramble.util.ByteUtils;
import java.io.EOFException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@@ -33,6 +33,21 @@ import static org.briarproject.bramble.api.sync.SyncConstants.PROTOCOL_VERSION;
@NotNullByDefault
class SyncRecordReaderImpl implements SyncRecordReader {
// Accept records with current protocol version, known record type
private static final Predicate<Record> ACCEPT = r ->
r.getProtocolVersion() == PROTOCOL_VERSION &&
isKnownRecordType(r.getRecordType());
// Ignore records with current protocol version, unknown record type
private static final Predicate<Record> IGNORE = r ->
r.getProtocolVersion() == PROTOCOL_VERSION &&
!isKnownRecordType(r.getRecordType());
private static boolean isKnownRecordType(byte type) {
return type == ACK || type == MESSAGE || type == OFFER ||
type == REQUEST;
}
private final MessageFactory messageFactory;
private final RecordReader reader;
@@ -45,22 +60,6 @@ class SyncRecordReaderImpl implements SyncRecordReader {
this.reader = reader;
}
private void readRecord() throws IOException {
if (nextRecord != null) throw new AssertionError();
while (true) {
nextRecord = reader.readRecord();
// Check the protocol version
byte version = nextRecord.getProtocolVersion();
if (version != PROTOCOL_VERSION) throw new FormatException();
byte type = nextRecord.getRecordType();
// Return if this is a known record type, otherwise continue
if (type == ACK || type == MESSAGE || type == OFFER ||
type == REQUEST) {
return;
}
}
}
private byte getNextRecordType() {
if (nextRecord == null) throw new AssertionError();
return nextRecord.getRecordType();
@@ -78,14 +77,9 @@ class SyncRecordReaderImpl implements SyncRecordReader {
public boolean eof() throws IOException {
if (nextRecord != null) return false;
if (eof) return true;
try {
readRecord();
return false;
} catch (EOFException e) {
nextRecord = null;
eof = true;
return true;
}
nextRecord = reader.readRecord(ACCEPT, IGNORE);
if (nextRecord == null) eof = true;
return eof;
}
@Override
@@ -154,5 +148,4 @@ class SyncRecordReaderImpl implements SyncRecordReader {
if (!hasRequest()) throw new FormatException();
return new Request(readMessageIds());
}
}