java.lang.Object
net.i2p.router.transport.ntcp.EstablishBase
All Implemented Interfaces:
EstablishState
Direct Known Subclasses:
EstablishBase.FailedEstablishState, EstablishBase.VerifiedEstablishState, InboundEstablishState

abstract class EstablishBaseextends Object implements EstablishState
Base class for NTCP connection establishment. Provides common functionality for NTCP 2 handshake state machines, including cryptographic operations, timing calculations, and peer skew management. Inbound NTCP 2 only - OutboundNTCP2State does not extend this.
  • Field Details

    • VERIFIED

      public static final EstablishBase.VerifiedEstablishState VERIFIED
      Shared instance marking a handshake that completed and verified successfully.
    • FAILED

      public static final EstablishBase.FailedEstablishState FAILED
      Shared instance marking a handshake that ended in corruption.
    • _context

      protected final RouterContext _context
      Router context, supplied to the subclass that constructs this.
    • _log

      protected final Log _log
      Log scoped to the subclass that constructs this.
    • _X

      protected final byte[] _X
      Key block Bob receives and Alice sends, doubled as padding on inbound handshakes.
    • _Y

      protected final byte[] _Y
      Key block Alice receives and Bob sends, null on outbound handshakes.
    • _peerSkew

      protected long _peerSkew
      OUR clock minus HIS clock, in seconds. Inbound: tsB - tsA - rtt/2, Outbound: tsA - tsB - rtt/2.
    • _prevEncrypted

      protected byte[] _prevEncrypted
      Previously received encrypted block (or the IV).
    • _received

      protected int _received
      Bytes received so far.
    • _transport

      protected final NTCPTransport _transport
      NTCP transport running this handshake.
    • _con

      protected final NTCPConnection _con
      Connection being established by this handshake.
    • AES_SIZE

      protected static final int AES_SIZE
      AES block and key size in bytes.
      See Also:
    • _stateLock

      protected final Object _stateLock
      Monitor serialising every handshake state change.
    • _state

      protected volatile EstablishBase.State _state
      Current handshake state, replaced under the state lock.
    • _isCorrupt

      private final AtomicBoolean _isCorrupt
      Set when the handshake has failed for good.
    • _isComplete

      private final AtomicBoolean _isComplete
      Set when the handshake has finished.
    • _failReason

      protected String _failReason
      Why the handshake failed, or null while it is still viable.
    • STATES_DONE

      protected static final Set<EstablishBase.State> STATES_DONE
      The two terminal states, VERIFIED and CORRUPT.
  • Constructor Details

    • EstablishBase

      private EstablishBase()
      Creates the instance used only for its constants, with no context.
    • EstablishBase

      protected EstablishBase(RouterContext ctx, NTCPTransport transport, NTCPConnection con)
      Binds the context, transport and connection, sizing the padding buffer.
      Parameters:
      ctx - the router context, supplying the log manager
      transport - the NTCP transport this handshake runs under
      con - the connection being established, whose inbound flag sizes the padding buffer
  • Method Details

    • changeState

      protected void changeState(EstablishBase.State state)
      Change the handshake state and update completion/corruption flags.
      Parameters:
      state - the new state
    • receive

      public void receive(ByteBuffer src)
      Parse the contents of the buffer as part of the handshake.

      All data must be copied out of the buffer as Reader.processRead(NTCPConnection) will return it to the pool.

      If there are additional data in the buffer after the handshake is complete, the EstablishState is responsible for passing it to NTCPConnection.

      Specified by:
      receive in interface EstablishState
      Parameters:
      src - the buffer to parse
    • prepareOutbound

      public void prepareOutbound()
      Does nothing. Outbound (Alice) must override. We are establishing an outbound connection, so prepare ourselves by queueing up the write of the first part of the handshake.
      Specified by:
      prepareOutbound in interface EstablishState
    • isCorrupt

      public boolean isCorrupt()
      Did the handshake fail for some reason?
      Specified by:
      isCorrupt in interface EstablishState
      Returns:
      whether the handshake failed
    • isComplete

      public boolean isComplete()
      If synchronized on this, fails with deadlocks from all over via CSFI.isEstablished(). Also CSFI.getFramedAveragePeerClockSkew().
      Specified by:
      isComplete in interface EstablishState
      Returns:
      is the handshake complete and valid?
    • getVersion

      public abstract int getVersion()
      NTCP version.
      Specified by:
      getVersion in interface EstablishState
      Returns:
      1, 2, or 0 if unknown
    • close

      public void close(String reason, Exception e)
      Release resources on timeout.
      Specified by:
      close in interface EstablishState
      Parameters:
      reason - the text logged with the warning and returned by EstablishState.getFailReason()
      e - may be null
    • fail

      protected void fail(String reason)
      Fail the handshake. Caller must synch.
      Parameters:
      reason - the text logged with the warning and returned by getFailReason()
    • fail

      protected void fail(String reason, Exception e)
      Fail the handshake with exception. Caller must synch.
      Parameters:
      reason - the text logged with the warning and returned by getFailReason()
      e - the exception cause, may be null
    • fail

      protected void fail(String reason, Exception e, boolean bySkew)
      Fail the handshake optionally suppressing skew stat. Caller must synch.
      Parameters:
      reason - the text logged with the warning and returned by getFailReason()
      e - the exception cause, may be null
      bySkew - true to suppress the skew stat, as when we caused the skew ourselves
    • getFailReason

      public String getFailReason()
      Failure reason if the handshake failed, or null if not failed/corrupt.
      Specified by:
      getFailReason in interface EstablishState
      Returns:
      failure reason string or null
    • releaseBufs

      protected void releaseBufs(boolean isVerified)
      Only call once. Caller must synch.
      Parameters:
      isVerified - true when the handshake completed and was not marked corrupt
    • prefix

      protected String prefix()
      Logging prefix string.
      Returns:
      string representation of this state
    • toString

      public String toString()
      String representation of this state.
      Overrides:
      toString in class Object
    • storeFailCause

      static String storeFailCause(IllegalArgumentException iae)
      The bare cause from a netdb store rejection, stripped of the "Invalid NetDbStore attempt" wrapper, capability flags and router ID.
      Parameters:
      iae - the rejection from the netdb
      Returns:
      the cause, for log output
    • _x

      protected static final String _x(String s)
      Marker function for i18n, returns the string unchanged.
      Parameters:
      s - the string to mark for translation
      Returns:
      the same string passed in