java.lang.Object
net.i2p.data.i2np.I2NPMessageImpl
net.i2p.data.i2np.FastI2NPMessageImpl
net.i2p.data.i2np.UnknownI2NPMessage
- All Implemented Interfaces:
I2NPMessage
This is similar to DataMessage or GarlicMessage but with a variable message type. This is defined so routers can route messages they don't know about. We don't extend those classes so that any code that does (instanceof foo) won't return true for this type. Load tests use
DataMessage, for example. Also, those classes include an additional length field that we can't use here. See InboundMessageDistributor. There is no setData() method, the only way to create one of these is to read it with readMessage(byte[], int, int, int) (i.e., it came from some other router)
As of 0.8.12 this class is working. It is used at the IBGW to reduce the processing required. For zero-hop IB tunnels, the convert() method is used to reconstitute a standard message class.
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
FieldsFields inherited from class FastI2NPMessageImpl
_checksum, _hasChecksumModifier 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
ConstructorsConstructorDescriptionUnknownI2NPMessage(I2PAppContext context, int type) Message with the given type, unknown to the router.Method Summary
Public MethodsModifier and TypeMethodDescriptionprotected intCalculate the message body's length (not including the header and footer).convert()Attempt to convert this message to a known message class.booleanintgetType()Note that this returns the "true" type, so that the IBGW can correctly make drop decisions.inthashCode()voidreadMessage(byte[] data, int offset, int dataSize, int type) Raw message data; may only be set once.toString()protected intwriteMessageBody(byte[] out, int curIndex) Write the message body to the output array, starting at the given index.Methods inherited from class FastI2NPMessageImpl
readBytes, toByteArray, toByteArrayWithSavedChecksumModifier 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
createMessage, fromRawByteArray, fromRawByteArrayNTCP2, getMessageExpiration, getMessageSize, getRawMessageSize, getUniqueId, getUniqueId, readBytes, readMessage, setMessageExpiration, setUniqueId, toByteArray, toByteArray, toRawByteArray, toRawByteArrayNTCP2Modifier and TypeMethodDescriptionstatic 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).
Field Details
_data
private byte[] _dataThe message contents, as read from the wire._type
private final int _typeThe message type number.
Constructor Details
UnknownI2NPMessage
Message with the given type, unknown to the router.- Parameters:
context- the app context for the messagetype- 0-255
Method Details
readMessage
public void readMessage(byte[] data, int offset, int dataSize, int type) throws I2NPMessageException Raw message data; may only be set once.- Parameters:
data- data to read fromoffset- where to start in the data arraydataSize- how long into the data to readtype- I2NP message type- Throws:
IllegalStateException- if data previously set, to protect saved checksumI2NPMessageException- if the stream doesn't contain a valid message that this class can read.
calculateWrittenLength
protected int calculateWrittenLength()Calculate the message body's length (not including the header and footer).- Specified by:
calculateWrittenLengthin classI2NPMessageImpl- Returns:
- the number of bytes the body will occupy once serialized
writeMessageBody
protected int writeMessageBody(byte[] out, int curIndex) Write the message body to the output array, starting at the given index.- Specified by:
writeMessageBodyin classI2NPMessageImpl- Parameters:
out- the buffer to serialize the body intocurIndex- the offset into out at which to begin writing; leave room for the signature where the header format calls for one- Returns:
- the index into the array after the last byte written (NOT the length)
getType
public int getType()Note that this returns the "true" type, so that the IBGW can correctly make drop decisions.- Returns:
- 0-255
convert
Attempt to convert this message to a known message class. This does the delayed verification using the saved checksum.Used by TunnelGatewayZeroHop.
- Returns:
- the message as its known subclass, verified against the saved checksum
- Throws:
I2NPMessageException- if the conversion fails
hashCode
equals
toString