java.lang.Object
net.i2p.data.i2np.I2NPMessageImpl
net.i2p.data.i2np.FastI2NPMessageImpl
- All Implemented Interfaces:
I2NPMessage
- Direct Known Subclasses:
DatabaseLookupMessage, DatabaseSearchReplyMessage, DatabaseStoreMessage, DataMessage, DeliveryStatusMessage, GarlicMessage, TunnelDataMessage, TunnelGatewayMessage, UnknownI2NPMessage
Ignore, but save, the SHA-256 checksum in the full 16-byte header when read in. Use the same checksum when writing out.
This is a savings for NTCP in, and for NTCP-in to NTCP-out for TunnelDataMessages. It's also a savings for messages embedded in other messages. Note that SSU does not use the SHA-256 checksum.
Subclasses must take care to set _hasChecksum to false to invalidate it if the message payload changes between reading and writing.
It isn't clear where, if anywhere, we actually need to send a checksum. For point-to-point messages over NTCP where we know the router version of the peer, we could add a method to skip checksum generation. For end-to-end I2NP messages embedded in a Garlic, TGM, etc... we would need a flag day.
Nested Class Summary
Nested classes/interfaces inherited from class I2NPMessageImpl
I2NPMessageImpl.BuilderModifier and TypeClassDescriptionstatic interfaceInterface for extending the types of messages handled - unused.Field Summary
FieldsModifier and TypeFieldDescriptionprotected byteOne-byte checksum.protected booleanWhether the message includes a checksum.Fields inherited from class I2NPMessageImpl
_context, _expiration, _log, CHECKSUM_LENGTH, DEFAULT_EXPIRATION_MS, HEADER_LENGTHModifier and TypeFieldDescriptionprotected final I2PAppContextApplication context this message belongs to.protected longExpiration time in milliseconds since the epoch, as an I2P Date.protected final LogLog for the message class, taken from the context's log manager.static final intNumber of checksum bytes the message header carries.static final longDefault lifetime of an unsent message, one minute in milliseconds.static final intI2NP message header size in bytes.Fields inherited from interface I2NPMessage
MAX_ID_VALUE, MAX_SIZEModifier and TypeFieldDescriptionstatic final long4 bytes unsigned.static final intNominal limit, actual max is much less.Constructor Summary
ConstructorsConstructorDescriptionFastI2NPMessageImpl(I2PAppContext context) Create a message that will carry the one-byte checksum of the fast (5 byte header) format.Method Summary
Public MethodsModifier and TypeMethodDescriptionintreadBytes(byte[] data, int type, int offset, int maxLen) Ignore, but save, the checksum, to be used later if necessary.inttoByteArray(byte[] buffer) If available, use the previously-computed or previously-read checksum for speed.protected inttoByteArrayWithSavedChecksum(byte[] buffer) Use a previously-computed checksum for speed.Methods inherited from class I2NPMessageImpl
calculateWrittenLength, createMessage, fromRawByteArray, fromRawByteArrayNTCP2, getMessageExpiration, getMessageSize, getRawMessageSize, getUniqueId, getUniqueId, readBytes, readMessage, setMessageExpiration, setUniqueId, toByteArray, toByteArray, toRawByteArray, toRawByteArrayNTCP2, writeMessageBodyModifier and TypeMethodDescriptionprotected abstract intCalculate the message body's length (not including the header and footer).static I2NPMessagecreateMessage(I2PAppContext context, int type) Yes, this is fairly ugly, but its the only place it ever happens.static I2NPMessagefromRawByteArray(I2PAppContext ctx, byte[] buffer, int offset, int len, I2NPMessageHandler handler) Read the message with a short 5-byte header.static I2NPMessagefromRawByteArrayNTCP2(I2PAppContext ctx, byte[] buffer, int offset, int len, I2NPMessageHandler handler) Read the message with a short 9-byte header.longDate after which the message should be dropped (and the associated uniqueId forgotten).intSerialized message size, including the 16-byte header.intThe raw header consists of a one-byte type and a 4-byte expiration in seconds only.longUnique id for this message, lazily initialized when negative.longgetUniqueId(long msgIDBloomXor) Replay resistant message Id.intreadBytes(byte[] data, int type, int offset) Read the header, then read the rest into buffer, then call readMessage in the implemented message type.voidreadMessage(byte[] data, int offset, int dataSize, int type, I2NPMessageHandler handler) Read the message fields from the byte array, ignoring the handler.voidsetMessageExpiration(long exp) The expiration is set to one minute from now in the constructor but it can be overridden here.voidsetUniqueId(long id) The ID is set to a random value when written but it can be overridden here.byte[]The message serialized into a new byte array.inttoByteArray(byte[] buffer, int off) Write the message to the buffer, returning the new offset (NOT the length).inttoRawByteArray(byte[] buffer) Write the message with a short 5-byte header.inttoRawByteArrayNTCP2(byte[] buffer, int off) Write the message to the buffer, returning the new offset (NOT the length).protected abstract intwriteMessageBody(byte[] out, int curIndex) Write the message body to the output array, starting at the given index.Methods inherited from class Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface I2NPMessage
getType, readMessageModifier and TypeMethodDescriptionintgetType()Return the unique identifier for this type of I2NP message, as defined in the I2NP spec.voidreadMessage(byte[] data, int offset, int dataSize, int type) Read the body into the data structures, after the initial type byte and the uniqueId / expiration, using the current class's format as defined by the I2NP specification.
Field Details
_checksum
protected byte _checksumOne-byte checksum._hasChecksum
protected boolean _hasChecksumWhether the message includes a checksum.
Constructor Details
FastI2NPMessageImpl
Create a message that will carry the one-byte checksum of the fast (5 byte header) format.- Parameters:
context- the app context the parent uses for its RNG and properties
Method Details
readBytes
Ignore, but save, the checksum, to be used later if necessary.- Specified by:
readBytesin interfaceI2NPMessage- Overrides:
readBytesin classI2NPMessageImpl- Parameters:
data- the data, may or may not include the typetype- the message type or -1 if we should read it hereoffset- where to start starting at type if type is < 0 (16 byte header) starting at ID if type is >= 0 (15 byte header)maxLen- read no more than this many bytes from data starting at offset, even if it is longer This includes the type byte only if type < 0- Returns:
- total length of the message
- Throws:
IllegalStateException- if called twice, to protect saved checksumI2NPMessageException- if there is no valid message
toByteArray
public int toByteArray(byte[] buffer) If available, use the previously-computed or previously-read checksum for speed.- Specified by:
toByteArrayin interfaceI2NPMessage- Overrides:
toByteArrayin classI2NPMessageImpl- Parameters:
buffer- the destination array, which must have room forI2NPMessage.getMessageSize()bytes- Returns:
- the length written
toByteArrayWithSavedChecksum
protected int toByteArrayWithSavedChecksum(byte[] buffer) Use a previously-computed checksum for speed.- Parameters:
buffer- the destination array, must be at leastI2NPMessageImpl.HEADER_LENGTHbytes long- Returns:
- the number of bytes written into buffer