Protocol refactoring. Each bundle now consists of a signed header and zero or more signed batches. There is no overall signature on the bundle, since the bundle's contents may need to be processed before the entire bundle has been read. The protocol does not prevent an adversary from removing batches from a bundle, reordering batches, moving them from one bundle to another, etc. However, since each batch is signed and acknowledged independently, no such guarantees are required. Bundle IDs will go away when the retransmission mechanism is changed.

This commit is contained in:
akwizgran
2011-07-12 12:55:46 +01:00
parent 4977695a79
commit e0509db45d
28 changed files with 1198 additions and 228 deletions

View File

@@ -1,5 +1,7 @@
package net.sf.briar.api.db;
import java.io.IOException;
import java.security.SignatureException;
import java.util.Map;
import java.util.Set;
@@ -49,7 +51,7 @@ public interface DatabaseComponent {
* Generates a bundle of acknowledgements, subscriptions, and batches of
* messages for the given contact.
*/
Bundle generateBundle(ContactId c, BundleBuilder bundleBuilder) throws DbException;
Bundle generateBundle(ContactId c, BundleBuilder bundleBuilder) throws DbException, IOException, SignatureException;
/** Returns the IDs of all contacts. */
Set<ContactId> getContacts() throws DbException;
@@ -71,7 +73,7 @@ public interface DatabaseComponent {
* messages received from the given contact. Some or all of the messages
* in the bundle may be stored.
*/
void receiveBundle(ContactId c, Bundle b) throws DbException;
void receiveBundle(ContactId c, Bundle b) throws DbException, IOException, SignatureException;
/** Removes a contact (and all associated state) from the database. */
void removeContact(ContactId c) throws DbException;

View File

@@ -1,16 +1,19 @@
package net.sf.briar.api.protocol;
/** A batch of messages up to CAPACITY bytes in total size. */
/** A batch of messages up to MAX_SIZE bytes in total size. */
public interface Batch {
public static final long CAPACITY = 1024L * 1024L;
public static final int MAX_SIZE = 1024 * 1024;
/** Returns the batch's unique identifier. */
BatchId getId();
/** Returns the size of the batch in bytes. */
/** Returns the size of the serialised batch in bytes. */
long getSize();
/** Returns the messages contained in the batch. */
Iterable<Message> getMessages();
/** Returns the sender's signature over the contents of the batch. */
byte[] getSignature();
}

View File

@@ -1,10 +1,15 @@
package net.sf.briar.api.protocol;
import java.security.SignatureException;
public interface BatchBuilder {
/** Adds a message to the batch. */
void addMessage(Message m);
/** Sets the sender's signature over the contents of the batch. */
void setSignature(byte[] sig);
/** Builds and returns the batch. */
Batch build();
Batch build() throws SignatureException;
}

View File

@@ -1,28 +1,21 @@
package net.sf.briar.api.protocol;
import java.util.Map;
import java.io.IOException;
import java.security.SignatureException;
/** A bundle of acknowledgements, subscriptions, and batches of messages. */
/**
* A bundle of acknowledgements, subscriptions, transport details and batches.
*/
public interface Bundle {
/** Returns the bundle's unique identifier. */
BundleId getId();
/** Returns the size of the serialised bundle in bytes. */
long getSize() throws IOException;
/** Returns the bundle's capacity in bytes. */
long getCapacity();
/** Returns the bundle's header. */
Header getHeader() throws IOException, SignatureException;
/** Returns the bundle's size in bytes. */
long getSize();
/** Returns the acknowledgements contained in the bundle. */
Iterable<BatchId> getAcks();
/** Returns the subscriptions contained in the bundle. */
Iterable<GroupId> getSubscriptions();
/** Returns the transport details contained in the bundle. */
Map<String, String> getTransports();
/** Returns the batches of messages contained in the bundle. */
Iterable<Batch> getBatches();
/**
* Returns the next batch of messages, or null if there are no more batches.
*/
Batch getNextBatch() throws IOException, SignatureException;
}

View File

@@ -1,22 +1,18 @@
package net.sf.briar.api.protocol;
import java.io.IOException;
public interface BundleBuilder {
/** Returns the bundle's capacity in bytes. */
long getCapacity();
long getCapacity() throws IOException;
/** Adds an acknowledgement to the bundle. */
void addAck(BatchId b);
/** Adds a subscription to the bundle. */
void addSubscription(GroupId g);
/** Adds a transport detail to the bundle. */
void addTransport(String key, String value);
/** Adds a header to the bundle. */
void addHeader(Header h) throws IOException;
/** Adds a batch of messages to the bundle. */
void addBatch(Batch b);
void addBatch(Batch b) throws IOException;
/** Builds and returns the bundle. */
Bundle build();
Bundle build() throws IOException;
}

View File

@@ -0,0 +1,28 @@
package net.sf.briar.api.protocol;
import java.util.Map;
import java.util.Set;
/** A bundle header up to MAX_SIZE bytes in total size. */
public interface Header {
static final int MAX_SIZE = 1024 * 1024;
// FIXME: Remove BundleId when refactoring is complete
BundleId getId();
/** Returns the size of the serialised header in bytes. */
long getSize();
/** Returns the acknowledgements contained in the header. */
Set<BatchId> getAcks();
/** Returns the subscriptions contained in the header. */
Set<GroupId> getSubscriptions();
/** Returns the transport details contained in the header. */
Map<String, String> getTransports();
/** Returns the sender's signature over the contents of the header. */
byte[] getSignature();
}

View File

@@ -0,0 +1,24 @@
package net.sf.briar.api.protocol;
import java.io.IOException;
import java.security.SignatureException;
import java.util.Map;
import java.util.Set;
public interface HeaderBuilder {
/** Adds acknowledgements to the header. */
void addAcks(Set<BatchId> acks) throws IOException;
/** Adds subscriptions to the header. */
void addSubscriptions(Set<GroupId> subs) throws IOException;
/** Adds transport details to the header. */
void addTransports(Map<String, String> transports) throws IOException;
/** Sets the sender's signature over the contents of the header. */
void setSignature(byte[] sig) throws IOException;
/** Builds and returns the header. */
Header build() throws SignatureException;
}

View File

@@ -0,0 +1,10 @@
package net.sf.briar.api.protocol;
import java.security.SignatureException;
import net.sf.briar.api.serial.FormatException;
public interface MessageParser {
Message parseMessage(byte[] body) throws FormatException, SignatureException;
}

View File

@@ -2,7 +2,9 @@ package net.sf.briar.api.protocol;
import java.util.Arrays;
public abstract class UniqueId {
import net.sf.briar.api.serial.Raw;
public abstract class UniqueId implements Raw {
public static final int LENGTH = 32;