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

    Fields
    Modifier and Type
    Field
    Description
    private ByteArray
    Buffer holding the frame being read.
    private int
    Valid frames received in this data phase.
    private int
    Size of the next frame, valid only once _received is non-negative.
    private final CipherState
    Cipher state for inbound data.
    private int
    Bytes 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 long
    First SipHash ratchet key for inbound data.
    private final long
    Second SipHash ratchet key for inbound data.
    private boolean
    True once a termination block was seen, logging only.
  • Constructor Summary

    Constructors
    Constructor
    Description
    NTCP2ReadState(CipherState rcvr, byte[] keyData)
    NTCP2 read state initialized from the given key data.
  • Method Summary

    Modifier and Type
    Method
    Description
    private ByteArray
    allocReadBuf(int framelen)
    Allocate the scratch buffer for the announced frame on the buffered (split-frame) read path.
    private boolean
    decryptAndProcess(byte[] data, int off)
    Decrypts in place.
    void
    Releases the buffers this state holds.
    int
    Gives the valid frames read so far.
    void
    gotDateTime(long time)
    Receive the data/time block.
    void
    Receive an I2NP message block.
    void
    gotOptions(byte[] options, boolean isHandshake)
    Receive an options block, the peer's padding negotiation parameters.
    void
    gotPadding(int paddingLength, int frameLength)
    For stats.
    void
    gotRI(RouterInfo ri, boolean isHandshake, boolean flood)
    / PayloadCallbacks
    void
    gotTermination(int reason, long lastReceived)
    Receive a termination block, the peer is closing the connection.
    void
    gotUnknown(int type, int len)
    Receive an unknown block type.
    private void
    processFrameBlocks(byte[] data, int off)
    Process the decrypted payload of the current frame, delivering each block to the PayloadCallback.
    void
    Feeds freshly read data to this state.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • _recvLen

      private final byte[] _recvLen
      Scratch buffer for the encrypted frame length.
    • _sipk1

      private final long _sipk1
      First SipHash ratchet key for inbound data.
    • _sipk2

      private final long _sipk2
      Second SipHash ratchet key for inbound data.
    • _sipIV

      private final byte[] _sipIV
      The SipHash ratchet IV, as a byte array.
    • _rcvr

      private final CipherState _rcvr
      Cipher state for inbound data.
    • _framelen

      private int _framelen
      Size of the next frame, valid only once _received is non-negative.
    • _received

      private int _received
      Bytes received of the current frame, -2 until the length is known.
    • _dataBuf

      private ByteArray _dataBuf
      Buffer holding the frame being read.
    • _frameCount

      private int _frameCount
      Valid frames received in this data phase.
    • _terminated

      private boolean _terminated
      True once a termination block was seen, logging only.
  • Constructor Details

    • NTCP2ReadState

      public NTCP2ReadState(CipherState rcvr, byte[] keyData)
      NTCP2 read state initialized from the given key data.
      Parameters:
      rcvr - the cipher state receiving the data
      keyData - using first 24 bytes
  • Method Details

    • receive

      public void receive(ByteBuffer buf)
      Description copied from interface: NTCPConnection.ReadState
      Feeds freshly read data to this state.
      Specified by:
      receive in interface NTCPConnection.ReadState
      Parameters:
      buf - the data read
    • allocReadBuf

      private ByteArray allocReadBuf(int framelen)
      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, calls destroy() 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 packet
      off - 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 frame
      off - the offset of the frame in the buffer
    • destroy

      public void destroy()
      Description copied from interface: NTCPConnection.ReadState
      Releases the buffers this state holds.
      Specified by:
      destroy in interface NTCPConnection.ReadState
    • getFramesReceived

      public int getFramesReceived()
      Description copied from interface: NTCPConnection.ReadState
      Gives the valid frames read so far.
      Specified by:
      getFramesReceived in interface NTCPConnection.ReadState
      Returns:
      the frame count
    • gotRI

      public void gotRI(RouterInfo ri, boolean isHandshake, boolean flood) throws DataFormatException
      / PayloadCallbacks
      Specified by:
      gotRI in interface NTCP2Payload.PayloadCallback
      Parameters:
      ri - will already be validated
      isHandshake - true only for message 3 part 2
      flood - 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.PayloadCallback
      Receive the data/time block.
      Specified by:
      gotDateTime in interface NTCP2Payload.PayloadCallback
      Parameters:
      time - milliseconds since the epoch, as sent in the block
    • gotI2NP

      public void gotI2NP(I2NPMessage msg)
      Description copied from interface: NTCP2Payload.PayloadCallback
      Receive an I2NP message block.
      Specified by:
      gotI2NP in interface NTCP2Payload.PayloadCallback
      Parameters:
      msg - the message, already deserialized out of the frame
    • gotOptions

      public void gotOptions(byte[] options, boolean isHandshake)
      Description copied from interface: NTCP2Payload.PayloadCallback
      Receive an options block, the peer's padding negotiation parameters.
      Specified by:
      gotOptions in interface NTCP2Payload.PayloadCallback
      Parameters:
      options - the option bytes, copied out of the frame
      isHandshake - true only for message 3 part 2
    • gotTermination

      public void gotTermination(int reason, long lastReceived)
      Description copied from interface: NTCP2Payload.PayloadCallback
      Receive a termination block, the peer is closing the connection.
      Specified by:
      gotTermination in interface NTCP2Payload.PayloadCallback
      Parameters:
      reason - the termination reason code
      lastReceived - in theory could wrap around to negative, but very unlikely
    • gotUnknown

      public void gotUnknown(int type, int len)
      Description copied from interface: NTCP2Payload.PayloadCallback
      Receive an unknown block type.
      Specified by:
      gotUnknown in interface NTCP2Payload.PayloadCallback
      Parameters:
      type - the block type byte from the block header
      len - the block length in bytes, not counting the 3-byte header
    • gotPadding

      public void gotPadding(int paddingLength, int frameLength)
      Description copied from interface: NTCP2Payload.PayloadCallback
      For stats.
      Specified by:
      gotPadding in interface NTCP2Payload.PayloadCallback
      Parameters:
      paddingLength - the number of padding bytes, not including the 3-byte block header
      frameLength - the total size of the frame, including all blocks and block headers