net.i2p.router.transport.ntcp

## Class EstablishBase

- java.lang.Object
- - net.i2p.router.transport.ntcp.EstablishBase

- All Implemented Interfaces:
    EstablishState
  Direct Known Subclasses:
    InboundEstablishState

      abstract class EstablishBase
      extends 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.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `protected static class` | `EstablishBase.State` States for NTCP connection establishment process.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected NTCPConnection` | `_con`
    `protected RouterContext` | `_context`
    `protected Log` | `_log`
    `protected long` | `_peerSkew` OUR clock minus HIS clock, in seconds Inbound: tsB - tsA - rtt/2 Outbound: tsA - tsB - rtt/2
    `protected byte[]` | `_prevEncrypted` previously received encrypted block (or the IV)
    `protected int` | `_received` bytes received so far
    `protected EstablishBase.State` | `_state`
    `protected Object` | `_stateLock`
    `protected NTCPTransport` | `_transport`
    `protected byte[]` | `_X`
    `protected byte[]` | `_Y`
    `protected static int` | `AES_SIZE`
    `static net.i2p.router.transport.ntcp.EstablishBase.FailedEstablishState` | `FAILED`
    `protected static int` | `MAX_RI_SIZE`
    `protected static int` | `MIN_RI_SIZE`
    `protected static Set<EstablishBase.State>` | `STATES_DONE`
    `static net.i2p.router.transport.ntcp.EstablishBase.VerifiedEstablishState` | `VERIFIED`
    `protected static int` | `XY_SIZE`

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `EstablishBase(RouterContext ctx, NTCPTransport transport, NTCPConnection con)`

  - ### Method Summary

    All Methods Static Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected static String` | `_x(String s)` Mark a string for extraction by xgettext and translation.
    `protected void` | `changeState(EstablishBase.State state)`
    `void` | `close(String reason, Exception e)` Release resources on timeout.
    `protected void` | `fail(String reason)` Caller must synch.
    `protected void` | `fail(String reason, Exception e)` Caller must synch.
    `protected void` | `fail(String reason, Exception e, boolean bySkew)` Caller must synch.
    `abstract int` | `getVersion()` Get the NTCP version
    `boolean` | `isComplete()` If synchronized on this, fails with deadlocks from all over via CSFI.isEstablished().
    `boolean` | `isCorrupt()` did the handshake fail for some reason?
    `protected String` | `prefix()`
    `void` | `prepareOutbound()` Does nothing.
    `void` | `receive(ByteBuffer src)` Parse the contents of the buffer as part of the handshake.
    `protected void` | `releaseBufs(boolean isVerified)` Only call once.
    `String` | `toString()`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### _con

          protected final NTCPConnection _con

    - #### _context

          protected final RouterContext _context

    - #### _log

          protected final Log _log

    - #### _peerSkew

          protected transient 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

    - #### _state

          protected volatile EstablishBase.State _state

    - #### _stateLock

          protected final Object _stateLock

    - #### _transport

          protected final NTCPTransport _transport

    - #### _X

          protected final byte[] _X

    - #### _Y

          protected final byte[] _Y

    - #### AES_SIZE

          protected static final int AES_SIZE

      See Also:
        Constant Field Values

    - #### FAILED

          public static final net.i2p.router.transport.ntcp.EstablishBase.FailedEstablishState FAILED

    - #### MAX_RI_SIZE

          protected static final int MAX_RI_SIZE

      See Also:
        Constant Field Values

    - #### MIN_RI_SIZE

          protected static final int MIN_RI_SIZE

      See Also:
        Constant Field Values

    - #### STATES_DONE

          protected static final Set<EstablishBase.State> STATES_DONE

    - #### VERIFIED

          public static final net.i2p.router.transport.ntcp.EstablishBase.VerifiedEstablishState VERIFIED

    - #### XY_SIZE

          protected static final int XY_SIZE

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### EstablishBase

          protected EstablishBase(RouterContext ctx,
                                  NTCPTransport transport,
                                  NTCPConnection con)

  - ### Method Detail

    - #### _x

          protected static final String _x(String s)

      Mark a string for extraction by xgettext and translation. Use this only in static initializers. It does not translate!
      Returns:
        s

    - #### changeState

          protected void changeState(EstablishBase.State state)

    - #### close

          public void close(String reason,
                            Exception e)

      Release resources on timeout.
      Specified by:
        `close` in interface `EstablishState`
      Parameters:
        `e` - may be null

    - #### fail

          protected void fail(String reason)

      Caller must synch.

    - #### fail

          protected void fail(String reason,
                              Exception e)

      Caller must synch.

    - #### fail

          protected void fail(String reason,
                              Exception e,
                              boolean bySkew)

      Caller must synch.

    - #### getVersion

          public abstract int getVersion()

      Get the NTCP version
      Specified by:
        `getVersion` in interface `EstablishState`
      Returns:
        1, 2, or 0 if unknown

    - #### 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?

    - #### isCorrupt

          public boolean isCorrupt()

      did the handshake fail for some reason?
      Specified by:
        `isCorrupt` in interface `EstablishState`

    - #### prefix

          protected String prefix()

    - #### 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`

    - #### 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() 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`

    - #### releaseBufs

          protected void releaseBufs(boolean isVerified)

      Only call once. Caller must synch.

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`