mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 20:29:52 +01:00
Updated transport constants and renamed some test classes.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package net.sf.briar.protocol;
|
||||
|
||||
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.BitSet;
|
||||
@@ -10,7 +12,6 @@ import net.sf.briar.api.protocol.BatchId;
|
||||
import net.sf.briar.api.protocol.Group;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.Offer;
|
||||
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
|
||||
import net.sf.briar.api.protocol.ProtocolWriter;
|
||||
import net.sf.briar.api.protocol.RawBatch;
|
||||
import net.sf.briar.api.protocol.Request;
|
||||
|
||||
@@ -49,8 +49,8 @@ class ConnectionReaderFactoryImpl implements ConnectionReaderFactory {
|
||||
Cipher tagCipher = crypto.getTagCipher();
|
||||
Cipher frameCipher = crypto.getFrameCipher();
|
||||
Mac mac = crypto.getMac();
|
||||
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in, tagCipher,
|
||||
frameCipher, tagKey, frameKey, mac.getMacLength(), false);
|
||||
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
|
||||
tagCipher, frameCipher, tagKey, frameKey, false);
|
||||
// Create the reader
|
||||
return new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||
|
||||
@@ -18,13 +19,13 @@ class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
||||
|
||||
private final IncomingEncryptionLayer decrypter;
|
||||
private final Mac mac;
|
||||
private final int macLength;
|
||||
private final byte[] buf;
|
||||
|
||||
private long frame = 0L;
|
||||
private int bufOffset = 0, bufLength = 0;
|
||||
private int offset = 0, length = 0;
|
||||
|
||||
ConnectionReaderImpl(IncomingEncryptionLayer decrypter, Mac mac, ErasableKey macKey) {
|
||||
ConnectionReaderImpl(IncomingEncryptionLayer decrypter, Mac mac,
|
||||
ErasableKey macKey) {
|
||||
this.decrypter = decrypter;
|
||||
this.mac = mac;
|
||||
// Initialise the MAC
|
||||
@@ -34,7 +35,8 @@ class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
macKey.erase();
|
||||
macLength = mac.getMacLength();
|
||||
if(mac.getMacLength() != MAC_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
buf = new byte[MAX_FRAME_LENGTH];
|
||||
}
|
||||
|
||||
@@ -44,10 +46,10 @@ class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
while(bufLength == 0) if(!readFrame()) return -1;
|
||||
int b = buf[bufOffset] & 0xff;
|
||||
bufOffset++;
|
||||
bufLength--;
|
||||
while(length == 0) if(!readFrame()) return -1;
|
||||
int b = buf[offset] & 0xff;
|
||||
offset++;
|
||||
length--;
|
||||
return b;
|
||||
}
|
||||
|
||||
@@ -58,28 +60,27 @@ class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
||||
|
||||
@Override
|
||||
public int read(byte[] b, int off, int len) throws IOException {
|
||||
while(bufLength == 0) if(!readFrame()) return -1;
|
||||
len = Math.min(len, bufLength);
|
||||
System.arraycopy(buf, bufOffset, b, off, len);
|
||||
bufOffset += len;
|
||||
bufLength -= len;
|
||||
while(length == 0) if(!readFrame()) return -1;
|
||||
len = Math.min(len, length);
|
||||
System.arraycopy(buf, offset, b, off, len);
|
||||
offset += len;
|
||||
length -= len;
|
||||
return len;
|
||||
}
|
||||
|
||||
private boolean readFrame() throws IOException {
|
||||
assert bufLength == 0;
|
||||
assert length == 0;
|
||||
// Don't allow more than 2^32 frames to be read
|
||||
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
||||
// Read a frame
|
||||
int length = decrypter.readFrame(buf);
|
||||
if(length == -1) return false;
|
||||
int frameLength = decrypter.readFrame(buf);
|
||||
if(frameLength == -1) return false;
|
||||
// Check that the frame number is correct and the length is legal
|
||||
int max = MAX_FRAME_LENGTH - FRAME_HEADER_LENGTH - macLength;
|
||||
if(!HeaderEncoder.validateHeader(buf, frame, max))
|
||||
if(!HeaderEncoder.validateHeader(buf, frame))
|
||||
throw new FormatException();
|
||||
int payload = HeaderEncoder.getPayloadLength(buf);
|
||||
int padding = HeaderEncoder.getPaddingLength(buf);
|
||||
if(length != FRAME_HEADER_LENGTH + payload + padding + macLength)
|
||||
if(frameLength != FRAME_HEADER_LENGTH + payload + padding + MAC_LENGTH)
|
||||
throw new FormatException();
|
||||
// Check that the padding is all zeroes
|
||||
int paddingStart = FRAME_HEADER_LENGTH + payload;
|
||||
@@ -90,11 +91,11 @@ class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
||||
int macStart = FRAME_HEADER_LENGTH + payload + padding;
|
||||
mac.update(buf, 0, macStart);
|
||||
byte[] expectedMac = mac.doFinal();
|
||||
for(int i = 0; i < macLength; i++) {
|
||||
for(int i = 0; i < expectedMac.length; i++) {
|
||||
if(expectedMac[i] != buf[macStart + i]) throw new FormatException();
|
||||
}
|
||||
bufOffset = FRAME_HEADER_LENGTH;
|
||||
bufLength = payload;
|
||||
offset = FRAME_HEADER_LENGTH;
|
||||
length = payload;
|
||||
frame++;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||
|
||||
@@ -26,7 +27,7 @@ class ConnectionWriterImpl extends OutputStream implements ConnectionWriter {
|
||||
private final Mac mac;
|
||||
private final byte[] buf;
|
||||
|
||||
private int bufLength = FRAME_HEADER_LENGTH;
|
||||
private int length = FRAME_HEADER_LENGTH;
|
||||
private long frame = 0L;
|
||||
|
||||
ConnectionWriterImpl(OutgoingEncryptionLayer encrypter, Mac mac,
|
||||
@@ -40,6 +41,8 @@ class ConnectionWriterImpl extends OutputStream implements ConnectionWriter {
|
||||
throw new IllegalArgumentException(badKey);
|
||||
}
|
||||
macKey.erase();
|
||||
if(mac.getMacLength() != MAC_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
buf = new byte[MAX_FRAME_LENGTH];
|
||||
}
|
||||
|
||||
@@ -49,25 +52,25 @@ class ConnectionWriterImpl extends OutputStream implements ConnectionWriter {
|
||||
|
||||
public long getRemainingCapacity() {
|
||||
long capacity = encrypter.getRemainingCapacity();
|
||||
// If there's any data buffered, subtract it and its auth overhead
|
||||
if(bufLength > FRAME_HEADER_LENGTH)
|
||||
capacity -= bufLength + mac.getMacLength();
|
||||
// Subtract the auth overhead from the remaining capacity
|
||||
// If there's any data buffered, subtract it and its overhead
|
||||
if(length > FRAME_HEADER_LENGTH)
|
||||
capacity -= length + MAC_LENGTH;
|
||||
// Subtract the overhead from the remaining capacity
|
||||
long frames = (long) Math.ceil((double) capacity / MAX_FRAME_LENGTH);
|
||||
int overheadPerFrame = FRAME_HEADER_LENGTH + mac.getMacLength();
|
||||
int overheadPerFrame = FRAME_HEADER_LENGTH + MAC_LENGTH;
|
||||
return Math.max(0L, capacity - frames * overheadPerFrame);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() throws IOException {
|
||||
if(bufLength > FRAME_HEADER_LENGTH) writeFrame();
|
||||
if(length > FRAME_HEADER_LENGTH) writeFrame();
|
||||
encrypter.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
buf[bufLength++] = (byte) b;
|
||||
if(bufLength + mac.getMacLength() == MAX_FRAME_LENGTH) writeFrame();
|
||||
buf[length++] = (byte) b;
|
||||
if(length + MAC_LENGTH == MAX_FRAME_LENGTH) writeFrame();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -77,32 +80,32 @@ class ConnectionWriterImpl extends OutputStream implements ConnectionWriter {
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
int available = MAX_FRAME_LENGTH - bufLength - mac.getMacLength();
|
||||
int available = MAX_FRAME_LENGTH - length - MAC_LENGTH;
|
||||
while(available <= len) {
|
||||
System.arraycopy(b, off, buf, bufLength, available);
|
||||
bufLength += available;
|
||||
System.arraycopy(b, off, buf, length, available);
|
||||
length += available;
|
||||
writeFrame();
|
||||
off += available;
|
||||
len -= available;
|
||||
available = MAX_FRAME_LENGTH - bufLength - mac.getMacLength();
|
||||
available = MAX_FRAME_LENGTH - length - MAC_LENGTH;
|
||||
}
|
||||
System.arraycopy(b, off, buf, bufLength, len);
|
||||
bufLength += len;
|
||||
System.arraycopy(b, off, buf, length, len);
|
||||
length += len;
|
||||
}
|
||||
|
||||
private void writeFrame() throws IOException {
|
||||
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
||||
int payloadLength = bufLength - FRAME_HEADER_LENGTH;
|
||||
int payloadLength = length - FRAME_HEADER_LENGTH;
|
||||
assert payloadLength > 0;
|
||||
HeaderEncoder.encodeHeader(buf, frame, payloadLength, 0);
|
||||
mac.update(buf, 0, bufLength);
|
||||
mac.update(buf, 0, length);
|
||||
try {
|
||||
mac.doFinal(buf, bufLength);
|
||||
mac.doFinal(buf, length);
|
||||
} catch(ShortBufferException badMac) {
|
||||
throw new RuntimeException(badMac);
|
||||
}
|
||||
encrypter.writeFrame(buf, bufLength + mac.getMacLength());
|
||||
bufLength = FRAME_HEADER_LENGTH;
|
||||
encrypter.writeFrame(buf, length + MAC_LENGTH);
|
||||
length = FRAME_HEADER_LENGTH;
|
||||
frame++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +1,43 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import net.sf.briar.util.ByteUtils;
|
||||
|
||||
class HeaderEncoder {
|
||||
|
||||
static void encodeHeader(byte[] header, long frame, int payload,
|
||||
static void encodeHeader(byte[] header, long frameNumber, int payload,
|
||||
int padding) {
|
||||
if(header.length < FRAME_HEADER_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
if(frame < 0 || frame > ByteUtils.MAX_32_BIT_UNSIGNED)
|
||||
if(frameNumber < 0 || frameNumber > ByteUtils.MAX_32_BIT_UNSIGNED)
|
||||
throw new IllegalArgumentException();
|
||||
if(payload < 0 || payload > ByteUtils.MAX_16_BIT_UNSIGNED)
|
||||
throw new IllegalArgumentException();
|
||||
if(padding < 0 || padding > ByteUtils.MAX_16_BIT_UNSIGNED)
|
||||
throw new IllegalArgumentException();
|
||||
ByteUtils.writeUint32(frame, header, 0);
|
||||
ByteUtils.writeUint32(frameNumber, header, 0);
|
||||
ByteUtils.writeUint16(payload, header, 4);
|
||||
ByteUtils.writeUint16(padding, header, 6);
|
||||
}
|
||||
|
||||
static boolean validateHeader(byte[] header, long frame, int max) {
|
||||
static boolean validateHeader(byte[] header, long frameNumber) {
|
||||
if(header.length < FRAME_HEADER_LENGTH) return false;
|
||||
if(ByteUtils.readUint32(header, 0) != frame) return false;
|
||||
if(ByteUtils.readUint32(header, 0) != frameNumber) return false;
|
||||
int payload = ByteUtils.readUint16(header, 4);
|
||||
int padding = ByteUtils.readUint16(header, 6);
|
||||
if(payload + padding > max) return false;
|
||||
int frameLength = FRAME_HEADER_LENGTH + payload + padding + MAC_LENGTH;
|
||||
if(frameLength > MAX_FRAME_LENGTH) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static long getFrameNumber(byte[] header) {
|
||||
if(header.length < FRAME_HEADER_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
return ByteUtils.readUint32(header, 0);
|
||||
}
|
||||
|
||||
static int getPayloadLength(byte[] header) {
|
||||
if(header.length < FRAME_HEADER_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||
|
||||
@@ -21,7 +21,7 @@ class IncomingEncryptionLayerImpl implements IncomingEncryptionLayer {
|
||||
private final InputStream in;
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
private final int macLength, blockSize;
|
||||
private final int blockSize;
|
||||
private final byte[] iv;
|
||||
private final boolean tagEverySegment;
|
||||
|
||||
@@ -29,13 +29,12 @@ class IncomingEncryptionLayerImpl implements IncomingEncryptionLayer {
|
||||
|
||||
IncomingEncryptionLayerImpl(InputStream in, Cipher tagCipher,
|
||||
Cipher frameCipher, ErasableKey tagKey, ErasableKey frameKey,
|
||||
int macLength, boolean tagEverySegment) {
|
||||
boolean tagEverySegment) {
|
||||
this.in = in;
|
||||
this.tagCipher = tagCipher;
|
||||
this.frameCipher = frameCipher;
|
||||
this.tagKey = tagKey;
|
||||
this.frameKey = frameKey;
|
||||
this.macLength = macLength;
|
||||
this.tagEverySegment = tagEverySegment;
|
||||
blockSize = frameCipher.getBlockSize();
|
||||
if(blockSize < FRAME_HEADER_LENGTH)
|
||||
@@ -44,7 +43,6 @@ class IncomingEncryptionLayerImpl implements IncomingEncryptionLayer {
|
||||
}
|
||||
|
||||
public int readFrame(byte[] b) throws IOException {
|
||||
if(b.length < MAX_FRAME_LENGTH) throw new IllegalArgumentException();
|
||||
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
||||
boolean tag = tagEverySegment && frame > 0;
|
||||
// Clear the buffer before exposing it to the transport plugin
|
||||
@@ -85,13 +83,11 @@ class IncomingEncryptionLayerImpl implements IncomingEncryptionLayer {
|
||||
throw new RuntimeException(badCipher);
|
||||
}
|
||||
// Validate and parse the header
|
||||
int max = MAX_FRAME_LENGTH - FRAME_HEADER_LENGTH - macLength;
|
||||
if(!HeaderEncoder.validateHeader(b, frame, max))
|
||||
if(!HeaderEncoder.validateHeader(b, frame))
|
||||
throw new FormatException();
|
||||
int payload = HeaderEncoder.getPayloadLength(b);
|
||||
int padding = HeaderEncoder.getPaddingLength(b);
|
||||
int length = FRAME_HEADER_LENGTH + payload + padding + macLength;
|
||||
if(length > MAX_FRAME_LENGTH) throw new FormatException();
|
||||
int length = FRAME_HEADER_LENGTH + payload + padding + MAC_LENGTH;
|
||||
// Read the remainder of the frame
|
||||
while(offset < length) {
|
||||
int read = in.read(b, offset, length - offset);
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_SEGMENT_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||
|
||||
@@ -21,7 +22,7 @@ class IncomingSegmentedEncryptionLayer implements IncomingEncryptionLayer {
|
||||
private final SegmentSource in;
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
private final int macLength, blockSize;
|
||||
private final int blockSize;
|
||||
private final byte[] iv;
|
||||
private final Segment segment;
|
||||
private final boolean tagEverySegment;
|
||||
@@ -30,13 +31,12 @@ class IncomingSegmentedEncryptionLayer implements IncomingEncryptionLayer {
|
||||
|
||||
IncomingSegmentedEncryptionLayer(SegmentSource in, Cipher tagCipher,
|
||||
Cipher frameCipher, ErasableKey tagKey, ErasableKey frameKey,
|
||||
int macLength, boolean tagEverySegment) {
|
||||
boolean tagEverySegment) {
|
||||
this.in = in;
|
||||
this.tagCipher = tagCipher;
|
||||
this.frameCipher = frameCipher;
|
||||
this.tagKey = tagKey;
|
||||
this.frameKey = frameKey;
|
||||
this.macLength = macLength;
|
||||
this.tagEverySegment = tagEverySegment;
|
||||
blockSize = frameCipher.getBlockSize();
|
||||
if(blockSize < FRAME_HEADER_LENGTH)
|
||||
@@ -46,7 +46,6 @@ class IncomingSegmentedEncryptionLayer implements IncomingEncryptionLayer {
|
||||
}
|
||||
|
||||
public int readFrame(byte[] b) throws IOException {
|
||||
if(b.length < MAX_FRAME_LENGTH) throw new IllegalArgumentException();
|
||||
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
||||
boolean tag = tagEverySegment && frame > 0;
|
||||
// Clear the buffer before exposing it to the transport plugin
|
||||
@@ -55,8 +54,8 @@ class IncomingSegmentedEncryptionLayer implements IncomingEncryptionLayer {
|
||||
// Read the segment
|
||||
if(!in.readSegment(segment)) return -1;
|
||||
int offset = tag ? TAG_LENGTH : 0, length = segment.getLength();
|
||||
if(length > MAX_FRAME_LENGTH) throw new FormatException();
|
||||
if(length < offset + FRAME_HEADER_LENGTH + macLength)
|
||||
if(length > MAX_SEGMENT_LENGTH) throw new FormatException();
|
||||
if(length < offset + FRAME_HEADER_LENGTH + MAC_LENGTH)
|
||||
throw new FormatException();
|
||||
// If a tag is expected, decrypt and validate it
|
||||
if(tag && !TagEncoder.validateTag(segment.getBuffer(), frame,
|
||||
@@ -73,13 +72,12 @@ class IncomingSegmentedEncryptionLayer implements IncomingEncryptionLayer {
|
||||
throw new RuntimeException(badCipher);
|
||||
}
|
||||
// Validate and parse the header
|
||||
int max = MAX_FRAME_LENGTH - FRAME_HEADER_LENGTH - macLength;
|
||||
if(!HeaderEncoder.validateHeader(b, frame, max))
|
||||
if(!HeaderEncoder.validateHeader(b, frame))
|
||||
throw new FormatException();
|
||||
int payload = HeaderEncoder.getPayloadLength(b);
|
||||
int padding = HeaderEncoder.getPaddingLength(b);
|
||||
if(length != offset + FRAME_HEADER_LENGTH + payload + padding
|
||||
+ macLength) throw new FormatException();
|
||||
+ MAC_LENGTH) throw new FormatException();
|
||||
frame++;
|
||||
return length - offset;
|
||||
} catch(IOException e) {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||
|
||||
@@ -43,8 +42,6 @@ class OutgoingSegmentedEncryptionLayer implements OutgoingEncryptionLayer {
|
||||
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
||||
int offset = 0;
|
||||
if(tagEverySegment || frame == 0) {
|
||||
if(len + TAG_LENGTH > MAX_FRAME_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
TagEncoder.encodeTag(segment.getBuffer(), frame, tagCipher, tagKey);
|
||||
offset = TAG_LENGTH;
|
||||
capacity -= TAG_LENGTH;
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_SEGMENT_LENGTH;
|
||||
import net.sf.briar.api.plugins.Segment;
|
||||
import net.sf.briar.util.ByteUtils;
|
||||
|
||||
class SegmentImpl implements Segment {
|
||||
|
||||
private final byte[] buf = new byte[MAX_FRAME_LENGTH];
|
||||
private final byte[] buf = new byte[MAX_SEGMENT_LENGTH];
|
||||
|
||||
private int length = -1;
|
||||
private long transmission = -1;
|
||||
private long segmentNumber = -1;
|
||||
|
||||
public void clear() {
|
||||
for(int i = 0; i < buf.length; i++) buf[i] = 0;
|
||||
length = -1;
|
||||
transmission = -1;
|
||||
segmentNumber = -1;
|
||||
}
|
||||
|
||||
public byte[] getBuffer() {
|
||||
@@ -26,9 +26,9 @@ class SegmentImpl implements Segment {
|
||||
return length;
|
||||
}
|
||||
|
||||
public long getTransmissionNumber() {
|
||||
if(transmission == -1) throw new IllegalStateException();
|
||||
return transmission;
|
||||
public long getSegmentNumber() {
|
||||
if(segmentNumber == -1) throw new IllegalStateException();
|
||||
return segmentNumber;
|
||||
}
|
||||
|
||||
public void setLength(int length) {
|
||||
@@ -37,9 +37,9 @@ class SegmentImpl implements Segment {
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public void setTransmissionNumber(int transmission) {
|
||||
if(transmission < 0 || transmission > ByteUtils.MAX_32_BIT_UNSIGNED)
|
||||
public void setSegmentNumber(long segmentNumber) {
|
||||
if(segmentNumber < 0 || segmentNumber > ByteUtils.MAX_32_BIT_UNSIGNED)
|
||||
throw new IllegalArgumentException();
|
||||
this.transmission = transmission;
|
||||
this.segmentNumber = segmentNumber;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,10 @@ import net.sf.briar.api.transport.ConnectionContextFactory;
|
||||
import net.sf.briar.api.transport.ConnectionDispatcher;
|
||||
import net.sf.briar.api.transport.ConnectionReaderFactory;
|
||||
import net.sf.briar.api.transport.ConnectionRecogniser;
|
||||
import net.sf.briar.api.transport.IncomingConnectionExecutor;
|
||||
import net.sf.briar.api.transport.ConnectionRegistry;
|
||||
import net.sf.briar.api.transport.ConnectionWindowFactory;
|
||||
import net.sf.briar.api.transport.ConnectionWriterFactory;
|
||||
import net.sf.briar.api.transport.IncomingConnectionExecutor;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user