Changed maximum packet and message sizes in preparation for new

transport format.
This commit is contained in:
akwizgran
2011-08-18 15:14:48 +02:00
parent 5e0aadd373
commit 4dd303d9e1
40 changed files with 210 additions and 146 deletions

View File

@@ -5,6 +5,7 @@ import java.util.Collection;
import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.Consumer;
import net.sf.briar.api.serial.ObjectReader;
@@ -22,7 +23,8 @@ class AckReader implements ObjectReader<Ack> {
public Ack readObject(Reader r) throws IOException {
// Initialise the consumer
Consumer counting = new CountingConsumer(Ack.MAX_SIZE);
Consumer counting =
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
// Read and digest the data
r.addConsumer(counting);
r.readUserDefinedTag(Tags.ACK);

View File

@@ -8,6 +8,7 @@ import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.protocol.Batch;
import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.Consumer;
import net.sf.briar.api.serial.ObjectReader;
@@ -28,7 +29,8 @@ class BatchReader implements ObjectReader<Batch> {
public Batch readObject(Reader r) throws IOException {
// Initialise the consumers
Consumer counting = new CountingConsumer(Batch.MAX_SIZE);
Consumer counting =
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
DigestingConsumer digesting = new DigestingConsumer(messageDigest);
messageDigest.reset();
// Read and digest the data

View File

@@ -41,7 +41,7 @@ class MessageReader implements ObjectReader<Message> {
public Message readObject(Reader r) throws IOException {
CopyingConsumer copying = new CopyingConsumer();
CountingConsumer counting = new CountingConsumer(Message.MAX_SIZE);
CountingConsumer counting = new CountingConsumer(Message.MAX_LENGTH);
r.addConsumer(copying);
r.addConsumer(counting);
// Read the initial tag
@@ -64,7 +64,7 @@ class MessageReader implements ObjectReader<Message> {
long timestamp = r.readInt64();
if(timestamp < 0L) throw new FormatException();
// Skip the message body
r.readBytes(Message.MAX_SIZE);
r.readBytes(Message.MAX_LENGTH);
// Record the length of the data covered by the author's signature
int signedByAuthor = (int) counting.getCount();
// Read the author's signature, if there is one

View File

@@ -8,6 +8,7 @@ import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.Offer;
import net.sf.briar.api.protocol.OfferId;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.Consumer;
import net.sf.briar.api.serial.ObjectReader;
@@ -28,7 +29,8 @@ class OfferReader implements ObjectReader<Offer> {
public Offer readObject(Reader r) throws IOException {
// Initialise the consumers
Consumer counting = new CountingConsumer(Offer.MAX_SIZE);
Consumer counting =
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
DigestingConsumer digesting = new DigestingConsumer(messageDigest);
messageDigest.reset();
// Read the data

View File

@@ -4,6 +4,7 @@ import java.io.IOException;
import java.util.BitSet;
import net.sf.briar.api.protocol.OfferId;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Request;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.Consumer;
@@ -23,13 +24,14 @@ class RequestReader implements ObjectReader<Request> {
public Request readObject(Reader r) throws IOException {
// Initialise the consumer
Consumer counting = new CountingConsumer(Request.MAX_SIZE);
Consumer counting =
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
// Read the data
r.addConsumer(counting);
r.readUserDefinedTag(Tags.REQUEST);
r.addObjectReader(Tags.OFFER_ID, offerIdReader);
OfferId offerId = r.readUserDefined(Tags.OFFER_ID, OfferId.class);
byte[] bitmap = r.readBytes(Request.MAX_SIZE);
byte[] bitmap = r.readBytes(ProtocolConstants.MAX_PACKET_LENGTH);
r.removeConsumer(counting);
// Convert the bitmap into a BitSet
BitSet b = new BitSet(bitmap.length * 8);

View File

@@ -4,6 +4,7 @@ import java.io.IOException;
import java.util.Map;
import net.sf.briar.api.protocol.Group;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.SubscriptionUpdate;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.Consumer;
@@ -23,7 +24,8 @@ class SubscriptionReader implements ObjectReader<SubscriptionUpdate> {
public SubscriptionUpdate readObject(Reader r) throws IOException {
// Initialise the consumer
Consumer counting = new CountingConsumer(SubscriptionUpdate.MAX_SIZE);
Consumer counting =
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
// Read the data
r.addConsumer(counting);
r.readUserDefinedTag(Tags.SUBSCRIPTION_UPDATE);

View File

@@ -5,6 +5,7 @@ import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.TransportUpdate;
import net.sf.briar.api.serial.Consumer;
@@ -23,12 +24,13 @@ class TransportReader implements ObjectReader<TransportUpdate> {
public TransportUpdate readObject(Reader r) throws IOException {
// Initialise the consumer
Consumer counting = new CountingConsumer(TransportUpdate.MAX_SIZE);
Consumer counting =
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
// Read the data
r.addConsumer(counting);
r.readUserDefinedTag(Tags.TRANSPORT_UPDATE);
r.addObjectReader(Tags.TRANSPORT_PROPERTIES, propertiesReader);
r.setMaxStringLength(TransportUpdate.MAX_SIZE);
r.setMaxStringLength(ProtocolConstants.MAX_PACKET_LENGTH);
List<TransportProperties> l = r.readList(TransportProperties.class);
r.resetMaxStringLength();
r.removeObjectReader(Tags.TRANSPORT_PROPERTIES);

View File

@@ -3,8 +3,8 @@ package net.sf.briar.protocol.writers;
import java.io.IOException;
import java.io.OutputStream;
import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.writers.AckWriter;
import net.sf.briar.api.serial.Writer;
@@ -28,8 +28,11 @@ class AckWriterImpl implements AckWriter {
w.writeListStart();
started = true;
}
int capacity = Ack.MAX_SIZE - (int) w.getBytesWritten() - 1;
if(capacity < BatchId.SERIALISED_LENGTH) return false;
int capacity = ProtocolConstants.MAX_PACKET_LENGTH
- (int) w.getBytesWritten() - 1;
// Allow one byte for the BATCH_ID tag, one byte for the BYTES tag and
// one byte for the length as a uint7
if(capacity < BatchId.LENGTH + 3) return false;
b.writeTo(w);
return true;
}

View File

@@ -5,8 +5,8 @@ import java.io.OutputStream;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import net.sf.briar.api.protocol.Batch;
import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.writers.BatchWriter;
import net.sf.briar.api.serial.Writer;
@@ -28,7 +28,9 @@ class BatchWriterImpl implements BatchWriter {
}
public int getCapacity() {
return Batch.MAX_SIZE - 3;
// Allow one byte for the batch tag, one for the list start tag and
// one for the list end tag
return ProtocolConstants.MAX_PACKET_LENGTH - 3;
}
public boolean writeMessage(byte[] message) throws IOException {
@@ -38,7 +40,8 @@ class BatchWriterImpl implements BatchWriter {
w.writeListStart();
started = true;
}
int capacity = Batch.MAX_SIZE - (int) w.getBytesWritten() - 1;
int capacity = ProtocolConstants.MAX_PACKET_LENGTH
- (int) w.getBytesWritten() - 1;
if(capacity < message.length) return false;
// Bypass the writer and write each raw message directly
out.write(message);

View File

@@ -6,8 +6,8 @@ import java.security.DigestOutputStream;
import java.security.MessageDigest;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.Offer;
import net.sf.briar.api.protocol.OfferId;
import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.writers.OfferWriter;
import net.sf.briar.api.serial.Writer;
@@ -35,8 +35,11 @@ class OfferWriterImpl implements OfferWriter {
w.writeListStart();
started = true;
}
int capacity = Offer.MAX_SIZE - (int) w.getBytesWritten() - 1;
if(capacity < MessageId.SERIALISED_LENGTH) return false;
int capacity = ProtocolConstants.MAX_PACKET_LENGTH
- (int) w.getBytesWritten() - 1;
// Allow one byte for the MESSAGE_ID tag, one byte for the BYTES tag
// and one byte for the length as a uint7
if(capacity < MessageId.LENGTH + 3) return false;
m.writeTo(w);
return true;
}

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.security.InvalidKeyException;
import java.util.HashMap;
import java.util.Map;
@@ -92,7 +94,7 @@ DatabaseListener {
public synchronized ContactId acceptConnection(byte[] tag)
throws DbException {
if(tag.length != Constants.TAG_BYTES)
if(tag.length != TAG_LENGTH)
throw new IllegalArgumentException();
if(!initialised) initialise();
Bytes b = new Bytes(tag);

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.MAX_32_BIT_UNSIGNED;
import java.util.ArrayList;
import java.util.Collection;
@@ -31,7 +33,7 @@ class ConnectionWindowImpl implements ConnectionWindow {
private int getOffset(long connection) {
if(connection < 0L) throw new IllegalArgumentException();
if(connection > Constants.MAX_32_BIT_UNSIGNED)
if(connection > MAX_32_BIT_UNSIGNED)
throw new IllegalArgumentException();
int offset = (int) (connection - centre) + 16;
if(offset < 0 || offset > 31) throw new IllegalArgumentException();
@@ -56,11 +58,12 @@ class ConnectionWindowImpl implements ConnectionWindow {
int mask = 0x80000000 >>> i;
if((bitmap & mask) == 0) {
long c = centre - 16 + i;
if(c >= 0L && c <= Constants.MAX_32_BIT_UNSIGNED) unseen.add(c);
if(c >= 0L && c <= MAX_32_BIT_UNSIGNED) unseen.add(c);
}
}
assert unseen.contains(centre)
|| centre == Constants.MAX_32_BIT_UNSIGNED + 1;
// The centre of the window should be an unseen value unless the
// maximum possible value has been seen
assert unseen.contains(centre) || centre == MAX_32_BIT_UNSIGNED + 1;
return unseen;
}
}

View File

@@ -1,8 +0,0 @@
package net.sf.briar.transport;
interface Constants {
static final int TAG_BYTES = 16;
static final int MAX_16_BIT_UNSIGNED = 65535; // 2^16 - 1
static final long MAX_32_BIT_UNSIGNED = 4294967295L; // 2^32 - 1
}

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.io.EOFException;
import java.io.FilterInputStream;
import java.io.IOException;
@@ -21,16 +23,16 @@ class PacketDecrypterImpl extends FilterInputStream implements PacketDecrypter {
private final SecretKey packetKey;
private byte[] cipherBuf, plainBuf;
private int bufOff = 0, bufLen = Constants.TAG_BYTES;
private int bufOff = 0, bufLen = TAG_LENGTH;
private boolean betweenPackets = true;
PacketDecrypterImpl(byte[] firstTag, InputStream in, Cipher tagCipher,
Cipher packetCipher, SecretKey tagKey, SecretKey packetKey) {
super(in);
if(firstTag.length != Constants.TAG_BYTES)
if(firstTag.length != TAG_LENGTH)
throw new IllegalArgumentException();
cipherBuf = Arrays.copyOf(firstTag, firstTag.length);
plainBuf = new byte[Constants.TAG_BYTES];
plainBuf = new byte[TAG_LENGTH];
this.tagCipher = tagCipher;
this.packetCipher = packetCipher;
this.packetKey = packetKey;
@@ -39,7 +41,7 @@ class PacketDecrypterImpl extends FilterInputStream implements PacketDecrypter {
} catch(InvalidKeyException e) {
throw new IllegalArgumentException(e);
}
if(tagCipher.getOutputSize(Constants.TAG_BYTES) != Constants.TAG_BYTES)
if(tagCipher.getOutputSize(TAG_LENGTH) != TAG_LENGTH)
throw new IllegalArgumentException();
}
@@ -48,7 +50,7 @@ class PacketDecrypterImpl extends FilterInputStream implements PacketDecrypter {
}
public byte[] readTag() throws IOException {
byte[] tag = new byte[Constants.TAG_BYTES];
byte[] tag = new byte[TAG_LENGTH];
System.arraycopy(cipherBuf, bufOff, tag, 0, bufLen);
int offset = bufLen;
bufOff = bufLen = 0;

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
@@ -29,7 +31,7 @@ implements PacketEncrypter {
} catch(InvalidKeyException e) {
throw new IllegalArgumentException(e);
}
if(tagCipher.getOutputSize(Constants.TAG_BYTES) != Constants.TAG_BYTES)
if(tagCipher.getOutputSize(TAG_LENGTH) != TAG_LENGTH)
throw new IllegalArgumentException();
}
@@ -38,8 +40,7 @@ implements PacketEncrypter {
}
public void writeTag(byte[] tag) throws IOException {
if(tag.length != Constants.TAG_BYTES)
throw new IllegalArgumentException();
if(tag.length != TAG_LENGTH) throw new IllegalArgumentException();
IvParameterSpec iv = new IvParameterSpec(tag);
try {
out.write(tagCipher.doFinal(tag));

View File

@@ -1,5 +1,7 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.MAX_32_BIT_UNSIGNED;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -78,8 +80,7 @@ class PacketReaderImpl extends FilterInputStream implements PacketReader {
private void readTag() throws IOException {
assert betweenPackets;
if(packet > Constants.MAX_32_BIT_UNSIGNED)
throw new IllegalStateException();
if(packet > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
byte[] tag = decrypter.readTag();
if(tag == null) return; // EOF
if(!TagDecoder.decodeTag(tag, transportId, connection, packet))

View File

@@ -1,5 +1,8 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.MAX_16_BIT_UNSIGNED;
import static net.sf.briar.api.transport.TransportConstants.MAX_32_BIT_UNSIGNED;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
@@ -24,11 +27,11 @@ class PacketWriterImpl extends FilterOutputStream implements PacketWriter {
this.encrypter = encrypter;
this.mac = mac;
if(transportId < 0) throw new IllegalArgumentException();
if(transportId > Constants.MAX_16_BIT_UNSIGNED)
if(transportId > MAX_16_BIT_UNSIGNED)
throw new IllegalArgumentException();
this.transportId = transportId;
if(connection < 0L) throw new IllegalArgumentException();
if(connection > Constants.MAX_32_BIT_UNSIGNED)
if(connection > MAX_32_BIT_UNSIGNED)
throw new IllegalArgumentException();
this.connection = connection;
}
@@ -68,8 +71,7 @@ class PacketWriterImpl extends FilterOutputStream implements PacketWriter {
private void writeTag() throws IOException {
assert betweenPackets;
if(packet > Constants.MAX_32_BIT_UNSIGNED)
throw new IllegalStateException();
if(packet > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
byte[] tag = TagEncoder.encodeTag(transportId, connection,
packet);
// Write the tag to the encrypter and start calculating the MAC

View File

@@ -1,10 +1,12 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
class TagDecoder {
static boolean decodeTag(byte[] tag, int transportId, long connection,
long packet) {
if(tag.length != Constants.TAG_BYTES) return false;
if(tag.length != TAG_LENGTH) return false;
// First 16 bits must be zero
if(readUint16(tag, 0) != 0) return false;
// Transport identifier is encoded as an unsigned 16-bit integer

View File

@@ -1,10 +1,14 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAX_16_BIT_UNSIGNED;
import static net.sf.briar.api.transport.TransportConstants.MAX_32_BIT_UNSIGNED;
class TagEncoder {
static byte[] encodeTag(int transportId, long connection,
long packet) {
byte[] tag = new byte[Constants.TAG_BYTES];
byte[] tag = new byte[TAG_LENGTH];
// Encode the transport identifier as an unsigned 16-bit integer
writeUint16(transportId, tag, 2);
// Encode the connection number as an unsigned 32-bit integer
@@ -17,7 +21,7 @@ class TagEncoder {
// Package access for testing
static void writeUint16(int i, byte[] b, int offset) {
assert i >= 0;
assert i <= Constants.MAX_16_BIT_UNSIGNED;
assert i <= MAX_16_BIT_UNSIGNED;
assert b.length >= offset + 2;
b[offset] = (byte) (i >> 8);
b[offset + 1] = (byte) (i & 0xFF);
@@ -26,7 +30,7 @@ class TagEncoder {
// Package access for testing
static void writeUint32(long i, byte[] b, int offset) {
assert i >= 0L;
assert i <= Constants.MAX_32_BIT_UNSIGNED;
assert i <= MAX_32_BIT_UNSIGNED;
assert b.length >= offset + 4;
b[offset] = (byte) (i >> 24);
b[offset + 1] = (byte) (i >> 16 & 0xFF);