mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 19:59:05 +01:00
Renamed ContactTransport -> Endpoint, added more database exceptions.
This commit is contained in:
@@ -22,7 +22,7 @@ import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.TransportAck;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.transport.ContactTransport;
|
||||
import net.sf.briar.api.transport.Endpoint;
|
||||
import net.sf.briar.api.transport.TemporarySecret;
|
||||
|
||||
/**
|
||||
@@ -82,11 +82,11 @@ interface Database<T> {
|
||||
ContactId addContact(T txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Adds a contact transport to the database.
|
||||
* Adds an endpoint to the database.
|
||||
* <p>
|
||||
* Locking: contact read, transport read, window write.
|
||||
*/
|
||||
void addContactTransport(T txn, ContactTransport ct) throws DbException;
|
||||
void addEndpoint(T txn, Endpoint ep) throws DbException;
|
||||
|
||||
/**
|
||||
* Stores the given message, or returns false if the message is already in
|
||||
@@ -156,14 +156,6 @@ interface Database<T> {
|
||||
*/
|
||||
boolean containsContact(T txn, ContactId c) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns true if the database contains the given contact transport.
|
||||
* <p>
|
||||
* Locking: contact read, transport read, window read.
|
||||
*/
|
||||
boolean containsContactTransport(T txn, ContactId c, TransportId t)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns true if the database contains the given message.
|
||||
* <p>
|
||||
@@ -178,6 +170,13 @@ interface Database<T> {
|
||||
*/
|
||||
boolean containsSubscription(T txn, GroupId g) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns true if the database contains the given transport.
|
||||
* <p>
|
||||
* Locking: contact read, transport read.
|
||||
*/
|
||||
boolean containsTransport(T txn, TransportId t) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns true if the user subscribes to the given group and the
|
||||
* subscription is visible to the given contact.
|
||||
@@ -202,11 +201,11 @@ interface Database<T> {
|
||||
Collection<ContactId> getContacts(T txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all contact transports.
|
||||
* Returns all endpoints.
|
||||
* <p>
|
||||
* Locking: contact read, transport read, window read.
|
||||
*/
|
||||
Collection<ContactTransport> getContactTransports(T txn) throws DbException;
|
||||
Collection<Endpoint> getEndpoints(T txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the amount of free storage space available to the database, in
|
||||
@@ -541,8 +540,8 @@ interface Database<T> {
|
||||
void removeVisibility(T txn, ContactId c, GroupId g) throws DbException;
|
||||
|
||||
/**
|
||||
* Sets the connection reordering window for the given contact transport in
|
||||
* the given rotation period.
|
||||
* Sets the connection reordering window for the given endpoint in the
|
||||
* given rotation period.
|
||||
* <p>
|
||||
* Locking: contact read, transport read, window write.
|
||||
*/
|
||||
|
||||
@@ -29,7 +29,8 @@ import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.db.MessageHeader;
|
||||
import net.sf.briar.api.db.NoSuchContactException;
|
||||
import net.sf.briar.api.db.NoSuchContactTransportException;
|
||||
import net.sf.briar.api.db.NoSuchSubscriptionException;
|
||||
import net.sf.briar.api.db.NoSuchTransportException;
|
||||
import net.sf.briar.api.db.event.ContactAddedEvent;
|
||||
import net.sf.briar.api.db.event.ContactRemovedEvent;
|
||||
import net.sf.briar.api.db.event.DatabaseEvent;
|
||||
@@ -61,7 +62,7 @@ import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.TransportAck;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.transport.ContactTransport;
|
||||
import net.sf.briar.api.transport.Endpoint;
|
||||
import net.sf.briar.api.transport.TemporarySecret;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
@@ -196,7 +197,7 @@ DatabaseCleaner.Callback {
|
||||
for(DatabaseListener d : listeners) d.eventOccurred(e);
|
||||
}
|
||||
|
||||
public void addContactTransport(ContactTransport ct) throws DbException {
|
||||
public void addEndpoint(Endpoint ep) throws DbException {
|
||||
contactLock.readLock().lock();
|
||||
try {
|
||||
transportLock.readLock().lock();
|
||||
@@ -205,9 +206,11 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if(!db.containsContact(txn, ct.getContactId()))
|
||||
if(!db.containsContact(txn, ep.getContactId()))
|
||||
throw new NoSuchContactException();
|
||||
db.addContactTransport(txn, ct);
|
||||
if(!db.containsTransport(txn, ep.getTransportId()))
|
||||
throw new NoSuchTransportException();
|
||||
db.addEndpoint(txn, ep);
|
||||
db.commitTransaction(txn);
|
||||
} catch(DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
@@ -381,9 +384,10 @@ DatabaseCleaner.Callback {
|
||||
new ArrayList<TemporarySecret>();
|
||||
for(TemporarySecret s : secrets) {
|
||||
ContactId c = s.getContactId();
|
||||
if(!db.containsContact(txn, c)) continue;
|
||||
TransportId t = s.getTransportId();
|
||||
if(db.containsContactTransport(txn, c, t))
|
||||
relevant.add(s);
|
||||
if(!db.containsTransport(txn, t)) continue;
|
||||
relevant.add(s);
|
||||
}
|
||||
if(!secrets.isEmpty()) db.addSecrets(txn, relevant);
|
||||
db.commitTransaction(txn);
|
||||
@@ -725,6 +729,8 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if(!db.containsContact(txn, c))
|
||||
throw new NoSuchContactException();
|
||||
Collection<TransportAck> acks = db.getTransportAcks(txn, c);
|
||||
db.commitTransaction(txn);
|
||||
return acks;
|
||||
@@ -748,6 +754,8 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if(!db.containsContact(txn, c))
|
||||
throw new NoSuchContactException();
|
||||
Collection<TransportUpdate> updates =
|
||||
db.getTransportUpdates(txn, c);
|
||||
db.commitTransaction(txn);
|
||||
@@ -769,6 +777,8 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if(!db.containsTransport(txn, t))
|
||||
throw new NoSuchTransportException();
|
||||
TransportConfig config = db.getConfig(txn, t);
|
||||
db.commitTransaction(txn);
|
||||
return config;
|
||||
@@ -804,6 +814,8 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if(!db.containsTransport(txn, t))
|
||||
throw new NoSuchTransportException();
|
||||
TransportProperties properties = db.getLocalProperties(txn, t);
|
||||
db.commitTransaction(txn);
|
||||
return properties;
|
||||
@@ -822,6 +834,8 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if(!db.containsSubscription(txn, g))
|
||||
throw new NoSuchSubscriptionException();
|
||||
Collection<MessageHeader> headers =
|
||||
db.getMessageHeaders(txn, g);
|
||||
db.commitTransaction(txn);
|
||||
@@ -951,6 +965,8 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if(!db.containsSubscription(txn, g))
|
||||
throw new NoSuchSubscriptionException();
|
||||
Collection<ContactId> visible = db.getVisibility(txn, g);
|
||||
db.commitTransaction(txn);
|
||||
return visible;
|
||||
@@ -1005,8 +1021,10 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if(!db.containsContactTransport(txn, c, t))
|
||||
throw new NoSuchContactTransportException();
|
||||
if(!db.containsContact(txn, c))
|
||||
throw new NoSuchContactException();
|
||||
if(!db.containsTransport(txn, t))
|
||||
throw new NoSuchTransportException();
|
||||
long counter = db.incrementConnectionCounter(txn, c, t,
|
||||
period);
|
||||
db.commitTransaction(txn);
|
||||
@@ -1032,6 +1050,8 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if(!db.containsTransport(txn, t))
|
||||
throw new NoSuchTransportException();
|
||||
db.mergeConfig(txn, t, c);
|
||||
db.commitTransaction(txn);
|
||||
} catch(DbException e) {
|
||||
@@ -1050,6 +1070,8 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if(!db.containsTransport(txn, t))
|
||||
throw new NoSuchTransportException();
|
||||
if(!p.equals(db.getLocalProperties(txn, t))) {
|
||||
db.mergeLocalProperties(txn, t, p);
|
||||
changed = true;
|
||||
@@ -1285,6 +1307,8 @@ DatabaseCleaner.Callback {
|
||||
if(!db.containsContact(txn, c))
|
||||
throw new NoSuchContactException();
|
||||
TransportId t = a.getId();
|
||||
if(!db.containsTransport(txn, t))
|
||||
throw new NoSuchTransportException();
|
||||
db.setTransportUpdateAcked(txn, c, t, a.getVersionNumber());
|
||||
db.commitTransaction(txn);
|
||||
} catch(DbException e) {
|
||||
@@ -1368,6 +1392,8 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if(!db.containsTransport(txn, t))
|
||||
throw new NoSuchTransportException();
|
||||
db.removeTransport(txn, t);
|
||||
db.commitTransaction(txn);
|
||||
} catch(DbException e) {
|
||||
@@ -1390,8 +1416,10 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if(!db.containsContactTransport(txn, c, t))
|
||||
throw new NoSuchContactTransportException();
|
||||
if(!db.containsContact(txn, c))
|
||||
throw new NoSuchContactException();
|
||||
if(!db.containsTransport(txn, t))
|
||||
throw new NoSuchTransportException();
|
||||
db.setConnectionWindow(txn, c, t, period, centre,
|
||||
bitmap);
|
||||
db.commitTransaction(txn);
|
||||
@@ -1504,6 +1532,8 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if(!db.containsSubscription(txn, g))
|
||||
throw new NoSuchSubscriptionException();
|
||||
// Use HashSets for O(1) lookups, O(n) overall running time
|
||||
HashSet<ContactId> newVisible =
|
||||
new HashSet<ContactId>(visible);
|
||||
@@ -1554,7 +1584,7 @@ DatabaseCleaner.Callback {
|
||||
}
|
||||
|
||||
public void unsubscribe(GroupId g) throws DbException {
|
||||
Collection<ContactId> affected = null;
|
||||
Collection<ContactId> affected;
|
||||
contactLock.writeLock().lock();
|
||||
try {
|
||||
messageLock.writeLock().lock();
|
||||
@@ -1563,10 +1593,10 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if(db.containsSubscription(txn, g)) {
|
||||
affected = db.getVisibility(txn, g);
|
||||
db.removeSubscription(txn, g);
|
||||
}
|
||||
if(!db.containsSubscription(txn, g))
|
||||
throw new NoSuchSubscriptionException();
|
||||
affected = db.getVisibility(txn, g);
|
||||
db.removeSubscription(txn, g);
|
||||
db.commitTransaction(txn);
|
||||
} catch(DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
|
||||
@@ -31,18 +31,18 @@ import net.sf.briar.api.db.DbClosedException;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.db.MessageHeader;
|
||||
import net.sf.briar.api.protocol.AuthorId;
|
||||
import net.sf.briar.api.protocol.RetentionAck;
|
||||
import net.sf.briar.api.protocol.RetentionUpdate;
|
||||
import net.sf.briar.api.protocol.Group;
|
||||
import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.RetentionAck;
|
||||
import net.sf.briar.api.protocol.RetentionUpdate;
|
||||
import net.sf.briar.api.protocol.SubscriptionAck;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.TransportAck;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.transport.ContactTransport;
|
||||
import net.sf.briar.api.transport.Endpoint;
|
||||
import net.sf.briar.api.transport.TemporarySecret;
|
||||
import net.sf.briar.util.FileUtils;
|
||||
|
||||
@@ -268,8 +268,8 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
+ " ON DELETE CASCADE)";
|
||||
|
||||
// Locking: contact read, transport read, window
|
||||
private static final String CREATE_CONTACT_TRANSPORTS =
|
||||
"CREATE TABLE contactTransports"
|
||||
private static final String CREATE_ENDPOINTS =
|
||||
"CREATE TABLE endpoints"
|
||||
+ " (contactId INT NOT NULL,"
|
||||
+ " transportId HASH NOT NULL,"
|
||||
+ " epoch BIGINT NOT NULL,"
|
||||
@@ -377,7 +377,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
s.executeUpdate(insertTypeNames(CREATE_TRANSPORT_VERSIONS));
|
||||
s.executeUpdate(insertTypeNames(CREATE_CONTACT_TRANSPORT_PROPS));
|
||||
s.executeUpdate(insertTypeNames(CREATE_CONTACT_TRANSPORT_VERSIONS));
|
||||
s.executeUpdate(insertTypeNames(CREATE_CONTACT_TRANSPORTS));
|
||||
s.executeUpdate(insertTypeNames(CREATE_ENDPOINTS));
|
||||
s.executeUpdate(insertTypeNames(CREATE_SECRETS));
|
||||
s.close();
|
||||
} catch(SQLException e) {
|
||||
@@ -567,20 +567,19 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public void addContactTransport(Connection txn, ContactTransport ct)
|
||||
throws DbException {
|
||||
public void addEndpoint(Connection txn, Endpoint ep) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
String sql = "INSERT INTO contactTransports (contactId,"
|
||||
+ " transportId, epoch, clockDiff, latency, alice)"
|
||||
String sql = "INSERT INTO endpoints (contactId, transportId,"
|
||||
+ " epoch, clockDiff, latency, alice)"
|
||||
+ " VALUES (?, ?, ?, ?, ?, ?)";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, ct.getContactId().getInt());
|
||||
ps.setBytes(2, ct.getTransportId().getBytes());
|
||||
ps.setLong(3, ct.getEpoch());
|
||||
ps.setLong(4, ct.getClockDifference());
|
||||
ps.setLong(5, ct.getLatency());
|
||||
ps.setBoolean(6, ct.getAlice());
|
||||
ps.setInt(1, ep.getContactId().getInt());
|
||||
ps.setBytes(2, ep.getTransportId().getBytes());
|
||||
ps.setLong(3, ep.getEpoch());
|
||||
ps.setLong(4, ep.getClockDifference());
|
||||
ps.setLong(5, ep.getLatency());
|
||||
ps.setBoolean(6, ep.getAlice());
|
||||
int affected = ps.executeUpdate();
|
||||
if(affected != 1) throw new DbStateException();
|
||||
ps.close();
|
||||
@@ -873,29 +872,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean containsContactTransport(Connection txn, ContactId c,
|
||||
TransportId t) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT NULL FROM contactTransports"
|
||||
+ " WHERE contactId = ? AND transportId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, c.getInt());
|
||||
ps.setBytes(2, t.getBytes());
|
||||
rs = ps.executeQuery();
|
||||
boolean found = rs.next();
|
||||
if(rs.next()) throw new DbStateException();
|
||||
rs.close();
|
||||
ps.close();
|
||||
return found;
|
||||
} catch(SQLException e) {
|
||||
tryToClose(rs);
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean containsMessage(Connection txn, MessageId m)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
@@ -938,6 +914,27 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean containsTransport(Connection txn, TransportId t)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT NULL FROM transports WHERE transportId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setBytes(1, t.getBytes());
|
||||
rs = ps.executeQuery();
|
||||
boolean found = rs.next();
|
||||
if(rs.next()) throw new DbStateException();
|
||||
rs.close();
|
||||
ps.close();
|
||||
return found;
|
||||
} catch(SQLException e) {
|
||||
tryToClose(rs);
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean containsVisibleSubscription(Connection txn, ContactId c,
|
||||
GroupId g) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
@@ -1005,17 +1002,17 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<ContactTransport> getContactTransports(Connection txn)
|
||||
public Collection<Endpoint> getEndpoints(Connection txn)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT contactId, transportId, epoch, clockDiff,"
|
||||
+ " latency, alice"
|
||||
+ " FROM contactTransports";
|
||||
+ " FROM endpoints";
|
||||
ps = txn.prepareStatement(sql);
|
||||
rs = ps.executeQuery();
|
||||
List<ContactTransport> cts = new ArrayList<ContactTransport>();
|
||||
List<Endpoint> endpoints = new ArrayList<Endpoint>();
|
||||
while(rs.next()) {
|
||||
ContactId c = new ContactId(rs.getInt(1));
|
||||
TransportId t = new TransportId(rs.getBytes(2));
|
||||
@@ -1023,10 +1020,10 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
long clockDiff = rs.getLong(4);
|
||||
long latency = rs.getLong(5);
|
||||
boolean alice = rs.getBoolean(6);
|
||||
cts.add(new ContactTransport(c, t, epoch, clockDiff, latency,
|
||||
endpoints.add(new Endpoint(c, t, epoch, clockDiff, latency,
|
||||
alice));
|
||||
}
|
||||
return Collections.unmodifiableList(cts);
|
||||
return Collections.unmodifiableList(endpoints);
|
||||
} catch(SQLException e) {
|
||||
tryToClose(rs);
|
||||
tryToClose(ps);
|
||||
@@ -1556,10 +1553,10 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
String sql = "SELECT ct.contactId, ct.transportId, epoch,"
|
||||
+ " clockDiff, latency, alice, period, secret, outgoing,"
|
||||
+ " centre, bitmap"
|
||||
+ " FROM contactTransports AS ct"
|
||||
+ " FROM endpoints AS e"
|
||||
+ " JOIN secrets AS s"
|
||||
+ " ON ct.contactId = s.contactId"
|
||||
+ " AND ct.transportId = s.transportId";
|
||||
+ " ON e.contactId = s.contactId"
|
||||
+ " AND e.transportId = s.transportId";
|
||||
ps = txn.prepareStatement(sql);
|
||||
rs = ps.executeQuery();
|
||||
List<TemporarySecret> secrets = new ArrayList<TemporarySecret>();
|
||||
|
||||
@@ -24,7 +24,7 @@ import net.sf.briar.api.db.event.DatabaseListener;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
import net.sf.briar.api.transport.ConnectionContext;
|
||||
import net.sf.briar.api.transport.ConnectionRecogniser;
|
||||
import net.sf.briar.api.transport.ContactTransport;
|
||||
import net.sf.briar.api.transport.Endpoint;
|
||||
import net.sf.briar.api.transport.TemporarySecret;
|
||||
import net.sf.briar.util.ByteUtils;
|
||||
|
||||
@@ -43,11 +43,11 @@ class KeyManagerImpl extends TimerTask implements KeyManager, DatabaseListener {
|
||||
private final Clock clock;
|
||||
private final Timer timer;
|
||||
// Locking: this
|
||||
private final Map<ContactTransportKey, TemporarySecret> outgoing;
|
||||
private final Map<EndpointKey, TemporarySecret> outgoing;
|
||||
// Locking: this
|
||||
private final Map<ContactTransportKey, TemporarySecret> incomingOld;
|
||||
private final Map<EndpointKey, TemporarySecret> incomingOld;
|
||||
// Locking: this
|
||||
private final Map<ContactTransportKey, TemporarySecret> incomingNew;
|
||||
private final Map<EndpointKey, TemporarySecret> incomingNew;
|
||||
|
||||
@Inject
|
||||
KeyManagerImpl(CryptoComponent crypto, DatabaseComponent db,
|
||||
@@ -57,9 +57,9 @@ class KeyManagerImpl extends TimerTask implements KeyManager, DatabaseListener {
|
||||
this.recogniser = recogniser;
|
||||
this.clock = clock;
|
||||
this.timer = timer;
|
||||
outgoing = new HashMap<ContactTransportKey, TemporarySecret>();
|
||||
incomingOld = new HashMap<ContactTransportKey, TemporarySecret>();
|
||||
incomingNew = new HashMap<ContactTransportKey, TemporarySecret>();
|
||||
outgoing = new HashMap<EndpointKey, TemporarySecret>();
|
||||
incomingOld = new HashMap<EndpointKey, TemporarySecret>();
|
||||
incomingNew = new HashMap<EndpointKey, TemporarySecret>();
|
||||
}
|
||||
|
||||
public synchronized boolean start() {
|
||||
@@ -97,7 +97,7 @@ class KeyManagerImpl extends TimerTask implements KeyManager, DatabaseListener {
|
||||
Collection<TemporarySecret> secrets) {
|
||||
Collection<TemporarySecret> dead = new ArrayList<TemporarySecret>();
|
||||
for(TemporarySecret s : secrets) {
|
||||
ContactTransportKey k = new ContactTransportKey(s);
|
||||
EndpointKey k = new EndpointKey(s);
|
||||
long rotationPeriod = getRotationPeriod(s);
|
||||
long creationTime = getCreationTime(s);
|
||||
long activationTime = creationTime + s.getClockDifference();
|
||||
@@ -136,7 +136,7 @@ class KeyManagerImpl extends TimerTask implements KeyManager, DatabaseListener {
|
||||
Collection<TemporarySecret> dead) {
|
||||
Collection<TemporarySecret> created = new ArrayList<TemporarySecret>();
|
||||
for(TemporarySecret s : dead) {
|
||||
ContactTransportKey k = new ContactTransportKey(s);
|
||||
EndpointKey k = new EndpointKey(s);
|
||||
if(incomingNew.containsKey(k)) throw new IllegalStateException();
|
||||
byte[] secret = s.getSecret();
|
||||
long period = s.getPeriod();
|
||||
@@ -196,8 +196,8 @@ class KeyManagerImpl extends TimerTask implements KeyManager, DatabaseListener {
|
||||
return created;
|
||||
}
|
||||
|
||||
private long getRotationPeriod(ContactTransport s) {
|
||||
return 2 * s.getClockDifference() + s.getLatency();
|
||||
private long getRotationPeriod(Endpoint ep) {
|
||||
return 2 * ep.getClockDifference() + ep.getLatency();
|
||||
}
|
||||
|
||||
private long getCreationTime(TemporarySecret s) {
|
||||
@@ -221,7 +221,7 @@ class KeyManagerImpl extends TimerTask implements KeyManager, DatabaseListener {
|
||||
|
||||
public synchronized ConnectionContext getConnectionContext(ContactId c,
|
||||
TransportId t) {
|
||||
TemporarySecret s = outgoing.get(new ContactTransportKey(c, t));
|
||||
TemporarySecret s = outgoing.get(new EndpointKey(c, t));
|
||||
if(s == null) return null;
|
||||
long connection;
|
||||
try {
|
||||
@@ -234,11 +234,10 @@ class KeyManagerImpl extends TimerTask implements KeyManager, DatabaseListener {
|
||||
return new ConnectionContext(c, t, secret, connection, s.getAlice());
|
||||
}
|
||||
|
||||
public synchronized void contactTransportAdded(ContactTransport ct,
|
||||
byte[] initialSecret) {
|
||||
public synchronized void endpointAdded(Endpoint ep, byte[] initialSecret) {
|
||||
long now = clock.currentTimeMillis();
|
||||
long rotationPeriod = getRotationPeriod(ct);
|
||||
long elapsed = now - ct.getEpoch();
|
||||
long rotationPeriod = getRotationPeriod(ep);
|
||||
long elapsed = now - ep.getEpoch();
|
||||
long currentPeriod = elapsed / rotationPeriod;
|
||||
if(currentPeriod < 1) throw new IllegalArgumentException();
|
||||
// Derive the two current incoming secrets
|
||||
@@ -251,15 +250,15 @@ class KeyManagerImpl extends TimerTask implements KeyManager, DatabaseListener {
|
||||
}
|
||||
secret2 = crypto.deriveNextSecret(secret1, currentPeriod);
|
||||
// One of the incoming secrets is the current outgoing secret
|
||||
ContactTransportKey k = new ContactTransportKey(ct);
|
||||
EndpointKey k = new EndpointKey(ep);
|
||||
TemporarySecret s1, s2, dupe;
|
||||
s1 = new TemporarySecret(ct, currentPeriod - 1, secret1);
|
||||
s1 = new TemporarySecret(ep, currentPeriod - 1, secret1);
|
||||
dupe = incomingOld.put(k, s1);
|
||||
if(dupe != null) throw new IllegalStateException();
|
||||
s2 = new TemporarySecret(ct, currentPeriod, secret2);
|
||||
s2 = new TemporarySecret(ep, currentPeriod, secret2);
|
||||
dupe = incomingNew.put(k, s2);
|
||||
if(dupe != null) throw new IllegalStateException();
|
||||
if(elapsed % rotationPeriod < ct.getClockDifference()) {
|
||||
if(elapsed % rotationPeriod < ep.getClockDifference()) {
|
||||
// The outgoing secret is the newer incoming secret
|
||||
dupe = outgoing.put(k, s2);
|
||||
if(dupe != null) throw new IllegalStateException();
|
||||
@@ -338,18 +337,17 @@ class KeyManagerImpl extends TimerTask implements KeyManager, DatabaseListener {
|
||||
}
|
||||
}
|
||||
|
||||
private static class ContactTransportKey {
|
||||
private static class EndpointKey {
|
||||
|
||||
private final ContactId contactId;
|
||||
private final TransportId transportId;
|
||||
|
||||
private ContactTransportKey(ContactId contactId,
|
||||
TransportId transportId) {
|
||||
private EndpointKey(ContactId contactId, TransportId transportId) {
|
||||
this.contactId = contactId;
|
||||
this.transportId = transportId;
|
||||
}
|
||||
|
||||
private ContactTransportKey(ContactTransport ct) {
|
||||
private EndpointKey(Endpoint ct) {
|
||||
this(ct.getContactId(), ct.getTransportId());
|
||||
}
|
||||
|
||||
@@ -360,8 +358,8 @@ class KeyManagerImpl extends TimerTask implements KeyManager, DatabaseListener {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if(o instanceof ContactTransportKey) {
|
||||
ContactTransportKey k = (ContactTransportKey) o;
|
||||
if(o instanceof EndpointKey) {
|
||||
EndpointKey k = (EndpointKey) o;
|
||||
return contactId.equals(k.contactId) &&
|
||||
transportId.equals(k.transportId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user