mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 22:29:53 +01:00
Added QueueMessage support to BdfMessageValidator.
This commit is contained in:
@@ -2,6 +2,8 @@ package org.briarproject.clients;
|
|||||||
|
|
||||||
import org.briarproject.api.FormatException;
|
import org.briarproject.api.FormatException;
|
||||||
import org.briarproject.api.clients.ClientHelper;
|
import org.briarproject.api.clients.ClientHelper;
|
||||||
|
import org.briarproject.api.clients.QueueMessage;
|
||||||
|
import org.briarproject.api.clients.QueueMessageValidator;
|
||||||
import org.briarproject.api.data.BdfDictionary;
|
import org.briarproject.api.data.BdfDictionary;
|
||||||
import org.briarproject.api.data.BdfList;
|
import org.briarproject.api.data.BdfList;
|
||||||
import org.briarproject.api.data.MetadataEncoder;
|
import org.briarproject.api.data.MetadataEncoder;
|
||||||
@@ -14,10 +16,12 @@ import org.briarproject.util.StringUtils;
|
|||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import static org.briarproject.api.clients.QueueMessage.QUEUE_MESSAGE_HEADER_LENGTH;
|
||||||
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
|
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
|
||||||
import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
|
import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
|
||||||
|
|
||||||
public abstract class BdfMessageValidator implements MessageValidator {
|
public abstract class BdfMessageValidator implements MessageValidator,
|
||||||
|
QueueMessageValidator {
|
||||||
|
|
||||||
protected static final Logger LOG =
|
protected static final Logger LOG =
|
||||||
Logger.getLogger(BdfMessageValidator.class.getName());
|
Logger.getLogger(BdfMessageValidator.class.getName());
|
||||||
@@ -38,6 +42,15 @@ public abstract class BdfMessageValidator implements MessageValidator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Metadata validateMessage(Message m, Group g) {
|
public Metadata validateMessage(Message m, Group g) {
|
||||||
|
return validateMessage(m, g, MESSAGE_HEADER_LENGTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Metadata validateMessage(QueueMessage q, Group g) {
|
||||||
|
return validateMessage(q, g, QUEUE_MESSAGE_HEADER_LENGTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Metadata validateMessage(Message m, Group g, int headerLength) {
|
||||||
// Reject the message if it's too far in the future
|
// Reject the message if it's too far in the future
|
||||||
long now = clock.currentTimeMillis();
|
long now = clock.currentTimeMillis();
|
||||||
if (m.getTimestamp() - now > MAX_CLOCK_DIFFERENCE) {
|
if (m.getTimestamp() - now > MAX_CLOCK_DIFFERENCE) {
|
||||||
@@ -45,9 +58,13 @@ public abstract class BdfMessageValidator implements MessageValidator {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
byte[] raw = m.getRaw();
|
byte[] raw = m.getRaw();
|
||||||
|
if (raw.length <= headerLength) {
|
||||||
|
LOG.info("Message is too short");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
BdfList message = clientHelper.toList(raw, MESSAGE_HEADER_LENGTH,
|
BdfList message = clientHelper.toList(raw, headerLength,
|
||||||
raw.length - MESSAGE_HEADER_LENGTH);
|
raw.length - headerLength);
|
||||||
BdfDictionary meta = validateMessage(message, g, m.getTimestamp());
|
BdfDictionary meta = validateMessage(message, g, m.getTimestamp());
|
||||||
if (meta == null) {
|
if (meta == null) {
|
||||||
LOG.info("Invalid message");
|
LOG.info("Invalid message");
|
||||||
|
|||||||
Reference in New Issue
Block a user