mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Add a method for removing unbound transport keys.
This commit is contained in:
@@ -481,6 +481,12 @@ public interface DatabaseComponent {
|
||||
*/
|
||||
void removeTransport(Transaction txn, TransportId t) throws DbException;
|
||||
|
||||
/**
|
||||
* Removes the given transport keys from the database.
|
||||
*/
|
||||
void removeTransportKeys(Transaction txn, TransportId t, KeySetId k)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Marks the given contact as verified.
|
||||
*/
|
||||
|
||||
@@ -40,6 +40,13 @@ public interface KeyManager {
|
||||
void bindKeys(Transaction txn, ContactId c, Map<TransportId, KeySetId> keys)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Removes the given transport keys, which must not have been bound, from
|
||||
* the manager and the database.
|
||||
*/
|
||||
void removeKeys(Transaction txn, Map<TransportId, KeySetId> keys)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns a {@link StreamContext} for sending a stream to the given
|
||||
* contact over the given transport, or null if an error occurs or the
|
||||
|
||||
@@ -593,6 +593,12 @@ interface Database<T> {
|
||||
*/
|
||||
void removeTransport(T txn, TransportId t) throws DbException;
|
||||
|
||||
/**
|
||||
* Removes the given transport keys from the database.
|
||||
*/
|
||||
void removeTransportKeys(T txn, TransportId t, KeySetId k)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Resets the transmission count and expiry time of the given message with
|
||||
* respect to the given contact.
|
||||
|
||||
@@ -791,6 +791,16 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
db.removeTransport(txn, t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeTransportKeys(Transaction transaction,
|
||||
TransportId t, KeySetId k) throws DbException {
|
||||
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
||||
T txn = unbox(transaction);
|
||||
if (!db.containsTransport(txn, t))
|
||||
throw new NoSuchTransportException();
|
||||
db.removeTransportKeys(txn, t, k);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContactVerified(Transaction transaction, ContactId c)
|
||||
throws DbException {
|
||||
|
||||
@@ -2681,6 +2681,27 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeTransportKeys(Connection txn, TransportId t, KeySetId k)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
// Delete any existing outgoing keys - this will also remove any
|
||||
// incoming keys with the same key set ID
|
||||
String sql = "DELETE FROM outgoingKeys"
|
||||
+ " WHERE transportId = ? AND keySetId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setString(1, t.getString());
|
||||
ps.setInt(2, k.getInt());
|
||||
int affected = ps.executeUpdate();
|
||||
if (affected < 0) throw new DbStateException();
|
||||
ps.close();
|
||||
} catch (SQLException e) {
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetExpiryTime(Connection txn, ContactId c, MessageId m)
|
||||
throws DbException {
|
||||
@@ -2905,30 +2926,10 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
@Override
|
||||
public void updateTransportKeys(Connection txn, Collection<KeySet> keys)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
// Delete any existing outgoing keys - this will also remove any
|
||||
// incoming keys with the same key set ID
|
||||
// TODO: Add an index to speed this up?
|
||||
String sql = "DELETE FROM outgoingKeys WHERE keySetId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
for (KeySet ks : keys) {
|
||||
ps.setInt(1, ks.getKeySetId().getInt());
|
||||
ps.addBatch();
|
||||
}
|
||||
int[] batchAffected = ps.executeBatch();
|
||||
if (batchAffected.length != keys.size())
|
||||
throw new DbStateException();
|
||||
for (int rows: batchAffected)
|
||||
if (rows < 0) throw new DbStateException();
|
||||
ps.close();
|
||||
} catch (SQLException e) {
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
// Store the new keys
|
||||
for (KeySet ks : keys) {
|
||||
addTransportKeys(txn, ks.getContactId(), ks.getTransportKeys());
|
||||
TransportKeys k = ks.getTransportKeys();
|
||||
removeTransportKeys(txn, k.getTransportId(), ks.getKeySetId());
|
||||
addTransportKeys(txn, ks.getContactId(), k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,6 +132,20 @@ class KeyManagerImpl implements KeyManager, Service, EventListener {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeKeys(Transaction txn, Map<TransportId, KeySetId> keys)
|
||||
throws DbException {
|
||||
for (Entry<TransportId, KeySetId> e : keys.entrySet()) {
|
||||
TransportId t = e.getKey();
|
||||
TransportKeyManager m = managers.get(t);
|
||||
if (m == null) {
|
||||
if (LOG.isLoggable(INFO)) LOG.info("No key manager for " + t);
|
||||
} else {
|
||||
m.removeKeys(txn, e.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public StreamContext getStreamContext(ContactId c, TransportId t)
|
||||
throws DbException {
|
||||
|
||||
@@ -23,6 +23,8 @@ interface TransportKeyManager {
|
||||
|
||||
void bindKeys(Transaction txn, ContactId c, KeySetId k) throws DbException;
|
||||
|
||||
void removeKeys(Transaction txn, KeySetId k) throws DbException;
|
||||
|
||||
void removeContact(ContactId c);
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -219,6 +219,20 @@ class TransportKeyManagerImpl implements TransportKeyManager {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeKeys(Transaction txn, KeySetId k) throws DbException {
|
||||
lock.lock();
|
||||
try {
|
||||
MutableKeySet ks = keys.remove(k);
|
||||
if (ks == null) throw new IllegalArgumentException();
|
||||
if (ks.getContactId() != null) throw new IllegalArgumentException();
|
||||
TransportId t = ks.getTransportKeys().getTransportId();
|
||||
db.removeTransportKeys(txn, t, k);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeContact(ContactId c) {
|
||||
lock.lock();
|
||||
|
||||
Reference in New Issue
Block a user