Find correct session state in case the same one is used twice.

The code made the assumption that a session state can be identified by
the unique session ID. However, when multiple identities from the same
device are involved, there are two sessions with the same ID running on
the device.

Hence, a second identifying criteria has to be used to uniquely identify
the correct session. Here, the ID of the group was chosen.
Unfortunately, the session state can not be cached easily anymore
leading to a small performance penalty when getting all messages for the
UI.
This commit is contained in:
Torsten Grote
2016-04-04 12:04:17 -03:00
parent 5ea7ff2857
commit 4b7a32a5ee
4 changed files with 51 additions and 34 deletions

View File

@@ -663,9 +663,9 @@ public class ConversationActivity extends BriarActivity
public void run() {
try {
if (accept) {
introductionManager.acceptIntroduction(sessionId);
introductionManager.acceptIntroduction(contactId, sessionId);
} else {
introductionManager.declineIntroduction(sessionId);
introductionManager.declineIntroduction(contactId, sessionId);
}
loadMessages();
} catch (DbException e) {

View File

@@ -8,6 +8,7 @@ import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.sync.ClientId;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.MessageId;
import java.util.Collection;
@@ -26,14 +27,14 @@ public interface IntroductionManager {
/**
* Accept an introduction that had been made
*/
void acceptIntroduction(final SessionId sessionId)
throws DbException, FormatException;
void acceptIntroduction(final ContactId contactId,
final SessionId sessionId) throws DbException, FormatException;
/**
* Decline an introduction that had been made
*/
void declineIntroduction(final SessionId sessionId)
throws DbException, FormatException;
void declineIntroduction(final ContactId contactId,
final SessionId sessionId) throws DbException, FormatException;
/**
* Get all introduction messages for the contact with this contactId
@@ -46,8 +47,8 @@ public interface IntroductionManager {
/** Get the session state for the given session ID */
BdfDictionary getSessionState(Transaction txn, byte[] sessionId)
throws DbException, FormatException;
BdfDictionary getSessionState(Transaction txn, GroupId groupId,
byte[] sessionId) throws DbException, FormatException;
/** Gets the group used for introductions with Contact c */
Group getIntroductionGroup(Contact c);

View File

@@ -28,6 +28,7 @@ import org.briarproject.api.introduction.IntroductionManager;
import org.briarproject.api.introduction.SessionId;
import org.briarproject.api.properties.TransportProperties;
import org.briarproject.api.properties.TransportPropertyManager;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
@@ -157,11 +158,14 @@ class IntroduceeManager {
processStateUpdate(txn, engine.onMessageReceived(state, message));
}
public void acceptIntroduction(Transaction txn,
public void acceptIntroduction(Transaction txn, final ContactId contactId,
final SessionId sessionId) throws DbException, FormatException {
BdfDictionary state =
introductionManager.getSessionState(txn, sessionId.getBytes());
Contact c = contactManager.getContact(contactId);
Group g = introductionManager.getIntroductionGroup(c);
BdfDictionary state = introductionManager
.getSessionState(txn, g.getId(), sessionId.getBytes());
// get data to connect and derive a shared secret later
long now = clock.currentTimeMillis();
@@ -188,11 +192,14 @@ class IntroduceeManager {
processStateUpdate(txn, engine.onLocalAction(state, localAction));
}
public void declineIntroduction(Transaction txn, final SessionId sessionId)
throws DbException, FormatException {
public void declineIntroduction(Transaction txn, final ContactId contactId,
final SessionId sessionId) throws DbException, FormatException {
BdfDictionary state =
introductionManager.getSessionState(txn, sessionId.getBytes());
Contact c = contactManager.getContact(contactId);
Group g = introductionManager.getIntroductionGroup(c);
BdfDictionary state = introductionManager
.getSessionState(txn, g.getId(), sessionId.getBytes());
// update session state
state.put(ACCEPT, false);

View File

@@ -62,6 +62,8 @@ import static org.briarproject.api.introduction.IntroductionConstants.CONTACT_ID
import static org.briarproject.api.introduction.IntroductionConstants.CONTACT_ID_2;
import static org.briarproject.api.introduction.IntroductionConstants.EXISTS;
import static org.briarproject.api.introduction.IntroductionConstants.GROUP_ID;
import static org.briarproject.api.introduction.IntroductionConstants.GROUP_ID_1;
import static org.briarproject.api.introduction.IntroductionConstants.GROUP_ID_2;
import static org.briarproject.api.introduction.IntroductionConstants.MESSAGE_TIME;
import static org.briarproject.api.introduction.IntroductionConstants.MSG;
import static org.briarproject.api.introduction.IntroductionConstants.NAME;
@@ -227,7 +229,7 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
else if (type == TYPE_RESPONSE || type == TYPE_ACK || type == TYPE_ABORT) {
BdfDictionary state;
try {
state = getSessionState(txn,
state = getSessionState(txn, groupId,
message.getRaw(SESSION_ID, new byte[0]));
} catch (FormatException e) {
LOG.warning("Could not find state for message, deleting...");
@@ -279,12 +281,12 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
}
@Override
public void acceptIntroduction(final SessionId sessionId)
throws DbException, FormatException {
public void acceptIntroduction(final ContactId contactId,
final SessionId sessionId) throws DbException, FormatException {
Transaction txn = db.startTransaction(false);
try {
introduceeManager.acceptIntroduction(txn, sessionId);
introduceeManager.acceptIntroduction(txn, contactId, sessionId);
txn.setComplete();
} finally {
db.endTransaction(txn);
@@ -292,12 +294,12 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
}
@Override
public void declineIntroduction(final SessionId sessionId)
throws DbException, FormatException {
public void declineIntroduction(final ContactId contactId,
final SessionId sessionId) throws DbException, FormatException {
Transaction txn = db.startTransaction(false);
try {
introduceeManager.declineIntroduction(txn, sessionId);
introduceeManager.declineIntroduction(txn, contactId, sessionId);
txn.setComplete();
} finally {
db.endTransaction(txn);
@@ -322,8 +324,6 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
statuses = db.getMessageStatus(txn, contactId, g);
// turn messages into classes for the UI
Map<SessionId, BdfDictionary> sessionStates =
new HashMap<SessionId, BdfDictionary>();
for (MessageStatus s : statuses) {
MessageId messageId = s.getMessageId();
BdfDictionary msg = metadata.get(messageId);
@@ -335,11 +335,8 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
// get session state
SessionId sessionId = new SessionId(msg.getRaw(SESSION_ID));
BdfDictionary state = sessionStates.get(sessionId);
if (state == null) {
state = getSessionState(txn, sessionId.getBytes());
}
sessionStates.put(sessionId, state);
BdfDictionary state =
getSessionState(txn, g, sessionId.getBytes());
boolean local;
long time = msg.getLong(MESSAGE_TIME);
@@ -453,19 +450,31 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
}
}
public BdfDictionary getSessionState(Transaction txn, byte[] sessionId)
throws DbException, FormatException {
public BdfDictionary getSessionState(Transaction txn, GroupId groupId,
byte[] sessionId) throws DbException, FormatException {
try {
return clientHelper.getMessageMetadataAsDictionary(txn,
new MessageId(sessionId));
// See if we can find the state directly for the introducer
BdfDictionary state = clientHelper
.getMessageMetadataAsDictionary(txn,
new MessageId(sessionId));
GroupId g1 = new GroupId(state.getRaw(GROUP_ID_1));
GroupId g2 = new GroupId(state.getRaw(GROUP_ID_2));
if (!g1.equals(groupId) && !g2.equals(groupId)) {
throw new NoSuchMessageException();
}
return state;
} catch (NoSuchMessageException e) {
// State not found directly, so iterate over all states
// to find state for introducee
Map<MessageId, BdfDictionary> map = clientHelper
.getMessageMetadataAsDictionary(txn,
localGroup.getId());
for (Map.Entry<MessageId, BdfDictionary> m : map.entrySet()) {
if (Arrays.equals(m.getValue().getRaw(SESSION_ID), sessionId)) {
return m.getValue();
BdfDictionary state = m.getValue();
GroupId g = new GroupId(state.getRaw(GROUP_ID));
if (g.equals(groupId)) return state;
}
}
if (LOG.isLoggable(WARNING)) {