forked from I2P_Developers/i2p.i2p
I2NP: Locking for message ID
This commit is contained in:
@ -68,7 +68,7 @@ public abstract class FastI2NPMessageImpl extends I2NPMessageImpl {
|
|||||||
type = data[cur] & 0xff;
|
type = data[cur] & 0xff;
|
||||||
cur++;
|
cur++;
|
||||||
}
|
}
|
||||||
_uniqueId = DataHelper.fromLong(data, cur, 4);
|
setUniqueId(DataHelper.fromLong(data, cur, 4));
|
||||||
cur += 4;
|
cur += 4;
|
||||||
_expiration = DataHelper.fromLong(data, cur, DataHelper.DATE_LENGTH);
|
_expiration = DataHelper.fromLong(data, cur, DataHelper.DATE_LENGTH);
|
||||||
cur += DataHelper.DATE_LENGTH;
|
cur += DataHelper.DATE_LENGTH;
|
||||||
@ -140,7 +140,7 @@ public abstract class FastI2NPMessageImpl extends I2NPMessageImpl {
|
|||||||
int payloadLen = writtenLen - HEADER_LENGTH;
|
int payloadLen = writtenLen - HEADER_LENGTH;
|
||||||
int off = 0;
|
int off = 0;
|
||||||
buffer[off++] = (byte) getType();
|
buffer[off++] = (byte) getType();
|
||||||
DataHelper.toLong(buffer, off, 4, _uniqueId);
|
DataHelper.toLong(buffer, off, 4, getUniqueId());
|
||||||
off += 4;
|
off += 4;
|
||||||
DataHelper.toLong(buffer, off, DataHelper.DATE_LENGTH, _expiration);
|
DataHelper.toLong(buffer, off, DataHelper.DATE_LENGTH, _expiration);
|
||||||
off += DataHelper.DATE_LENGTH;
|
off += DataHelper.DATE_LENGTH;
|
||||||
|
@ -38,7 +38,7 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
|
|||||||
* Extending classes should take care when accessing this field;
|
* Extending classes should take care when accessing this field;
|
||||||
* to ensure initialization, use getUniqueId() instead.
|
* to ensure initialization, use getUniqueId() instead.
|
||||||
*/
|
*/
|
||||||
protected long _uniqueId = -1;
|
private long _uniqueId = -1;
|
||||||
|
|
||||||
public final static long DEFAULT_EXPIRATION_MS = 1*60*1000; // 1 minute by default
|
public final static long DEFAULT_EXPIRATION_MS = 1*60*1000; // 1 minute by default
|
||||||
public final static int CHECKSUM_LENGTH = 1; //Hash.HASH_LENGTH;
|
public final static int CHECKSUM_LENGTH = 1; //Hash.HASH_LENGTH;
|
||||||
@ -145,7 +145,8 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
|
|||||||
|
|
||||||
// Compare the checksum in data to the checksum of the data after the checksum
|
// Compare the checksum in data to the checksum of the data after the checksum
|
||||||
_context.sha().calculateHash(data, cur + CHECKSUM_LENGTH, sz, calc, 0);
|
_context.sha().calculateHash(data, cur + CHECKSUM_LENGTH, sz, calc, 0);
|
||||||
boolean eq = DataHelper.eq(data, cur, calc, 0, CHECKSUM_LENGTH);
|
//boolean eq = DataHelper.eq(data, cur, calc, 0, CHECKSUM_LENGTH);
|
||||||
|
boolean eq = data[cur] == calc[0];
|
||||||
cur += CHECKSUM_LENGTH;
|
cur += CHECKSUM_LENGTH;
|
||||||
|
|
||||||
SimpleByteCache.release(calc);
|
SimpleByteCache.release(calc);
|
||||||
@ -177,7 +178,7 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
|
|||||||
/**
|
/**
|
||||||
* Replay resistant message Id
|
* Replay resistant message Id
|
||||||
*/
|
*/
|
||||||
public long getUniqueId() {
|
public synchronized long getUniqueId() {
|
||||||
// Lazy initialization of value
|
// Lazy initialization of value
|
||||||
if (_uniqueId < 0) {
|
if (_uniqueId < 0) {
|
||||||
_uniqueId = _context.random().nextLong(MAX_ID_VALUE);
|
_uniqueId = _context.random().nextLong(MAX_ID_VALUE);
|
||||||
@ -188,7 +189,7 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
|
|||||||
/**
|
/**
|
||||||
* The ID is set to a random value when written but it can be overridden here.
|
* The ID is set to a random value when written but it can be overridden here.
|
||||||
*/
|
*/
|
||||||
public void setUniqueId(long id) { _uniqueId = id; }
|
public synchronized void setUniqueId(long id) { _uniqueId = id; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Date after which the message should be dropped (and the associated uniqueId forgotten)
|
* Date after which the message should be dropped (and the associated uniqueId forgotten)
|
||||||
@ -256,19 +257,14 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
|
|||||||
_context.sha().calculateHash(buffer, off + HEADER_LENGTH, payloadLen, h, 0);
|
_context.sha().calculateHash(buffer, off + HEADER_LENGTH, payloadLen, h, 0);
|
||||||
|
|
||||||
buffer[off++] = (byte) getType();
|
buffer[off++] = (byte) getType();
|
||||||
|
DataHelper.toLong(buffer, off, 4, getUniqueId());
|
||||||
// Lazy initialization of value
|
|
||||||
if (_uniqueId < 0) {
|
|
||||||
_uniqueId = _context.random().nextLong(MAX_ID_VALUE);
|
|
||||||
}
|
|
||||||
DataHelper.toLong(buffer, off, 4, _uniqueId);
|
|
||||||
|
|
||||||
off += 4;
|
off += 4;
|
||||||
DataHelper.toLong(buffer, off, DataHelper.DATE_LENGTH, _expiration);
|
DataHelper.toLong(buffer, off, DataHelper.DATE_LENGTH, _expiration);
|
||||||
off += DataHelper.DATE_LENGTH;
|
off += DataHelper.DATE_LENGTH;
|
||||||
DataHelper.toLong(buffer, off, 2, payloadLen);
|
DataHelper.toLong(buffer, off, 2, payloadLen);
|
||||||
off += 2;
|
off += 2;
|
||||||
System.arraycopy(h, 0, buffer, off, CHECKSUM_LENGTH);
|
//System.arraycopy(h, 0, buffer, off, CHECKSUM_LENGTH);
|
||||||
|
buffer[off] = h[0];
|
||||||
SimpleByteCache.release(h);
|
SimpleByteCache.release(h);
|
||||||
|
|
||||||
return rv;
|
return rv;
|
||||||
@ -322,11 +318,7 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
|
|||||||
public int toRawByteArrayNTCP2(byte buffer[], int off) {
|
public int toRawByteArrayNTCP2(byte buffer[], int off) {
|
||||||
try {
|
try {
|
||||||
buffer[off++] = (byte) getType();
|
buffer[off++] = (byte) getType();
|
||||||
// Lazy initialization of value
|
DataHelper.toLong(buffer, off, 4, getUniqueId());
|
||||||
if (_uniqueId < 0) {
|
|
||||||
_uniqueId = _context.random().nextLong(MAX_ID_VALUE);
|
|
||||||
}
|
|
||||||
DataHelper.toLong(buffer, off, 4, _uniqueId);
|
|
||||||
off += 4;
|
off += 4;
|
||||||
// January 19 2038? No, unsigned, good until Feb. 7 2106
|
// January 19 2038? No, unsigned, good until Feb. 7 2106
|
||||||
// in seconds, round up so we don't lose time every hop
|
// in seconds, round up so we don't lose time every hop
|
||||||
@ -393,7 +385,7 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
|
|||||||
I2NPMessage msg = createMessage(ctx, type);
|
I2NPMessage msg = createMessage(ctx, type);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
long id = DataHelper.fromLong(buffer, offset, 4);
|
msg.setUniqueId(DataHelper.fromLong(buffer, offset, 4));
|
||||||
offset += 4;
|
offset += 4;
|
||||||
// January 19 2038? No, unsigned, good until Feb. 7 2106
|
// January 19 2038? No, unsigned, good until Feb. 7 2106
|
||||||
// in seconds, round up so we don't lose time every hop
|
// in seconds, round up so we don't lose time every hop
|
||||||
@ -401,7 +393,6 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
|
|||||||
offset += 4;
|
offset += 4;
|
||||||
int dataSize = len - 9;
|
int dataSize = len - 9;
|
||||||
msg.readMessage(buffer, offset, dataSize, type, handler);
|
msg.readMessage(buffer, offset, dataSize, type, handler);
|
||||||
msg.setUniqueId(id);
|
|
||||||
msg.setMessageExpiration(expiration);
|
msg.setMessageExpiration(expiration);
|
||||||
return msg;
|
return msg;
|
||||||
} catch (IllegalArgumentException iae) {
|
} catch (IllegalArgumentException iae) {
|
||||||
|
@ -100,7 +100,7 @@ public class UnknownI2NPMessage extends FastI2NPMessageImpl {
|
|||||||
if (!eq)
|
if (!eq)
|
||||||
throw new I2NPMessageException("Bad checksum on " + _data.length + " byte msg type " + _type);
|
throw new I2NPMessageException("Bad checksum on " + _data.length + " byte msg type " + _type);
|
||||||
msg.readMessage(_data, 0, _data.length, _type);
|
msg.readMessage(_data, 0, _data.length, _type);
|
||||||
msg.setUniqueId(_uniqueId);
|
msg.setUniqueId(getUniqueId());
|
||||||
msg.setMessageExpiration(_expiration);
|
msg.setMessageExpiration(_expiration);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user