java.lang.Object
net.i2p.router.transport.ntcp.NTCPConnection.NTCP2ReadState
- All Implemented Interfaces:
NTCP2Payload.PayloadCallback, NTCPConnection.ReadState
- Enclosing class:
NTCPConnection
private class NTCPConnection.NTCP2ReadStateextends Object implements NTCPConnection.ReadState, NTCP2Payload.PayloadCallback
Read the encrypted message
Call all methods from synchronized parent method.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ByteArrayBuffer holding the frame being read.private intValid frames received in this data phase.private intSize of the next frame, valid only once _received is non-negative.private final CipherStateCipher state for inbound data.private intBytes received of the current frame, -2 until the length is known.private final byte[]Scratch buffer for the encrypted frame length.private final byte[]The SipHash ratchet IV, as a byte array.private final longFirst SipHash ratchet key for inbound data.private final longSecond SipHash ratchet key for inbound data.private booleanTrue once a termination block was seen, logging only.Constructor Summary
ConstructorsConstructorDescriptionNTCP2ReadState(CipherState rcvr, byte[] keyData) NTCP2 read state initialized from the given key data.Method Summary
Modifier and TypeMethodDescriptionprivate ByteArrayallocReadBuf(int framelen) Allocate the scratch buffer for the announced frame on the buffered (split-frame) read path.private booleandecryptAndProcess(byte[] data, int off) Decrypts in place.voiddestroy()Releases the buffers this state holds.intGives the valid frames read so far.voidgotDateTime(long time) Receive the data/time block.voidgotI2NP(I2NPMessage msg) Receive an I2NP message block.voidgotOptions(byte[] options, boolean isHandshake) Receive an options block, the peer's padding negotiation parameters.voidgotPadding(int paddingLength, int frameLength) For stats.voidgotRI(RouterInfo ri, boolean isHandshake, boolean flood) / PayloadCallbacksvoidgotTermination(int reason, long lastReceived) Receive a termination block, the peer is closing the connection.voidgotUnknown(int type, int len) Receive an unknown block type.private voidprocessFrameBlocks(byte[] data, int off) Process the decrypted payload of the current frame, delivering each block to the PayloadCallback.voidreceive(ByteBuffer buf) Feeds freshly read data to this state.
Field Details
_recvLen
private final byte[] _recvLenScratch buffer for the encrypted frame length._sipk1
private final long _sipk1First SipHash ratchet key for inbound data._sipk2
private final long _sipk2Second SipHash ratchet key for inbound data._sipIV
private final byte[] _sipIVThe SipHash ratchet IV, as a byte array._rcvr
Cipher state for inbound data._framelen
private int _framelenSize of the next frame, valid only once _received is non-negative._received
private int _receivedBytes received of the current frame, -2 until the length is known._dataBuf
Buffer holding the frame being read._frameCount
private int _frameCountValid frames received in this data phase._terminated
private boolean _terminatedTrue once a termination block was seen, logging only.
Constructor Details
NTCP2ReadState
NTCP2 read state initialized from the given key data.- Parameters:
rcvr- the cipher state receiving the datakeyData- using first 24 bytes
Method Details
receive
Description copied from interface:NTCPConnection.ReadStateFeeds freshly read data to this state.- Specified by:
receivein interfaceNTCPConnection.ReadState- Parameters:
buf- the data read
allocReadBuf
Allocate the scratch buffer for the announced frame on the buffered (split-frame) read path.A pooled 16 KB buffer is used for regular frames; oversized frames get a one-off allocation since the pool only holds the fixed 16 KB class and a pooled buffer would be too small. A previous pooled buffer that is too small is returned to the pool first so the bounded pool is not drained by repeated large frames.
- Parameters:
framelen- the announced frame length- Returns:
- a fresh buffer for the frame; never null
decryptAndProcess
private boolean decryptAndProcess(byte[] data, int off) Decrypts in place. Length is _framelen Side effects: Sets _received = -2, increments _frameCount and _blockCount if valid, callsdestroy()and sets _terminated if termination block received or if invalid.Does not call close() on failure. Caller MUST call
NTCPConnection.delayedClose(ByteBuffer, int)if this returns false.- Parameters:
data- the buffer holding the packetoff- the offset of the packet in the buffer- Returns:
- success, false for fatal error (AEAD) only
processFrameBlocks
private void processFrameBlocks(byte[] data, int off) Process the decrypted payload of the current frame, delivering each block to the PayloadCallback. Payload errors are logged but never escalate to a connection close; only AEAD failures (handled by the caller before this point) are fatal.- Parameters:
data- the buffer holding the decrypted frameoff- the offset of the frame in the buffer
destroy
public void destroy()Description copied from interface:NTCPConnection.ReadStateReleases the buffers this state holds.- Specified by:
destroyin interfaceNTCPConnection.ReadState
getFramesReceived
public int getFramesReceived()Description copied from interface:NTCPConnection.ReadStateGives the valid frames read so far.- Specified by:
getFramesReceivedin interfaceNTCPConnection.ReadState- Returns:
- the frame count
gotRI
/ PayloadCallbacks- Specified by:
gotRIin interfaceNTCP2Payload.PayloadCallback- Parameters:
ri- will already be validatedisHandshake- true only for message 3 part 2flood- true if the flag byte asks the receiver to flood this RouterInfo- Throws:
DataFormatException- if the RouterInfo is rejected, e.g. an unpublished date
gotDateTime
public void gotDateTime(long time) Description copied from interface:NTCP2Payload.PayloadCallbackReceive the data/time block.- Specified by:
gotDateTimein interfaceNTCP2Payload.PayloadCallback- Parameters:
time- milliseconds since the epoch, as sent in the block
gotI2NP
Description copied from interface:NTCP2Payload.PayloadCallbackReceive an I2NP message block.- Specified by:
gotI2NPin interfaceNTCP2Payload.PayloadCallback- Parameters:
msg- the message, already deserialized out of the frame
gotOptions
public void gotOptions(byte[] options, boolean isHandshake) Description copied from interface:NTCP2Payload.PayloadCallbackReceive an options block, the peer's padding negotiation parameters.- Specified by:
gotOptionsin interfaceNTCP2Payload.PayloadCallback- Parameters:
options- the option bytes, copied out of the frameisHandshake- true only for message 3 part 2
gotTermination
public void gotTermination(int reason, long lastReceived) Description copied from interface:NTCP2Payload.PayloadCallbackReceive a termination block, the peer is closing the connection.- Specified by:
gotTerminationin interfaceNTCP2Payload.PayloadCallback- Parameters:
reason- the termination reason codelastReceived- in theory could wrap around to negative, but very unlikely
gotUnknown
public void gotUnknown(int type, int len) Description copied from interface:NTCP2Payload.PayloadCallbackReceive an unknown block type.- Specified by:
gotUnknownin interfaceNTCP2Payload.PayloadCallback- Parameters:
type- the block type byte from the block headerlen- the block length in bytes, not counting the 3-byte header
gotPadding
public void gotPadding(int paddingLength, int frameLength) Description copied from interface:NTCP2Payload.PayloadCallbackFor stats.- Specified by:
gotPaddingin interfaceNTCP2Payload.PayloadCallback- Parameters:
paddingLength- the number of padding bytes, not including the 3-byte block headerframeLength- the total size of the frame, including all blocks and block headers